blob: a4d01a10a72188a0ad00e06b4d308bda3710f597 [file] [log] [blame]
Arnold Schwaighofer92226dd2007-10-12 21:53:12 +00001//===-- X86ISelLowering.cpp - X86 DAG Lowering Implementation -------------===//
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that X86 uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
Evan Chengb1712452010-01-27 06:25:16 +000015#define DEBUG_TYPE "x86-isel"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000016#include "X86.h"
Evan Cheng0cc39452006-01-16 21:21:29 +000017#include "X86InstrBuilder.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000018#include "X86ISelLowering.h"
19#include "X86TargetMachine.h"
Chris Lattner8c6ed052009-09-16 01:46:41 +000020#include "X86TargetObjectFile.h"
David Greene583b68f2011-02-17 19:18:59 +000021#include "Utils/X86ShuffleDecode.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000022#include "llvm/CallingConv.h"
Evan Cheng223547a2006-01-31 22:28:30 +000023#include "llvm/Constants.h"
Evan Cheng347d5f72006-04-28 21:29:37 +000024#include "llvm/DerivedTypes.h"
Chris Lattnerb903bed2009-06-26 21:20:29 +000025#include "llvm/GlobalAlias.h"
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000026#include "llvm/GlobalVariable.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000027#include "llvm/Function.h"
Chris Lattnerb8105652009-07-20 17:51:36 +000028#include "llvm/Instructions.h"
Evan Cheng6be2c582006-04-05 23:38:46 +000029#include "llvm/Intrinsics.h"
Owen Andersona90b3dc2009-07-15 21:51:10 +000030#include "llvm/LLVMContext.h"
Evan Cheng55d42002011-01-08 01:24:27 +000031#include "llvm/CodeGen/IntrinsicLowering.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000032#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng4a460802006-01-11 00:33:36 +000033#include "llvm/CodeGen/MachineFunction.h"
34#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner5e1df8d2010-01-25 23:38:14 +000035#include "llvm/CodeGen/MachineJumpTableInfo.h"
Evan Chenga844bde2008-02-02 04:07:54 +000036#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000037#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman69de1932008-02-06 22:27:42 +000038#include "llvm/CodeGen/PseudoSourceValue.h"
Chris Lattner589c6f62010-01-26 06:28:43 +000039#include "llvm/MC/MCAsmInfo.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000040#include "llvm/MC/MCContext.h"
Daniel Dunbar4e815f82010-03-15 23:51:06 +000041#include "llvm/MC/MCExpr.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000042#include "llvm/MC/MCSymbol.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000043#include "llvm/ADT/BitVector.h"
Evan Cheng14b32e12007-12-11 01:46:18 +000044#include "llvm/ADT/SmallSet.h"
Evan Chengb1712452010-01-27 06:25:16 +000045#include "llvm/ADT/Statistic.h"
Chris Lattner1a60aa72006-10-31 19:42:44 +000046#include "llvm/ADT/StringExtras.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000047#include "llvm/ADT/VectorExtras.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000048#include "llvm/Support/Debug.h"
Bill Wendlingec041eb2010-03-12 19:20:40 +000049#include "llvm/Support/Dwarf.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000050#include "llvm/Support/ErrorHandling.h"
51#include "llvm/Support/MathExtras.h"
Torok Edwindac237e2009-07-08 20:53:28 +000052#include "llvm/Support/raw_ostream.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000053using namespace llvm;
Bill Wendlingec041eb2010-03-12 19:20:40 +000054using namespace dwarf;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000055
Evan Chengb1712452010-01-27 06:25:16 +000056STATISTIC(NumTailCalls, "Number of tail calls");
57
Evan Cheng10e86422008-04-25 19:11:04 +000058// Forward declarations.
Owen Andersone50ed302009-08-10 22:56:29 +000059static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +000060 SDValue V2);
Evan Cheng10e86422008-04-25 19:11:04 +000061
David Greenea5f26012011-02-07 19:36:54 +000062static SDValue Insert128BitVector(SDValue Result,
63 SDValue Vec,
64 SDValue Idx,
65 SelectionDAG &DAG,
66 DebugLoc dl);
David Greenef125a292011-02-08 19:04:41 +000067
David Greenea5f26012011-02-07 19:36:54 +000068static SDValue Extract128BitVector(SDValue Vec,
69 SDValue Idx,
70 SelectionDAG &DAG,
71 DebugLoc dl);
72
David Greenef125a292011-02-08 19:04:41 +000073static SDValue ConcatVectors(SDValue Lower, SDValue Upper, SelectionDAG &DAG);
74
75
David Greenea5f26012011-02-07 19:36:54 +000076/// Generate a DAG to grab 128-bits from a vector > 128 bits. This
77/// sets things up to match to an AVX VEXTRACTF128 instruction or a
David Greene74a579d2011-02-10 16:57:36 +000078/// simple subregister reference. Idx is an index in the 128 bits we
79/// want. It need not be aligned to a 128-bit bounday. That makes
80/// lowering EXTRACT_VECTOR_ELT operations easier.
David Greenea5f26012011-02-07 19:36:54 +000081static SDValue Extract128BitVector(SDValue Vec,
82 SDValue Idx,
83 SelectionDAG &DAG,
84 DebugLoc dl) {
85 EVT VT = Vec.getValueType();
86 assert(VT.getSizeInBits() == 256 && "Unexpected vector size!");
87
88 EVT ElVT = VT.getVectorElementType();
89
90 int Factor = VT.getSizeInBits() / 128;
91
92 EVT ResultVT = EVT::getVectorVT(*DAG.getContext(),
93 ElVT,
94 VT.getVectorNumElements() / Factor);
95
96 // Extract from UNDEF is UNDEF.
97 if (Vec.getOpcode() == ISD::UNDEF)
98 return DAG.getNode(ISD::UNDEF, dl, ResultVT);
99
100 if (isa<ConstantSDNode>(Idx)) {
101 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
102
103 // Extract the relevant 128 bits. Generate an EXTRACT_SUBVECTOR
104 // we can match to VEXTRACTF128.
105 unsigned ElemsPerChunk = 128 / ElVT.getSizeInBits();
106
107 // This is the index of the first element of the 128-bit chunk
108 // we want.
109 unsigned NormalizedIdxVal = (((IdxVal * ElVT.getSizeInBits()) / 128)
110 * ElemsPerChunk);
111
112 SDValue VecIdx = DAG.getConstant(NormalizedIdxVal, MVT::i32);
113
114 SDValue Result = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, ResultVT, Vec,
115 VecIdx);
116
117 return Result;
118 }
119
120 return SDValue();
121}
122
123/// Generate a DAG to put 128-bits into a vector > 128 bits. This
124/// sets things up to match to an AVX VINSERTF128 instruction or a
David Greene6b381262011-02-09 15:32:06 +0000125/// simple superregister reference. Idx is an index in the 128 bits
126/// we want. It need not be aligned to a 128-bit bounday. That makes
127/// lowering INSERT_VECTOR_ELT operations easier.
David Greenea5f26012011-02-07 19:36:54 +0000128static SDValue Insert128BitVector(SDValue Result,
129 SDValue Vec,
130 SDValue Idx,
131 SelectionDAG &DAG,
132 DebugLoc dl) {
133 if (isa<ConstantSDNode>(Idx)) {
134 EVT VT = Vec.getValueType();
135 assert(VT.getSizeInBits() == 128 && "Unexpected vector size!");
136
137 EVT ElVT = VT.getVectorElementType();
138
139 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
140
141 EVT ResultVT = Result.getValueType();
142
143 // Insert the relevant 128 bits.
144 unsigned ElemsPerChunk = 128 / ElVT.getSizeInBits();
145
146 // This is the index of the first element of the 128-bit chunk
147 // we want.
148 unsigned NormalizedIdxVal = (((IdxVal * ElVT.getSizeInBits()) / 128)
149 * ElemsPerChunk);
150
151 SDValue VecIdx = DAG.getConstant(NormalizedIdxVal, MVT::i32);
152
153 Result = DAG.getNode(ISD::INSERT_SUBVECTOR, dl, ResultVT, Result, Vec,
154 VecIdx);
155 return Result;
156 }
157
158 return SDValue();
159}
160
David Greenef125a292011-02-08 19:04:41 +0000161/// Given two vectors, concat them.
162static SDValue ConcatVectors(SDValue Lower, SDValue Upper, SelectionDAG &DAG) {
163 DebugLoc dl = Lower.getDebugLoc();
164
165 assert(Lower.getValueType() == Upper.getValueType() && "Mismatched vectors!");
166
167 EVT VT = EVT::getVectorVT(*DAG.getContext(),
168 Lower.getValueType().getVectorElementType(),
169 Lower.getValueType().getVectorNumElements() * 2);
170
171 // TODO: Generalize to arbitrary vector length (this assumes 256-bit vectors).
172 assert(VT.getSizeInBits() == 256 && "Unsupported vector concat!");
173
174 // Insert the upper subvector.
175 SDValue Vec = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT), Upper,
176 DAG.getConstant(
177 // This is half the length of the result
178 // vector. Start inserting the upper 128
179 // bits here.
180 Lower.getValueType().getVectorNumElements(),
181 MVT::i32),
182 DAG, dl);
183
184 // Insert the lower subvector.
185 Vec = Insert128BitVector(Vec, Lower, DAG.getConstant(0, MVT::i32), DAG, dl);
186 return Vec;
187}
188
Chris Lattnerf0144122009-07-28 03:13:23 +0000189static TargetLoweringObjectFile *createTLOF(X86TargetMachine &TM) {
Evan Cheng2bffee22011-02-01 01:14:13 +0000190 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
191 bool is64Bit = Subtarget->is64Bit();
NAKAMURA Takumi27635382011-02-05 15:10:54 +0000192
Evan Cheng2bffee22011-02-01 01:14:13 +0000193 if (Subtarget->isTargetEnvMacho()) {
Chris Lattnere019ec12010-12-19 20:07:10 +0000194 if (is64Bit)
195 return new X8664_MachoTargetObjectFile();
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000196 return new TargetLoweringObjectFileMachO();
Michael J. Spencerec38de22010-10-10 22:04:20 +0000197 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000198
Evan Cheng2bffee22011-02-01 01:14:13 +0000199 if (Subtarget->isTargetELF()) {
Chris Lattnere019ec12010-12-19 20:07:10 +0000200 if (is64Bit)
201 return new X8664_ELFTargetObjectFile(TM);
202 return new X8632_ELFTargetObjectFile(TM);
203 }
Evan Cheng2bffee22011-02-01 01:14:13 +0000204 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
Chris Lattnere019ec12010-12-19 20:07:10 +0000205 return new TargetLoweringObjectFileCOFF();
Eric Christopher62f35a22010-07-05 19:26:33 +0000206 llvm_unreachable("unknown subtarget type");
Chris Lattnerf0144122009-07-28 03:13:23 +0000207}
208
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +0000209X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
Chris Lattnerf0144122009-07-28 03:13:23 +0000210 : TargetLowering(TM, createTLOF(TM)) {
Evan Cheng559806f2006-01-27 08:10:46 +0000211 Subtarget = &TM.getSubtarget<X86Subtarget>();
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000212 X86ScalarSSEf64 = Subtarget->hasXMMInt();
213 X86ScalarSSEf32 = Subtarget->hasXMM();
Evan Cheng25ab6902006-09-08 06:48:29 +0000214 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000215
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000216 RegInfo = TM.getRegisterInfo();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000217 TD = getTargetData();
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000218
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000219 // Set up the TargetLowering object.
Chris Lattnera34b3cf2010-12-19 20:03:11 +0000220 static MVT IntVTs[] = { MVT::i8, MVT::i16, MVT::i32, MVT::i64 };
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000221
222 // X86 is weird, it always uses i8 for shift amounts and setcc results.
Duncan Sands03228082008-11-23 15:47:28 +0000223 setBooleanContents(ZeroOrOneBooleanContent);
Eric Christopherde5e1012011-03-11 01:05:58 +0000224
225 // For 64-bit since we have so many registers use the ILP scheduler, for
226 // 32-bit code use the register pressure specific scheduling.
227 if (Subtarget->is64Bit())
228 setSchedulingPreference(Sched::ILP);
229 else
230 setSchedulingPreference(Sched::RegPressure);
Evan Cheng25ab6902006-09-08 06:48:29 +0000231 setStackPointerRegisterToSaveRestore(X86StackPtr);
Evan Cheng714554d2006-03-16 21:47:42 +0000232
Michael J. Spencer92bf38c2010-10-10 23:11:06 +0000233 if (Subtarget->isTargetWindows() && !Subtarget->isTargetCygMing()) {
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000234 // Setup Windows compiler runtime calls.
235 setLibcallName(RTLIB::SDIV_I64, "_alldiv");
Michael J. Spencer335b8062010-10-11 05:29:15 +0000236 setLibcallName(RTLIB::UDIV_I64, "_aulldiv");
237 setLibcallName(RTLIB::FPTOUINT_F64_I64, "_ftol2");
Michael J. Spencer94f7eeb2010-10-19 07:32:52 +0000238 setLibcallName(RTLIB::FPTOUINT_F32_I64, "_ftol2");
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000239 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::X86_StdCall);
Michael J. Spencer335b8062010-10-11 05:29:15 +0000240 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::X86_StdCall);
Michael J. Spencer6dad10e2010-10-27 18:52:38 +0000241 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I64, CallingConv::C);
242 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I64, CallingConv::C);
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000243 }
244
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000245 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +0000246 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000247 setUseUnderscoreSetJmp(false);
248 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000249 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000250 // MS runtime is weird: it exports _setjmp, but longjmp!
251 setUseUnderscoreSetJmp(true);
252 setUseUnderscoreLongJmp(false);
253 } else {
254 setUseUnderscoreSetJmp(true);
255 setUseUnderscoreLongJmp(true);
256 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000257
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000258 // Set up the register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000259 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
Dan Gohman71edb242010-04-30 18:30:26 +0000260 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000261 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
Evan Cheng25ab6902006-09-08 06:48:29 +0000262 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000263 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000264
Owen Anderson825b72b2009-08-11 20:47:22 +0000265 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Evan Chengc5484282006-10-04 00:56:09 +0000266
Scott Michelfdc40a02009-02-17 22:15:04 +0000267 // We don't accept any truncstore of integer registers.
Owen Anderson825b72b2009-08-11 20:47:22 +0000268 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000269 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000270 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000271 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000272 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
273 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
Evan Cheng7f042682008-10-15 02:05:31 +0000274
275 // SETOEQ and SETUNE require checking two conditions.
Owen Anderson825b72b2009-08-11 20:47:22 +0000276 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
277 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
278 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
279 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
280 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
281 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
Chris Lattnerddf89562008-01-17 19:59:44 +0000282
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000283 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
284 // operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000285 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
286 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
287 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +0000288
Evan Cheng25ab6902006-09-08 06:48:29 +0000289 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000290 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
291 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
Eli Friedman948e95a2009-05-23 09:59:16 +0000292 } else if (!UseSoftFloat) {
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000293 // We have an algorithm for SSE2->double, and we turn this into a
294 // 64-bit FILD followed by conditional FADD for other targets.
295 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Eli Friedman948e95a2009-05-23 09:59:16 +0000296 // We have an algorithm for SSE2, and we turn this into a 64-bit
297 // FILD for other targets.
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000298 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000299 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000300
301 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
302 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000303 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
304 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000305
Devang Patel6a784892009-06-05 18:48:29 +0000306 if (!UseSoftFloat) {
Bill Wendling105be5a2009-03-13 08:41:47 +0000307 // SSE has no i16 to fp conversion, only i32
308 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000309 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000310 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000311 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000312 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000313 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
314 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000315 }
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000316 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000317 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
318 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000319 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000320
Dale Johannesen73328d12007-09-19 23:55:34 +0000321 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
322 // are Legal, f80 is custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000323 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
324 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000325
Evan Cheng02568ff2006-01-30 22:13:22 +0000326 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
327 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000328 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
329 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
Evan Cheng02568ff2006-01-30 22:13:22 +0000330
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000331 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000332 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000333 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000334 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000335 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000336 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
337 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000338 }
339
340 // Handle FP_TO_UINT by promoting the destination to a larger signed
341 // conversion.
Owen Anderson825b72b2009-08-11 20:47:22 +0000342 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
343 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
344 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000345
Evan Cheng25ab6902006-09-08 06:48:29 +0000346 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000347 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
348 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Eli Friedman948e95a2009-05-23 09:59:16 +0000349 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000350 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000351 // Expand FP_TO_UINT into a select.
352 // FIXME: We would like to use a Custom expander here eventually to do
353 // the optimal thing for SSE vs. the default expansion in the legalizer.
Owen Anderson825b72b2009-08-11 20:47:22 +0000354 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000355 else
Eli Friedman948e95a2009-05-23 09:59:16 +0000356 // With SSE3 we can use fisttpll to convert to a signed i64; without
357 // SSE, we're stuck with a fistpll.
Owen Anderson825b72b2009-08-11 20:47:22 +0000358 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000359 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000360
Chris Lattner399610a2006-12-05 18:22:22 +0000361 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Michael J. Spencerec38de22010-10-10 22:04:20 +0000362 if (!X86ScalarSSEf64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000363 setOperationAction(ISD::BITCAST , MVT::f32 , Expand);
364 setOperationAction(ISD::BITCAST , MVT::i32 , Expand);
Dale Johannesene39859a2010-05-21 18:40:15 +0000365 if (Subtarget->is64Bit()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000366 setOperationAction(ISD::BITCAST , MVT::f64 , Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000367 // Without SSE, i64->f64 goes through memory.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000368 setOperationAction(ISD::BITCAST , MVT::i64 , Expand);
Dale Johannesen7d07b482010-05-21 00:52:33 +0000369 }
Chris Lattnerf3597a12006-12-05 18:45:06 +0000370 }
Chris Lattner21f66852005-12-23 05:15:23 +0000371
Dan Gohmanb00ee212008-02-18 19:34:53 +0000372 // Scalar integer divide and remainder are lowered to use operations that
373 // produce two results, to match the available instructions. This exposes
374 // the two-result form to trivial CSE, which is able to combine x/y and x%y
375 // into a single instruction.
376 //
377 // Scalar integer multiply-high is also lowered to use two-result
378 // operations, to match the available instructions. However, plain multiply
379 // (low) operations are left as Legal, as there are single-result
380 // instructions for this in x86. Using the two-result multiply instructions
381 // when both high and low results are needed must be arranged by dagcombine.
Chris Lattnere019ec12010-12-19 20:07:10 +0000382 for (unsigned i = 0, e = 4; i != e; ++i) {
383 MVT VT = IntVTs[i];
384 setOperationAction(ISD::MULHS, VT, Expand);
385 setOperationAction(ISD::MULHU, VT, Expand);
386 setOperationAction(ISD::SDIV, VT, Expand);
387 setOperationAction(ISD::UDIV, VT, Expand);
388 setOperationAction(ISD::SREM, VT, Expand);
389 setOperationAction(ISD::UREM, VT, Expand);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000390
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000391 // Add/Sub overflow ops with MVT::Glues are lowered to EFLAGS dependences.
Chris Lattnerd8ff7ec2010-12-20 01:03:27 +0000392 setOperationAction(ISD::ADDC, VT, Custom);
393 setOperationAction(ISD::ADDE, VT, Custom);
394 setOperationAction(ISD::SUBC, VT, Custom);
395 setOperationAction(ISD::SUBE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000396 }
Dan Gohmana37c9f72007-09-25 18:23:27 +0000397
Owen Anderson825b72b2009-08-11 20:47:22 +0000398 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
399 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
400 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
401 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000402 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000403 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
404 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
405 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
406 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
407 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
408 setOperationAction(ISD::FREM , MVT::f32 , Expand);
409 setOperationAction(ISD::FREM , MVT::f64 , Expand);
410 setOperationAction(ISD::FREM , MVT::f80 , Expand);
411 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000412
Owen Anderson825b72b2009-08-11 20:47:22 +0000413 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
414 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000415 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
416 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000417 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
418 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000419 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000420 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
421 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000422 }
423
Benjamin Kramer1292c222010-12-04 20:32:23 +0000424 if (Subtarget->hasPOPCNT()) {
425 setOperationAction(ISD::CTPOP , MVT::i8 , Promote);
426 } else {
427 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
428 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
429 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
430 if (Subtarget->is64Bit())
431 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
432 }
433
Owen Anderson825b72b2009-08-11 20:47:22 +0000434 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
435 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000436
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000437 // These should be promoted to a larger select which is supported.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000438 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000439 // X86 wants to expand cmov itself.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000440 setOperationAction(ISD::SELECT , MVT::i8 , Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000441 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000442 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
443 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
444 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
445 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
446 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000447 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000448 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
449 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
450 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
451 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000452 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000453 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
454 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000455 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000456 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000457
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000458 // Darwin ABI issue.
Owen Anderson825b72b2009-08-11 20:47:22 +0000459 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
460 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
461 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
462 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000463 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000464 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
465 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000466 setOperationAction(ISD::BlockAddress , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000467 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000468 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
469 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
470 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
471 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000472 setOperationAction(ISD::BlockAddress , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000473 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000474 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Owen Anderson825b72b2009-08-11 20:47:22 +0000475 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
476 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
477 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000478 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000479 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
480 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
481 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000482 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000483
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000484 if (Subtarget->hasXMM())
Owen Anderson825b72b2009-08-11 20:47:22 +0000485 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000486
Eric Christopher9a9d2752010-07-22 02:48:34 +0000487 // We may not have a libcall for MEMBARRIER so we should lower this.
488 setOperationAction(ISD::MEMBARRIER , MVT::Other, Custom);
Michael J. Spencerec38de22010-10-10 22:04:20 +0000489
Jim Grosbachf1ab49e2010-06-23 16:25:07 +0000490 // On X86 and X86-64, atomic operations are lowered to locked instructions.
491 // Locked instructions, in turn, have implicit fence semantics (all memory
492 // operations are flushed before issuing the locked instruction, and they
493 // are not buffered), so we can fold away the common pattern of
494 // fence-atomic-fence.
495 setShouldFoldAtomicFences(true);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000496
Mon P Wang63307c32008-05-05 19:05:59 +0000497 // Expand certain atomics
Chris Lattnere019ec12010-12-19 20:07:10 +0000498 for (unsigned i = 0, e = 4; i != e; ++i) {
499 MVT VT = IntVTs[i];
500 setOperationAction(ISD::ATOMIC_CMP_SWAP, VT, Custom);
501 setOperationAction(ISD::ATOMIC_LOAD_SUB, VT, Custom);
502 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000503
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000504 if (!Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000505 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
506 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
507 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
508 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
509 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
510 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
511 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000512 }
513
Evan Cheng3c992d22006-03-07 02:02:57 +0000514 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000515 if (!Subtarget->isTargetDarwin() &&
516 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000517 !Subtarget->isTargetCygMing()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000518 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Dan Gohman44066042008-07-01 00:05:16 +0000519 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000520
Owen Anderson825b72b2009-08-11 20:47:22 +0000521 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
522 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
523 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
524 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000525 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000526 setExceptionPointerRegister(X86::RAX);
527 setExceptionSelectorRegister(X86::RDX);
528 } else {
529 setExceptionPointerRegister(X86::EAX);
530 setExceptionSelectorRegister(X86::EDX);
531 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000532 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
533 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000534
Owen Anderson825b72b2009-08-11 20:47:22 +0000535 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000536
Owen Anderson825b72b2009-08-11 20:47:22 +0000537 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000538
Nate Begemanacc398c2006-01-25 18:21:52 +0000539 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000540 setOperationAction(ISD::VASTART , MVT::Other, Custom);
541 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000542 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000543 setOperationAction(ISD::VAARG , MVT::Other, Custom);
544 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000545 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000546 setOperationAction(ISD::VAARG , MVT::Other, Expand);
547 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000548 }
Evan Chengae642192007-03-02 23:16:35 +0000549
Owen Anderson825b72b2009-08-11 20:47:22 +0000550 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
551 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000552 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000553 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Michael J. Spencere9c253e2010-10-21 01:41:01 +0000554 if (Subtarget->isTargetCygMing() || Subtarget->isTargetWindows())
Owen Anderson825b72b2009-08-11 20:47:22 +0000555 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +0000556 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000557 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000558
Evan Chengc7ce29b2009-02-13 22:36:38 +0000559 if (!UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000560 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000561 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000562 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
563 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000564
Evan Cheng223547a2006-01-31 22:28:30 +0000565 // Use ANDPD to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000566 setOperationAction(ISD::FABS , MVT::f64, Custom);
567 setOperationAction(ISD::FABS , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000568
569 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000570 setOperationAction(ISD::FNEG , MVT::f64, Custom);
571 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000572
Evan Cheng68c47cb2007-01-05 07:55:56 +0000573 // Use ANDPD and ORPD to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000574 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
575 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000576
Evan Chengd25e9e82006-02-02 00:28:23 +0000577 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000578 setOperationAction(ISD::FSIN , MVT::f64, Expand);
579 setOperationAction(ISD::FCOS , MVT::f64, Expand);
580 setOperationAction(ISD::FSIN , MVT::f32, Expand);
581 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000582
Chris Lattnera54aa942006-01-29 06:26:08 +0000583 // Expand FP immediates into loads from the stack, except for the special
584 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000585 addLegalFPImmediate(APFloat(+0.0)); // xorpd
586 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Evan Chengc7ce29b2009-02-13 22:36:38 +0000587 } else if (!UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000588 // Use SSE for f32, x87 for f64.
589 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000590 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
591 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000592
593 // Use ANDPS to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000594 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000595
596 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000597 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000598
Owen Anderson825b72b2009-08-11 20:47:22 +0000599 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000600
601 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000602 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
603 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000604
605 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000606 setOperationAction(ISD::FSIN , MVT::f32, Expand);
607 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000608
Nate Begemane1795842008-02-14 08:57:00 +0000609 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000610 addLegalFPImmediate(APFloat(+0.0f)); // xorps
611 addLegalFPImmediate(APFloat(+0.0)); // FLD0
612 addLegalFPImmediate(APFloat(+1.0)); // FLD1
613 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
614 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
615
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000616 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000617 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
618 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000619 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000620 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000621 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000622 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000623 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
624 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000625
Owen Anderson825b72b2009-08-11 20:47:22 +0000626 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
627 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
628 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
629 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000630
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000631 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000632 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
633 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000634 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000635 addLegalFPImmediate(APFloat(+0.0)); // FLD0
636 addLegalFPImmediate(APFloat(+1.0)); // FLD1
637 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
638 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000639 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
640 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
641 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
642 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000643 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000644
Dale Johannesen59a58732007-08-05 18:49:15 +0000645 // Long double always uses X87.
Evan Cheng92722532009-03-26 23:06:32 +0000646 if (!UseSoftFloat) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000647 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
648 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
649 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000650 {
Benjamin Kramer98383962010-12-04 14:22:24 +0000651 APFloat TmpFlt = APFloat::getZero(APFloat::x87DoubleExtended);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000652 addLegalFPImmediate(TmpFlt); // FLD0
653 TmpFlt.changeSign();
654 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
Benjamin Kramer98383962010-12-04 14:22:24 +0000655
656 bool ignored;
Evan Chengc7ce29b2009-02-13 22:36:38 +0000657 APFloat TmpFlt2(+1.0);
658 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
659 &ignored);
660 addLegalFPImmediate(TmpFlt2); // FLD1
661 TmpFlt2.changeSign();
662 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
663 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000664
Evan Chengc7ce29b2009-02-13 22:36:38 +0000665 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000666 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
667 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000668 }
Dale Johannesen2f429012007-09-26 21:10:55 +0000669 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000670
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000671 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000672 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
673 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
674 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000675
Owen Anderson825b72b2009-08-11 20:47:22 +0000676 setOperationAction(ISD::FLOG, MVT::f80, Expand);
677 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
678 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
679 setOperationAction(ISD::FEXP, MVT::f80, Expand);
680 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000681
Mon P Wangf007a8b2008-11-06 05:31:54 +0000682 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000683 // (for widening) or expand (for scalarization). Then we will selectively
684 // turn on ones that can be effectively codegen'd.
Owen Anderson825b72b2009-08-11 20:47:22 +0000685 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
686 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
687 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
688 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
689 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
690 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
691 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
692 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
693 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
694 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
695 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
696 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
697 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
698 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
699 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
700 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
701 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000702 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
David Greenecfe33c42011-01-26 19:13:22 +0000703 setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
704 setOperationAction(ISD::INSERT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000705 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
706 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
707 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
708 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
709 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
710 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
711 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
712 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
713 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
714 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
715 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
716 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
717 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
718 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
719 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
720 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
721 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
722 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
723 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
724 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
725 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
726 setOperationAction(ISD::VSETCC, (MVT::SimpleValueType)VT, Expand);
727 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
728 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
729 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
730 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
731 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
732 setOperationAction(ISD::FP_TO_UINT, (MVT::SimpleValueType)VT, Expand);
733 setOperationAction(ISD::FP_TO_SINT, (MVT::SimpleValueType)VT, Expand);
734 setOperationAction(ISD::UINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
735 setOperationAction(ISD::SINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
Dan Gohman87862e72009-12-11 21:31:27 +0000736 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000737 setOperationAction(ISD::TRUNCATE, (MVT::SimpleValueType)VT, Expand);
738 setOperationAction(ISD::SIGN_EXTEND, (MVT::SimpleValueType)VT, Expand);
739 setOperationAction(ISD::ZERO_EXTEND, (MVT::SimpleValueType)VT, Expand);
740 setOperationAction(ISD::ANY_EXTEND, (MVT::SimpleValueType)VT, Expand);
741 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
742 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
743 setTruncStoreAction((MVT::SimpleValueType)VT,
744 (MVT::SimpleValueType)InnerVT, Expand);
745 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
746 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
747 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000748 }
749
Evan Chengc7ce29b2009-02-13 22:36:38 +0000750 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
751 // with -msoft-float, disable use of MMX as well.
Chris Lattner2a786eb2010-12-19 20:19:20 +0000752 if (!UseSoftFloat && Subtarget->hasMMX()) {
Dale Johannesene93d99c2010-10-20 21:32:10 +0000753 addRegisterClass(MVT::x86mmx, X86::VR64RegisterClass);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000754 // No operations on x86mmx supported, everything uses intrinsics.
Evan Cheng470a6ad2006-02-22 02:26:30 +0000755 }
756
Dale Johannesen0488fb62010-09-30 23:57:10 +0000757 // MMX-sized vectors (other than x86mmx) are expected to be expanded
758 // into smaller operations.
759 setOperationAction(ISD::MULHS, MVT::v8i8, Expand);
760 setOperationAction(ISD::MULHS, MVT::v4i16, Expand);
761 setOperationAction(ISD::MULHS, MVT::v2i32, Expand);
762 setOperationAction(ISD::MULHS, MVT::v1i64, Expand);
763 setOperationAction(ISD::AND, MVT::v8i8, Expand);
764 setOperationAction(ISD::AND, MVT::v4i16, Expand);
765 setOperationAction(ISD::AND, MVT::v2i32, Expand);
766 setOperationAction(ISD::AND, MVT::v1i64, Expand);
767 setOperationAction(ISD::OR, MVT::v8i8, Expand);
768 setOperationAction(ISD::OR, MVT::v4i16, Expand);
769 setOperationAction(ISD::OR, MVT::v2i32, Expand);
770 setOperationAction(ISD::OR, MVT::v1i64, Expand);
771 setOperationAction(ISD::XOR, MVT::v8i8, Expand);
772 setOperationAction(ISD::XOR, MVT::v4i16, Expand);
773 setOperationAction(ISD::XOR, MVT::v2i32, Expand);
774 setOperationAction(ISD::XOR, MVT::v1i64, Expand);
775 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Expand);
776 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Expand);
777 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Expand);
778 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Expand);
779 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v1i64, Expand);
780 setOperationAction(ISD::SELECT, MVT::v8i8, Expand);
781 setOperationAction(ISD::SELECT, MVT::v4i16, Expand);
782 setOperationAction(ISD::SELECT, MVT::v2i32, Expand);
783 setOperationAction(ISD::SELECT, MVT::v1i64, Expand);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000784 setOperationAction(ISD::BITCAST, MVT::v8i8, Expand);
785 setOperationAction(ISD::BITCAST, MVT::v4i16, Expand);
786 setOperationAction(ISD::BITCAST, MVT::v2i32, Expand);
787 setOperationAction(ISD::BITCAST, MVT::v1i64, Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000788
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000789 if (!UseSoftFloat && Subtarget->hasXMM()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000790 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000791
Owen Anderson825b72b2009-08-11 20:47:22 +0000792 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
793 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
794 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
795 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
796 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
797 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
798 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
799 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
800 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
801 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
802 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
803 setOperationAction(ISD::VSETCC, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000804 }
805
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000806 if (!UseSoftFloat && Subtarget->hasXMMInt()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000807 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000808
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000809 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
810 // registers cannot be used even for integer operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000811 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
812 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
813 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
814 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000815
Owen Anderson825b72b2009-08-11 20:47:22 +0000816 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
817 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
818 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
819 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
820 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
821 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
822 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
823 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
824 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
825 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
826 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
827 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
828 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
829 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
830 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
831 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000832
Owen Anderson825b72b2009-08-11 20:47:22 +0000833 setOperationAction(ISD::VSETCC, MVT::v2f64, Custom);
834 setOperationAction(ISD::VSETCC, MVT::v16i8, Custom);
835 setOperationAction(ISD::VSETCC, MVT::v8i16, Custom);
836 setOperationAction(ISD::VSETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000837
Owen Anderson825b72b2009-08-11 20:47:22 +0000838 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
839 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
840 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
841 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
842 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000843
Mon P Wangeb38ebf2010-01-24 00:05:03 +0000844 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2f64, Custom);
845 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2i64, Custom);
846 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i8, Custom);
847 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i16, Custom);
848 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Custom);
849
Evan Cheng2c3ae372006-04-12 21:21:57 +0000850 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Owen Anderson825b72b2009-08-11 20:47:22 +0000851 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
852 EVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000853 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000854 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000855 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000856 // Do not attempt to custom lower non-128-bit vectors
857 if (!VT.is128BitVector())
858 continue;
Owen Anderson825b72b2009-08-11 20:47:22 +0000859 setOperationAction(ISD::BUILD_VECTOR,
860 VT.getSimpleVT().SimpleTy, Custom);
861 setOperationAction(ISD::VECTOR_SHUFFLE,
862 VT.getSimpleVT().SimpleTy, Custom);
863 setOperationAction(ISD::EXTRACT_VECTOR_ELT,
864 VT.getSimpleVT().SimpleTy, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000865 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000866
Owen Anderson825b72b2009-08-11 20:47:22 +0000867 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
868 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
869 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
870 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
871 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
872 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000873
Nate Begemancdd1eec2008-02-12 22:51:28 +0000874 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000875 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
876 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000877 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000878
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000879 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000880 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; i++) {
881 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
Owen Andersone50ed302009-08-10 22:56:29 +0000882 EVT VT = SVT;
David Greene9b9838d2009-06-29 16:47:10 +0000883
884 // Do not attempt to promote non-128-bit vectors
Chris Lattner32b4b5a2010-07-05 05:53:14 +0000885 if (!VT.is128BitVector())
David Greene9b9838d2009-06-29 16:47:10 +0000886 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +0000887
Owen Andersond6662ad2009-08-10 20:46:15 +0000888 setOperationAction(ISD::AND, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000889 AddPromotedToType (ISD::AND, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000890 setOperationAction(ISD::OR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000891 AddPromotedToType (ISD::OR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000892 setOperationAction(ISD::XOR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000893 AddPromotedToType (ISD::XOR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000894 setOperationAction(ISD::LOAD, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000895 AddPromotedToType (ISD::LOAD, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000896 setOperationAction(ISD::SELECT, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000897 AddPromotedToType (ISD::SELECT, SVT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000898 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000899
Owen Anderson825b72b2009-08-11 20:47:22 +0000900 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000901
Evan Cheng2c3ae372006-04-12 21:21:57 +0000902 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000903 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
904 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
905 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
906 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000907
Owen Anderson825b72b2009-08-11 20:47:22 +0000908 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
909 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000910 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000911
Nate Begeman14d12ca2008-02-11 04:19:36 +0000912 if (Subtarget->hasSSE41()) {
Dale Johannesen54feef22010-05-27 20:12:41 +0000913 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
914 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
915 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
916 setOperationAction(ISD::FRINT, MVT::f32, Legal);
917 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
918 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
919 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
920 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
921 setOperationAction(ISD::FRINT, MVT::f64, Legal);
922 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
923
Nate Begeman14d12ca2008-02-11 04:19:36 +0000924 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +0000925 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000926
Nate Begemanbdcb5af2010-07-27 22:37:06 +0000927 // Can turn SHL into an integer multiply.
928 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
Nate Begeman51409212010-07-28 00:21:48 +0000929 setOperationAction(ISD::SHL, MVT::v16i8, Custom);
Nate Begemanbdcb5af2010-07-27 22:37:06 +0000930
Nate Begeman14d12ca2008-02-11 04:19:36 +0000931 // i8 and i16 vectors are custom , because the source register and source
932 // source memory operand types are not the same width. f32 vectors are
933 // custom since the immediate controlling the insert encodes additional
934 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +0000935 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
936 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
937 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
938 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000939
Owen Anderson825b72b2009-08-11 20:47:22 +0000940 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
941 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
942 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
943 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000944
945 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000946 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
947 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000948 }
949 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000950
Chris Lattnera34b3cf2010-12-19 20:03:11 +0000951 if (Subtarget->hasSSE42())
Owen Anderson825b72b2009-08-11 20:47:22 +0000952 setOperationAction(ISD::VSETCC, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000953
David Greene9b9838d2009-06-29 16:47:10 +0000954 if (!UseSoftFloat && Subtarget->hasAVX()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000955 addRegisterClass(MVT::v8f32, X86::VR256RegisterClass);
956 addRegisterClass(MVT::v4f64, X86::VR256RegisterClass);
957 addRegisterClass(MVT::v8i32, X86::VR256RegisterClass);
958 addRegisterClass(MVT::v4i64, X86::VR256RegisterClass);
Bruno Cardoso Lopes405f11b2010-08-10 01:43:16 +0000959 addRegisterClass(MVT::v32i8, X86::VR256RegisterClass);
David Greened94c1012009-06-29 22:50:51 +0000960
Owen Anderson825b72b2009-08-11 20:47:22 +0000961 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
962 setOperationAction(ISD::LOAD, MVT::v8i32, Legal);
963 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
964 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
David Greene54d8eba2011-01-27 22:38:56 +0000965
Owen Anderson825b72b2009-08-11 20:47:22 +0000966 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
967 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
968 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
969 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
970 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
971 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000972
Owen Anderson825b72b2009-08-11 20:47:22 +0000973 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
974 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
975 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
976 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
977 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
978 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000979
David Greene54d8eba2011-01-27 22:38:56 +0000980 // Custom lower build_vector, vector_shuffle, scalar_to_vector,
981 // insert_vector_elt extract_subvector and extract_vector_elt for
982 // 256-bit types.
983 for (unsigned i = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
984 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE;
985 ++i) {
986 MVT::SimpleValueType VT = (MVT::SimpleValueType)i;
987 // Do not attempt to custom lower non-256-bit vectors
988 if (!isPowerOf2_32(MVT(VT).getVectorNumElements())
989 || (MVT(VT).getSizeInBits() < 256))
David Greene9b9838d2009-06-29 16:47:10 +0000990 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000991 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
992 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
David Greene54d8eba2011-01-27 22:38:56 +0000993 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000994 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
David Greene54d8eba2011-01-27 22:38:56 +0000995 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000996 }
David Greene54d8eba2011-01-27 22:38:56 +0000997 // Custom-lower insert_subvector and extract_subvector based on
998 // the result type.
999 for (unsigned i = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
1000 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE;
1001 ++i) {
1002 MVT::SimpleValueType VT = (MVT::SimpleValueType)i;
1003 // Do not attempt to custom lower non-256-bit vectors
1004 if (!isPowerOf2_32(MVT(VT).getVectorNumElements()))
David Greene9b9838d2009-06-29 16:47:10 +00001005 continue;
David Greene54d8eba2011-01-27 22:38:56 +00001006
1007 if (MVT(VT).getSizeInBits() == 128) {
1008 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001009 }
David Greene54d8eba2011-01-27 22:38:56 +00001010 else if (MVT(VT).getSizeInBits() == 256) {
1011 setOperationAction(ISD::INSERT_SUBVECTOR, VT, Custom);
1012 }
David Greene9b9838d2009-06-29 16:47:10 +00001013 }
1014
David Greene54d8eba2011-01-27 22:38:56 +00001015 // Promote v32i8, v16i16, v8i32 select, and, or, xor to v4i64.
1016 // Don't promote loads because we need them for VPERM vector index versions.
1017
1018 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
1019 VT != (unsigned)MVT::LAST_VECTOR_VALUETYPE;
1020 VT++) {
1021 if (!isPowerOf2_32(MVT((MVT::SimpleValueType)VT).getVectorNumElements())
1022 || (MVT((MVT::SimpleValueType)VT).getSizeInBits() < 256))
1023 continue;
1024 setOperationAction(ISD::AND, (MVT::SimpleValueType)VT, Promote);
1025 AddPromotedToType (ISD::AND, (MVT::SimpleValueType)VT, MVT::v4i64);
1026 setOperationAction(ISD::OR, (MVT::SimpleValueType)VT, Promote);
1027 AddPromotedToType (ISD::OR, (MVT::SimpleValueType)VT, MVT::v4i64);
1028 setOperationAction(ISD::XOR, (MVT::SimpleValueType)VT, Promote);
1029 AddPromotedToType (ISD::XOR, (MVT::SimpleValueType)VT, MVT::v4i64);
1030 //setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Promote);
1031 //AddPromotedToType (ISD::LOAD, (MVT::SimpleValueType)VT, MVT::v4i64);
1032 setOperationAction(ISD::SELECT, (MVT::SimpleValueType)VT, Promote);
1033 AddPromotedToType (ISD::SELECT, (MVT::SimpleValueType)VT, MVT::v4i64);
1034 }
David Greene9b9838d2009-06-29 16:47:10 +00001035 }
1036
Evan Cheng6be2c582006-04-05 23:38:46 +00001037 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +00001038 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +00001039
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001040
Eli Friedman962f5492010-06-02 19:35:46 +00001041 // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
1042 // handle type legalization for these operations here.
Dan Gohman71c62a22010-06-02 19:13:40 +00001043 //
Eli Friedman962f5492010-06-02 19:35:46 +00001044 // FIXME: We really should do custom legalization for addition and
1045 // subtraction on x86-32 once PR3203 is fixed. We really can't do much better
1046 // than generic legalization for 64-bit multiplication-with-overflow, though.
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001047 for (unsigned i = 0, e = 3+Subtarget->is64Bit(); i != e; ++i) {
1048 // Add/Sub/Mul with overflow operations are custom lowered.
1049 MVT VT = IntVTs[i];
1050 setOperationAction(ISD::SADDO, VT, Custom);
1051 setOperationAction(ISD::UADDO, VT, Custom);
1052 setOperationAction(ISD::SSUBO, VT, Custom);
1053 setOperationAction(ISD::USUBO, VT, Custom);
1054 setOperationAction(ISD::SMULO, VT, Custom);
1055 setOperationAction(ISD::UMULO, VT, Custom);
Eli Friedmana993f0a2010-06-02 00:27:18 +00001056 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001057
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001058 // There are no 8-bit 3-address imul/mul instructions
1059 setOperationAction(ISD::SMULO, MVT::i8, Expand);
1060 setOperationAction(ISD::UMULO, MVT::i8, Expand);
Bill Wendling41ea7e72008-11-24 19:21:46 +00001061
Evan Chengd54f2d52009-03-31 19:38:51 +00001062 if (!Subtarget->is64Bit()) {
1063 // These libcalls are not available in 32-bit.
1064 setLibcallName(RTLIB::SHL_I128, 0);
1065 setLibcallName(RTLIB::SRL_I128, 0);
1066 setLibcallName(RTLIB::SRA_I128, 0);
1067 }
1068
Evan Cheng206ee9d2006-07-07 08:33:52 +00001069 // We have target-specific dag combine patterns for the following nodes:
1070 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00001071 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Evan Chengd880b972008-05-09 21:53:03 +00001072 setTargetDAGCombine(ISD::BUILD_VECTOR);
Chris Lattner83e6c992006-10-04 06:57:07 +00001073 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +00001074 setTargetDAGCombine(ISD::SHL);
1075 setTargetDAGCombine(ISD::SRA);
1076 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +00001077 setTargetDAGCombine(ISD::OR);
Nate Begemanb65c1752010-12-17 22:55:37 +00001078 setTargetDAGCombine(ISD::AND);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001079 setTargetDAGCombine(ISD::ADD);
1080 setTargetDAGCombine(ISD::SUB);
Chris Lattner149a4e52008-02-22 02:09:43 +00001081 setTargetDAGCombine(ISD::STORE);
Evan Cheng2e489c42009-12-16 00:53:11 +00001082 setTargetDAGCombine(ISD::ZERO_EXTEND);
Evan Cheng0b0cd912009-03-28 05:57:29 +00001083 if (Subtarget->is64Bit())
1084 setTargetDAGCombine(ISD::MUL);
Evan Cheng206ee9d2006-07-07 08:33:52 +00001085
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001086 computeRegisterProperties();
1087
Evan Cheng05219282011-01-06 06:52:41 +00001088 // On Darwin, -Os means optimize for size without hurting performance,
1089 // do not reduce the limit.
Dan Gohman87060f52008-06-30 21:00:56 +00001090 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001091 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 16 : 8;
Evan Cheng255f20f2010-04-01 06:04:33 +00001092 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001093 maxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
1094 maxStoresPerMemmove = 8; // For @llvm.memmove -> sequence of stores
1095 maxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Evan Chengfb8075d2008-02-28 00:43:03 +00001096 setPrefLoopAlignment(16);
Evan Cheng6ebf7bc2009-05-13 21:42:09 +00001097 benefitFromCodePlacementOpt = true;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001098}
1099
Scott Michel5b8f82e2008-03-10 15:42:14 +00001100
Owen Anderson825b72b2009-08-11 20:47:22 +00001101MVT::SimpleValueType X86TargetLowering::getSetCCResultType(EVT VT) const {
1102 return MVT::i8;
Scott Michel5b8f82e2008-03-10 15:42:14 +00001103}
1104
1105
Evan Cheng29286502008-01-23 23:17:41 +00001106/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1107/// the desired ByVal argument alignment.
1108static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
1109 if (MaxAlign == 16)
1110 return;
1111 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
1112 if (VTy->getBitWidth() == 128)
1113 MaxAlign = 16;
Evan Cheng29286502008-01-23 23:17:41 +00001114 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
1115 unsigned EltAlign = 0;
1116 getMaxByValAlign(ATy->getElementType(), EltAlign);
1117 if (EltAlign > MaxAlign)
1118 MaxAlign = EltAlign;
1119 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
1120 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1121 unsigned EltAlign = 0;
1122 getMaxByValAlign(STy->getElementType(i), EltAlign);
1123 if (EltAlign > MaxAlign)
1124 MaxAlign = EltAlign;
1125 if (MaxAlign == 16)
1126 break;
1127 }
1128 }
1129 return;
1130}
1131
1132/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1133/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001134/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1135/// are at 4-byte boundaries.
Evan Cheng29286502008-01-23 23:17:41 +00001136unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001137 if (Subtarget->is64Bit()) {
1138 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001139 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001140 if (TyAlign > 8)
1141 return TyAlign;
1142 return 8;
1143 }
1144
Evan Cheng29286502008-01-23 23:17:41 +00001145 unsigned Align = 4;
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001146 if (Subtarget->hasXMM())
Dale Johannesen0c191872008-02-08 19:48:20 +00001147 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001148 return Align;
1149}
Chris Lattner2b02a442007-02-25 08:29:00 +00001150
Evan Chengf0df0312008-05-15 08:39:06 +00001151/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001152/// and store operations as a result of memset, memcpy, and memmove
1153/// lowering. If DstAlign is zero that means it's safe to destination
1154/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1155/// means there isn't a need to check it against alignment requirement,
1156/// probably because the source does not need to be loaded. If
1157/// 'NonScalarIntSafe' is true, that means it's safe to return a
1158/// non-scalar-integer type, e.g. empty string source, constant, or loaded
1159/// from memory. 'MemcpyStrSrc' indicates whether the memcpy source is
1160/// constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001161/// It returns EVT::Other if the type should be determined using generic
1162/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001163EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001164X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1165 unsigned DstAlign, unsigned SrcAlign,
Evan Chengf28f8bc2010-04-02 19:36:14 +00001166 bool NonScalarIntSafe,
Evan Chengc3b0c342010-04-08 07:37:57 +00001167 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001168 MachineFunction &MF) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001169 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1170 // linux. This is because the stack realignment code can't handle certain
1171 // cases like PR2962. This should be removed when PR2962 is fixed.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001172 const Function *F = MF.getFunction();
Evan Chenga5e13622011-01-07 19:35:30 +00001173 if (NonScalarIntSafe &&
1174 !F->hasFnAttr(Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001175 if (Size >= 16 &&
Evan Chenga5e13622011-01-07 19:35:30 +00001176 (Subtarget->isUnalignedMemAccessFast() ||
1177 ((DstAlign == 0 || DstAlign >= 16) &&
1178 (SrcAlign == 0 || SrcAlign >= 16))) &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001179 Subtarget->getStackAlignment() >= 16) {
1180 if (Subtarget->hasSSE2())
1181 return MVT::v4i32;
Evan Chengf28f8bc2010-04-02 19:36:14 +00001182 if (Subtarget->hasSSE1())
Evan Cheng255f20f2010-04-01 06:04:33 +00001183 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001184 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001185 !Subtarget->is64Bit() &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001186 Subtarget->getStackAlignment() >= 8 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001187 Subtarget->hasXMMInt()) {
Evan Chengc3b0c342010-04-08 07:37:57 +00001188 // Do not use f64 to lower memcpy if source is string constant. It's
1189 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001190 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001191 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001192 }
Evan Chengf0df0312008-05-15 08:39:06 +00001193 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001194 return MVT::i64;
1195 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001196}
1197
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001198/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1199/// current function. The returned value is a member of the
1200/// MachineJumpTableInfo::JTEntryKind enum.
1201unsigned X86TargetLowering::getJumpTableEncoding() const {
1202 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1203 // symbol.
1204 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1205 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001206 return MachineJumpTableInfo::EK_Custom32;
Michael J. Spencerec38de22010-10-10 22:04:20 +00001207
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001208 // Otherwise, use the normal jump table encoding heuristics.
1209 return TargetLowering::getJumpTableEncoding();
1210}
1211
Chris Lattnerc64daab2010-01-26 05:02:42 +00001212const MCExpr *
1213X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1214 const MachineBasicBlock *MBB,
1215 unsigned uid,MCContext &Ctx) const{
1216 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1217 Subtarget->isPICStyleGOT());
1218 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1219 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001220 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1221 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001222}
1223
Evan Chengcc415862007-11-09 01:32:10 +00001224/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1225/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001226SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001227 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001228 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001229 // This doesn't have DebugLoc associated with it, but is not really the
1230 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001231 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001232 return Table;
1233}
1234
Chris Lattner589c6f62010-01-26 06:28:43 +00001235/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1236/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1237/// MCExpr.
1238const MCExpr *X86TargetLowering::
1239getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1240 MCContext &Ctx) const {
1241 // X86-64 uses RIP relative addressing based on the jump table label.
1242 if (Subtarget->isPICStyleRIPRel())
1243 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1244
1245 // Otherwise, the reference is relative to the PIC base.
Chris Lattner142b5312010-11-14 22:48:15 +00001246 return MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
Chris Lattner589c6f62010-01-26 06:28:43 +00001247}
1248
Bill Wendlingb4202b82009-07-01 18:50:55 +00001249/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +00001250unsigned X86TargetLowering::getFunctionAlignment(const Function *F) const {
Dan Gohman25103a22009-08-18 00:20:06 +00001251 return F->hasFnAttr(Attribute::OptimizeForSize) ? 0 : 4;
Bill Wendling20c568f2009-06-30 22:38:32 +00001252}
1253
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001254// FIXME: Why this routine is here? Move to RegInfo!
Evan Chengdee81012010-07-26 21:50:05 +00001255std::pair<const TargetRegisterClass*, uint8_t>
1256X86TargetLowering::findRepresentativeClass(EVT VT) const{
1257 const TargetRegisterClass *RRC = 0;
1258 uint8_t Cost = 1;
1259 switch (VT.getSimpleVT().SimpleTy) {
1260 default:
1261 return TargetLowering::findRepresentativeClass(VT);
1262 case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
1263 RRC = (Subtarget->is64Bit()
1264 ? X86::GR64RegisterClass : X86::GR32RegisterClass);
1265 break;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001266 case MVT::x86mmx:
Evan Chengdee81012010-07-26 21:50:05 +00001267 RRC = X86::VR64RegisterClass;
1268 break;
1269 case MVT::f32: case MVT::f64:
1270 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1271 case MVT::v4f32: case MVT::v2f64:
1272 case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1273 case MVT::v4f64:
1274 RRC = X86::VR128RegisterClass;
1275 break;
1276 }
1277 return std::make_pair(RRC, Cost);
1278}
1279
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001280bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1281 unsigned &Offset) const {
1282 if (!Subtarget->isTargetLinux())
1283 return false;
1284
1285 if (Subtarget->is64Bit()) {
1286 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1287 Offset = 0x28;
1288 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1289 AddressSpace = 256;
1290 else
1291 AddressSpace = 257;
1292 } else {
1293 // %gs:0x14 on i386
1294 Offset = 0x14;
1295 AddressSpace = 256;
1296 }
1297 return true;
1298}
1299
1300
Chris Lattner2b02a442007-02-25 08:29:00 +00001301//===----------------------------------------------------------------------===//
1302// Return Value Calling Convention Implementation
1303//===----------------------------------------------------------------------===//
1304
Chris Lattner59ed56b2007-02-28 04:55:35 +00001305#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001306
Michael J. Spencerec38de22010-10-10 22:04:20 +00001307bool
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001308X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001309 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001310 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001311 SmallVector<CCValAssign, 16> RVLocs;
1312 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001313 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001314 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001315}
1316
Dan Gohman98ca4f22009-08-05 01:29:28 +00001317SDValue
1318X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001319 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001320 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001321 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001322 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001323 MachineFunction &MF = DAG.getMachineFunction();
1324 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001325
Chris Lattner9774c912007-02-27 05:28:59 +00001326 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001327 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1328 RVLocs, *DAG.getContext());
1329 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001330
Evan Chengdcea1632010-02-04 02:40:39 +00001331 // Add the regs to the liveout set for the function.
1332 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1333 for (unsigned i = 0; i != RVLocs.size(); ++i)
1334 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1335 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001336
Dan Gohman475871a2008-07-27 21:46:04 +00001337 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001338
Dan Gohman475871a2008-07-27 21:46:04 +00001339 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001340 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1341 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001342 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1343 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001344
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001345 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001346 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1347 CCValAssign &VA = RVLocs[i];
1348 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001349 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001350 EVT ValVT = ValToCopy.getValueType();
1351
Dale Johannesenc4510512010-09-24 19:05:48 +00001352 // If this is x86-64, and we disabled SSE, we can't return FP values,
1353 // or SSE or MMX vectors.
1354 if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
1355 VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001356 (Subtarget->is64Bit() && !Subtarget->hasXMM())) {
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001357 report_fatal_error("SSE register return with SSE disabled");
1358 }
1359 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1360 // llvm-gcc has never done it right and no one has noticed, so this
1361 // should be OK for now.
1362 if (ValVT == MVT::f64 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001363 (Subtarget->is64Bit() && !Subtarget->hasXMMInt()))
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001364 report_fatal_error("SSE2 register return with SSE2 disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001365
Chris Lattner447ff682008-03-11 03:23:40 +00001366 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1367 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001368 if (VA.getLocReg() == X86::ST0 ||
1369 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001370 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1371 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001372 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001373 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001374 RetOps.push_back(ValToCopy);
1375 // Don't emit a copytoreg.
1376 continue;
1377 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001378
Evan Cheng242b38b2009-02-23 09:03:22 +00001379 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1380 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001381 if (Subtarget->is64Bit()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001382 if (ValVT == MVT::x86mmx) {
Chris Lattner97a2a562010-08-26 05:24:29 +00001383 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001384 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ValToCopy);
Eric Christopher90eb4022010-07-22 00:26:08 +00001385 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1386 ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001387 // If we don't have SSE2 available, convert to v4f32 so the generated
1388 // register is legal.
1389 if (!Subtarget->hasSSE2())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001390 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32,ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001391 }
Evan Cheng242b38b2009-02-23 09:03:22 +00001392 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001393 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00001394
Dale Johannesendd64c412009-02-04 00:33:20 +00001395 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001396 Flag = Chain.getValue(1);
1397 }
Dan Gohman61a92132008-04-21 23:59:07 +00001398
1399 // The x86-64 ABI for returning structs by value requires that we copy
1400 // the sret argument into %rax for the return. We saved the argument into
1401 // a virtual register in the entry block, so now we copy the value out
1402 // and into %rax.
1403 if (Subtarget->is64Bit() &&
1404 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1405 MachineFunction &MF = DAG.getMachineFunction();
1406 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1407 unsigned Reg = FuncInfo->getSRetReturnReg();
Michael J. Spencerec38de22010-10-10 22:04:20 +00001408 assert(Reg &&
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001409 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001410 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001411
Dale Johannesendd64c412009-02-04 00:33:20 +00001412 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001413 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001414
1415 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001416 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001417 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001418
Chris Lattner447ff682008-03-11 03:23:40 +00001419 RetOps[0] = Chain; // Update chain.
1420
1421 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001422 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001423 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001424
1425 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001426 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001427}
1428
Evan Cheng3d2125c2010-11-30 23:55:39 +00001429bool X86TargetLowering::isUsedByReturnOnly(SDNode *N) const {
1430 if (N->getNumValues() != 1)
1431 return false;
1432 if (!N->hasNUsesOfValue(1, 0))
1433 return false;
1434
1435 SDNode *Copy = *N->use_begin();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001436 if (Copy->getOpcode() != ISD::CopyToReg &&
1437 Copy->getOpcode() != ISD::FP_EXTEND)
Evan Cheng3d2125c2010-11-30 23:55:39 +00001438 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001439
1440 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001441 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001442 UI != UE; ++UI) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001443 if (UI->getOpcode() != X86ISD::RET_FLAG)
1444 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001445 HasRet = true;
1446 }
Evan Cheng3d2125c2010-11-30 23:55:39 +00001447
Evan Cheng1bf891a2010-12-01 22:59:46 +00001448 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001449}
1450
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001451EVT
1452X86TargetLowering::getTypeForExtArgOrReturn(LLVMContext &Context, EVT VT,
Cameron Zwarich44579682011-03-17 14:21:56 +00001453 ISD::NodeType ExtendKind) const {
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001454 MVT ReturnMVT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001455 // TODO: Is this also valid on 32-bit?
1456 if (Subtarget->is64Bit() && VT == MVT::i1 && ExtendKind == ISD::ZERO_EXTEND)
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001457 ReturnMVT = MVT::i8;
1458 else
1459 ReturnMVT = MVT::i32;
1460
1461 EVT MinVT = getRegisterType(Context, ReturnMVT);
1462 return VT.bitsLT(MinVT) ? MinVT : VT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001463}
1464
Dan Gohman98ca4f22009-08-05 01:29:28 +00001465/// LowerCallResult - Lower the result values of a call into the
1466/// appropriate copies out of appropriate physical registers.
1467///
1468SDValue
1469X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001470 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001471 const SmallVectorImpl<ISD::InputArg> &Ins,
1472 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001473 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001474
Chris Lattnere32bbf62007-02-28 07:09:55 +00001475 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001476 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001477 bool Is64Bit = Subtarget->is64Bit();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001478 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +00001479 RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001480 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001481
Chris Lattner3085e152007-02-25 08:59:22 +00001482 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001483 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001484 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001485 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001486
Torok Edwin3f142c32009-02-01 18:15:56 +00001487 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001488 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001489 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasXMM())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001490 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001491 }
1492
Evan Cheng79fb3b42009-02-20 20:43:02 +00001493 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001494
1495 // If this is a call to a function that returns an fp value on the floating
1496 // point stack, we must guarantee the the value is popped from the stack, so
1497 // a CopyFromReg is not good enough - the copy instruction may be eliminated
1498 // if the return value is not used. We use the FpGET_ST0 instructions
1499 // instead.
1500 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1501 // If we prefer to use the value in xmm registers, copy it out as f80 and
1502 // use a truncate to move it from fp stack reg to xmm reg.
1503 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
1504 bool isST0 = VA.getLocReg() == X86::ST0;
1505 unsigned Opc = 0;
1506 if (CopyVT == MVT::f32) Opc = isST0 ? X86::FpGET_ST0_32:X86::FpGET_ST1_32;
1507 if (CopyVT == MVT::f64) Opc = isST0 ? X86::FpGET_ST0_64:X86::FpGET_ST1_64;
1508 if (CopyVT == MVT::f80) Opc = isST0 ? X86::FpGET_ST0_80:X86::FpGET_ST1_80;
1509 SDValue Ops[] = { Chain, InFlag };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001510 Chain = SDValue(DAG.getMachineNode(Opc, dl, CopyVT, MVT::Other, MVT::Glue,
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001511 Ops, 2), 1);
1512 Val = Chain.getValue(0);
1513
1514 // Round the f80 to the right size, which also moves it to the appropriate
1515 // xmm register.
1516 if (CopyVT != VA.getValVT())
1517 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1518 // This truncation won't change the value.
1519 DAG.getIntPtrConstant(1));
1520 } else if (Is64Bit && CopyVT.isVector() && CopyVT.getSizeInBits() == 64) {
Evan Cheng242b38b2009-02-23 09:03:22 +00001521 // For x86-64, MMX values are returned in XMM0 / XMM1 except for v1i64.
1522 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
1523 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001524 MVT::v2i64, InFlag).getValue(1);
Evan Cheng242b38b2009-02-23 09:03:22 +00001525 Val = Chain.getValue(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00001526 Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1527 Val, DAG.getConstant(0, MVT::i64));
Evan Cheng242b38b2009-02-23 09:03:22 +00001528 } else {
1529 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001530 MVT::i64, InFlag).getValue(1);
Evan Cheng242b38b2009-02-23 09:03:22 +00001531 Val = Chain.getValue(0);
1532 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001533 Val = DAG.getNode(ISD::BITCAST, dl, CopyVT, Val);
Evan Cheng79fb3b42009-02-20 20:43:02 +00001534 } else {
1535 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1536 CopyVT, InFlag).getValue(1);
1537 Val = Chain.getValue(0);
1538 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001539 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001540 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001541 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001542
Dan Gohman98ca4f22009-08-05 01:29:28 +00001543 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001544}
1545
1546
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001547//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001548// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001549//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001550// StdCall calling convention seems to be standard for many Windows' API
1551// routines and around. It differs from C calling convention just a little:
1552// callee should clean up the stack, not caller. Symbols should be also
1553// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001554// For info on fast calling convention see Fast Calling Convention (tail call)
1555// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001556
Dan Gohman98ca4f22009-08-05 01:29:28 +00001557/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001558/// semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001559static bool CallIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
1560 if (Outs.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001561 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001562
Dan Gohman98ca4f22009-08-05 01:29:28 +00001563 return Outs[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001564}
1565
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001566/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001567/// return semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001568static bool
1569ArgsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
1570 if (Ins.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001571 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001572
Dan Gohman98ca4f22009-08-05 01:29:28 +00001573 return Ins[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001574}
1575
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001576/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1577/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001578/// the specific parameter attribute. The copy will be passed as a byval
1579/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001580static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001581CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001582 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1583 DebugLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00001584 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Michael J. Spencerec38de22010-10-10 22:04:20 +00001585
Dale Johannesendd64c412009-02-04 00:33:20 +00001586 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Stuart Hastings03d58262011-03-10 00:25:53 +00001587 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001588 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001589}
1590
Chris Lattner29689432010-03-11 00:22:57 +00001591/// IsTailCallConvention - Return true if the calling convention is one that
1592/// supports tail call optimization.
1593static bool IsTailCallConvention(CallingConv::ID CC) {
1594 return (CC == CallingConv::Fast || CC == CallingConv::GHC);
1595}
1596
Evan Cheng0c439eb2010-01-27 00:07:07 +00001597/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1598/// a tailcall target by changing its ABI.
1599static bool FuncIsMadeTailCallSafe(CallingConv::ID CC) {
Chris Lattner29689432010-03-11 00:22:57 +00001600 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001601}
1602
Dan Gohman98ca4f22009-08-05 01:29:28 +00001603SDValue
1604X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001605 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001606 const SmallVectorImpl<ISD::InputArg> &Ins,
1607 DebugLoc dl, SelectionDAG &DAG,
1608 const CCValAssign &VA,
1609 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001610 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001611 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001612 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Evan Cheng0c439eb2010-01-27 00:07:07 +00001613 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001614 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001615 EVT ValVT;
1616
1617 // If value is passed by pointer we have address passed instead of the value
1618 // itself.
1619 if (VA.getLocInfo() == CCValAssign::Indirect)
1620 ValVT = VA.getLocVT();
1621 else
1622 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001623
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001624 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001625 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001626 // In case of tail call optimization mark all arguments mutable. Since they
1627 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001628 if (Flags.isByVal()) {
1629 int FI = MFI->CreateFixedObject(Flags.getByValSize(),
Evan Chenged2ae132010-07-03 00:40:23 +00001630 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001631 return DAG.getFrameIndex(FI, getPointerTy());
1632 } else {
1633 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001634 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001635 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1636 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001637 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00001638 false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001639 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001640}
1641
Dan Gohman475871a2008-07-27 21:46:04 +00001642SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001643X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001644 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001645 bool isVarArg,
1646 const SmallVectorImpl<ISD::InputArg> &Ins,
1647 DebugLoc dl,
1648 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001649 SmallVectorImpl<SDValue> &InVals)
1650 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001651 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001652 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001653
Gordon Henriksen86737662008-01-05 16:56:59 +00001654 const Function* Fn = MF.getFunction();
1655 if (Fn->hasExternalLinkage() &&
1656 Subtarget->isTargetCygMing() &&
1657 Fn->getName() == "main")
1658 FuncInfo->setForceFramePointer(true);
1659
Evan Cheng1bc78042006-04-26 01:20:17 +00001660 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001661 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001662 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001663
Chris Lattner29689432010-03-11 00:22:57 +00001664 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1665 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001666
Chris Lattner638402b2007-02-28 07:00:42 +00001667 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001668 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001669 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1670 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001671
1672 // Allocate shadow area for Win64
1673 if (IsWin64) {
1674 CCInfo.AllocateStack(32, 8);
1675 }
1676
Duncan Sands45907662010-10-31 13:21:44 +00001677 CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001678
Chris Lattnerf39f7712007-02-28 05:46:49 +00001679 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001680 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001681 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1682 CCValAssign &VA = ArgLocs[i];
1683 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1684 // places.
1685 assert(VA.getValNo() != LastVal &&
1686 "Don't support value assigned to multiple locs yet");
1687 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001688
Chris Lattnerf39f7712007-02-28 05:46:49 +00001689 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001690 EVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001691 TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001692 if (RegVT == MVT::i32)
Chris Lattnerf39f7712007-02-28 05:46:49 +00001693 RC = X86::GR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001694 else if (Is64Bit && RegVT == MVT::i64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001695 RC = X86::GR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001696 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001697 RC = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001698 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001699 RC = X86::FR64RegisterClass;
Bruno Cardoso Lopesac098352010-08-05 23:35:51 +00001700 else if (RegVT.isVector() && RegVT.getSizeInBits() == 256)
1701 RC = X86::VR256RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001702 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001703 RC = X86::VR128RegisterClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001704 else if (RegVT == MVT::x86mmx)
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001705 RC = X86::VR64RegisterClass;
1706 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001707 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001708
Devang Patel68e6bee2011-02-21 23:21:26 +00001709 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001710 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001711
Chris Lattnerf39f7712007-02-28 05:46:49 +00001712 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1713 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1714 // right size.
1715 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001716 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001717 DAG.getValueType(VA.getValVT()));
1718 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001719 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001720 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001721 else if (VA.getLocInfo() == CCValAssign::BCvt)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001722 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001723
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001724 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001725 // Handle MMX values passed in XMM regs.
1726 if (RegVT.isVector()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001727 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(),
1728 ArgValue);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001729 } else
1730 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001731 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001732 } else {
1733 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001734 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001735 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001736
1737 // If value is passed via pointer - do a load.
1738 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00001739 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
1740 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001741
Dan Gohman98ca4f22009-08-05 01:29:28 +00001742 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001743 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001744
Dan Gohman61a92132008-04-21 23:59:07 +00001745 // The x86-64 ABI for returning structs by value requires that we copy
1746 // the sret argument into %rax for the return. Save the argument into
1747 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001748 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001749 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1750 unsigned Reg = FuncInfo->getSRetReturnReg();
1751 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001752 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001753 FuncInfo->setSRetReturnReg(Reg);
1754 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001755 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001756 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001757 }
1758
Chris Lattnerf39f7712007-02-28 05:46:49 +00001759 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001760 // Align stack specially for tail calls.
1761 if (FuncIsMadeTailCallSafe(CallConv))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001762 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001763
Evan Cheng1bc78042006-04-26 01:20:17 +00001764 // If the function takes variable number of arguments, make a frame index for
1765 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001766 if (isVarArg) {
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001767 if (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
1768 CallConv != CallingConv::X86_ThisCall)) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00001769 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00001770 }
1771 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001772 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1773
1774 // FIXME: We should really autogenerate these arrays
1775 static const unsigned GPR64ArgRegsWin64[] = {
1776 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001777 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001778 static const unsigned GPR64ArgRegs64Bit[] = {
1779 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1780 };
1781 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001782 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1783 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1784 };
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001785 const unsigned *GPR64ArgRegs;
1786 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001787
1788 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001789 // The XMM registers which might contain var arg parameters are shadowed
1790 // in their paired GPR. So we only need to save the GPR to their home
1791 // slots.
1792 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001793 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001794 } else {
1795 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1796 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001797
1798 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit, TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001799 }
1800 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1801 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001802
Devang Patel578efa92009-06-05 21:57:13 +00001803 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001804 assert(!(NumXMMRegs && !Subtarget->hasXMM()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001805 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001806 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001807 "SSE register cannot be used when SSE is disabled!");
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001808 if (UseSoftFloat || NoImplicitFloatOps || !Subtarget->hasXMM())
Torok Edwin3f142c32009-02-01 18:15:56 +00001809 // Kernel mode asks for SSE to be disabled, so don't push them
1810 // on the stack.
1811 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001812
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001813 if (IsWin64) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001814 const TargetFrameLowering &TFI = *getTargetMachine().getFrameLowering();
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001815 // Get to the caller-allocated home save location. Add 8 to account
1816 // for the return address.
1817 int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001818 FuncInfo->setRegSaveFrameIndex(
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001819 MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001820 // Fixup to set vararg frame on shadow area (4 x i64).
1821 if (NumIntRegs < 4)
1822 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001823 } else {
1824 // For X86-64, if there are vararg parameters that are passed via
1825 // registers, then we must store them to their spots on the stack so they
1826 // may be loaded by deferencing the result of va_next.
1827 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
1828 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
1829 FuncInfo->setRegSaveFrameIndex(
1830 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00001831 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001832 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001833
Gordon Henriksen86737662008-01-05 16:56:59 +00001834 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001835 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00001836 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
1837 getPointerTy());
1838 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001839 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00001840 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
1841 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00001842 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00001843 X86::GR64RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00001844 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001845 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001846 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001847 MachinePointerInfo::getFixedStack(
1848 FuncInfo->getRegSaveFrameIndex(), Offset),
1849 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001850 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001851 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001852 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001853
Dan Gohmanface41a2009-08-16 21:24:25 +00001854 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
1855 // Now store the XMM (fp + vector) parameter registers.
1856 SmallVector<SDValue, 11> SaveXMMOps;
1857 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001858
Devang Patel68e6bee2011-02-21 23:21:26 +00001859 unsigned AL = MF.addLiveIn(X86::AL, X86::GR8RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00001860 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
1861 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001862
Dan Gohman1e93df62010-04-17 14:41:14 +00001863 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1864 FuncInfo->getRegSaveFrameIndex()));
1865 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1866 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00001867
Dan Gohmanface41a2009-08-16 21:24:25 +00001868 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001869 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00001870 X86::VR128RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00001871 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
1872 SaveXMMOps.push_back(Val);
1873 }
1874 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
1875 MVT::Other,
1876 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00001877 }
Dan Gohmanface41a2009-08-16 21:24:25 +00001878
1879 if (!MemOps.empty())
1880 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1881 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001882 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001883 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001884
Gordon Henriksen86737662008-01-05 16:56:59 +00001885 // Some CCs need callee pop.
Dan Gohman4d3d6e12010-05-27 18:43:40 +00001886 if (Subtarget->IsCalleePop(isVarArg, CallConv)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001887 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001888 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00001889 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001890 // If this is an sret function, the return should pop the hidden pointer.
Chris Lattner29689432010-03-11 00:22:57 +00001891 if (!Is64Bit && !IsTailCallConvention(CallConv) && ArgsAreStructReturn(Ins))
Dan Gohman1e93df62010-04-17 14:41:14 +00001892 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001893 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001894
Gordon Henriksen86737662008-01-05 16:56:59 +00001895 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001896 // RegSaveFrameIndex is X86-64 only.
1897 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001898 if (CallConv == CallingConv::X86_FastCall ||
1899 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00001900 // fastcc functions can't have varargs.
1901 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00001902 }
Evan Cheng25caf632006-05-23 21:06:34 +00001903
Dan Gohman98ca4f22009-08-05 01:29:28 +00001904 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001905}
1906
Dan Gohman475871a2008-07-27 21:46:04 +00001907SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001908X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
1909 SDValue StackPtr, SDValue Arg,
1910 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00001911 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001912 ISD::ArgFlagsTy Flags) const {
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001913 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00001914 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00001915 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001916 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00001917 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001918
1919 return DAG.getStore(Chain, dl, Arg, PtrOff,
1920 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00001921 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00001922}
1923
Bill Wendling64e87322009-01-16 19:25:27 +00001924/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001925/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00001926SDValue
1927X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00001928 SDValue &OutRetAddr, SDValue Chain,
1929 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00001930 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001931 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00001932 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001933 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00001934
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001935 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00001936 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
1937 false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001938 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001939}
1940
1941/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1942/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00001943static SDValue
1944EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00001945 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00001946 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001947 // Store the return address to the appropriate stack slot.
1948 if (!FPDiff) return Chain;
1949 // Calculate the new stack slot for the return address.
1950 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00001951 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00001952 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00001953 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00001954 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001955 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00001956 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00001957 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001958 return Chain;
1959}
1960
Dan Gohman98ca4f22009-08-05 01:29:28 +00001961SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001962X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001963 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001964 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001965 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001966 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001967 const SmallVectorImpl<ISD::InputArg> &Ins,
1968 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001969 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001970 MachineFunction &MF = DAG.getMachineFunction();
1971 bool Is64Bit = Subtarget->is64Bit();
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00001972 bool IsWin64 = Subtarget->isTargetWin64();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001973 bool IsStructRet = CallIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00001974 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001975
Evan Cheng5f941932010-02-05 02:21:12 +00001976 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00001977 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00001978 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1979 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001980 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00001981
1982 // Sibcalls are automatically detected tailcalls which do not require
1983 // ABI changes.
Dan Gohman1797ed52010-02-08 20:27:50 +00001984 if (!GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00001985 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00001986
1987 if (isTailCall)
1988 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00001989 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00001990
Chris Lattner29689432010-03-11 00:22:57 +00001991 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1992 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001993
Chris Lattner638402b2007-02-28 07:00:42 +00001994 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00001995 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001996 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1997 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001998
1999 // Allocate shadow area for Win64
2000 if (IsWin64) {
2001 CCInfo.AllocateStack(32, 8);
2002 }
2003
Duncan Sands45907662010-10-31 13:21:44 +00002004 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00002005
Chris Lattner423c5f42007-02-28 05:31:48 +00002006 // Get a count of how many bytes are to be pushed on the stack.
2007 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00002008 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00002009 // This is a sibcall. The memory operands are available in caller's
2010 // own caller's stack.
2011 NumBytes = 0;
Chris Lattner29689432010-03-11 00:22:57 +00002012 else if (GuaranteedTailCallOpt && IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00002013 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002014
Gordon Henriksen86737662008-01-05 16:56:59 +00002015 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00002016 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002017 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00002018 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00002019 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
2020 FPDiff = NumBytesCallerPushed - NumBytes;
2021
2022 // Set the delta of movement of the returnaddr stackslot.
2023 // But only set if delta is greater than previous delta.
2024 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
2025 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
2026 }
2027
Evan Chengf22f9b32010-02-06 03:28:46 +00002028 if (!IsSibcall)
2029 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002030
Dan Gohman475871a2008-07-27 21:46:04 +00002031 SDValue RetAddrFrIdx;
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002032 // Load return adress for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00002033 if (isTailCall && FPDiff)
2034 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
2035 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002036
Dan Gohman475871a2008-07-27 21:46:04 +00002037 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
2038 SmallVector<SDValue, 8> MemOpChains;
2039 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00002040
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002041 // Walk the register/memloc assignments, inserting copies/loads. In the case
2042 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00002043 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2044 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00002045 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00002046 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002047 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00002048 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00002049
Chris Lattner423c5f42007-02-28 05:31:48 +00002050 // Promote the value if needed.
2051 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002052 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00002053 case CCValAssign::Full: break;
2054 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002055 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002056 break;
2057 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002058 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002059 break;
2060 case CCValAssign::AExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002061 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
2062 // Special case: passing MMX values in XMM registers.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002063 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00002064 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
2065 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002066 } else
2067 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
2068 break;
2069 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002070 Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002071 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002072 case CCValAssign::Indirect: {
2073 // Store the argument.
2074 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00002075 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002076 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00002077 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002078 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002079 Arg = SpillSlot;
2080 break;
2081 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00002082 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002083
Chris Lattner423c5f42007-02-28 05:31:48 +00002084 if (VA.isRegLoc()) {
2085 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002086 if (isVarArg && IsWin64) {
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002087 // Win64 ABI requires argument XMM reg to be copied to the corresponding
2088 // shadow reg if callee is a varargs function.
2089 unsigned ShadowReg = 0;
2090 switch (VA.getLocReg()) {
2091 case X86::XMM0: ShadowReg = X86::RCX; break;
2092 case X86::XMM1: ShadowReg = X86::RDX; break;
2093 case X86::XMM2: ShadowReg = X86::R8; break;
2094 case X86::XMM3: ShadowReg = X86::R9; break;
2095 }
2096 if (ShadowReg)
2097 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
2098 }
Evan Chengf22f9b32010-02-06 03:28:46 +00002099 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00002100 assert(VA.isMemLoc());
2101 if (StackPtr.getNode() == 0)
2102 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
2103 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
2104 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002105 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002106 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002107
Evan Cheng32fe1032006-05-25 00:59:30 +00002108 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002109 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002110 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002111
Evan Cheng347d5f72006-04-28 21:29:37 +00002112 // Build a sequence of copy-to-reg nodes chained together with token chain
2113 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002114 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002115 // Tail call byval lowering might overwrite argument registers so in case of
2116 // tail call optimization the copies to registers are lowered later.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002117 if (!isTailCall)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002118 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002119 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002120 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002121 InFlag = Chain.getValue(1);
2122 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002123
Chris Lattner88e1fd52009-07-09 04:24:46 +00002124 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002125 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2126 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002127 if (!isTailCall) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002128 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
2129 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00002130 DebugLoc(), getPointerTy()),
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002131 InFlag);
2132 InFlag = Chain.getValue(1);
2133 } else {
2134 // If we are tail calling and generating PIC/GOT style code load the
2135 // address of the callee into ECX. The value in ecx is used as target of
2136 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2137 // for tail calls on PIC/GOT architectures. Normally we would just put the
2138 // address of GOT into ebx and then call target@PLT. But for tail calls
2139 // ebx would be restored (since ebx is callee saved) before jumping to the
2140 // target@PLT.
2141
2142 // Note: The actual moving to ECX is done further down.
2143 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2144 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2145 !G->getGlobal()->hasProtectedVisibility())
2146 Callee = LowerGlobalAddress(Callee, DAG);
2147 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002148 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002149 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002150 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002151
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002152 if (Is64Bit && isVarArg && !IsWin64) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002153 // From AMD64 ABI document:
2154 // For calls that may call functions that use varargs or stdargs
2155 // (prototype-less calls or calls to functions containing ellipsis (...) in
2156 // the declaration) %al is used as hidden argument to specify the number
2157 // of SSE registers used. The contents of %al do not need to match exactly
2158 // the number of registers, but must be an ubound on the number of SSE
2159 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002160
Gordon Henriksen86737662008-01-05 16:56:59 +00002161 // Count the number of XMM registers allocated.
2162 static const unsigned XMMArgRegs[] = {
2163 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2164 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2165 };
2166 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00002167 assert((Subtarget->hasXMM() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002168 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002169
Dale Johannesendd64c412009-02-04 00:33:20 +00002170 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Owen Anderson825b72b2009-08-11 20:47:22 +00002171 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002172 InFlag = Chain.getValue(1);
2173 }
2174
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002175
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002176 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002177 if (isTailCall) {
2178 // Force all the incoming stack arguments to be loaded from the stack
2179 // before any new outgoing arguments are stored to the stack, because the
2180 // outgoing stack slots may alias the incoming argument stack slots, and
2181 // the alias isn't otherwise explicit. This is slightly more conservative
2182 // than necessary, because it means that each store effectively depends
2183 // on every argument instead of just those arguments it would clobber.
2184 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2185
Dan Gohman475871a2008-07-27 21:46:04 +00002186 SmallVector<SDValue, 8> MemOpChains2;
2187 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002188 int FI = 0;
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002189 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00002190 InFlag = SDValue();
Dan Gohman1797ed52010-02-08 20:27:50 +00002191 if (GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002192 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2193 CCValAssign &VA = ArgLocs[i];
2194 if (VA.isRegLoc())
2195 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002196 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002197 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002198 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002199 // Create frame index.
2200 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002201 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002202 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002203 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002204
Duncan Sands276dcbd2008-03-21 09:14:45 +00002205 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002206 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002207 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002208 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002209 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00002210 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002211 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002212
Dan Gohman98ca4f22009-08-05 01:29:28 +00002213 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2214 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002215 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002216 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002217 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002218 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002219 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002220 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002221 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002222 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002223 }
2224 }
2225
2226 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002227 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002228 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002229
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002230 // Copy arguments to their registers.
2231 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002232 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002233 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002234 InFlag = Chain.getValue(1);
2235 }
Dan Gohman475871a2008-07-27 21:46:04 +00002236 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002237
Gordon Henriksen86737662008-01-05 16:56:59 +00002238 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002239 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002240 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002241 }
2242
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002243 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2244 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2245 // In the 64-bit large code model, we have to make all calls
2246 // through a register, since the call instruction's 32-bit
2247 // pc-relative offset may not be large enough to hold the whole
2248 // address.
2249 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002250 // If the callee is a GlobalAddress node (quite common, every direct call
2251 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2252 // it.
2253
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002254 // We should use extra load for direct calls to dllimported functions in
2255 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002256 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002257 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002258 unsigned char OpFlags = 0;
Eric Christopherfd179292009-08-27 18:07:15 +00002259
Chris Lattner48a7d022009-07-09 05:02:21 +00002260 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2261 // external symbols most go through the PLT in PIC mode. If the symbol
2262 // has hidden or protected visibility, or if it is static or local, then
2263 // we don't need to use the PLT - we can directly call it.
2264 if (Subtarget->isTargetELF() &&
2265 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002266 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002267 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002268 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002269 (GV->isDeclaration() || GV->isWeakForLinker()) &&
2270 Subtarget->getDarwinVers() < 9) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002271 // PC-relative references to external symbols should go through $stub,
2272 // unless we're building with the leopard linker or later, which
2273 // automatically synthesizes these stubs.
2274 OpFlags = X86II::MO_DARWIN_STUB;
2275 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002276
Devang Patel0d881da2010-07-06 22:08:15 +00002277 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002278 G->getOffset(), OpFlags);
2279 }
Bill Wendling056292f2008-09-16 21:48:12 +00002280 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002281 unsigned char OpFlags = 0;
2282
Evan Cheng1bf891a2010-12-01 22:59:46 +00002283 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2284 // external symbols should go through the PLT.
2285 if (Subtarget->isTargetELF() &&
2286 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2287 OpFlags = X86II::MO_PLT;
2288 } else if (Subtarget->isPICStyleStubAny() &&
2289 Subtarget->getDarwinVers() < 9) {
2290 // PC-relative references to external symbols should go through $stub,
2291 // unless we're building with the leopard linker or later, which
2292 // automatically synthesizes these stubs.
2293 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002294 }
Eric Christopherfd179292009-08-27 18:07:15 +00002295
Chris Lattner48a7d022009-07-09 05:02:21 +00002296 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2297 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002298 }
2299
Chris Lattnerd96d0722007-02-25 06:40:16 +00002300 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002301 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002302 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002303
Evan Chengf22f9b32010-02-06 03:28:46 +00002304 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002305 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2306 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002307 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002308 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002309
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002310 Ops.push_back(Chain);
2311 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002312
Dan Gohman98ca4f22009-08-05 01:29:28 +00002313 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002314 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002315
Gordon Henriksen86737662008-01-05 16:56:59 +00002316 // Add argument registers to the end of the list so that they are known live
2317 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002318 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2319 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2320 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002321
Evan Cheng586ccac2008-03-18 23:36:35 +00002322 // Add an implicit use GOT pointer in EBX.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002323 if (!isTailCall && Subtarget->isPICStyleGOT())
Evan Cheng586ccac2008-03-18 23:36:35 +00002324 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
2325
Anton Korobeynikov3a1e54a2010-08-17 21:06:07 +00002326 // Add an implicit use of AL for non-Windows x86 64-bit vararg functions.
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002327 if (Is64Bit && isVarArg && !IsWin64)
Owen Anderson825b72b2009-08-11 20:47:22 +00002328 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
Evan Cheng586ccac2008-03-18 23:36:35 +00002329
Gabor Greifba36cb52008-08-28 21:40:38 +00002330 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002331 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002332
Dan Gohman98ca4f22009-08-05 01:29:28 +00002333 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002334 // We used to do:
2335 //// If this is the first return lowered for this function, add the regs
2336 //// to the liveout set for the function.
2337 // This isn't right, although it's probably harmless on x86; liveouts
2338 // should be computed from returns not tail calls. Consider a void
2339 // function making a tail call to a function returning int.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002340 return DAG.getNode(X86ISD::TC_RETURN, dl,
2341 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002342 }
2343
Dale Johannesenace16102009-02-03 19:33:06 +00002344 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002345 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002346
Chris Lattner2d297092006-05-23 18:50:38 +00002347 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002348 unsigned NumBytesForCalleeToPush;
Dan Gohman4d3d6e12010-05-27 18:43:40 +00002349 if (Subtarget->IsCalleePop(isVarArg, CallConv))
Gordon Henriksen86737662008-01-05 16:56:59 +00002350 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Chris Lattner29689432010-03-11 00:22:57 +00002351 else if (!Is64Bit && !IsTailCallConvention(CallConv) && IsStructRet)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002352 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002353 // pops the hidden struct pointer, so we have to push it back.
2354 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002355 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002356 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002357 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002358
Gordon Henriksenae636f82008-01-03 16:47:34 +00002359 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002360 if (!IsSibcall) {
2361 Chain = DAG.getCALLSEQ_END(Chain,
2362 DAG.getIntPtrConstant(NumBytes, true),
2363 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2364 true),
2365 InFlag);
2366 InFlag = Chain.getValue(1);
2367 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002368
Chris Lattner3085e152007-02-25 08:59:22 +00002369 // Handle result values, copying them out of physregs into vregs that we
2370 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002371 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2372 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002373}
2374
Evan Cheng25ab6902006-09-08 06:48:29 +00002375
2376//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002377// Fast Calling Convention (tail call) implementation
2378//===----------------------------------------------------------------------===//
2379
2380// Like std call, callee cleans arguments, convention except that ECX is
2381// reserved for storing the tail called function address. Only 2 registers are
2382// free for argument passing (inreg). Tail call optimization is performed
2383// provided:
2384// * tailcallopt is enabled
2385// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002386// On X86_64 architecture with GOT-style position independent code only local
2387// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002388// To keep the stack aligned according to platform abi the function
2389// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2390// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002391// If a tail called function callee has more arguments than the caller the
2392// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002393// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002394// original REtADDR, but before the saved framepointer or the spilled registers
2395// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2396// stack layout:
2397// arg1
2398// arg2
2399// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002400// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002401// move area ]
2402// (possible EBP)
2403// ESI
2404// EDI
2405// local1 ..
2406
2407/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2408/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002409unsigned
2410X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2411 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002412 MachineFunction &MF = DAG.getMachineFunction();
2413 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002414 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002415 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002416 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002417 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002418 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002419 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2420 // Number smaller than 12 so just add the difference.
2421 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2422 } else {
2423 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002424 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002425 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002426 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002427 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002428}
2429
Evan Cheng5f941932010-02-05 02:21:12 +00002430/// MatchingStackOffset - Return true if the given stack call argument is
2431/// already available in the same position (relatively) of the caller's
2432/// incoming argument stack.
2433static
2434bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2435 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2436 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002437 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2438 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002439 if (Arg.getOpcode() == ISD::CopyFromReg) {
2440 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00002441 if (!TargetRegisterInfo::isVirtualRegister(VR))
Evan Cheng5f941932010-02-05 02:21:12 +00002442 return false;
2443 MachineInstr *Def = MRI->getVRegDef(VR);
2444 if (!Def)
2445 return false;
2446 if (!Flags.isByVal()) {
2447 if (!TII->isLoadFromStackSlot(Def, FI))
2448 return false;
2449 } else {
2450 unsigned Opcode = Def->getOpcode();
2451 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2452 Def->getOperand(1).isFI()) {
2453 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002454 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002455 } else
2456 return false;
2457 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002458 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2459 if (Flags.isByVal())
2460 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002461 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002462 // define @foo(%struct.X* %A) {
2463 // tail call @bar(%struct.X* byval %A)
2464 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002465 return false;
2466 SDValue Ptr = Ld->getBasePtr();
2467 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2468 if (!FINode)
2469 return false;
2470 FI = FINode->getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002471 } else
2472 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002473
Evan Cheng4cae1332010-03-05 08:38:04 +00002474 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002475 if (!MFI->isFixedObjectIndex(FI))
2476 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002477 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002478}
2479
Dan Gohman98ca4f22009-08-05 01:29:28 +00002480/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2481/// for tail call optimization. Targets which want to do tail call
2482/// optimization should implement this function.
2483bool
2484X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002485 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002486 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002487 bool isCalleeStructRet,
2488 bool isCallerStructRet,
Evan Chengb1712452010-01-27 06:25:16 +00002489 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002490 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002491 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002492 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002493 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002494 CalleeCC != CallingConv::C)
2495 return false;
2496
Evan Cheng7096ae42010-01-29 06:45:59 +00002497 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002498 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002499 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Cheng13617962010-04-30 01:12:32 +00002500 CallingConv::ID CallerCC = CallerF->getCallingConv();
2501 bool CCMatch = CallerCC == CalleeCC;
2502
Dan Gohman1797ed52010-02-08 20:27:50 +00002503 if (GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002504 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002505 return true;
2506 return false;
2507 }
2508
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002509 // Look for obvious safe cases to perform tail call optimization that do not
2510 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002511
Evan Cheng2c12cb42010-03-26 16:26:03 +00002512 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2513 // emit a special epilogue.
2514 if (RegInfo->needsStackRealignment(MF))
2515 return false;
2516
Eric Christopher90eb4022010-07-22 00:26:08 +00002517 // Do not sibcall optimize vararg calls unless the call site is not passing
2518 // any arguments.
Evan Cheng3c262ee2010-03-26 02:13:13 +00002519 if (isVarArg && !Outs.empty())
Evan Cheng843bd692010-01-31 06:44:49 +00002520 return false;
2521
Evan Chenga375d472010-03-15 18:54:48 +00002522 // Also avoid sibcall optimization if either caller or callee uses struct
2523 // return semantics.
2524 if (isCalleeStructRet || isCallerStructRet)
2525 return false;
2526
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002527 // If the call result is in ST0 / ST1, it needs to be popped off the x87 stack.
2528 // Therefore if it's not used by the call it is not safe to optimize this into
2529 // a sibcall.
2530 bool Unused = false;
2531 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2532 if (!Ins[i].Used) {
2533 Unused = true;
2534 break;
2535 }
2536 }
2537 if (Unused) {
2538 SmallVector<CCValAssign, 16> RVLocs;
2539 CCState CCInfo(CalleeCC, false, getTargetMachine(),
2540 RVLocs, *DAG.getContext());
2541 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002542 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002543 CCValAssign &VA = RVLocs[i];
2544 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2545 return false;
2546 }
2547 }
2548
Evan Cheng13617962010-04-30 01:12:32 +00002549 // If the calling conventions do not match, then we'd better make sure the
2550 // results are returned in the same way as what the caller expects.
2551 if (!CCMatch) {
2552 SmallVector<CCValAssign, 16> RVLocs1;
2553 CCState CCInfo1(CalleeCC, false, getTargetMachine(),
2554 RVLocs1, *DAG.getContext());
2555 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2556
2557 SmallVector<CCValAssign, 16> RVLocs2;
2558 CCState CCInfo2(CallerCC, false, getTargetMachine(),
2559 RVLocs2, *DAG.getContext());
2560 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2561
2562 if (RVLocs1.size() != RVLocs2.size())
2563 return false;
2564 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2565 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2566 return false;
2567 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2568 return false;
2569 if (RVLocs1[i].isRegLoc()) {
2570 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2571 return false;
2572 } else {
2573 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2574 return false;
2575 }
2576 }
2577 }
2578
Evan Chenga6bff982010-01-30 01:22:00 +00002579 // If the callee takes no arguments then go on to check the results of the
2580 // call.
2581 if (!Outs.empty()) {
2582 // Check if stack adjustment is needed. For now, do not do this if any
2583 // argument is passed on the stack.
2584 SmallVector<CCValAssign, 16> ArgLocs;
2585 CCState CCInfo(CalleeCC, isVarArg, getTargetMachine(),
2586 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002587
2588 // Allocate shadow area for Win64
2589 if (Subtarget->isTargetWin64()) {
2590 CCInfo.AllocateStack(32, 8);
2591 }
2592
Duncan Sands45907662010-10-31 13:21:44 +00002593 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Evan Chengb2c92902010-02-02 02:22:50 +00002594 if (CCInfo.getNextStackOffset()) {
2595 MachineFunction &MF = DAG.getMachineFunction();
2596 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2597 return false;
Evan Chengb2c92902010-02-02 02:22:50 +00002598
2599 // Check if the arguments are already laid out in the right way as
2600 // the caller's fixed stack objects.
2601 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002602 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2603 const X86InstrInfo *TII =
2604 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002605 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2606 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002607 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002608 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002609 if (VA.getLocInfo() == CCValAssign::Indirect)
2610 return false;
2611 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002612 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2613 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002614 return false;
2615 }
2616 }
2617 }
Evan Cheng9c044672010-05-29 01:35:22 +00002618
2619 // If the tailcall address may be in a register, then make sure it's
2620 // possible to register allocate for it. In 32-bit, the call address can
2621 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002622 // callee-saved registers are restored. These happen to be the same
2623 // registers used to pass 'inreg' arguments so watch out for those.
2624 if (!Subtarget->is64Bit() &&
2625 !isa<GlobalAddressSDNode>(Callee) &&
Evan Cheng9c044672010-05-29 01:35:22 +00002626 !isa<ExternalSymbolSDNode>(Callee)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002627 unsigned NumInRegs = 0;
2628 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2629 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002630 if (!VA.isRegLoc())
2631 continue;
2632 unsigned Reg = VA.getLocReg();
2633 switch (Reg) {
2634 default: break;
2635 case X86::EAX: case X86::EDX: case X86::ECX:
2636 if (++NumInRegs == 3)
Evan Cheng9c044672010-05-29 01:35:22 +00002637 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002638 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002639 }
2640 }
2641 }
Evan Chenga6bff982010-01-30 01:22:00 +00002642 }
Evan Chengb1712452010-01-27 06:25:16 +00002643
Dale Johannesend155d7e2010-10-25 22:17:05 +00002644 // An stdcall caller is expected to clean up its arguments; the callee
Dale Johannesen0e034562010-11-12 00:43:18 +00002645 // isn't going to do that.
Dale Johannesend155d7e2010-10-25 22:17:05 +00002646 if (!CCMatch && CallerCC==CallingConv::X86_StdCall)
2647 return false;
2648
Evan Cheng86809cc2010-02-03 03:28:02 +00002649 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002650}
2651
Dan Gohman3df24e62008-09-03 23:12:08 +00002652FastISel *
Dan Gohmana4160c32010-07-07 16:29:44 +00002653X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
2654 return X86::createFastISel(funcInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00002655}
2656
2657
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002658//===----------------------------------------------------------------------===//
2659// Other Lowering Hooks
2660//===----------------------------------------------------------------------===//
2661
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00002662static bool MayFoldLoad(SDValue Op) {
2663 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
2664}
2665
2666static bool MayFoldIntoStore(SDValue Op) {
2667 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
2668}
2669
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002670static bool isTargetShuffle(unsigned Opcode) {
2671 switch(Opcode) {
2672 default: return false;
2673 case X86ISD::PSHUFD:
2674 case X86ISD::PSHUFHW:
2675 case X86ISD::PSHUFLW:
2676 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002677 case X86ISD::PALIGN:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002678 case X86ISD::SHUFPS:
2679 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002680 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002681 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002682 case X86ISD::MOVLPS:
2683 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002684 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002685 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002686 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002687 case X86ISD::MOVSS:
2688 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002689 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002690 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +00002691 case X86ISD::VUNPCKLPS:
2692 case X86ISD::VUNPCKLPD:
2693 case X86ISD::VUNPCKLPSY:
2694 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002695 case X86ISD::PUNPCKLWD:
2696 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002697 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002698 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002699 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002700 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002701 case X86ISD::PUNPCKHWD:
2702 case X86ISD::PUNPCKHBW:
2703 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002704 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002705 return true;
2706 }
2707 return false;
2708}
2709
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002710static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002711 SDValue V1, SelectionDAG &DAG) {
2712 switch(Opc) {
2713 default: llvm_unreachable("Unknown x86 shuffle node");
2714 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002715 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002716 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002717 return DAG.getNode(Opc, dl, VT, V1);
2718 }
2719
2720 return SDValue();
2721}
2722
2723static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00002724 SDValue V1, unsigned TargetMask, SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002725 switch(Opc) {
2726 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002727 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002728 case X86ISD::PSHUFHW:
2729 case X86ISD::PSHUFLW:
2730 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
2731 }
2732
2733 return SDValue();
2734}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002735
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002736static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2737 SDValue V1, SDValue V2, unsigned TargetMask, SelectionDAG &DAG) {
2738 switch(Opc) {
2739 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002740 case X86ISD::PALIGN:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002741 case X86ISD::SHUFPD:
2742 case X86ISD::SHUFPS:
2743 return DAG.getNode(Opc, dl, VT, V1, V2,
2744 DAG.getConstant(TargetMask, MVT::i8));
2745 }
2746 return SDValue();
2747}
2748
2749static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2750 SDValue V1, SDValue V2, SelectionDAG &DAG) {
2751 switch(Opc) {
2752 default: llvm_unreachable("Unknown x86 shuffle node");
2753 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00002754 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002755 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002756 case X86ISD::MOVLPS:
2757 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002758 case X86ISD::MOVSS:
2759 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002760 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002761 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +00002762 case X86ISD::VUNPCKLPS:
2763 case X86ISD::VUNPCKLPD:
2764 case X86ISD::VUNPCKLPSY:
2765 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002766 case X86ISD::PUNPCKLWD:
2767 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002768 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002769 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002770 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002771 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002772 case X86ISD::PUNPCKHWD:
2773 case X86ISD::PUNPCKHBW:
2774 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002775 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002776 return DAG.getNode(Opc, dl, VT, V1, V2);
2777 }
2778 return SDValue();
2779}
2780
Dan Gohmand858e902010-04-17 15:26:15 +00002781SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002782 MachineFunction &MF = DAG.getMachineFunction();
2783 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2784 int ReturnAddrIndex = FuncInfo->getRAIndex();
2785
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002786 if (ReturnAddrIndex == 0) {
2787 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002788 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002789 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00002790 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002791 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002792 }
2793
Evan Cheng25ab6902006-09-08 06:48:29 +00002794 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002795}
2796
2797
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002798bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2799 bool hasSymbolicDisplacement) {
2800 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00002801 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002802 return false;
2803
2804 // If we don't have a symbolic displacement - we don't have any extra
2805 // restrictions.
2806 if (!hasSymbolicDisplacement)
2807 return true;
2808
2809 // FIXME: Some tweaks might be needed for medium code model.
2810 if (M != CodeModel::Small && M != CodeModel::Kernel)
2811 return false;
2812
2813 // For small code model we assume that latest object is 16MB before end of 31
2814 // bits boundary. We may also accept pretty large negative constants knowing
2815 // that all objects are in the positive half of address space.
2816 if (M == CodeModel::Small && Offset < 16*1024*1024)
2817 return true;
2818
2819 // For kernel code model we know that all object resist in the negative half
2820 // of 32bits address space. We may not accept negative offsets, since they may
2821 // be just off and we may accept pretty large positive ones.
2822 if (M == CodeModel::Kernel && Offset > 0)
2823 return true;
2824
2825 return false;
2826}
2827
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002828/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2829/// specific condition code, returning the condition code and the LHS/RHS of the
2830/// comparison to make.
2831static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2832 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00002833 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002834 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2835 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2836 // X > -1 -> X == 0, jump !sign.
2837 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002838 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002839 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2840 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002841 return X86::COND_S;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002842 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00002843 // X < 1 -> X <= 0
2844 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002845 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002846 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002847 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002848
Evan Chengd9558e02006-01-06 00:43:03 +00002849 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002850 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002851 case ISD::SETEQ: return X86::COND_E;
2852 case ISD::SETGT: return X86::COND_G;
2853 case ISD::SETGE: return X86::COND_GE;
2854 case ISD::SETLT: return X86::COND_L;
2855 case ISD::SETLE: return X86::COND_LE;
2856 case ISD::SETNE: return X86::COND_NE;
2857 case ISD::SETULT: return X86::COND_B;
2858 case ISD::SETUGT: return X86::COND_A;
2859 case ISD::SETULE: return X86::COND_BE;
2860 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00002861 }
Chris Lattner4c78e022008-12-23 23:42:27 +00002862 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002863
Chris Lattner4c78e022008-12-23 23:42:27 +00002864 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00002865
Chris Lattner4c78e022008-12-23 23:42:27 +00002866 // If LHS is a foldable load, but RHS is not, flip the condition.
Rafael Espindolaf297c932011-02-03 03:58:05 +00002867 if (ISD::isNON_EXTLoad(LHS.getNode()) &&
2868 !ISD::isNON_EXTLoad(RHS.getNode())) {
Chris Lattner4c78e022008-12-23 23:42:27 +00002869 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
2870 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00002871 }
2872
Chris Lattner4c78e022008-12-23 23:42:27 +00002873 switch (SetCCOpcode) {
2874 default: break;
2875 case ISD::SETOLT:
2876 case ISD::SETOLE:
2877 case ISD::SETUGT:
2878 case ISD::SETUGE:
2879 std::swap(LHS, RHS);
2880 break;
2881 }
2882
2883 // On a floating point condition, the flags are set as follows:
2884 // ZF PF CF op
2885 // 0 | 0 | 0 | X > Y
2886 // 0 | 0 | 1 | X < Y
2887 // 1 | 0 | 0 | X == Y
2888 // 1 | 1 | 1 | unordered
2889 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002890 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00002891 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002892 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00002893 case ISD::SETOLT: // flipped
2894 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002895 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00002896 case ISD::SETOLE: // flipped
2897 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002898 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002899 case ISD::SETUGT: // flipped
2900 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002901 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00002902 case ISD::SETUGE: // flipped
2903 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002904 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002905 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002906 case ISD::SETNE: return X86::COND_NE;
2907 case ISD::SETUO: return X86::COND_P;
2908 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00002909 case ISD::SETOEQ:
2910 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00002911 }
Evan Chengd9558e02006-01-06 00:43:03 +00002912}
2913
Evan Cheng4a460802006-01-11 00:33:36 +00002914/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2915/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00002916/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00002917static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00002918 switch (X86CC) {
2919 default:
2920 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00002921 case X86::COND_B:
2922 case X86::COND_BE:
2923 case X86::COND_E:
2924 case X86::COND_P:
2925 case X86::COND_A:
2926 case X86::COND_AE:
2927 case X86::COND_NE:
2928 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00002929 return true;
2930 }
2931}
2932
Evan Chengeb2f9692009-10-27 19:56:55 +00002933/// isFPImmLegal - Returns true if the target can instruction select the
2934/// specified FP immediate natively. If false, the legalizer will
2935/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00002936bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00002937 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
2938 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
2939 return true;
2940 }
2941 return false;
2942}
2943
Nate Begeman9008ca62009-04-27 18:41:29 +00002944/// isUndefOrInRange - Return true if Val is undef or if its value falls within
2945/// the specified range (L, H].
2946static bool isUndefOrInRange(int Val, int Low, int Hi) {
2947 return (Val < 0) || (Val >= Low && Val < Hi);
2948}
2949
2950/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
2951/// specified value.
2952static bool isUndefOrEqual(int Val, int CmpVal) {
2953 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002954 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00002955 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00002956}
2957
Nate Begeman9008ca62009-04-27 18:41:29 +00002958/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
2959/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
2960/// the second operand.
Owen Andersone50ed302009-08-10 22:56:29 +00002961static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00002962 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00002963 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002964 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00002965 return (Mask[0] < 2 && Mask[1] < 2);
2966 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002967}
2968
Nate Begeman9008ca62009-04-27 18:41:29 +00002969bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002970 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002971 N->getMask(M);
2972 return ::isPSHUFDMask(M, N->getValueType(0));
2973}
Evan Cheng0188ecb2006-03-22 18:59:22 +00002974
Nate Begeman9008ca62009-04-27 18:41:29 +00002975/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
2976/// is suitable for input to PSHUFHW.
Owen Andersone50ed302009-08-10 22:56:29 +00002977static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002978 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00002979 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002980
Nate Begeman9008ca62009-04-27 18:41:29 +00002981 // Lower quadword copied in order or undef.
2982 for (int i = 0; i != 4; ++i)
2983 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00002984 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002985
Evan Cheng506d3df2006-03-29 23:07:14 +00002986 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002987 for (int i = 4; i != 8; ++i)
2988 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00002989 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002990
Evan Cheng506d3df2006-03-29 23:07:14 +00002991 return true;
2992}
2993
Nate Begeman9008ca62009-04-27 18:41:29 +00002994bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002995 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002996 N->getMask(M);
2997 return ::isPSHUFHWMask(M, N->getValueType(0));
2998}
Evan Cheng506d3df2006-03-29 23:07:14 +00002999
Nate Begeman9008ca62009-04-27 18:41:29 +00003000/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
3001/// is suitable for input to PSHUFLW.
Owen Andersone50ed302009-08-10 22:56:29 +00003002static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003003 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00003004 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003005
Rafael Espindola15684b22009-04-24 12:40:33 +00003006 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00003007 for (int i = 4; i != 8; ++i)
3008 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00003009 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003010
Rafael Espindola15684b22009-04-24 12:40:33 +00003011 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00003012 for (int i = 0; i != 4; ++i)
3013 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00003014 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003015
Rafael Espindola15684b22009-04-24 12:40:33 +00003016 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003017}
3018
Nate Begeman9008ca62009-04-27 18:41:29 +00003019bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003020 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003021 N->getMask(M);
3022 return ::isPSHUFLWMask(M, N->getValueType(0));
3023}
3024
Nate Begemana09008b2009-10-19 02:17:23 +00003025/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
3026/// is suitable for input to PALIGNR.
3027static bool isPALIGNRMask(const SmallVectorImpl<int> &Mask, EVT VT,
3028 bool hasSSSE3) {
3029 int i, e = VT.getVectorNumElements();
Michael J. Spencerec38de22010-10-10 22:04:20 +00003030
Nate Begemana09008b2009-10-19 02:17:23 +00003031 // Do not handle v2i64 / v2f64 shuffles with palignr.
3032 if (e < 4 || !hasSSSE3)
3033 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003034
Nate Begemana09008b2009-10-19 02:17:23 +00003035 for (i = 0; i != e; ++i)
3036 if (Mask[i] >= 0)
3037 break;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003038
Nate Begemana09008b2009-10-19 02:17:23 +00003039 // All undef, not a palignr.
3040 if (i == e)
3041 return false;
3042
3043 // Determine if it's ok to perform a palignr with only the LHS, since we
3044 // don't have access to the actual shuffle elements to see if RHS is undef.
3045 bool Unary = Mask[i] < (int)e;
3046 bool NeedsUnary = false;
3047
3048 int s = Mask[i] - i;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003049
Nate Begemana09008b2009-10-19 02:17:23 +00003050 // Check the rest of the elements to see if they are consecutive.
3051 for (++i; i != e; ++i) {
3052 int m = Mask[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00003053 if (m < 0)
Nate Begemana09008b2009-10-19 02:17:23 +00003054 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003055
Nate Begemana09008b2009-10-19 02:17:23 +00003056 Unary = Unary && (m < (int)e);
3057 NeedsUnary = NeedsUnary || (m < s);
3058
3059 if (NeedsUnary && !Unary)
3060 return false;
3061 if (Unary && m != ((s+i) & (e-1)))
3062 return false;
3063 if (!Unary && m != (s+i))
3064 return false;
3065 }
3066 return true;
3067}
3068
3069bool X86::isPALIGNRMask(ShuffleVectorSDNode *N) {
3070 SmallVector<int, 8> M;
3071 N->getMask(M);
3072 return ::isPALIGNRMask(M, N->getValueType(0), true);
3073}
3074
Evan Cheng14aed5e2006-03-24 01:18:28 +00003075/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
3076/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Owen Andersone50ed302009-08-10 22:56:29 +00003077static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003078 int NumElems = VT.getVectorNumElements();
3079 if (NumElems != 2 && NumElems != 4)
3080 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003081
Nate Begeman9008ca62009-04-27 18:41:29 +00003082 int Half = NumElems / 2;
3083 for (int i = 0; i < Half; ++i)
3084 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003085 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003086 for (int i = Half; i < NumElems; ++i)
3087 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00003088 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003089
Evan Cheng14aed5e2006-03-24 01:18:28 +00003090 return true;
3091}
3092
Nate Begeman9008ca62009-04-27 18:41:29 +00003093bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
3094 SmallVector<int, 8> M;
3095 N->getMask(M);
3096 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003097}
3098
Evan Cheng213d2cf2007-05-17 18:45:50 +00003099/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00003100/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
3101/// half elements to come from vector 1 (which would equal the dest.) and
3102/// the upper half to come from vector 2.
Owen Andersone50ed302009-08-10 22:56:29 +00003103static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003104 int NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003105
3106 if (NumElems != 2 && NumElems != 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00003107 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003108
Nate Begeman9008ca62009-04-27 18:41:29 +00003109 int Half = NumElems / 2;
3110 for (int i = 0; i < Half; ++i)
3111 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00003112 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003113 for (int i = Half; i < NumElems; ++i)
3114 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003115 return false;
3116 return true;
3117}
3118
Nate Begeman9008ca62009-04-27 18:41:29 +00003119static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
3120 SmallVector<int, 8> M;
3121 N->getMask(M);
3122 return isCommutedSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003123}
3124
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003125/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
3126/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00003127bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
3128 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003129 return false;
3130
Evan Cheng2064a2b2006-03-28 06:50:32 +00003131 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00003132 return isUndefOrEqual(N->getMaskElt(0), 6) &&
3133 isUndefOrEqual(N->getMaskElt(1), 7) &&
3134 isUndefOrEqual(N->getMaskElt(2), 2) &&
3135 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003136}
3137
Nate Begeman0b10b912009-11-07 23:17:15 +00003138/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3139/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3140/// <2, 3, 2, 3>
3141bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
3142 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Michael J. Spencerec38de22010-10-10 22:04:20 +00003143
Nate Begeman0b10b912009-11-07 23:17:15 +00003144 if (NumElems != 4)
3145 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003146
Nate Begeman0b10b912009-11-07 23:17:15 +00003147 return isUndefOrEqual(N->getMaskElt(0), 2) &&
3148 isUndefOrEqual(N->getMaskElt(1), 3) &&
3149 isUndefOrEqual(N->getMaskElt(2), 2) &&
3150 isUndefOrEqual(N->getMaskElt(3), 3);
3151}
3152
Evan Cheng5ced1d82006-04-06 23:23:56 +00003153/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3154/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00003155bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
3156 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003157
Evan Cheng5ced1d82006-04-06 23:23:56 +00003158 if (NumElems != 2 && NumElems != 4)
3159 return false;
3160
Evan Chengc5cdff22006-04-07 21:53:05 +00003161 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003162 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003163 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003164
Evan Chengc5cdff22006-04-07 21:53:05 +00003165 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003166 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003167 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003168
3169 return true;
3170}
3171
Nate Begeman0b10b912009-11-07 23:17:15 +00003172/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3173/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
3174bool X86::isMOVLHPSMask(ShuffleVectorSDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003175 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003176
David Greenea20244d2011-03-02 17:23:43 +00003177 if ((NumElems != 2 && NumElems != 4)
3178 || N->getValueType(0).getSizeInBits() > 128)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003179 return false;
3180
Evan Chengc5cdff22006-04-07 21:53:05 +00003181 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003182 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003183 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003184
Nate Begeman9008ca62009-04-27 18:41:29 +00003185 for (unsigned i = 0; i < NumElems/2; ++i)
3186 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003187 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003188
3189 return true;
3190}
3191
Evan Cheng0038e592006-03-28 00:39:58 +00003192/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3193/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Owen Andersone50ed302009-08-10 22:56:29 +00003194static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003195 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003196 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003197 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng0038e592006-03-28 00:39:58 +00003198 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003199
David Greenea20244d2011-03-02 17:23:43 +00003200 // Handle vector lengths > 128 bits. Define a "section" as a set of
3201 // 128 bits. AVX defines UNPCK* to operate independently on 128-bit
3202 // sections.
3203 unsigned NumSections = VT.getSizeInBits() / 128;
3204 if (NumSections == 0 ) NumSections = 1; // Handle MMX
3205 unsigned NumSectionElts = NumElts / NumSections;
3206
3207 unsigned Start = 0;
3208 unsigned End = NumSectionElts;
3209 for (unsigned s = 0; s < NumSections; ++s) {
3210 for (unsigned i = Start, j = s * NumSectionElts;
3211 i != End;
3212 i += 2, ++j) {
3213 int BitI = Mask[i];
3214 int BitI1 = Mask[i+1];
3215 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003216 return false;
David Greenea20244d2011-03-02 17:23:43 +00003217 if (V2IsSplat) {
3218 if (!isUndefOrEqual(BitI1, NumElts))
3219 return false;
3220 } else {
3221 if (!isUndefOrEqual(BitI1, j + NumElts))
3222 return false;
3223 }
Evan Cheng39623da2006-04-20 08:58:49 +00003224 }
David Greenea20244d2011-03-02 17:23:43 +00003225 // Process the next 128 bits.
3226 Start += NumSectionElts;
3227 End += NumSectionElts;
Evan Cheng0038e592006-03-28 00:39:58 +00003228 }
David Greenea20244d2011-03-02 17:23:43 +00003229
Evan Cheng0038e592006-03-28 00:39:58 +00003230 return true;
3231}
3232
Nate Begeman9008ca62009-04-27 18:41:29 +00003233bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3234 SmallVector<int, 8> M;
3235 N->getMask(M);
3236 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003237}
3238
Evan Cheng4fcb9222006-03-28 02:43:26 +00003239/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3240/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Eric Christopherfd179292009-08-27 18:07:15 +00003241static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003242 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003243 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003244 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng4fcb9222006-03-28 02:43:26 +00003245 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003246
Nate Begeman9008ca62009-04-27 18:41:29 +00003247 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
3248 int BitI = Mask[i];
3249 int BitI1 = Mask[i+1];
Chris Lattner5a88b832007-02-25 07:10:00 +00003250 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengc5cdff22006-04-07 21:53:05 +00003251 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00003252 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00003253 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003254 return false;
3255 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00003256 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003257 return false;
3258 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003259 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003260 return true;
3261}
3262
Nate Begeman9008ca62009-04-27 18:41:29 +00003263bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3264 SmallVector<int, 8> M;
3265 N->getMask(M);
3266 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003267}
3268
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003269/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3270/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3271/// <0, 0, 1, 1>
Owen Andersone50ed302009-08-10 22:56:29 +00003272static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003273 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003274 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003275 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003276
David Greenea20244d2011-03-02 17:23:43 +00003277 // Handle vector lengths > 128 bits. Define a "section" as a set of
3278 // 128 bits. AVX defines UNPCK* to operate independently on 128-bit
3279 // sections.
3280 unsigned NumSections = VT.getSizeInBits() / 128;
3281 if (NumSections == 0 ) NumSections = 1; // Handle MMX
3282 unsigned NumSectionElts = NumElems / NumSections;
3283
3284 for (unsigned s = 0; s < NumSections; ++s) {
3285 for (unsigned i = s * NumSectionElts, j = s * NumSectionElts;
3286 i != NumSectionElts * (s + 1);
3287 i += 2, ++j) {
3288 int BitI = Mask[i];
3289 int BitI1 = Mask[i+1];
3290
3291 if (!isUndefOrEqual(BitI, j))
3292 return false;
3293 if (!isUndefOrEqual(BitI1, j))
3294 return false;
3295 }
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003296 }
David Greenea20244d2011-03-02 17:23:43 +00003297
Rafael Espindola15684b22009-04-24 12:40:33 +00003298 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003299}
3300
Nate Begeman9008ca62009-04-27 18:41:29 +00003301bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
3302 SmallVector<int, 8> M;
3303 N->getMask(M);
3304 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
3305}
3306
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003307/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3308/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3309/// <2, 2, 3, 3>
Owen Andersone50ed302009-08-10 22:56:29 +00003310static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003311 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003312 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
3313 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003314
Nate Begeman9008ca62009-04-27 18:41:29 +00003315 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
3316 int BitI = Mask[i];
3317 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003318 if (!isUndefOrEqual(BitI, j))
3319 return false;
3320 if (!isUndefOrEqual(BitI1, j))
3321 return false;
3322 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003323 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003324}
3325
Nate Begeman9008ca62009-04-27 18:41:29 +00003326bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
3327 SmallVector<int, 8> M;
3328 N->getMask(M);
3329 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
3330}
3331
Evan Cheng017dcc62006-04-21 01:05:10 +00003332/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3333/// specifies a shuffle of elements that is suitable for input to MOVSS,
3334/// MOVSD, and MOVD, i.e. setting the lowest element.
Owen Andersone50ed302009-08-10 22:56:29 +00003335static bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003336 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003337 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003338
3339 int NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003340
Nate Begeman9008ca62009-04-27 18:41:29 +00003341 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003342 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003343
Nate Begeman9008ca62009-04-27 18:41:29 +00003344 for (int i = 1; i < NumElts; ++i)
3345 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003346 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003347
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003348 return true;
3349}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003350
Nate Begeman9008ca62009-04-27 18:41:29 +00003351bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
3352 SmallVector<int, 8> M;
3353 N->getMask(M);
3354 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003355}
3356
Evan Cheng017dcc62006-04-21 01:05:10 +00003357/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
3358/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003359/// element of vector 2 and the other elements to come from vector 1 in order.
Owen Andersone50ed302009-08-10 22:56:29 +00003360static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003361 bool V2IsSplat = false, bool V2IsUndef = false) {
3362 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003363 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003364 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003365
Nate Begeman9008ca62009-04-27 18:41:29 +00003366 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003367 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003368
Nate Begeman9008ca62009-04-27 18:41:29 +00003369 for (int i = 1; i < NumOps; ++i)
3370 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3371 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3372 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003373 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003374
Evan Cheng39623da2006-04-20 08:58:49 +00003375 return true;
3376}
3377
Nate Begeman9008ca62009-04-27 18:41:29 +00003378static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00003379 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003380 SmallVector<int, 8> M;
3381 N->getMask(M);
3382 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00003383}
3384
Evan Chengd9539472006-04-14 21:59:03 +00003385/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3386/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003387bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N) {
3388 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003389 return false;
3390
3391 // Expect 1, 1, 3, 3
Rafael Espindola15684b22009-04-24 12:40:33 +00003392 for (unsigned i = 0; i < 2; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003393 int Elt = N->getMaskElt(i);
3394 if (Elt >= 0 && Elt != 1)
3395 return false;
Rafael Espindola15684b22009-04-24 12:40:33 +00003396 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003397
3398 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003399 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003400 int Elt = N->getMaskElt(i);
3401 if (Elt >= 0 && Elt != 3)
3402 return false;
3403 if (Elt == 3)
3404 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003405 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003406 // Don't use movshdup if it can be done with a shufps.
Nate Begeman9008ca62009-04-27 18:41:29 +00003407 // FIXME: verify that matching u, u, 3, 3 is what we want.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003408 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003409}
3410
3411/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3412/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003413bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N) {
3414 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003415 return false;
3416
3417 // Expect 0, 0, 2, 2
Nate Begeman9008ca62009-04-27 18:41:29 +00003418 for (unsigned i = 0; i < 2; ++i)
3419 if (N->getMaskElt(i) > 0)
3420 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003421
3422 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003423 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003424 int Elt = N->getMaskElt(i);
3425 if (Elt >= 0 && Elt != 2)
3426 return false;
3427 if (Elt == 2)
3428 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003429 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003430 // Don't use movsldup if it can be done with a shufps.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003431 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003432}
3433
Evan Cheng0b457f02008-09-25 20:50:48 +00003434/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3435/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003436bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
3437 int e = N->getValueType(0).getVectorNumElements() / 2;
Eric Christopherfd179292009-08-27 18:07:15 +00003438
Nate Begeman9008ca62009-04-27 18:41:29 +00003439 for (int i = 0; i < e; ++i)
3440 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003441 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003442 for (int i = 0; i < e; ++i)
3443 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003444 return false;
3445 return true;
3446}
3447
David Greenec38a03e2011-02-03 15:50:00 +00003448/// isVEXTRACTF128Index - Return true if the specified
3449/// EXTRACT_SUBVECTOR operand specifies a vector extract that is
3450/// suitable for input to VEXTRACTF128.
3451bool X86::isVEXTRACTF128Index(SDNode *N) {
3452 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3453 return false;
3454
3455 // The index should be aligned on a 128-bit boundary.
3456 uint64_t Index =
3457 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
3458
3459 unsigned VL = N->getValueType(0).getVectorNumElements();
3460 unsigned VBits = N->getValueType(0).getSizeInBits();
3461 unsigned ElSize = VBits / VL;
3462 bool Result = (Index * ElSize) % 128 == 0;
3463
3464 return Result;
3465}
3466
David Greeneccacdc12011-02-04 16:08:29 +00003467/// isVINSERTF128Index - Return true if the specified INSERT_SUBVECTOR
3468/// operand specifies a subvector insert that is suitable for input to
3469/// VINSERTF128.
3470bool X86::isVINSERTF128Index(SDNode *N) {
3471 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
3472 return false;
3473
3474 // The index should be aligned on a 128-bit boundary.
3475 uint64_t Index =
3476 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
3477
3478 unsigned VL = N->getValueType(0).getVectorNumElements();
3479 unsigned VBits = N->getValueType(0).getSizeInBits();
3480 unsigned ElSize = VBits / VL;
3481 bool Result = (Index * ElSize) % 128 == 0;
3482
3483 return Result;
3484}
3485
Evan Cheng63d33002006-03-22 08:01:21 +00003486/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003487/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Evan Cheng63d33002006-03-22 08:01:21 +00003488unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003489 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3490 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
3491
Evan Chengb9df0ca2006-03-22 02:53:00 +00003492 unsigned Shift = (NumOperands == 4) ? 2 : 1;
3493 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00003494 for (int i = 0; i < NumOperands; ++i) {
3495 int Val = SVOp->getMaskElt(NumOperands-i-1);
3496 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00003497 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00003498 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00003499 if (i != NumOperands - 1)
3500 Mask <<= Shift;
3501 }
Evan Cheng63d33002006-03-22 08:01:21 +00003502 return Mask;
3503}
3504
Evan Cheng506d3df2006-03-29 23:07:14 +00003505/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003506/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003507unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003508 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003509 unsigned Mask = 0;
3510 // 8 nodes, but we only care about the last 4.
3511 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003512 int Val = SVOp->getMaskElt(i);
3513 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00003514 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00003515 if (i != 4)
3516 Mask <<= 2;
3517 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003518 return Mask;
3519}
3520
3521/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003522/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003523unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003524 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003525 unsigned Mask = 0;
3526 // 8 nodes, but we only care about the first 4.
3527 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003528 int Val = SVOp->getMaskElt(i);
3529 if (Val >= 0)
3530 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00003531 if (i != 0)
3532 Mask <<= 2;
3533 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003534 return Mask;
3535}
3536
Nate Begemana09008b2009-10-19 02:17:23 +00003537/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
3538/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
3539unsigned X86::getShufflePALIGNRImmediate(SDNode *N) {
3540 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3541 EVT VVT = N->getValueType(0);
3542 unsigned EltSize = VVT.getVectorElementType().getSizeInBits() >> 3;
3543 int Val = 0;
3544
3545 unsigned i, e;
3546 for (i = 0, e = VVT.getVectorNumElements(); i != e; ++i) {
3547 Val = SVOp->getMaskElt(i);
3548 if (Val >= 0)
3549 break;
3550 }
3551 return (Val - i) * EltSize;
3552}
3553
David Greenec38a03e2011-02-03 15:50:00 +00003554/// getExtractVEXTRACTF128Immediate - Return the appropriate immediate
3555/// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF128
3556/// instructions.
3557unsigned X86::getExtractVEXTRACTF128Immediate(SDNode *N) {
3558 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3559 llvm_unreachable("Illegal extract subvector for VEXTRACTF128");
3560
3561 uint64_t Index =
3562 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
3563
3564 EVT VecVT = N->getOperand(0).getValueType();
3565 EVT ElVT = VecVT.getVectorElementType();
3566
3567 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
3568
3569 return Index / NumElemsPerChunk;
3570}
3571
David Greeneccacdc12011-02-04 16:08:29 +00003572/// getInsertVINSERTF128Immediate - Return the appropriate immediate
3573/// to insert at the specified INSERT_SUBVECTOR index with VINSERTF128
3574/// instructions.
3575unsigned X86::getInsertVINSERTF128Immediate(SDNode *N) {
3576 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
3577 llvm_unreachable("Illegal insert subvector for VINSERTF128");
3578
3579 uint64_t Index =
NAKAMURA Takumi27635382011-02-05 15:10:54 +00003580 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
David Greeneccacdc12011-02-04 16:08:29 +00003581
3582 EVT VecVT = N->getValueType(0);
3583 EVT ElVT = VecVT.getVectorElementType();
3584
3585 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
3586
3587 return Index / NumElemsPerChunk;
3588}
3589
Evan Cheng37b73872009-07-30 08:33:02 +00003590/// isZeroNode - Returns true if Elt is a constant zero or a floating point
3591/// constant +0.0.
3592bool X86::isZeroNode(SDValue Elt) {
3593 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00003594 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00003595 (isa<ConstantFPSDNode>(Elt) &&
3596 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
3597}
3598
Nate Begeman9008ca62009-04-27 18:41:29 +00003599/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
3600/// their permute mask.
3601static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
3602 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003603 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003604 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00003605 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00003606
Nate Begeman5a5ca152009-04-29 05:20:52 +00003607 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003608 int idx = SVOp->getMaskElt(i);
3609 if (idx < 0)
3610 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003611 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003612 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003613 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003614 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003615 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003616 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
3617 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003618}
3619
Evan Cheng779ccea2007-12-07 21:30:01 +00003620/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3621/// the two vector operands have swapped position.
Owen Andersone50ed302009-08-10 22:56:29 +00003622static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00003623 unsigned NumElems = VT.getVectorNumElements();
3624 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003625 int idx = Mask[i];
3626 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003627 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003628 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003629 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003630 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003631 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003632 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003633}
3634
Evan Cheng533a0aa2006-04-19 20:35:22 +00003635/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
3636/// match movhlps. The lower half elements should come from upper half of
3637/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003638/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00003639static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
3640 if (Op->getValueType(0).getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00003641 return false;
3642 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003643 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003644 return false;
3645 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003646 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003647 return false;
3648 return true;
3649}
3650
Evan Cheng5ced1d82006-04-06 23:23:56 +00003651/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00003652/// is promoted to a vector. It also returns the LoadSDNode by reference if
3653/// required.
3654static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00003655 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
3656 return false;
3657 N = N->getOperand(0).getNode();
3658 if (!ISD::isNON_EXTLoad(N))
3659 return false;
3660 if (LD)
3661 *LD = cast<LoadSDNode>(N);
3662 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003663}
3664
Evan Cheng533a0aa2006-04-19 20:35:22 +00003665/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
3666/// match movlp{s|d}. The lower half elements should come from lower half of
3667/// V1 (and in order), and the upper half elements should come from the upper
3668/// half of V2 (and in order). And since V1 will become the source of the
3669/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003670static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
3671 ShuffleVectorSDNode *Op) {
Evan Cheng466685d2006-10-09 20:57:25 +00003672 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003673 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00003674 // Is V2 is a vector load, don't do this transformation. We will try to use
3675 // load folding shufps op.
3676 if (ISD::isNON_EXTLoad(V2))
3677 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003678
Nate Begeman5a5ca152009-04-29 05:20:52 +00003679 unsigned NumElems = Op->getValueType(0).getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003680
Evan Cheng533a0aa2006-04-19 20:35:22 +00003681 if (NumElems != 2 && NumElems != 4)
3682 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003683 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003684 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003685 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003686 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003687 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003688 return false;
3689 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003690}
3691
Evan Cheng39623da2006-04-20 08:58:49 +00003692/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
3693/// all the same.
3694static bool isSplatVector(SDNode *N) {
3695 if (N->getOpcode() != ISD::BUILD_VECTOR)
3696 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003697
Dan Gohman475871a2008-07-27 21:46:04 +00003698 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00003699 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
3700 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003701 return false;
3702 return true;
3703}
3704
Evan Cheng213d2cf2007-05-17 18:45:50 +00003705/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00003706/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00003707/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00003708static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00003709 SDValue V1 = N->getOperand(0);
3710 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003711 unsigned NumElems = N->getValueType(0).getVectorNumElements();
3712 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003713 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003714 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003715 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00003716 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
3717 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003718 if (Opc != ISD::BUILD_VECTOR ||
3719 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00003720 return false;
3721 } else if (Idx >= 0) {
3722 unsigned Opc = V1.getOpcode();
3723 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
3724 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003725 if (Opc != ISD::BUILD_VECTOR ||
3726 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00003727 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00003728 }
3729 }
3730 return true;
3731}
3732
3733/// getZeroVector - Returns a vector of specified type with all zero elements.
3734///
Owen Andersone50ed302009-08-10 22:56:29 +00003735static SDValue getZeroVector(EVT VT, bool HasSSE2, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00003736 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003737 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003738
Dale Johannesen0488fb62010-09-30 23:57:10 +00003739 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003740 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00003741 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00003742 if (VT.getSizeInBits() == 128) { // SSE
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003743 if (HasSSE2) { // SSE2
3744 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3745 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
3746 } else { // SSE1
3747 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
3748 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
3749 }
3750 } else if (VT.getSizeInBits() == 256) { // AVX
3751 // 256-bit logic and arithmetic instructions in AVX are
3752 // all floating-point, no support for integer ops. Default
3753 // to emitting fp zeroed vectors then.
Owen Anderson825b72b2009-08-11 20:47:22 +00003754 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003755 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
3756 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
Evan Chengf0df0312008-05-15 08:39:06 +00003757 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003758 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00003759}
3760
Chris Lattner8a594482007-11-25 00:24:49 +00003761/// getOnesVector - Returns a vector of specified type with all bits set.
3762///
Owen Andersone50ed302009-08-10 22:56:29 +00003763static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003764 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003765
Chris Lattner8a594482007-11-25 00:24:49 +00003766 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
3767 // type. This ensures they get CSE'd.
Owen Anderson825b72b2009-08-11 20:47:22 +00003768 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00003769 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00003770 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003771 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00003772}
3773
3774
Evan Cheng39623da2006-04-20 08:58:49 +00003775/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
3776/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00003777static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003778 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003779 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003780
Evan Cheng39623da2006-04-20 08:58:49 +00003781 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003782 SmallVector<int, 8> MaskVec;
3783 SVOp->getMask(MaskVec);
Eric Christopherfd179292009-08-27 18:07:15 +00003784
Nate Begeman5a5ca152009-04-29 05:20:52 +00003785 for (unsigned i = 0; i != NumElems; ++i) {
3786 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003787 MaskVec[i] = NumElems;
3788 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00003789 }
Evan Cheng39623da2006-04-20 08:58:49 +00003790 }
Evan Cheng39623da2006-04-20 08:58:49 +00003791 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00003792 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
3793 SVOp->getOperand(1), &MaskVec[0]);
3794 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00003795}
3796
Evan Cheng017dcc62006-04-21 01:05:10 +00003797/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
3798/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00003799static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003800 SDValue V2) {
3801 unsigned NumElems = VT.getVectorNumElements();
3802 SmallVector<int, 8> Mask;
3803 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00003804 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003805 Mask.push_back(i);
3806 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00003807}
3808
Nate Begeman9008ca62009-04-27 18:41:29 +00003809/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003810static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003811 SDValue V2) {
3812 unsigned NumElems = VT.getVectorNumElements();
3813 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00003814 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003815 Mask.push_back(i);
3816 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00003817 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003818 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00003819}
3820
Nate Begeman9008ca62009-04-27 18:41:29 +00003821/// getUnpackhMask - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003822static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003823 SDValue V2) {
3824 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00003825 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00003826 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00003827 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003828 Mask.push_back(i + Half);
3829 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00003830 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003831 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003832}
3833
Bruno Cardoso Lopesbb0a9482010-08-13 17:50:47 +00003834/// PromoteSplat - Promote a splat of v4i32, v8i16 or v16i8 to v4f32.
3835static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003836 EVT PVT = MVT::v4f32;
Owen Andersone50ed302009-08-10 22:56:29 +00003837 EVT VT = SV->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00003838 DebugLoc dl = SV->getDebugLoc();
3839 SDValue V1 = SV->getOperand(0);
3840 int NumElems = VT.getVectorNumElements();
3841 int EltNo = SV->getSplatIndex();
Rafael Espindola15684b22009-04-24 12:40:33 +00003842
Nate Begeman9008ca62009-04-27 18:41:29 +00003843 // unpack elements to the correct location
3844 while (NumElems > 4) {
3845 if (EltNo < NumElems/2) {
3846 V1 = getUnpackl(DAG, dl, VT, V1, V1);
3847 } else {
3848 V1 = getUnpackh(DAG, dl, VT, V1, V1);
3849 EltNo -= NumElems/2;
3850 }
3851 NumElems >>= 1;
3852 }
Eric Christopherfd179292009-08-27 18:07:15 +00003853
Nate Begeman9008ca62009-04-27 18:41:29 +00003854 // Perform the splat.
3855 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003856 V1 = DAG.getNode(ISD::BITCAST, dl, PVT, V1);
Nate Begeman9008ca62009-04-27 18:41:29 +00003857 V1 = DAG.getVectorShuffle(PVT, dl, V1, DAG.getUNDEF(PVT), &SplatMask[0]);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003858 return DAG.getNode(ISD::BITCAST, dl, VT, V1);
Evan Chengc575ca22006-04-17 20:43:08 +00003859}
3860
Evan Chengba05f722006-04-21 23:03:30 +00003861/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00003862/// vector of zero or undef vector. This produces a shuffle where the low
3863/// element of V2 is swizzled into the zero/undef vector, landing at element
3864/// 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 +00003865static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Chengf0df0312008-05-15 08:39:06 +00003866 bool isZero, bool HasSSE2,
3867 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003868 EVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00003869 SDValue V1 = isZero
Nate Begeman9008ca62009-04-27 18:41:29 +00003870 ? getZeroVector(VT, HasSSE2, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
3871 unsigned NumElems = VT.getVectorNumElements();
3872 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00003873 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003874 // If this is the insertion idx, put the low elt of V2 here.
3875 MaskVec.push_back(i == Idx ? NumElems : i);
3876 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00003877}
3878
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003879/// getShuffleScalarElt - Returns the scalar element that will make up the ith
3880/// element of the result of the vector shuffle.
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003881SDValue getShuffleScalarElt(SDNode *N, int Index, SelectionDAG &DAG,
3882 unsigned Depth) {
3883 if (Depth == 6)
3884 return SDValue(); // Limit search depth.
3885
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003886 SDValue V = SDValue(N, 0);
3887 EVT VT = V.getValueType();
3888 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003889
3890 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
3891 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
3892 Index = SV->getMaskElt(Index);
3893
3894 if (Index < 0)
3895 return DAG.getUNDEF(VT.getVectorElementType());
3896
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003897 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003898 SDValue NewV = (Index < NumElems) ? SV->getOperand(0) : SV->getOperand(1);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003899 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00003900 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003901
3902 // Recurse into target specific vector shuffles to find scalars.
3903 if (isTargetShuffle(Opcode)) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003904 int NumElems = VT.getVectorNumElements();
3905 SmallVector<unsigned, 16> ShuffleMask;
3906 SDValue ImmN;
3907
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003908 switch(Opcode) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003909 case X86ISD::SHUFPS:
3910 case X86ISD::SHUFPD:
3911 ImmN = N->getOperand(N->getNumOperands()-1);
3912 DecodeSHUFPSMask(NumElems,
3913 cast<ConstantSDNode>(ImmN)->getZExtValue(),
3914 ShuffleMask);
3915 break;
3916 case X86ISD::PUNPCKHBW:
3917 case X86ISD::PUNPCKHWD:
3918 case X86ISD::PUNPCKHDQ:
3919 case X86ISD::PUNPCKHQDQ:
3920 DecodePUNPCKHMask(NumElems, ShuffleMask);
3921 break;
3922 case X86ISD::UNPCKHPS:
3923 case X86ISD::UNPCKHPD:
3924 DecodeUNPCKHPMask(NumElems, ShuffleMask);
3925 break;
3926 case X86ISD::PUNPCKLBW:
3927 case X86ISD::PUNPCKLWD:
3928 case X86ISD::PUNPCKLDQ:
3929 case X86ISD::PUNPCKLQDQ:
David Greenec4db4e52011-02-28 19:06:56 +00003930 DecodePUNPCKLMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003931 break;
3932 case X86ISD::UNPCKLPS:
3933 case X86ISD::UNPCKLPD:
David Greenec4db4e52011-02-28 19:06:56 +00003934 case X86ISD::VUNPCKLPS:
3935 case X86ISD::VUNPCKLPD:
3936 case X86ISD::VUNPCKLPSY:
3937 case X86ISD::VUNPCKLPDY:
3938 DecodeUNPCKLPMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003939 break;
3940 case X86ISD::MOVHLPS:
3941 DecodeMOVHLPSMask(NumElems, ShuffleMask);
3942 break;
3943 case X86ISD::MOVLHPS:
3944 DecodeMOVLHPSMask(NumElems, ShuffleMask);
3945 break;
3946 case X86ISD::PSHUFD:
3947 ImmN = N->getOperand(N->getNumOperands()-1);
3948 DecodePSHUFMask(NumElems,
3949 cast<ConstantSDNode>(ImmN)->getZExtValue(),
3950 ShuffleMask);
3951 break;
3952 case X86ISD::PSHUFHW:
3953 ImmN = N->getOperand(N->getNumOperands()-1);
3954 DecodePSHUFHWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
3955 ShuffleMask);
3956 break;
3957 case X86ISD::PSHUFLW:
3958 ImmN = N->getOperand(N->getNumOperands()-1);
3959 DecodePSHUFLWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
3960 ShuffleMask);
3961 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003962 case X86ISD::MOVSS:
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00003963 case X86ISD::MOVSD: {
3964 // The index 0 always comes from the first element of the second source,
3965 // this is why MOVSS and MOVSD are used in the first place. The other
3966 // elements come from the other positions of the first source vector.
3967 unsigned OpNum = (Index == 0) ? 1 : 0;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003968 return getShuffleScalarElt(V.getOperand(OpNum).getNode(), Index, DAG,
3969 Depth+1);
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00003970 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003971 default:
3972 assert("not implemented for target shuffle node");
3973 return SDValue();
3974 }
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003975
3976 Index = ShuffleMask[Index];
3977 if (Index < 0)
3978 return DAG.getUNDEF(VT.getVectorElementType());
3979
3980 SDValue NewV = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
3981 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG,
3982 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003983 }
3984
3985 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003986 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003987 V = V.getOperand(0);
3988 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003989 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003990
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003991 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003992 return SDValue();
3993 }
3994
3995 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
3996 return (Index == 0) ? V.getOperand(0)
3997 : DAG.getUNDEF(VT.getVectorElementType());
3998
3999 if (V.getOpcode() == ISD::BUILD_VECTOR)
4000 return V.getOperand(Index);
4001
4002 return SDValue();
4003}
4004
4005/// getNumOfConsecutiveZeros - Return the number of elements of a vector
4006/// shuffle operation which come from a consecutively from a zero. The
4007/// search can start in two diferent directions, from left or right.
4008static
4009unsigned getNumOfConsecutiveZeros(SDNode *N, int NumElems,
4010 bool ZerosFromLeft, SelectionDAG &DAG) {
4011 int i = 0;
4012
4013 while (i < NumElems) {
4014 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004015 SDValue Elt = getShuffleScalarElt(N, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004016 if (!(Elt.getNode() &&
4017 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
4018 break;
4019 ++i;
4020 }
4021
4022 return i;
4023}
4024
4025/// isShuffleMaskConsecutive - Check if the shuffle mask indicies from MaskI to
4026/// MaskE correspond consecutively to elements from one of the vector operands,
4027/// starting from its index OpIdx. Also tell OpNum which source vector operand.
4028static
4029bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp, int MaskI, int MaskE,
4030 int OpIdx, int NumElems, unsigned &OpNum) {
4031 bool SeenV1 = false;
4032 bool SeenV2 = false;
4033
4034 for (int i = MaskI; i <= MaskE; ++i, ++OpIdx) {
4035 int Idx = SVOp->getMaskElt(i);
4036 // Ignore undef indicies
4037 if (Idx < 0)
4038 continue;
4039
4040 if (Idx < NumElems)
4041 SeenV1 = true;
4042 else
4043 SeenV2 = true;
4044
4045 // Only accept consecutive elements from the same vector
4046 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
4047 return false;
4048 }
4049
4050 OpNum = SeenV1 ? 0 : 1;
4051 return true;
4052}
4053
4054/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
4055/// logical left shift of a vector.
4056static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4057 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4058 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4059 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4060 false /* check zeros from right */, DAG);
4061 unsigned OpSrc;
4062
4063 if (!NumZeros)
4064 return false;
4065
4066 // Considering the elements in the mask that are not consecutive zeros,
4067 // check if they consecutively come from only one of the source vectors.
4068 //
4069 // V1 = {X, A, B, C} 0
4070 // \ \ \ /
4071 // vector_shuffle V1, V2 <1, 2, 3, X>
4072 //
4073 if (!isShuffleMaskConsecutive(SVOp,
4074 0, // Mask Start Index
4075 NumElems-NumZeros-1, // Mask End Index
4076 NumZeros, // Where to start looking in the src vector
4077 NumElems, // Number of elements in vector
4078 OpSrc)) // Which source operand ?
4079 return false;
4080
4081 isLeft = false;
4082 ShAmt = NumZeros;
4083 ShVal = SVOp->getOperand(OpSrc);
4084 return true;
4085}
4086
4087/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
4088/// logical left shift of a vector.
4089static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4090 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4091 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4092 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4093 true /* check zeros from left */, DAG);
4094 unsigned OpSrc;
4095
4096 if (!NumZeros)
4097 return false;
4098
4099 // Considering the elements in the mask that are not consecutive zeros,
4100 // check if they consecutively come from only one of the source vectors.
4101 //
4102 // 0 { A, B, X, X } = V2
4103 // / \ / /
4104 // vector_shuffle V1, V2 <X, X, 4, 5>
4105 //
4106 if (!isShuffleMaskConsecutive(SVOp,
4107 NumZeros, // Mask Start Index
4108 NumElems-1, // Mask End Index
4109 0, // Where to start looking in the src vector
4110 NumElems, // Number of elements in vector
4111 OpSrc)) // Which source operand ?
4112 return false;
4113
4114 isLeft = true;
4115 ShAmt = NumZeros;
4116 ShVal = SVOp->getOperand(OpSrc);
4117 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004118}
4119
4120/// isVectorShift - Returns true if the shuffle can be implemented as a
4121/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004122static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00004123 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004124 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
4125 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
4126 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004127
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004128 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00004129}
4130
Evan Chengc78d3b42006-04-24 18:01:45 +00004131/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
4132///
Dan Gohman475871a2008-07-27 21:46:04 +00004133static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00004134 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00004135 SelectionDAG &DAG,
4136 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004137 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00004138 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004139
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004140 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004141 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004142 bool First = true;
4143 for (unsigned i = 0; i < 16; ++i) {
4144 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
4145 if (ThisIsNonZero && First) {
4146 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004147 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004148 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004149 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004150 First = false;
4151 }
4152
4153 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00004154 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004155 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
4156 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004157 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004158 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00004159 }
4160 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004161 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
4162 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
4163 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00004164 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004165 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00004166 } else
4167 ThisElt = LastElt;
4168
Gabor Greifba36cb52008-08-28 21:40:38 +00004169 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00004170 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00004171 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00004172 }
4173 }
4174
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004175 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00004176}
4177
Bill Wendlinga348c562007-03-22 18:42:45 +00004178/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00004179///
Dan Gohman475871a2008-07-27 21:46:04 +00004180static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00004181 unsigned NumNonZero, unsigned NumZero,
4182 SelectionDAG &DAG,
4183 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004184 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00004185 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004186
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004187 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004188 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004189 bool First = true;
4190 for (unsigned i = 0; i < 8; ++i) {
4191 bool isNonZero = (NonZeros & (1 << i)) != 0;
4192 if (isNonZero) {
4193 if (First) {
4194 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004195 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004196 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004197 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004198 First = false;
4199 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004200 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004201 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00004202 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00004203 }
4204 }
4205
4206 return V;
4207}
4208
Evan Chengf26ffe92008-05-29 08:22:04 +00004209/// getVShift - Return a vector logical shift node.
4210///
Owen Andersone50ed302009-08-10 22:56:29 +00004211static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00004212 unsigned NumBits, SelectionDAG &DAG,
4213 const TargetLowering &TLI, DebugLoc dl) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004214 EVT ShVT = MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00004215 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004216 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
4217 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004218 DAG.getNode(Opc, dl, ShVT, SrcOp,
Owen Anderson95771af2011-02-25 21:41:48 +00004219 DAG.getConstant(NumBits,
4220 TLI.getShiftAmountTy(SrcOp.getValueType()))));
Evan Chengf26ffe92008-05-29 08:22:04 +00004221}
4222
Dan Gohman475871a2008-07-27 21:46:04 +00004223SDValue
Evan Chengc3630942009-12-09 21:00:30 +00004224X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00004225 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00004226
Evan Chengc3630942009-12-09 21:00:30 +00004227 // Check if the scalar load can be widened into a vector load. And if
4228 // the address is "base + cst" see if the cst can be "absorbed" into
4229 // the shuffle mask.
4230 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
4231 SDValue Ptr = LD->getBasePtr();
4232 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
4233 return SDValue();
4234 EVT PVT = LD->getValueType(0);
4235 if (PVT != MVT::i32 && PVT != MVT::f32)
4236 return SDValue();
4237
4238 int FI = -1;
4239 int64_t Offset = 0;
4240 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
4241 FI = FINode->getIndex();
4242 Offset = 0;
Chris Lattner0a9481f2011-02-13 22:25:43 +00004243 } else if (DAG.isBaseWithConstantOffset(Ptr) &&
Evan Chengc3630942009-12-09 21:00:30 +00004244 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4245 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4246 Offset = Ptr.getConstantOperandVal(1);
4247 Ptr = Ptr.getOperand(0);
4248 } else {
4249 return SDValue();
4250 }
4251
4252 SDValue Chain = LD->getChain();
4253 // Make sure the stack object alignment is at least 16.
4254 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
4255 if (DAG.InferPtrAlignment(Ptr) < 16) {
4256 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00004257 // Can't change the alignment. FIXME: It's possible to compute
4258 // the exact stack offset and reference FI + adjust offset instead.
4259 // If someone *really* cares about this. That's the way to implement it.
4260 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004261 } else {
4262 MFI->setObjectAlignment(FI, 16);
4263 }
4264 }
4265
4266 // (Offset % 16) must be multiple of 4. Then address is then
4267 // Ptr + (Offset & ~15).
4268 if (Offset < 0)
4269 return SDValue();
4270 if ((Offset % 16) & 3)
4271 return SDValue();
4272 int64_t StartOffset = Offset & ~15;
4273 if (StartOffset)
4274 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
4275 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
4276
4277 int EltNo = (Offset - StartOffset) >> 2;
4278 int Mask[4] = { EltNo, EltNo, EltNo, EltNo };
4279 EVT VT = (PVT == MVT::i32) ? MVT::v4i32 : MVT::v4f32;
Chris Lattner51abfe42010-09-21 06:02:19 +00004280 SDValue V1 = DAG.getLoad(VT, dl, Chain, Ptr,
4281 LD->getPointerInfo().getWithOffset(StartOffset),
David Greene67c9d422010-02-15 16:53:33 +00004282 false, false, 0);
Evan Chengc3630942009-12-09 21:00:30 +00004283 // Canonicalize it to a v4i32 shuffle.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004284 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, V1);
4285 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Chengc3630942009-12-09 21:00:30 +00004286 DAG.getVectorShuffle(MVT::v4i32, dl, V1,
Chris Lattner51abfe42010-09-21 06:02:19 +00004287 DAG.getUNDEF(MVT::v4i32),&Mask[0]));
Evan Chengc3630942009-12-09 21:00:30 +00004288 }
4289
4290 return SDValue();
4291}
4292
Michael J. Spencerec38de22010-10-10 22:04:20 +00004293/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
4294/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00004295/// load which has the same value as a build_vector whose operands are 'elts'.
4296///
4297/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00004298///
Nate Begeman1449f292010-03-24 22:19:06 +00004299/// FIXME: we'd also like to handle the case where the last elements are zero
4300/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
4301/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004302static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00004303 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004304 EVT EltVT = VT.getVectorElementType();
4305 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00004306
Nate Begemanfdea31a2010-03-24 20:49:50 +00004307 LoadSDNode *LDBase = NULL;
4308 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004309
Nate Begeman1449f292010-03-24 22:19:06 +00004310 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00004311 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00004312 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004313 for (unsigned i = 0; i < NumElems; ++i) {
4314 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00004315
Nate Begemanfdea31a2010-03-24 20:49:50 +00004316 if (!Elt.getNode() ||
4317 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
4318 return SDValue();
4319 if (!LDBase) {
4320 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
4321 return SDValue();
4322 LDBase = cast<LoadSDNode>(Elt.getNode());
4323 LastLoadedElt = i;
4324 continue;
4325 }
4326 if (Elt.getOpcode() == ISD::UNDEF)
4327 continue;
4328
4329 LoadSDNode *LD = cast<LoadSDNode>(Elt);
4330 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
4331 return SDValue();
4332 LastLoadedElt = i;
4333 }
Nate Begeman1449f292010-03-24 22:19:06 +00004334
4335 // If we have found an entire vector of loads and undefs, then return a large
4336 // load of the entire vector width starting at the base pointer. If we found
4337 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004338 if (LastLoadedElt == NumElems - 1) {
4339 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00004340 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004341 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004342 LDBase->isVolatile(), LDBase->isNonTemporal(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00004343 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004344 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004345 LDBase->isVolatile(), LDBase->isNonTemporal(),
4346 LDBase->getAlignment());
4347 } else if (NumElems == 4 && LastLoadedElt == 1) {
4348 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
4349 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Chris Lattner88641552010-09-22 00:34:38 +00004350 SDValue ResNode = DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys,
4351 Ops, 2, MVT::i32,
4352 LDBase->getMemOperand());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004353 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00004354 }
4355 return SDValue();
4356}
4357
Evan Chengc3630942009-12-09 21:00:30 +00004358SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004359X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004360 DebugLoc dl = Op.getDebugLoc();
David Greenea5f26012011-02-07 19:36:54 +00004361
David Greenef125a292011-02-08 19:04:41 +00004362 EVT VT = Op.getValueType();
4363 EVT ExtVT = VT.getVectorElementType();
4364
4365 unsigned NumElems = Op.getNumOperands();
4366
4367 // For AVX-length vectors, build the individual 128-bit pieces and
4368 // use shuffles to put them in place.
Owen Anderson95771af2011-02-25 21:41:48 +00004369 if (VT.getSizeInBits() > 256 &&
4370 Subtarget->hasAVX() &&
David Greenef125a292011-02-08 19:04:41 +00004371 !ISD::isBuildVectorAllZeros(Op.getNode())) {
4372 SmallVector<SDValue, 8> V;
4373 V.resize(NumElems);
4374 for (unsigned i = 0; i < NumElems; ++i) {
4375 V[i] = Op.getOperand(i);
4376 }
Owen Anderson95771af2011-02-25 21:41:48 +00004377
David Greenef125a292011-02-08 19:04:41 +00004378 EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElems/2);
4379
4380 // Build the lower subvector.
4381 SDValue Lower = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[0], NumElems/2);
4382 // Build the upper subvector.
4383 SDValue Upper = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[NumElems / 2],
4384 NumElems/2);
4385
4386 return ConcatVectors(Lower, Upper, DAG);
4387 }
4388
Chris Lattner6e80e442010-08-28 17:15:43 +00004389 // All zero's are handled with pxor in SSE2 and above, xorps in SSE1.
4390 // All one's are handled with pcmpeqd. In AVX, zero's are handled with
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004391 // vpxor in 128-bit and xor{pd,ps} in 256-bit, but no 256 version of pcmpeqd
4392 // is present, so AllOnes is ignored.
4393 if (ISD::isBuildVectorAllZeros(Op.getNode()) ||
4394 (Op.getValueType().getSizeInBits() != 256 &&
4395 ISD::isBuildVectorAllOnes(Op.getNode()))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004396 // Canonicalize this to <4 x i32> (SSE) to
Chris Lattner8a594482007-11-25 00:24:49 +00004397 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
4398 // eliminated on x86-32 hosts.
Dale Johannesen0488fb62010-09-30 23:57:10 +00004399 if (Op.getValueType() == MVT::v4i32)
Chris Lattner8a594482007-11-25 00:24:49 +00004400 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004401
Gabor Greifba36cb52008-08-28 21:40:38 +00004402 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00004403 return getOnesVector(Op.getValueType(), DAG, dl);
4404 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00004405 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004406
Owen Andersone50ed302009-08-10 22:56:29 +00004407 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004408
Evan Cheng0db9fe62006-04-25 20:13:52 +00004409 unsigned NumZero = 0;
4410 unsigned NumNonZero = 0;
4411 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004412 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00004413 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004414 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00004415 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00004416 if (Elt.getOpcode() == ISD::UNDEF)
4417 continue;
4418 Values.insert(Elt);
4419 if (Elt.getOpcode() != ISD::Constant &&
4420 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004421 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00004422 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00004423 NumZero++;
4424 else {
4425 NonZeros |= (1 << i);
4426 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004427 }
4428 }
4429
Chris Lattner97a2a562010-08-26 05:24:29 +00004430 // All undef vector. Return an UNDEF. All zero vectors were handled above.
4431 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00004432 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004433
Chris Lattner67f453a2008-03-09 05:42:06 +00004434 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00004435 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004436 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00004437 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00004438
Chris Lattner62098042008-03-09 01:05:04 +00004439 // If this is an insertion of an i64 value on x86-32, and if the top bits of
4440 // the value are obviously zero, truncate the value to i32 and do the
4441 // insertion that way. Only do this if the value is non-constant or if the
4442 // value is a constant being inserted into element 0. It is cheaper to do
4443 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00004444 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00004445 (!IsAllConstants || Idx == 0)) {
4446 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004447 // Handle SSE only.
4448 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
4449 EVT VecVT = MVT::v4i32;
4450 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00004451
Chris Lattner62098042008-03-09 01:05:04 +00004452 // Truncate the value (which may itself be a constant) to i32, and
4453 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00004454 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00004455 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00004456 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
4457 Subtarget->hasSSE2(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00004458
Chris Lattner62098042008-03-09 01:05:04 +00004459 // Now we have our 32-bit value zero extended in the low element of
4460 // a vector. If Idx != 0, swizzle it into place.
4461 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004462 SmallVector<int, 4> Mask;
4463 Mask.push_back(Idx);
4464 for (unsigned i = 1; i != VecElts; ++i)
4465 Mask.push_back(i);
4466 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopherfd179292009-08-27 18:07:15 +00004467 DAG.getUNDEF(Item.getValueType()),
Nate Begeman9008ca62009-04-27 18:41:29 +00004468 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004469 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004470 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00004471 }
4472 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004473
Chris Lattner19f79692008-03-08 22:59:52 +00004474 // If we have a constant or non-constant insertion into the low element of
4475 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
4476 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00004477 // depending on what the source datatype is.
4478 if (Idx == 0) {
4479 if (NumZero == 0) {
4480 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Owen Anderson825b72b2009-08-11 20:47:22 +00004481 } else if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
4482 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Eli Friedman10415532009-06-06 06:05:10 +00004483 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
4484 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
4485 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget->hasSSE2(),
4486 DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00004487 } else if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
4488 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Dale Johannesen0488fb62010-09-30 23:57:10 +00004489 assert(VT.getSizeInBits() == 128 && "Expected an SSE value type!");
4490 EVT MiddleVT = MVT::v4i32;
Eli Friedman10415532009-06-06 06:05:10 +00004491 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
4492 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
4493 Subtarget->hasSSE2(), DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004494 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00004495 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004496 }
Evan Chengf26ffe92008-05-29 08:22:04 +00004497
4498 // Is it a vector logical left shift?
4499 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00004500 X86::isZeroNode(Op.getOperand(0)) &&
4501 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004502 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00004503 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00004504 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004505 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00004506 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004507 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004508
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004509 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00004510 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004511
Chris Lattner19f79692008-03-08 22:59:52 +00004512 // Otherwise, if this is a vector with i32 or f32 elements, and the element
4513 // is a non-constant being inserted into an element other than the low one,
4514 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
4515 // movd/movss) to move this into the low element, then shuffle it into
4516 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004517 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00004518 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00004519
Evan Cheng0db9fe62006-04-25 20:13:52 +00004520 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00004521 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
4522 Subtarget->hasSSE2(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00004523 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004524 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00004525 MaskVec.push_back(i == Idx ? 0 : 1);
4526 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004527 }
4528 }
4529
Chris Lattner67f453a2008-03-09 05:42:06 +00004530 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00004531 if (Values.size() == 1) {
4532 if (EVTBits == 32) {
4533 // Instead of a shuffle like this:
4534 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
4535 // Check if it's possible to issue this instead.
4536 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
4537 unsigned Idx = CountTrailingZeros_32(NonZeros);
4538 SDValue Item = Op.getOperand(Idx);
4539 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
4540 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
4541 }
Dan Gohman475871a2008-07-27 21:46:04 +00004542 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004543 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004544
Dan Gohmana3941172007-07-24 22:55:08 +00004545 // A vector full of immediates; various special cases are already
4546 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004547 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00004548 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00004549
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00004550 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00004551 if (EVTBits == 64) {
4552 if (NumNonZero == 1) {
4553 // One half is zero or undef.
4554 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00004555 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00004556 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00004557 return getShuffleVectorZeroOrUndef(V2, Idx, true,
4558 Subtarget->hasSSE2(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00004559 }
Dan Gohman475871a2008-07-27 21:46:04 +00004560 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00004561 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004562
4563 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00004564 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00004565 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00004566 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004567 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004568 }
4569
Bill Wendling826f36f2007-03-28 00:57:11 +00004570 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00004571 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Chris Lattner97a2a562010-08-26 05:24:29 +00004572 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004573 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004574 }
4575
4576 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00004577 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00004578 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004579 if (NumElems == 4 && NumZero > 0) {
4580 for (unsigned i = 0; i < 4; ++i) {
4581 bool isZero = !(NonZeros & (1 << i));
4582 if (isZero)
Dale Johannesenace16102009-02-03 19:33:06 +00004583 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004584 else
Dale Johannesenace16102009-02-03 19:33:06 +00004585 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004586 }
4587
4588 for (unsigned i = 0; i < 2; ++i) {
4589 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
4590 default: break;
4591 case 0:
4592 V[i] = V[i*2]; // Must be a zero vector.
4593 break;
4594 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00004595 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004596 break;
4597 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00004598 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004599 break;
4600 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00004601 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004602 break;
4603 }
4604 }
4605
Nate Begeman9008ca62009-04-27 18:41:29 +00004606 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004607 bool Reverse = (NonZeros & 0x3) == 2;
4608 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004609 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004610 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
4611 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004612 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
4613 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004614 }
4615
Nate Begemanfdea31a2010-03-24 20:49:50 +00004616 if (Values.size() > 1 && VT.getSizeInBits() == 128) {
4617 // Check for a build vector of consecutive loads.
4618 for (unsigned i = 0; i < NumElems; ++i)
4619 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00004620
Nate Begemanfdea31a2010-03-24 20:49:50 +00004621 // Check for elements which are consecutive loads.
4622 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
4623 if (LD.getNode())
4624 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004625
4626 // For SSE 4.1, use insertps to put the high elements into the low element.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004627 if (getSubtarget()->hasSSE41()) {
Chris Lattner24faf612010-08-28 17:59:08 +00004628 SDValue Result;
4629 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
4630 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
4631 else
4632 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00004633
Chris Lattner24faf612010-08-28 17:59:08 +00004634 for (unsigned i = 1; i < NumElems; ++i) {
4635 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
4636 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00004637 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00004638 }
4639 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00004640 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00004641
Chris Lattner6e80e442010-08-28 17:15:43 +00004642 // Otherwise, expand into a number of unpckl*, start by extending each of
4643 // our (non-undef) elements to the full vector width with the element in the
4644 // bottom slot of the vector (which generates no code for SSE).
4645 for (unsigned i = 0; i < NumElems; ++i) {
4646 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
4647 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
4648 else
4649 V[i] = DAG.getUNDEF(VT);
4650 }
4651
4652 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004653 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
4654 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
4655 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00004656 unsigned EltStride = NumElems >> 1;
4657 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00004658 for (unsigned i = 0; i < EltStride; ++i) {
4659 // If V[i+EltStride] is undef and this is the first round of mixing,
4660 // then it is safe to just drop this shuffle: V[i] is already in the
4661 // right place, the one element (since it's the first round) being
4662 // inserted as undef can be dropped. This isn't safe for successive
4663 // rounds because they will permute elements within both vectors.
4664 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
4665 EltStride == NumElems/2)
4666 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004667
Chris Lattner6e80e442010-08-28 17:15:43 +00004668 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00004669 }
Chris Lattner6e80e442010-08-28 17:15:43 +00004670 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004671 }
4672 return V[0];
4673 }
Dan Gohman475871a2008-07-27 21:46:04 +00004674 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004675}
4676
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004677SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004678X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const {
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004679 // We support concatenate two MMX registers and place them in a MMX
4680 // register. This is better than doing a stack convert.
4681 DebugLoc dl = Op.getDebugLoc();
4682 EVT ResVT = Op.getValueType();
4683 assert(Op.getNumOperands() == 2);
4684 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
4685 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
4686 int Mask[2];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004687 SDValue InVec = DAG.getNode(ISD::BITCAST,dl, MVT::v1i64, Op.getOperand(0));
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004688 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4689 InVec = Op.getOperand(1);
4690 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4691 unsigned NumElts = ResVT.getVectorNumElements();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004692 VecOp = DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004693 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
4694 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
4695 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004696 InVec = DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, InVec);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004697 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4698 Mask[0] = 0; Mask[1] = 2;
4699 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
4700 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004701 return DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004702}
4703
Nate Begemanb9a47b82009-02-23 08:49:38 +00004704// v8i16 shuffles - Prefer shuffles in the following order:
4705// 1. [all] pshuflw, pshufhw, optional move
4706// 2. [ssse3] 1 x pshufb
4707// 3. [ssse3] 2 x pshufb + 1 x por
4708// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004709SDValue
4710X86TargetLowering::LowerVECTOR_SHUFFLEv8i16(SDValue Op,
4711 SelectionDAG &DAG) const {
4712 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00004713 SDValue V1 = SVOp->getOperand(0);
4714 SDValue V2 = SVOp->getOperand(1);
4715 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004716 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00004717
Nate Begemanb9a47b82009-02-23 08:49:38 +00004718 // Determine if more than 1 of the words in each of the low and high quadwords
4719 // of the result come from the same quadword of one of the two inputs. Undef
4720 // mask values count as coming from any quadword, for better codegen.
4721 SmallVector<unsigned, 4> LoQuad(4);
4722 SmallVector<unsigned, 4> HiQuad(4);
4723 BitVector InputQuads(4);
4724 for (unsigned i = 0; i < 8; ++i) {
4725 SmallVectorImpl<unsigned> &Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00004726 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004727 MaskVals.push_back(EltIdx);
4728 if (EltIdx < 0) {
4729 ++Quad[0];
4730 ++Quad[1];
4731 ++Quad[2];
4732 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00004733 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004734 }
4735 ++Quad[EltIdx / 4];
4736 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00004737 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004738
Nate Begemanb9a47b82009-02-23 08:49:38 +00004739 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004740 unsigned MaxQuad = 1;
4741 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004742 if (LoQuad[i] > MaxQuad) {
4743 BestLoQuad = i;
4744 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004745 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004746 }
4747
Nate Begemanb9a47b82009-02-23 08:49:38 +00004748 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004749 MaxQuad = 1;
4750 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004751 if (HiQuad[i] > MaxQuad) {
4752 BestHiQuad = i;
4753 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004754 }
4755 }
4756
Nate Begemanb9a47b82009-02-23 08:49:38 +00004757 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00004758 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00004759 // single pshufb instruction is necessary. If There are more than 2 input
4760 // quads, disable the next transformation since it does not help SSSE3.
4761 bool V1Used = InputQuads[0] || InputQuads[1];
4762 bool V2Used = InputQuads[2] || InputQuads[3];
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004763 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004764 if (InputQuads.count() == 2 && V1Used && V2Used) {
4765 BestLoQuad = InputQuads.find_first();
4766 BestHiQuad = InputQuads.find_next(BestLoQuad);
4767 }
4768 if (InputQuads.count() > 2) {
4769 BestLoQuad = -1;
4770 BestHiQuad = -1;
4771 }
4772 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004773
Nate Begemanb9a47b82009-02-23 08:49:38 +00004774 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
4775 // the shuffle mask. If a quad is scored as -1, that means that it contains
4776 // words from all 4 input quadwords.
4777 SDValue NewV;
4778 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004779 SmallVector<int, 8> MaskV;
4780 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
4781 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Eric Christopherfd179292009-08-27 18:07:15 +00004782 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004783 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
4784 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
4785 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00004786
Nate Begemanb9a47b82009-02-23 08:49:38 +00004787 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
4788 // source words for the shuffle, to aid later transformations.
4789 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00004790 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00004791 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004792 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00004793 if (idx != (int)i)
4794 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004795 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00004796 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004797 AllWordsInNewV = false;
4798 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00004799 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004800
Nate Begemanb9a47b82009-02-23 08:49:38 +00004801 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
4802 if (AllWordsInNewV) {
4803 for (int i = 0; i != 8; ++i) {
4804 int idx = MaskVals[i];
4805 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00004806 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004807 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004808 if ((idx != i) && idx < 4)
4809 pshufhw = false;
4810 if ((idx != i) && idx > 3)
4811 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00004812 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00004813 V1 = NewV;
4814 V2Used = false;
4815 BestLoQuad = 0;
4816 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004817 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004818
Nate Begemanb9a47b82009-02-23 08:49:38 +00004819 // If we've eliminated the use of V2, and the new mask is a pshuflw or
4820 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00004821 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00004822 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
4823 unsigned TargetMask = 0;
4824 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00004825 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00004826 TargetMask = pshufhw ? X86::getShufflePSHUFHWImmediate(NewV.getNode()):
4827 X86::getShufflePSHUFLWImmediate(NewV.getNode());
4828 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004829 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00004830 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004831 }
Eric Christopherfd179292009-08-27 18:07:15 +00004832
Nate Begemanb9a47b82009-02-23 08:49:38 +00004833 // If we have SSSE3, and all words of the result are from 1 input vector,
4834 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
4835 // is present, fall back to case 4.
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004836 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004837 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004838
Nate Begemanb9a47b82009-02-23 08:49:38 +00004839 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00004840 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00004841 // mask, and elements that come from V1 in the V2 mask, so that the two
4842 // results can be OR'd together.
4843 bool TwoInputs = V1Used && V2Used;
4844 for (unsigned i = 0; i != 8; ++i) {
4845 int EltIdx = MaskVals[i] * 2;
4846 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004847 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4848 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004849 continue;
4850 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004851 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
4852 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004853 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004854 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004855 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004856 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004857 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004858 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004859 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004860
Nate Begemanb9a47b82009-02-23 08:49:38 +00004861 // Calculate the shuffle mask for the second input, shuffle it, and
4862 // OR it with the first shuffled input.
4863 pshufbMask.clear();
4864 for (unsigned i = 0; i != 8; ++i) {
4865 int EltIdx = MaskVals[i] * 2;
4866 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004867 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4868 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004869 continue;
4870 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004871 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
4872 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004873 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004874 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00004875 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00004876 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004877 MVT::v16i8, &pshufbMask[0], 16));
4878 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004879 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004880 }
4881
4882 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
4883 // and update MaskVals with new element order.
4884 BitVector InOrder(8);
4885 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004886 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004887 for (int i = 0; i != 4; ++i) {
4888 int idx = MaskVals[i];
4889 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004890 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004891 InOrder.set(i);
4892 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004893 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004894 InOrder.set(i);
4895 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004896 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004897 }
4898 }
4899 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004900 MaskV.push_back(i);
Owen Anderson825b72b2009-08-11 20:47:22 +00004901 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004902 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004903
4904 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
4905 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
4906 NewV.getOperand(0),
4907 X86::getShufflePSHUFLWImmediate(NewV.getNode()),
4908 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004909 }
Eric Christopherfd179292009-08-27 18:07:15 +00004910
Nate Begemanb9a47b82009-02-23 08:49:38 +00004911 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
4912 // and update MaskVals with the new element order.
4913 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004914 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004915 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004916 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004917 for (unsigned i = 4; i != 8; ++i) {
4918 int idx = MaskVals[i];
4919 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004920 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004921 InOrder.set(i);
4922 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004923 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004924 InOrder.set(i);
4925 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004926 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004927 }
4928 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004929 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004930 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004931
4932 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
4933 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
4934 NewV.getOperand(0),
4935 X86::getShufflePSHUFHWImmediate(NewV.getNode()),
4936 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004937 }
Eric Christopherfd179292009-08-27 18:07:15 +00004938
Nate Begemanb9a47b82009-02-23 08:49:38 +00004939 // In case BestHi & BestLo were both -1, which means each quadword has a word
4940 // from each of the four input quadwords, calculate the InOrder bitvector now
4941 // before falling through to the insert/extract cleanup.
4942 if (BestLoQuad == -1 && BestHiQuad == -1) {
4943 NewV = V1;
4944 for (int i = 0; i != 8; ++i)
4945 if (MaskVals[i] < 0 || MaskVals[i] == i)
4946 InOrder.set(i);
4947 }
Eric Christopherfd179292009-08-27 18:07:15 +00004948
Nate Begemanb9a47b82009-02-23 08:49:38 +00004949 // The other elements are put in the right place using pextrw and pinsrw.
4950 for (unsigned i = 0; i != 8; ++i) {
4951 if (InOrder[i])
4952 continue;
4953 int EltIdx = MaskVals[i];
4954 if (EltIdx < 0)
4955 continue;
4956 SDValue ExtOp = (EltIdx < 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00004957 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004958 DAG.getIntPtrConstant(EltIdx))
Owen Anderson825b72b2009-08-11 20:47:22 +00004959 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004960 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00004961 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004962 DAG.getIntPtrConstant(i));
4963 }
4964 return NewV;
4965}
4966
4967// v16i8 shuffles - Prefer shuffles in the following order:
4968// 1. [ssse3] 1 x pshufb
4969// 2. [ssse3] 2 x pshufb + 1 x por
4970// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
4971static
Nate Begeman9008ca62009-04-27 18:41:29 +00004972SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00004973 SelectionDAG &DAG,
4974 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004975 SDValue V1 = SVOp->getOperand(0);
4976 SDValue V2 = SVOp->getOperand(1);
4977 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004978 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00004979 SVOp->getMask(MaskVals);
Eric Christopherfd179292009-08-27 18:07:15 +00004980
Nate Begemanb9a47b82009-02-23 08:49:38 +00004981 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00004982 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00004983 // present, fall back to case 3.
4984 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
4985 bool V1Only = true;
4986 bool V2Only = true;
4987 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004988 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00004989 if (EltIdx < 0)
4990 continue;
4991 if (EltIdx < 16)
4992 V2Only = false;
4993 else
4994 V1Only = false;
4995 }
Eric Christopherfd179292009-08-27 18:07:15 +00004996
Nate Begemanb9a47b82009-02-23 08:49:38 +00004997 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
4998 if (TLI.getSubtarget()->hasSSSE3()) {
4999 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005000
Nate Begemanb9a47b82009-02-23 08:49:38 +00005001 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00005002 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005003 //
5004 // Otherwise, we have elements from both input vectors, and must zero out
5005 // elements that come from V2 in the first mask, and V1 in the second mask
5006 // so that we can OR them together.
5007 bool TwoInputs = !(V1Only || V2Only);
5008 for (unsigned i = 0; i != 16; ++i) {
5009 int EltIdx = MaskVals[i];
5010 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005011 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005012 continue;
5013 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005014 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005015 }
5016 // If all the elements are from V2, assign it to V1 and return after
5017 // building the first pshufb.
5018 if (V2Only)
5019 V1 = V2;
Owen Anderson825b72b2009-08-11 20:47:22 +00005020 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005021 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005022 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005023 if (!TwoInputs)
5024 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00005025
Nate Begemanb9a47b82009-02-23 08:49:38 +00005026 // Calculate the shuffle mask for the second input, shuffle it, and
5027 // OR it with the first shuffled input.
5028 pshufbMask.clear();
5029 for (unsigned i = 0; i != 16; ++i) {
5030 int EltIdx = MaskVals[i];
5031 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005032 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005033 continue;
5034 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005035 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005036 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005037 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005038 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005039 MVT::v16i8, &pshufbMask[0], 16));
5040 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005041 }
Eric Christopherfd179292009-08-27 18:07:15 +00005042
Nate Begemanb9a47b82009-02-23 08:49:38 +00005043 // No SSSE3 - Calculate in place words and then fix all out of place words
5044 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
5045 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005046 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
5047 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005048 SDValue NewV = V2Only ? V2 : V1;
5049 for (int i = 0; i != 8; ++i) {
5050 int Elt0 = MaskVals[i*2];
5051 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00005052
Nate Begemanb9a47b82009-02-23 08:49:38 +00005053 // This word of the result is all undef, skip it.
5054 if (Elt0 < 0 && Elt1 < 0)
5055 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005056
Nate Begemanb9a47b82009-02-23 08:49:38 +00005057 // This word of the result is already in the correct place, skip it.
5058 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
5059 continue;
5060 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
5061 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005062
Nate Begemanb9a47b82009-02-23 08:49:38 +00005063 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
5064 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
5065 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00005066
5067 // If Elt0 and Elt1 are defined, are consecutive, and can be load
5068 // using a single extract together, load it and store it.
5069 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005070 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005071 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00005072 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005073 DAG.getIntPtrConstant(i));
5074 continue;
5075 }
5076
Nate Begemanb9a47b82009-02-23 08:49:38 +00005077 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00005078 // source byte is not also odd, shift the extracted word left 8 bits
5079 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005080 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005081 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005082 DAG.getIntPtrConstant(Elt1 / 2));
5083 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005084 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Owen Anderson95771af2011-02-25 21:41:48 +00005085 DAG.getConstant(8,
5086 TLI.getShiftAmountTy(InsElt.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005087 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005088 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
5089 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005090 }
5091 // If Elt0 is defined, extract it from the appropriate source. If the
5092 // source byte is not also even, shift the extracted word right 8 bits. If
5093 // Elt1 was also defined, OR the extracted values together before
5094 // inserting them in the result.
5095 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005096 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005097 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
5098 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005099 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Owen Anderson95771af2011-02-25 21:41:48 +00005100 DAG.getConstant(8,
5101 TLI.getShiftAmountTy(InsElt0.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005102 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005103 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
5104 DAG.getConstant(0x00FF, MVT::i16));
5105 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00005106 : InsElt0;
5107 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005108 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005109 DAG.getIntPtrConstant(i));
5110 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005111 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005112}
5113
Evan Cheng7a831ce2007-12-15 03:00:47 +00005114/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005115/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00005116/// done when every pair / quad of shuffle mask elements point to elements in
5117/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005118/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00005119static
Nate Begeman9008ca62009-04-27 18:41:29 +00005120SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005121 SelectionDAG &DAG, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00005122 EVT VT = SVOp->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00005123 SDValue V1 = SVOp->getOperand(0);
5124 SDValue V2 = SVOp->getOperand(1);
5125 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00005126 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005127 EVT NewVT;
Owen Anderson825b72b2009-08-11 20:47:22 +00005128 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005129 default: assert(false && "Unexpected!");
Owen Anderson825b72b2009-08-11 20:47:22 +00005130 case MVT::v4f32: NewVT = MVT::v2f64; break;
5131 case MVT::v4i32: NewVT = MVT::v2i64; break;
5132 case MVT::v8i16: NewVT = MVT::v4i32; break;
5133 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00005134 }
5135
Nate Begeman9008ca62009-04-27 18:41:29 +00005136 int Scale = NumElems / NewWidth;
5137 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00005138 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005139 int StartIdx = -1;
5140 for (int j = 0; j < Scale; ++j) {
5141 int EltIdx = SVOp->getMaskElt(i+j);
5142 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005143 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00005144 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00005145 StartIdx = EltIdx - (EltIdx % Scale);
5146 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00005147 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00005148 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005149 if (StartIdx == -1)
5150 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00005151 else
Nate Begeman9008ca62009-04-27 18:41:29 +00005152 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005153 }
5154
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005155 V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, V1);
5156 V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00005157 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005158}
5159
Evan Chengd880b972008-05-09 21:53:03 +00005160/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005161///
Owen Andersone50ed302009-08-10 22:56:29 +00005162static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00005163 SDValue SrcOp, SelectionDAG &DAG,
5164 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005165 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005166 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00005167 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00005168 LD = dyn_cast<LoadSDNode>(SrcOp);
5169 if (!LD) {
5170 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
5171 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00005172 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00005173 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00005174 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005175 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00005176 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005177 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00005178 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005179 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005180 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
5181 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
5182 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00005183 SrcOp.getOperand(0)
5184 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005185 }
5186 }
5187 }
5188
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005189 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005190 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005191 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00005192 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005193}
5194
Evan Chengace3c172008-07-22 21:13:36 +00005195/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
5196/// shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00005197static SDValue
Nate Begeman9008ca62009-04-27 18:41:29 +00005198LowerVECTOR_SHUFFLE_4wide(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
5199 SDValue V1 = SVOp->getOperand(0);
5200 SDValue V2 = SVOp->getOperand(1);
5201 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005202 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00005203
Evan Chengace3c172008-07-22 21:13:36 +00005204 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00005205 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00005206 SmallVector<int, 8> Mask1(4U, -1);
5207 SmallVector<int, 8> PermMask;
5208 SVOp->getMask(PermMask);
5209
Evan Chengace3c172008-07-22 21:13:36 +00005210 unsigned NumHi = 0;
5211 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00005212 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005213 int Idx = PermMask[i];
5214 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00005215 Locs[i] = std::make_pair(-1, -1);
5216 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005217 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
5218 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00005219 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00005220 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005221 NumLo++;
5222 } else {
5223 Locs[i] = std::make_pair(1, NumHi);
5224 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00005225 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005226 NumHi++;
5227 }
5228 }
5229 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005230
Evan Chengace3c172008-07-22 21:13:36 +00005231 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005232 // If no more than two elements come from either vector. This can be
5233 // implemented with two shuffles. First shuffle gather the elements.
5234 // The second shuffle, which takes the first shuffle as both of its
5235 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00005236 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005237
Nate Begeman9008ca62009-04-27 18:41:29 +00005238 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopherfd179292009-08-27 18:07:15 +00005239
Evan Chengace3c172008-07-22 21:13:36 +00005240 for (unsigned i = 0; i != 4; ++i) {
5241 if (Locs[i].first == -1)
5242 continue;
5243 else {
5244 unsigned Idx = (i < 2) ? 0 : 4;
5245 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00005246 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005247 }
5248 }
5249
Nate Begeman9008ca62009-04-27 18:41:29 +00005250 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005251 } else if (NumLo == 3 || NumHi == 3) {
5252 // Otherwise, we must have three elements from one vector, call it X, and
5253 // one element from the other, call it Y. First, use a shufps to build an
5254 // intermediate vector with the one element from Y and the element from X
5255 // that will be in the same half in the final destination (the indexes don't
5256 // matter). Then, use a shufps to build the final vector, taking the half
5257 // containing the element from Y from the intermediate, and the other half
5258 // from X.
5259 if (NumHi == 3) {
5260 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00005261 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005262 std::swap(V1, V2);
5263 }
5264
5265 // Find the element from V2.
5266 unsigned HiIndex;
5267 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005268 int Val = PermMask[HiIndex];
5269 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005270 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005271 if (Val >= 4)
5272 break;
5273 }
5274
Nate Begeman9008ca62009-04-27 18:41:29 +00005275 Mask1[0] = PermMask[HiIndex];
5276 Mask1[1] = -1;
5277 Mask1[2] = PermMask[HiIndex^1];
5278 Mask1[3] = -1;
5279 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005280
5281 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005282 Mask1[0] = PermMask[0];
5283 Mask1[1] = PermMask[1];
5284 Mask1[2] = HiIndex & 1 ? 6 : 4;
5285 Mask1[3] = HiIndex & 1 ? 4 : 6;
5286 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005287 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005288 Mask1[0] = HiIndex & 1 ? 2 : 0;
5289 Mask1[1] = HiIndex & 1 ? 0 : 2;
5290 Mask1[2] = PermMask[2];
5291 Mask1[3] = PermMask[3];
5292 if (Mask1[2] >= 0)
5293 Mask1[2] += 4;
5294 if (Mask1[3] >= 0)
5295 Mask1[3] += 4;
5296 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005297 }
Evan Chengace3c172008-07-22 21:13:36 +00005298 }
5299
5300 // Break it into (shuffle shuffle_hi, shuffle_lo).
5301 Locs.clear();
David Greenec4db4e52011-02-28 19:06:56 +00005302 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00005303 SmallVector<int,8> LoMask(4U, -1);
5304 SmallVector<int,8> HiMask(4U, -1);
5305
5306 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00005307 unsigned MaskIdx = 0;
5308 unsigned LoIdx = 0;
5309 unsigned HiIdx = 2;
5310 for (unsigned i = 0; i != 4; ++i) {
5311 if (i == 2) {
5312 MaskPtr = &HiMask;
5313 MaskIdx = 1;
5314 LoIdx = 0;
5315 HiIdx = 2;
5316 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005317 int Idx = PermMask[i];
5318 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00005319 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00005320 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00005321 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00005322 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005323 LoIdx++;
5324 } else {
5325 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00005326 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005327 HiIdx++;
5328 }
5329 }
5330
Nate Begeman9008ca62009-04-27 18:41:29 +00005331 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
5332 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
5333 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00005334 for (unsigned i = 0; i != 4; ++i) {
5335 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005336 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00005337 } else {
5338 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00005339 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00005340 }
5341 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005342 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00005343}
5344
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005345static bool MayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005346 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005347 V = V.getOperand(0);
5348 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
5349 V = V.getOperand(0);
5350 if (MayFoldLoad(V))
5351 return true;
5352 return false;
5353}
5354
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005355// FIXME: the version above should always be used. Since there's
5356// a bug where several vector shuffles can't be folded because the
5357// DAG is not updated during lowering and a node claims to have two
5358// uses while it only has one, use this version, and let isel match
5359// another instruction if the load really happens to have more than
5360// one use. Remove this version after this bug get fixed.
Evan Cheng835580f2010-10-07 20:50:20 +00005361// rdar://8434668, PR8156
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005362static bool RelaxedMayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005363 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005364 V = V.getOperand(0);
5365 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
5366 V = V.getOperand(0);
5367 if (ISD::isNormalLoad(V.getNode()))
5368 return true;
5369 return false;
5370}
5371
5372/// CanFoldShuffleIntoVExtract - Check if the current shuffle is used by
5373/// a vector extract, and if both can be later optimized into a single load.
5374/// This is done in visitEXTRACT_VECTOR_ELT and the conditions are checked
5375/// here because otherwise a target specific shuffle node is going to be
5376/// emitted for this shuffle, and the optimization not done.
5377/// FIXME: This is probably not the best approach, but fix the problem
5378/// until the right path is decided.
5379static
5380bool CanXFormVExtractWithShuffleIntoLoad(SDValue V, SelectionDAG &DAG,
5381 const TargetLowering &TLI) {
5382 EVT VT = V.getValueType();
5383 ShuffleVectorSDNode *SVOp = dyn_cast<ShuffleVectorSDNode>(V);
5384
5385 // Be sure that the vector shuffle is present in a pattern like this:
5386 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), c) -> (f32 load $addr)
5387 if (!V.hasOneUse())
5388 return false;
5389
5390 SDNode *N = *V.getNode()->use_begin();
5391 if (N->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
5392 return false;
5393
5394 SDValue EltNo = N->getOperand(1);
5395 if (!isa<ConstantSDNode>(EltNo))
5396 return false;
5397
5398 // If the bit convert changed the number of elements, it is unsafe
5399 // to examine the mask.
5400 bool HasShuffleIntoBitcast = false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005401 if (V.getOpcode() == ISD::BITCAST) {
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005402 EVT SrcVT = V.getOperand(0).getValueType();
5403 if (SrcVT.getVectorNumElements() != VT.getVectorNumElements())
5404 return false;
5405 V = V.getOperand(0);
5406 HasShuffleIntoBitcast = true;
5407 }
5408
5409 // Select the input vector, guarding against out of range extract vector.
5410 unsigned NumElems = VT.getVectorNumElements();
5411 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
5412 int Idx = (Elt > NumElems) ? -1 : SVOp->getMaskElt(Elt);
5413 V = (Idx < (int)NumElems) ? V.getOperand(0) : V.getOperand(1);
5414
5415 // Skip one more bit_convert if necessary
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005416 if (V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005417 V = V.getOperand(0);
5418
5419 if (ISD::isNormalLoad(V.getNode())) {
5420 // Is the original load suitable?
5421 LoadSDNode *LN0 = cast<LoadSDNode>(V);
5422
5423 // FIXME: avoid the multi-use bug that is preventing lots of
5424 // of foldings to be detected, this is still wrong of course, but
5425 // give the temporary desired behavior, and if it happens that
5426 // the load has real more uses, during isel it will not fold, and
5427 // will generate poor code.
5428 if (!LN0 || LN0->isVolatile()) // || !LN0->hasOneUse()
5429 return false;
5430
5431 if (!HasShuffleIntoBitcast)
5432 return true;
5433
5434 // If there's a bitcast before the shuffle, check if the load type and
5435 // alignment is valid.
5436 unsigned Align = LN0->getAlignment();
5437 unsigned NewAlign =
5438 TLI.getTargetData()->getABITypeAlignment(
5439 VT.getTypeForEVT(*DAG.getContext()));
5440
5441 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
5442 return false;
5443 }
5444
5445 return true;
5446}
5447
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005448static
Evan Cheng835580f2010-10-07 20:50:20 +00005449SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
5450 EVT VT = Op.getValueType();
5451
5452 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005453 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
5454 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00005455 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
5456 V1, DAG));
5457}
5458
5459static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005460SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
5461 bool HasSSE2) {
5462 SDValue V1 = Op.getOperand(0);
5463 SDValue V2 = Op.getOperand(1);
5464 EVT VT = Op.getValueType();
5465
5466 assert(VT != MVT::v2i64 && "unsupported shuffle type");
5467
5468 if (HasSSE2 && VT == MVT::v2f64)
5469 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
5470
5471 // v4f32 or v4i32
5472 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V2, DAG);
5473}
5474
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00005475static
5476SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
5477 SDValue V1 = Op.getOperand(0);
5478 SDValue V2 = Op.getOperand(1);
5479 EVT VT = Op.getValueType();
5480
5481 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
5482 "unsupported shuffle type");
5483
5484 if (V2.getOpcode() == ISD::UNDEF)
5485 V2 = V1;
5486
5487 // v4i32 or v4f32
5488 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
5489}
5490
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005491static
5492SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasSSE2) {
5493 SDValue V1 = Op.getOperand(0);
5494 SDValue V2 = Op.getOperand(1);
5495 EVT VT = Op.getValueType();
5496 unsigned NumElems = VT.getVectorNumElements();
5497
5498 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
5499 // operand of these instructions is only memory, so check if there's a
5500 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
5501 // same masks.
5502 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005503
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00005504 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005505 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005506 CanFoldLoad = true;
5507
5508 // When V1 is a load, it can be folded later into a store in isel, example:
5509 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
5510 // turns into:
5511 // (MOVLPSmr addr:$src1, VR128:$src2)
5512 // So, recognize this potential and also use MOVLPS or MOVLPD
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005513 if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005514 CanFoldLoad = true;
5515
Eric Christopher893a8822011-02-20 05:04:42 +00005516 // Both of them can't be memory operations though.
5517 if (MayFoldVectorLoad(V1) && MayFoldVectorLoad(V2))
5518 CanFoldLoad = false;
Owen Anderson95771af2011-02-25 21:41:48 +00005519
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005520 if (CanFoldLoad) {
5521 if (HasSSE2 && NumElems == 2)
5522 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
5523
5524 if (NumElems == 4)
5525 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
5526 }
5527
5528 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5529 // movl and movlp will both match v2i64, but v2i64 is never matched by
5530 // movl earlier because we make it strict to avoid messing with the movlp load
5531 // folding logic (see the code above getMOVLP call). Match it here then,
5532 // this is horrible, but will stay like this until we move all shuffle
5533 // matching to x86 specific nodes. Note that for the 1st condition all
5534 // types are matched with movsd.
5535 if ((HasSSE2 && NumElems == 2) || !X86::isMOVLMask(SVOp))
5536 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
5537 else if (HasSSE2)
5538 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
5539
5540
5541 assert(VT != MVT::v4i32 && "unsupported shuffle type");
5542
5543 // Invert the operand order and use SHUFPS to match it.
5544 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V2, V1,
5545 X86::getShuffleSHUFImmediate(SVOp), DAG);
5546}
5547
David Greenec4db4e52011-02-28 19:06:56 +00005548static inline unsigned getUNPCKLOpcode(EVT VT, const X86Subtarget *Subtarget) {
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005549 switch(VT.getSimpleVT().SimpleTy) {
5550 case MVT::v4i32: return X86ISD::PUNPCKLDQ;
5551 case MVT::v2i64: return X86ISD::PUNPCKLQDQ;
David Greenec4db4e52011-02-28 19:06:56 +00005552 case MVT::v4f32:
5553 return Subtarget->hasAVX() ? X86ISD::VUNPCKLPS : X86ISD::UNPCKLPS;
5554 case MVT::v2f64:
5555 return Subtarget->hasAVX() ? X86ISD::VUNPCKLPD : X86ISD::UNPCKLPD;
5556 case MVT::v8f32: return X86ISD::VUNPCKLPSY;
5557 case MVT::v4f64: return X86ISD::VUNPCKLPDY;
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005558 case MVT::v16i8: return X86ISD::PUNPCKLBW;
5559 case MVT::v8i16: return X86ISD::PUNPCKLWD;
5560 default:
Eric Christopherdd6e40a2011-02-19 03:19:09 +00005561 llvm_unreachable("Unknown type for unpckl");
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005562 }
5563 return 0;
5564}
5565
5566static inline unsigned getUNPCKHOpcode(EVT VT) {
5567 switch(VT.getSimpleVT().SimpleTy) {
5568 case MVT::v4i32: return X86ISD::PUNPCKHDQ;
5569 case MVT::v2i64: return X86ISD::PUNPCKHQDQ;
5570 case MVT::v4f32: return X86ISD::UNPCKHPS;
5571 case MVT::v2f64: return X86ISD::UNPCKHPD;
5572 case MVT::v16i8: return X86ISD::PUNPCKHBW;
5573 case MVT::v8i16: return X86ISD::PUNPCKHWD;
5574 default:
Eric Christopherdd6e40a2011-02-19 03:19:09 +00005575 llvm_unreachable("Unknown type for unpckh");
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005576 }
5577 return 0;
5578}
5579
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005580static
5581SDValue NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG,
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005582 const TargetLowering &TLI,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005583 const X86Subtarget *Subtarget) {
5584 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5585 EVT VT = Op.getValueType();
5586 DebugLoc dl = Op.getDebugLoc();
5587 SDValue V1 = Op.getOperand(0);
5588 SDValue V2 = Op.getOperand(1);
5589
5590 if (isZeroShuffle(SVOp))
5591 return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
5592
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005593 // Handle splat operations
5594 if (SVOp->isSplat()) {
5595 // Special case, this is the only place now where it's
5596 // allowed to return a vector_shuffle operation without
5597 // using a target specific node, because *hopefully* it
5598 // will be optimized away by the dag combiner.
5599 if (VT.getVectorNumElements() <= 4 &&
5600 CanXFormVExtractWithShuffleIntoLoad(Op, DAG, TLI))
5601 return Op;
5602
5603 // Handle splats by matching through known masks
5604 if (VT.getVectorNumElements() <= 4)
5605 return SDValue();
5606
Evan Cheng835580f2010-10-07 20:50:20 +00005607 // Canonicalize all of the remaining to v4f32.
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005608 return PromoteSplat(SVOp, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005609 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005610
5611 // If the shuffle can be profitably rewritten as a narrower shuffle, then
5612 // do it!
5613 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
5614 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5615 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005616 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005617 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
5618 // FIXME: Figure out a cleaner way to do this.
5619 // Try to make use of movq to zero out the top part.
5620 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
5621 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5622 if (NewOp.getNode()) {
5623 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
5624 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
5625 DAG, Subtarget, dl);
5626 }
5627 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
5628 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5629 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
5630 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
5631 DAG, Subtarget, dl);
5632 }
5633 }
5634 return SDValue();
5635}
5636
Dan Gohman475871a2008-07-27 21:46:04 +00005637SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005638X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00005639 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00005640 SDValue V1 = Op.getOperand(0);
5641 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00005642 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005643 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00005644 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005645 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005646 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
5647 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00005648 bool V1IsSplat = false;
5649 bool V2IsSplat = false;
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005650 bool HasSSE2 = Subtarget->hasSSE2() || Subtarget->hasAVX();
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00005651 bool HasSSE3 = Subtarget->hasSSE3() || Subtarget->hasAVX();
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00005652 bool HasSSSE3 = Subtarget->hasSSSE3() || Subtarget->hasAVX();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005653 MachineFunction &MF = DAG.getMachineFunction();
5654 bool OptForSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005655
Dale Johannesen0488fb62010-09-30 23:57:10 +00005656 // Shuffle operations on MMX not supported.
5657 if (isMMX)
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00005658 return Op;
5659
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005660 // Vector shuffle lowering takes 3 steps:
5661 //
5662 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
5663 // narrowing and commutation of operands should be handled.
5664 // 2) Matching of shuffles with known shuffle masks to x86 target specific
5665 // shuffle nodes.
5666 // 3) Rewriting of unmatched masks into new generic shuffle operations,
5667 // so the shuffle can be broken into other shuffles and the legalizer can
5668 // try the lowering again.
5669 //
5670 // The general ideia is that no vector_shuffle operation should be left to
5671 // be matched during isel, all of them must be converted to a target specific
5672 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00005673
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005674 // Normalize the input vectors. Here splats, zeroed vectors, profitable
5675 // narrowing and commutation of operands should be handled. The actual code
5676 // doesn't include all of those, work in progress...
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005677 SDValue NewOp = NormalizeVectorShuffle(Op, DAG, *this, Subtarget);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005678 if (NewOp.getNode())
5679 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00005680
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005681 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
5682 // unpckh_undef). Only use pshufd if speed is more important than size.
5683 if (OptForSize && X86::isUNPCKL_v_undef_Mask(SVOp))
5684 if (VT != MVT::v2i64 && VT != MVT::v2f64)
David Greenec4db4e52011-02-28 19:06:56 +00005685 return getTargetShuffleNode(getUNPCKLOpcode(VT, getSubtarget()), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005686 if (OptForSize && X86::isUNPCKH_v_undef_Mask(SVOp))
5687 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5688 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00005689
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005690 if (X86::isMOVDDUPMask(SVOp) && HasSSE3 && V2IsUndef &&
Dale Johannesen0488fb62010-09-30 23:57:10 +00005691 RelaxedMayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00005692 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005693
Dale Johannesen0488fb62010-09-30 23:57:10 +00005694 if (X86::isMOVHLPS_v_undef_Mask(SVOp))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005695 return getMOVHighToLow(Op, dl, DAG);
5696
5697 // Use to match splats
5698 if (HasSSE2 && X86::isUNPCKHMask(SVOp) && V2IsUndef &&
5699 (VT == MVT::v2f64 || VT == MVT::v2i64))
5700 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
5701
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005702 if (X86::isPSHUFDMask(SVOp)) {
5703 // The actual implementation will match the mask in the if above and then
5704 // during isel it can match several different instructions, not only pshufd
5705 // as its name says, sad but true, emulate the behavior for now...
5706 if (X86::isMOVDDUPMask(SVOp) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
5707 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
5708
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005709 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
5710
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005711 if (HasSSE2 && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005712 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
5713
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005714 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005715 return getTargetShuffleNode(X86ISD::SHUFPD, dl, VT, V1, V1,
5716 TargetMask, DAG);
5717
5718 if (VT == MVT::v4f32)
5719 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V1, V1,
5720 TargetMask, DAG);
5721 }
Eric Christopherfd179292009-08-27 18:07:15 +00005722
Evan Chengf26ffe92008-05-29 08:22:04 +00005723 // Check if this can be converted into a logical shift.
5724 bool isLeft = false;
5725 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00005726 SDValue ShVal;
Nate Begeman9008ca62009-04-27 18:41:29 +00005727 bool isShift = getSubtarget()->hasSSE2() &&
Evan Chengc3630942009-12-09 21:00:30 +00005728 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00005729 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00005730 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00005731 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00005732 EVT EltVT = VT.getVectorElementType();
5733 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00005734 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005735 }
Eric Christopherfd179292009-08-27 18:07:15 +00005736
Nate Begeman9008ca62009-04-27 18:41:29 +00005737 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005738 if (V1IsUndef)
5739 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00005740 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00005741 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Dale Johannesen0488fb62010-09-30 23:57:10 +00005742 if (!X86::isMOVLPMask(SVOp)) {
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005743 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00005744 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
5745
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005746 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00005747 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
5748 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00005749 }
Eric Christopherfd179292009-08-27 18:07:15 +00005750
Nate Begeman9008ca62009-04-27 18:41:29 +00005751 // FIXME: fold these into legal mask.
Dale Johannesen0488fb62010-09-30 23:57:10 +00005752 if (X86::isMOVLHPSMask(SVOp) && !X86::isUNPCKLMask(SVOp))
5753 return getMOVLowToHigh(Op, dl, DAG, HasSSE2);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005754
Dale Johannesen0488fb62010-09-30 23:57:10 +00005755 if (X86::isMOVHLPSMask(SVOp))
5756 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00005757
Dale Johannesen0488fb62010-09-30 23:57:10 +00005758 if (X86::isMOVSHDUPMask(SVOp) && HasSSE3 && V2IsUndef && NumElems == 4)
5759 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00005760
Dale Johannesen0488fb62010-09-30 23:57:10 +00005761 if (X86::isMOVSLDUPMask(SVOp) && HasSSE3 && V2IsUndef && NumElems == 4)
5762 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00005763
Dale Johannesen0488fb62010-09-30 23:57:10 +00005764 if (X86::isMOVLPMask(SVOp))
5765 return getMOVLP(Op, dl, DAG, HasSSE2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005766
Nate Begeman9008ca62009-04-27 18:41:29 +00005767 if (ShouldXformToMOVHLPS(SVOp) ||
5768 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
5769 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005770
Evan Chengf26ffe92008-05-29 08:22:04 +00005771 if (isShift) {
5772 // No better options. Use a vshl / vsrl.
Dan Gohman8a55ce42009-09-23 21:02:20 +00005773 EVT EltVT = VT.getVectorElementType();
5774 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00005775 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005776 }
Eric Christopherfd179292009-08-27 18:07:15 +00005777
Evan Cheng9eca5e82006-10-25 21:49:50 +00005778 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00005779 // FIXME: This should also accept a bitcast of a splat? Be careful, not
5780 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00005781 V1IsSplat = isSplatVector(V1.getNode());
5782 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00005783
Chris Lattner8a594482007-11-25 00:24:49 +00005784 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00005785 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005786 Op = CommuteVectorShuffle(SVOp, DAG);
5787 SVOp = cast<ShuffleVectorSDNode>(Op);
5788 V1 = SVOp->getOperand(0);
5789 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00005790 std::swap(V1IsSplat, V2IsSplat);
5791 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00005792 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00005793 }
5794
Nate Begeman9008ca62009-04-27 18:41:29 +00005795 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
5796 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00005797 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00005798 return V1;
5799 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
5800 // the instruction selector will not match, so get a canonical MOVL with
5801 // swapped operands to undo the commute.
5802 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00005803 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005804
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005805 if (X86::isUNPCKLMask(SVOp))
David Greenec4db4e52011-02-28 19:06:56 +00005806 return getTargetShuffleNode(getUNPCKLOpcode(VT, getSubtarget()),
5807 dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005808
5809 if (X86::isUNPCKHMask(SVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005810 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00005811
Evan Cheng9bbbb982006-10-25 20:48:19 +00005812 if (V2IsSplat) {
5813 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005814 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00005815 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00005816 SDValue NewMask = NormalizeMask(SVOp, DAG);
5817 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
5818 if (NSVOp != SVOp) {
5819 if (X86::isUNPCKLMask(NSVOp, true)) {
5820 return NewMask;
5821 } else if (X86::isUNPCKHMask(NSVOp, true)) {
5822 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005823 }
5824 }
5825 }
5826
Evan Cheng9eca5e82006-10-25 21:49:50 +00005827 if (Commuted) {
5828 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00005829 // FIXME: this seems wrong.
5830 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
5831 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005832
5833 if (X86::isUNPCKLMask(NewSVOp))
David Greenec4db4e52011-02-28 19:06:56 +00005834 return getTargetShuffleNode(getUNPCKLOpcode(VT, getSubtarget()),
5835 dl, VT, V2, V1, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005836
5837 if (X86::isUNPCKHMask(NewSVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005838 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V2, V1, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00005839 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005840
Nate Begeman9008ca62009-04-27 18:41:29 +00005841 // Normalize the node to match x86 shuffle ops if needed
Dale Johannesen0488fb62010-09-30 23:57:10 +00005842 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
Nate Begeman9008ca62009-04-27 18:41:29 +00005843 return CommuteVectorShuffle(SVOp, DAG);
5844
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00005845 // The checks below are all present in isShuffleMaskLegal, but they are
5846 // inlined here right now to enable us to directly emit target specific
5847 // nodes, and remove one by one until they don't return Op anymore.
5848 SmallVector<int, 16> M;
5849 SVOp->getMask(M);
5850
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00005851 if (isPALIGNRMask(M, VT, HasSSSE3))
5852 return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
5853 X86::getShufflePALIGNRImmediate(SVOp),
5854 DAG);
5855
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00005856 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
5857 SVOp->getSplatIndex() == 0 && V2IsUndef) {
David Greenec4db4e52011-02-28 19:06:56 +00005858 if (VT == MVT::v2f64) {
5859 X86ISD::NodeType Opcode =
5860 getSubtarget()->hasAVX() ? X86ISD::VUNPCKLPD : X86ISD::UNPCKLPD;
5861 return getTargetShuffleNode(Opcode, dl, VT, V1, V1, DAG);
5862 }
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00005863 if (VT == MVT::v2i64)
5864 return getTargetShuffleNode(X86ISD::PUNPCKLQDQ, dl, VT, V1, V1, DAG);
5865 }
5866
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00005867 if (isPSHUFHWMask(M, VT))
5868 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
5869 X86::getShufflePSHUFHWImmediate(SVOp),
5870 DAG);
5871
5872 if (isPSHUFLWMask(M, VT))
5873 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
5874 X86::getShufflePSHUFLWImmediate(SVOp),
5875 DAG);
5876
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00005877 if (isSHUFPMask(M, VT)) {
5878 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
5879 if (VT == MVT::v4f32 || VT == MVT::v4i32)
5880 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V1, V2,
5881 TargetMask, DAG);
5882 if (VT == MVT::v2f64 || VT == MVT::v2i64)
5883 return getTargetShuffleNode(X86ISD::SHUFPD, dl, VT, V1, V2,
5884 TargetMask, DAG);
5885 }
5886
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005887 if (X86::isUNPCKL_v_undef_Mask(SVOp))
5888 if (VT != MVT::v2i64 && VT != MVT::v2f64)
David Greenec4db4e52011-02-28 19:06:56 +00005889 return getTargetShuffleNode(getUNPCKLOpcode(VT, getSubtarget()),
5890 dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005891 if (X86::isUNPCKH_v_undef_Mask(SVOp))
5892 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5893 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
5894
Evan Cheng14b32e12007-12-11 01:46:18 +00005895 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
Owen Anderson825b72b2009-08-11 20:47:22 +00005896 if (VT == MVT::v8i16) {
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005897 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00005898 if (NewOp.getNode())
Evan Cheng14b32e12007-12-11 01:46:18 +00005899 return NewOp;
5900 }
5901
Owen Anderson825b72b2009-08-11 20:47:22 +00005902 if (VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005903 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005904 if (NewOp.getNode())
5905 return NewOp;
5906 }
Eric Christopherfd179292009-08-27 18:07:15 +00005907
Dale Johannesen0488fb62010-09-30 23:57:10 +00005908 // Handle all 4 wide cases with a number of shuffles.
5909 if (NumElems == 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00005910 return LowerVECTOR_SHUFFLE_4wide(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005911
Dan Gohman475871a2008-07-27 21:46:04 +00005912 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005913}
5914
Dan Gohman475871a2008-07-27 21:46:04 +00005915SDValue
5916X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00005917 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005918 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005919 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005920 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005921 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005922 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00005923 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005924 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00005925 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005926 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00005927 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
5928 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
5929 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005930 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
5931 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005932 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005933 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00005934 Op.getOperand(0)),
5935 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00005936 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005937 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00005938 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005939 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00005940 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Anderson825b72b2009-08-11 20:47:22 +00005941 } else if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00005942 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
5943 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00005944 // result has a single use which is a store or a bitcast to i32. And in
5945 // the case of a store, it's not worth it if the index is a constant 0,
5946 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00005947 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00005948 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00005949 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00005950 if ((User->getOpcode() != ISD::STORE ||
5951 (isa<ConstantSDNode>(Op.getOperand(1)) &&
5952 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005953 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00005954 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00005955 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00005956 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005957 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00005958 Op.getOperand(0)),
5959 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005960 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Owen Anderson825b72b2009-08-11 20:47:22 +00005961 } else if (VT == MVT::i32) {
Mon P Wangf0fcdd82009-01-15 21:10:20 +00005962 // ExtractPS works with constant index.
5963 if (isa<ConstantSDNode>(Op.getOperand(1)))
5964 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00005965 }
Dan Gohman475871a2008-07-27 21:46:04 +00005966 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005967}
5968
5969
Dan Gohman475871a2008-07-27 21:46:04 +00005970SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005971X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
5972 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005973 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00005974 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005975
David Greene74a579d2011-02-10 16:57:36 +00005976 SDValue Vec = Op.getOperand(0);
5977 EVT VecVT = Vec.getValueType();
5978
5979 // If this is a 256-bit vector result, first extract the 128-bit
5980 // vector and then extract from the 128-bit vector.
5981 if (VecVT.getSizeInBits() > 128) {
5982 DebugLoc dl = Op.getNode()->getDebugLoc();
5983 unsigned NumElems = VecVT.getVectorNumElements();
5984 SDValue Idx = Op.getOperand(1);
5985
5986 if (!isa<ConstantSDNode>(Idx))
5987 return SDValue();
5988
5989 unsigned ExtractNumElems = NumElems / (VecVT.getSizeInBits() / 128);
5990 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
5991
5992 // Get the 128-bit vector.
5993 bool Upper = IdxVal >= ExtractNumElems;
5994 Vec = Extract128BitVector(Vec, Idx, DAG, dl);
5995
5996 // Extract from it.
5997 SDValue ScaledIdx = Idx;
5998 if (Upper)
5999 ScaledIdx = DAG.getNode(ISD::SUB, dl, Idx.getValueType(), Idx,
6000 DAG.getConstant(ExtractNumElems,
6001 Idx.getValueType()));
6002 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, Op.getValueType(), Vec,
6003 ScaledIdx);
6004 }
6005
6006 assert(Vec.getValueSizeInBits() <= 128 && "Unexpected vector length");
6007
Evan Cheng62a3f152008-03-24 21:52:23 +00006008 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006009 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006010 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00006011 return Res;
6012 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00006013
Owen Andersone50ed302009-08-10 22:56:29 +00006014 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006015 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006016 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006017 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00006018 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006019 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00006020 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006021 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6022 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006023 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006024 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00006025 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006026 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00006027 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00006028 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00006029 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00006030 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00006031 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006032 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006033 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006034 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006035 if (Idx == 0)
6036 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00006037
Evan Cheng0db9fe62006-04-25 20:13:52 +00006038 // SHUFPS the element to the lowest double word, then movss.
Nate Begeman9008ca62009-04-27 18:41:29 +00006039 int Mask[4] = { Idx, -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006040 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006041 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00006042 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00006043 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00006044 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00006045 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00006046 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
6047 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
6048 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006049 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006050 if (Idx == 0)
6051 return Op;
6052
6053 // UNPCKHPD the element to the lowest double word, then movsd.
6054 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
6055 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00006056 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006057 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006058 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00006059 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00006060 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00006061 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006062 }
6063
Dan Gohman475871a2008-07-27 21:46:04 +00006064 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006065}
6066
Dan Gohman475871a2008-07-27 21:46:04 +00006067SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006068X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
6069 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006070 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006071 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006072 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006073
Dan Gohman475871a2008-07-27 21:46:04 +00006074 SDValue N0 = Op.getOperand(0);
6075 SDValue N1 = Op.getOperand(1);
6076 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00006077
Dan Gohman8a55ce42009-09-23 21:02:20 +00006078 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00006079 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00006080 unsigned Opc;
6081 if (VT == MVT::v8i16)
6082 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00006083 else if (VT == MVT::v16i8)
6084 Opc = X86ISD::PINSRB;
6085 else
6086 Opc = X86ISD::PINSRB;
6087
Nate Begeman14d12ca2008-02-11 04:19:36 +00006088 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
6089 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00006090 if (N1.getValueType() != MVT::i32)
6091 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
6092 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006093 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00006094 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00006095 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00006096 // Bits [7:6] of the constant are the source select. This will always be
6097 // zero here. The DAG Combiner may combine an extract_elt index into these
6098 // bits. For example (insert (extract, 3), 2) could be matched by putting
6099 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00006100 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00006101 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00006102 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00006103 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006104 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00006105 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00006106 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00006107 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00006108 } else if (EltVT == MVT::i32 && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00006109 // PINSR* works with constant index.
6110 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00006111 }
Dan Gohman475871a2008-07-27 21:46:04 +00006112 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006113}
6114
Dan Gohman475871a2008-07-27 21:46:04 +00006115SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006116X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006117 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006118 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006119
David Greene6b381262011-02-09 15:32:06 +00006120 DebugLoc dl = Op.getDebugLoc();
6121 SDValue N0 = Op.getOperand(0);
6122 SDValue N1 = Op.getOperand(1);
6123 SDValue N2 = Op.getOperand(2);
6124
6125 // If this is a 256-bit vector result, first insert into a 128-bit
6126 // vector and then insert into the 256-bit vector.
6127 if (VT.getSizeInBits() > 128) {
6128 if (!isa<ConstantSDNode>(N2))
6129 return SDValue();
6130
6131 // Get the 128-bit vector.
6132 unsigned NumElems = VT.getVectorNumElements();
6133 unsigned IdxVal = cast<ConstantSDNode>(N2)->getZExtValue();
6134 bool Upper = IdxVal >= NumElems / 2;
6135
6136 SDValue SubN0 = Extract128BitVector(N0, N2, DAG, dl);
6137
6138 // Insert into it.
6139 SDValue ScaledN2 = N2;
6140 if (Upper)
6141 ScaledN2 = DAG.getNode(ISD::SUB, dl, N2.getValueType(), N2,
Owen Anderson95771af2011-02-25 21:41:48 +00006142 DAG.getConstant(NumElems /
David Greene6b381262011-02-09 15:32:06 +00006143 (VT.getSizeInBits() / 128),
6144 N2.getValueType()));
6145 Op = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, SubN0.getValueType(), SubN0,
6146 N1, ScaledN2);
6147
6148 // Insert the 128-bit vector
6149 // FIXME: Why UNDEF?
6150 return Insert128BitVector(N0, Op, N2, DAG, dl);
6151 }
6152
Nate Begeman14d12ca2008-02-11 04:19:36 +00006153 if (Subtarget->hasSSE41())
6154 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
6155
Dan Gohman8a55ce42009-09-23 21:02:20 +00006156 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00006157 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00006158
Dan Gohman8a55ce42009-09-23 21:02:20 +00006159 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00006160 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
6161 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00006162 if (N1.getValueType() != MVT::i32)
6163 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
6164 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006165 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00006166 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006167 }
Dan Gohman475871a2008-07-27 21:46:04 +00006168 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006169}
6170
Dan Gohman475871a2008-07-27 21:46:04 +00006171SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006172X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) const {
David Greene2fcdfb42011-02-10 23:11:29 +00006173 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006174 DebugLoc dl = Op.getDebugLoc();
David Greene2fcdfb42011-02-10 23:11:29 +00006175 EVT OpVT = Op.getValueType();
6176
6177 // If this is a 256-bit vector result, first insert into a 128-bit
6178 // vector and then insert into the 256-bit vector.
6179 if (OpVT.getSizeInBits() > 128) {
6180 // Insert into a 128-bit vector.
6181 EVT VT128 = EVT::getVectorVT(*Context,
6182 OpVT.getVectorElementType(),
6183 OpVT.getVectorNumElements() / 2);
6184
6185 Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT128, Op.getOperand(0));
6186
6187 // Insert the 128-bit vector.
6188 return Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, OpVT), Op,
6189 DAG.getConstant(0, MVT::i32),
6190 DAG, dl);
6191 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00006192
Chris Lattnerf172ecd2010-07-04 23:07:25 +00006193 if (Op.getValueType() == MVT::v1i64 &&
6194 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00006195 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00006196
Owen Anderson825b72b2009-08-11 20:47:22 +00006197 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Dale Johannesen0488fb62010-09-30 23:57:10 +00006198 assert(Op.getValueType().getSimpleVT().getSizeInBits() == 128 &&
6199 "Expected an SSE type!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006200 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(),
Dale Johannesen0488fb62010-09-30 23:57:10 +00006201 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006202}
6203
David Greene91585092011-01-26 15:38:49 +00006204// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
6205// a simple subregister reference or explicit instructions to grab
6206// upper bits of a vector.
6207SDValue
6208X86TargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
6209 if (Subtarget->hasAVX()) {
David Greenea5f26012011-02-07 19:36:54 +00006210 DebugLoc dl = Op.getNode()->getDebugLoc();
6211 SDValue Vec = Op.getNode()->getOperand(0);
6212 SDValue Idx = Op.getNode()->getOperand(1);
6213
6214 if (Op.getNode()->getValueType(0).getSizeInBits() == 128
6215 && Vec.getNode()->getValueType(0).getSizeInBits() == 256) {
6216 return Extract128BitVector(Vec, Idx, DAG, dl);
6217 }
David Greene91585092011-01-26 15:38:49 +00006218 }
6219 return SDValue();
6220}
6221
David Greenecfe33c42011-01-26 19:13:22 +00006222// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
6223// simple superregister reference or explicit instructions to insert
6224// the upper bits of a vector.
6225SDValue
6226X86TargetLowering::LowerINSERT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
6227 if (Subtarget->hasAVX()) {
6228 DebugLoc dl = Op.getNode()->getDebugLoc();
6229 SDValue Vec = Op.getNode()->getOperand(0);
6230 SDValue SubVec = Op.getNode()->getOperand(1);
6231 SDValue Idx = Op.getNode()->getOperand(2);
6232
6233 if (Op.getNode()->getValueType(0).getSizeInBits() == 256
6234 && SubVec.getNode()->getValueType(0).getSizeInBits() == 128) {
David Greenea5f26012011-02-07 19:36:54 +00006235 return Insert128BitVector(Vec, SubVec, Idx, DAG, dl);
David Greenecfe33c42011-01-26 19:13:22 +00006236 }
6237 }
6238 return SDValue();
6239}
6240
Bill Wendling056292f2008-09-16 21:48:12 +00006241// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
6242// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
6243// one of the above mentioned nodes. It has to be wrapped because otherwise
6244// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
6245// be used to form addressing mode. These wrapped nodes will be selected
6246// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00006247SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006248X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006249 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00006250
Chris Lattner41621a22009-06-26 19:22:52 +00006251 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6252 // global base reg.
6253 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006254 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006255 CodeModel::Model M = getTargetMachine().getCodeModel();
6256
Chris Lattner4f066492009-07-11 20:29:19 +00006257 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006258 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00006259 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00006260 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006261 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00006262 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006263 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00006264
Evan Cheng1606e8e2009-03-13 07:51:59 +00006265 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00006266 CP->getAlignment(),
6267 CP->getOffset(), OpFlag);
6268 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00006269 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006270 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00006271 if (OpFlag) {
6272 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00006273 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006274 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006275 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006276 }
6277
6278 return Result;
6279}
6280
Dan Gohmand858e902010-04-17 15:26:15 +00006281SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00006282 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00006283
Chris Lattner18c59872009-06-27 04:16:01 +00006284 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6285 // global base reg.
6286 unsigned char OpFlag = 0;
6287 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006288 CodeModel::Model M = getTargetMachine().getCodeModel();
6289
Chris Lattner4f066492009-07-11 20:29:19 +00006290 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006291 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00006292 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00006293 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006294 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00006295 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006296 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00006297
Chris Lattner18c59872009-06-27 04:16:01 +00006298 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
6299 OpFlag);
6300 DebugLoc DL = JT->getDebugLoc();
6301 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00006302
Chris Lattner18c59872009-06-27 04:16:01 +00006303 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00006304 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00006305 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6306 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006307 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00006308 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00006309
Chris Lattner18c59872009-06-27 04:16:01 +00006310 return Result;
6311}
6312
6313SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006314X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00006315 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00006316
Chris Lattner18c59872009-06-27 04:16:01 +00006317 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6318 // global base reg.
6319 unsigned char OpFlag = 0;
6320 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006321 CodeModel::Model M = getTargetMachine().getCodeModel();
6322
Chris Lattner4f066492009-07-11 20:29:19 +00006323 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006324 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00006325 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00006326 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006327 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00006328 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006329 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00006330
Chris Lattner18c59872009-06-27 04:16:01 +00006331 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00006332
Chris Lattner18c59872009-06-27 04:16:01 +00006333 DebugLoc DL = Op.getDebugLoc();
6334 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00006335
6336
Chris Lattner18c59872009-06-27 04:16:01 +00006337 // With PIC, the address is actually $g + Offset.
6338 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00006339 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00006340 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6341 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006342 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00006343 Result);
6344 }
Eric Christopherfd179292009-08-27 18:07:15 +00006345
Chris Lattner18c59872009-06-27 04:16:01 +00006346 return Result;
6347}
6348
Dan Gohman475871a2008-07-27 21:46:04 +00006349SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006350X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00006351 // Create the TargetBlockAddressAddress node.
6352 unsigned char OpFlags =
6353 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00006354 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00006355 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +00006356 DebugLoc dl = Op.getDebugLoc();
6357 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
6358 /*isTarget=*/true, OpFlags);
6359
Dan Gohmanf705adb2009-10-30 01:28:02 +00006360 if (Subtarget->isPICStyleRIPRel() &&
6361 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00006362 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
6363 else
6364 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00006365
Dan Gohman29cbade2009-11-20 23:18:13 +00006366 // With PIC, the address is actually $g + Offset.
6367 if (isGlobalRelativeToPICBase(OpFlags)) {
6368 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
6369 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
6370 Result);
6371 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00006372
6373 return Result;
6374}
6375
6376SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00006377X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00006378 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00006379 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00006380 // Create the TargetGlobalAddress node, folding in the constant
6381 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00006382 unsigned char OpFlags =
6383 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006384 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00006385 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006386 if (OpFlags == X86II::MO_NO_FLAG &&
6387 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00006388 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00006389 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00006390 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006391 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00006392 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00006393 }
Eric Christopherfd179292009-08-27 18:07:15 +00006394
Chris Lattner4f066492009-07-11 20:29:19 +00006395 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006396 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00006397 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
6398 else
6399 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00006400
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006401 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00006402 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00006403 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
6404 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006405 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006406 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006407
Chris Lattner36c25012009-07-10 07:34:39 +00006408 // For globals that require a load from a stub to get the address, emit the
6409 // load.
6410 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00006411 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00006412 MachinePointerInfo::getGOT(), false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006413
Dan Gohman6520e202008-10-18 02:06:02 +00006414 // If there was a non-zero offset that we didn't fold, create an explicit
6415 // addition for it.
6416 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00006417 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00006418 DAG.getConstant(Offset, getPointerTy()));
6419
Evan Cheng0db9fe62006-04-25 20:13:52 +00006420 return Result;
6421}
6422
Evan Chengda43bcf2008-09-24 00:05:32 +00006423SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006424X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00006425 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00006426 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006427 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00006428}
6429
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006430static SDValue
6431GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00006432 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerb903bed2009-06-26 21:20:29 +00006433 unsigned char OperandFlags) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006434 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006435 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006436 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00006437 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006438 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00006439 GA->getOffset(),
6440 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006441 if (InFlag) {
6442 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00006443 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006444 } else {
6445 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00006446 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006447 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006448
6449 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00006450 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006451
Rafael Espindola15f1b662009-04-24 12:59:40 +00006452 SDValue Flag = Chain.getValue(1);
6453 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006454}
6455
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006456// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00006457static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006458LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006459 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00006460 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00006461 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
6462 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006463 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006464 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006465 InFlag = Chain.getValue(1);
6466
Chris Lattnerb903bed2009-06-26 21:20:29 +00006467 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006468}
6469
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006470// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00006471static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006472LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006473 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00006474 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
6475 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006476}
6477
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006478// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
6479// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00006480static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006481 const EVT PtrVT, TLSModel::Model model,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00006482 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00006483 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00006484
Chris Lattnerf93b90c2010-09-22 04:39:11 +00006485 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
6486 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
6487 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00006488
Michael J. Spencerec38de22010-10-10 22:04:20 +00006489 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00006490 DAG.getIntPtrConstant(0),
6491 MachinePointerInfo(Ptr), false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00006492
Chris Lattnerb903bed2009-06-26 21:20:29 +00006493 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006494 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
6495 // initialexec.
6496 unsigned WrapperKind = X86ISD::Wrapper;
6497 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00006498 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00006499 } else if (is64Bit) {
6500 assert(model == TLSModel::InitialExec);
6501 OperandFlags = X86II::MO_GOTTPOFF;
6502 WrapperKind = X86ISD::WrapperRIP;
6503 } else {
6504 assert(model == TLSModel::InitialExec);
6505 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00006506 }
Eric Christopherfd179292009-08-27 18:07:15 +00006507
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006508 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
6509 // exec)
Michael J. Spencerec38de22010-10-10 22:04:20 +00006510 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00006511 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00006512 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00006513 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00006514
Rafael Espindola9a580232009-02-27 13:37:18 +00006515 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00006516 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00006517 MachinePointerInfo::getGOT(), false, false, 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00006518
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006519 // The address of the thread local variable is the add of the thread
6520 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00006521 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006522}
6523
Dan Gohman475871a2008-07-27 21:46:04 +00006524SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006525X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00006526
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006527 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00006528 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00006529
Eric Christopher30ef0e52010-06-03 04:07:48 +00006530 if (Subtarget->isTargetELF()) {
6531 // TODO: implement the "local dynamic" model
6532 // TODO: implement the "initial exec"model for pic executables
Michael J. Spencerec38de22010-10-10 22:04:20 +00006533
Eric Christopher30ef0e52010-06-03 04:07:48 +00006534 // If GV is an alias then use the aliasee for determining
6535 // thread-localness.
6536 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
6537 GV = GA->resolveAliasedGlobal(false);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006538
6539 TLSModel::Model model
Eric Christopher30ef0e52010-06-03 04:07:48 +00006540 = getTLSModel(GV, getTargetMachine().getRelocationModel());
Michael J. Spencerec38de22010-10-10 22:04:20 +00006541
Eric Christopher30ef0e52010-06-03 04:07:48 +00006542 switch (model) {
6543 case TLSModel::GeneralDynamic:
6544 case TLSModel::LocalDynamic: // not implemented
6545 if (Subtarget->is64Bit())
6546 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
6547 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Michael J. Spencerec38de22010-10-10 22:04:20 +00006548
Eric Christopher30ef0e52010-06-03 04:07:48 +00006549 case TLSModel::InitialExec:
6550 case TLSModel::LocalExec:
6551 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
6552 Subtarget->is64Bit());
6553 }
6554 } else if (Subtarget->isTargetDarwin()) {
6555 // Darwin only has one model of TLS. Lower to that.
6556 unsigned char OpFlag = 0;
6557 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
6558 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006559
Eric Christopher30ef0e52010-06-03 04:07:48 +00006560 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6561 // global base reg.
6562 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
6563 !Subtarget->is64Bit();
6564 if (PIC32)
6565 OpFlag = X86II::MO_TLVP_PIC_BASE;
6566 else
6567 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006568 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00006569 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00006570 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00006571 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00006572 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006573
Eric Christopher30ef0e52010-06-03 04:07:48 +00006574 // With PIC32, the address is actually $g + Offset.
6575 if (PIC32)
6576 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6577 DAG.getNode(X86ISD::GlobalBaseReg,
6578 DebugLoc(), getPointerTy()),
6579 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006580
Eric Christopher30ef0e52010-06-03 04:07:48 +00006581 // Lowering the machine isd will make sure everything is in the right
6582 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00006583 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006584 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00006585 SDValue Args[] = { Chain, Offset };
6586 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006587
Eric Christopher30ef0e52010-06-03 04:07:48 +00006588 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
6589 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
6590 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00006591
Eric Christopher30ef0e52010-06-03 04:07:48 +00006592 // And our return value (tls address) is in the standard call return value
6593 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00006594 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
6595 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy());
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006596 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00006597
Eric Christopher30ef0e52010-06-03 04:07:48 +00006598 assert(false &&
6599 "TLS not implemented for this target.");
Eric Christopherfd179292009-08-27 18:07:15 +00006600
Torok Edwinc23197a2009-07-14 16:55:14 +00006601 llvm_unreachable("Unreachable");
Chris Lattner5867de12009-04-01 22:14:45 +00006602 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006603}
6604
Evan Cheng0db9fe62006-04-25 20:13:52 +00006605
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006606/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00006607/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00006608SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman4c1fa612008-03-03 22:22:09 +00006609 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00006610 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006611 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006612 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006613 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00006614 SDValue ShOpLo = Op.getOperand(0);
6615 SDValue ShOpHi = Op.getOperand(1);
6616 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00006617 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00006618 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00006619 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00006620
Dan Gohman475871a2008-07-27 21:46:04 +00006621 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006622 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00006623 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
6624 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006625 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006626 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
6627 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006628 }
Evan Chenge3413162006-01-09 18:33:28 +00006629
Owen Anderson825b72b2009-08-11 20:47:22 +00006630 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
6631 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00006632 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00006633 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00006634
Dan Gohman475871a2008-07-27 21:46:04 +00006635 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00006636 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00006637 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
6638 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00006639
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006640 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00006641 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
6642 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006643 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006644 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
6645 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006646 }
6647
Dan Gohman475871a2008-07-27 21:46:04 +00006648 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00006649 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006650}
Evan Chenga3195e82006-01-12 22:54:21 +00006651
Dan Gohmand858e902010-04-17 15:26:15 +00006652SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
6653 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006654 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00006655
Dale Johannesen0488fb62010-09-30 23:57:10 +00006656 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00006657 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00006658
Owen Anderson825b72b2009-08-11 20:47:22 +00006659 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00006660 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00006661
Eli Friedman36df4992009-05-27 00:47:34 +00006662 // These are really Legal; return the operand so the caller accepts it as
6663 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00006664 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00006665 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00006666 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00006667 Subtarget->is64Bit()) {
6668 return Op;
6669 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006670
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006671 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006672 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006673 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00006674 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006675 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00006676 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00006677 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006678 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006679 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00006680 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
6681}
Evan Cheng0db9fe62006-04-25 20:13:52 +00006682
Owen Andersone50ed302009-08-10 22:56:29 +00006683SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00006684 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00006685 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006686 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00006687 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00006688 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00006689 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006690 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006691 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00006692 else
Owen Anderson825b72b2009-08-11 20:47:22 +00006693 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006694
Chris Lattner492a43e2010-09-22 01:28:21 +00006695 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006696
Chris Lattner492a43e2010-09-22 01:28:21 +00006697 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
6698 MachineMemOperand *MMO =
6699 DAG.getMachineFunction()
6700 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6701 MachineMemOperand::MOLoad, ByteSize, ByteSize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006702
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006703 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00006704 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
6705 X86ISD::FILD, DL,
6706 Tys, Ops, array_lengthof(Ops),
6707 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006708
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006709 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006710 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00006711 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006712
6713 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
6714 // shouldn't be necessary except that RFP cannot be live across
6715 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006716 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00006717 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
6718 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006719 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00006720 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006721 SDValue Ops[] = {
6722 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
6723 };
Chris Lattner492a43e2010-09-22 01:28:21 +00006724 MachineMemOperand *MMO =
6725 DAG.getMachineFunction()
6726 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00006727 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006728
Chris Lattner492a43e2010-09-22 01:28:21 +00006729 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
6730 Ops, array_lengthof(Ops),
6731 Op.getValueType(), MMO);
6732 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006733 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006734 false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006735 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006736
Evan Cheng0db9fe62006-04-25 20:13:52 +00006737 return Result;
6738}
6739
Bill Wendling8b8a6362009-01-17 03:56:04 +00006740// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00006741SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
6742 SelectionDAG &DAG) const {
Bill Wendling8b8a6362009-01-17 03:56:04 +00006743 // This algorithm is not obvious. Here it is in C code, more or less:
6744 /*
6745 double uint64_to_double( uint32_t hi, uint32_t lo ) {
6746 static const __m128i exp = { 0x4330000045300000ULL, 0 };
6747 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00006748
Bill Wendling8b8a6362009-01-17 03:56:04 +00006749 // Copy ints to xmm registers.
6750 __m128i xh = _mm_cvtsi32_si128( hi );
6751 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00006752
Bill Wendling8b8a6362009-01-17 03:56:04 +00006753 // Combine into low half of a single xmm register.
6754 __m128i x = _mm_unpacklo_epi32( xh, xl );
6755 __m128d d;
6756 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00006757
Bill Wendling8b8a6362009-01-17 03:56:04 +00006758 // Merge in appropriate exponents to give the integer bits the right
6759 // magnitude.
6760 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00006761
Bill Wendling8b8a6362009-01-17 03:56:04 +00006762 // Subtract away the biases to deal with the IEEE-754 double precision
6763 // implicit 1.
6764 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00006765
Bill Wendling8b8a6362009-01-17 03:56:04 +00006766 // All conversions up to here are exact. The correctly rounded result is
6767 // calculated using the current rounding mode using the following
6768 // horizontal add.
6769 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
6770 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
6771 // store doesn't really need to be here (except
6772 // maybe to zero the other double)
6773 return sd;
6774 }
6775 */
Dale Johannesen040225f2008-10-21 23:07:49 +00006776
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006777 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00006778 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00006779
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006780 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00006781 std::vector<Constant*> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00006782 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
6783 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
6784 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
6785 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00006786 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006787 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006788
Bill Wendling8b8a6362009-01-17 03:56:04 +00006789 std::vector<Constant*> CV1;
Owen Andersona90b3dc2009-07-15 21:51:10 +00006790 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006791 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
Owen Andersona90b3dc2009-07-15 21:51:10 +00006792 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006793 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00006794 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006795 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006796
Owen Anderson825b72b2009-08-11 20:47:22 +00006797 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6798 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00006799 Op.getOperand(0),
6800 DAG.getIntPtrConstant(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006801 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6802 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00006803 Op.getOperand(0),
6804 DAG.getIntPtrConstant(0)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006805 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
6806 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00006807 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006808 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00006809 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006810 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck2);
Owen Anderson825b72b2009-08-11 20:47:22 +00006811 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00006812 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006813 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00006814 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006815
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006816 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00006817 int ShufMask[2] = { 1, -1 };
Owen Anderson825b72b2009-08-11 20:47:22 +00006818 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
6819 DAG.getUNDEF(MVT::v2f64), ShufMask);
6820 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
6821 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006822 DAG.getIntPtrConstant(0));
6823}
6824
Bill Wendling8b8a6362009-01-17 03:56:04 +00006825// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00006826SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
6827 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006828 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00006829 // FP constant to bias correct the final result.
6830 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00006831 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006832
6833 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00006834 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6835 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Bill Wendling8b8a6362009-01-17 03:56:04 +00006836 Op.getOperand(0),
6837 DAG.getIntPtrConstant(0)));
6838
Owen Anderson825b72b2009-08-11 20:47:22 +00006839 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006840 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00006841 DAG.getIntPtrConstant(0));
6842
6843 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00006844 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006845 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00006846 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006847 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006848 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00006849 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006850 MVT::v2f64, Bias)));
6851 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006852 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00006853 DAG.getIntPtrConstant(0));
6854
6855 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00006856 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006857
6858 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00006859 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00006860
Owen Anderson825b72b2009-08-11 20:47:22 +00006861 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006862 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00006863 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00006864 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006865 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00006866 }
6867
6868 // Handle final rounding.
6869 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00006870}
6871
Dan Gohmand858e902010-04-17 15:26:15 +00006872SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
6873 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00006874 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006875 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00006876
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006877 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00006878 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
6879 // the optimization here.
6880 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00006881 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00006882
Owen Andersone50ed302009-08-10 22:56:29 +00006883 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006884 EVT DstVT = Op.getValueType();
6885 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00006886 return LowerUINT_TO_FP_i64(Op, DAG);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006887 else if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00006888 return LowerUINT_TO_FP_i32(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00006889
6890 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00006891 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006892 if (SrcVT == MVT::i32) {
6893 SDValue WordOff = DAG.getConstant(4, getPointerTy());
6894 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
6895 getPointerTy(), StackSlot, WordOff);
6896 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006897 StackSlot, MachinePointerInfo(),
6898 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006899 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006900 OffsetSlot, MachinePointerInfo(),
6901 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006902 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
6903 return Fild;
6904 }
6905
6906 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
6907 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006908 StackSlot, MachinePointerInfo(),
6909 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006910 // For i64 source, we need to add the appropriate power of 2 if the input
6911 // was negative. This is the same as the optimization in
6912 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
6913 // we must be careful to do the computation in x87 extended precision, not
6914 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00006915 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
6916 MachineMemOperand *MMO =
6917 DAG.getMachineFunction()
6918 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6919 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006920
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006921 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
6922 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00006923 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
6924 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006925
6926 APInt FF(32, 0x5F800000ULL);
6927
6928 // Check whether the sign bit is set.
6929 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
6930 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
6931 ISD::SETLT);
6932
6933 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
6934 SDValue FudgePtr = DAG.getConstantPool(
6935 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
6936 getPointerTy());
6937
6938 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
6939 SDValue Zero = DAG.getIntPtrConstant(0);
6940 SDValue Four = DAG.getIntPtrConstant(4);
6941 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
6942 Zero, Four);
6943 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
6944
6945 // Load the value out, extending it from f32 to f80.
6946 // FIXME: Avoid the extend by constructing the right constant pool?
Stuart Hastingsa9011292011-02-16 16:23:55 +00006947 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00006948 FudgePtr, MachinePointerInfo::getConstantPool(),
6949 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006950 // Extend everything to 80 bits to force it to be done on x87.
6951 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
6952 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00006953}
6954
Dan Gohman475871a2008-07-27 21:46:04 +00006955std::pair<SDValue,SDValue> X86TargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00006956FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) const {
Chris Lattner07290932010-09-22 01:05:16 +00006957 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00006958
Owen Andersone50ed302009-08-10 22:56:29 +00006959 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00006960
6961 if (!IsSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006962 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
6963 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00006964 }
6965
Owen Anderson825b72b2009-08-11 20:47:22 +00006966 assert(DstTy.getSimpleVT() <= MVT::i64 &&
6967 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00006968 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00006969
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006970 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00006971 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00006972 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00006973 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00006974 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00006975 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00006976 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00006977 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006978
Evan Cheng87c89352007-10-15 20:11:21 +00006979 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
6980 // stack slot.
6981 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00006982 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00006983 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006984 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00006985
Michael J. Spencerec38de22010-10-10 22:04:20 +00006986
6987
Evan Cheng0db9fe62006-04-25 20:13:52 +00006988 unsigned Opc;
Owen Anderson825b72b2009-08-11 20:47:22 +00006989 switch (DstTy.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00006990 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson825b72b2009-08-11 20:47:22 +00006991 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
6992 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
6993 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006994 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006995
Dan Gohman475871a2008-07-27 21:46:04 +00006996 SDValue Chain = DAG.getEntryNode();
6997 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00006998 EVT TheVT = Op.getOperand(0).getValueType();
6999 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007000 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00007001 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007002 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007003 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007004 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00007005 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00007006 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00007007 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00007008
Chris Lattner492a43e2010-09-22 01:28:21 +00007009 MachineMemOperand *MMO =
7010 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7011 MachineMemOperand::MOLoad, MemSize, MemSize);
7012 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
7013 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007014 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00007015 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007016 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
7017 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007018
Chris Lattner07290932010-09-22 01:05:16 +00007019 MachineMemOperand *MMO =
7020 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7021 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007022
Evan Cheng0db9fe62006-04-25 20:13:52 +00007023 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00007024 SDValue Ops[] = { Chain, Value, StackSlot };
Chris Lattner07290932010-09-22 01:05:16 +00007025 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
7026 Ops, 3, DstTy, MMO);
Evan Chengd9558e02006-01-06 00:43:03 +00007027
Chris Lattner27a6c732007-11-24 07:07:01 +00007028 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007029}
7030
Dan Gohmand858e902010-04-17 15:26:15 +00007031SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
7032 SelectionDAG &DAG) const {
Dale Johannesen0488fb62010-09-30 23:57:10 +00007033 if (Op.getValueType().isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007034 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007035
Eli Friedman948e95a2009-05-23 09:59:16 +00007036 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00007037 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00007038 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
7039 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00007040
Chris Lattner27a6c732007-11-24 07:07:01 +00007041 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007042 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00007043 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00007044}
7045
Dan Gohmand858e902010-04-17 15:26:15 +00007046SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
7047 SelectionDAG &DAG) const {
Eli Friedman948e95a2009-05-23 09:59:16 +00007048 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
7049 SDValue FIST = Vals.first, StackSlot = Vals.second;
7050 assert(FIST.getNode() && "Unexpected failure");
7051
7052 // Load the result.
7053 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00007054 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007055}
7056
Dan Gohmand858e902010-04-17 15:26:15 +00007057SDValue X86TargetLowering::LowerFABS(SDValue Op,
7058 SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007059 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007060 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007061 EVT VT = Op.getValueType();
7062 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007063 if (VT.isVector())
7064 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007065 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007066 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007067 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00007068 CV.push_back(C);
7069 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007070 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007071 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00007072 CV.push_back(C);
7073 CV.push_back(C);
7074 CV.push_back(C);
7075 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007076 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007077 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007078 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007079 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007080 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007081 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007082 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007083}
7084
Dan Gohmand858e902010-04-17 15:26:15 +00007085SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007086 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007087 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007088 EVT VT = Op.getValueType();
7089 EVT EltVT = VT;
Duncan Sandsda9ad382009-09-06 19:29:07 +00007090 if (VT.isVector())
Duncan Sands83ec4b62008-06-06 12:08:01 +00007091 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007092 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007093 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007094 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00007095 CV.push_back(C);
7096 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007097 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007098 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00007099 CV.push_back(C);
7100 CV.push_back(C);
7101 CV.push_back(C);
7102 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007103 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007104 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007105 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007106 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007107 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007108 false, false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00007109 if (VT.isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007110 return DAG.getNode(ISD::BITCAST, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007111 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007112 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007113 Op.getOperand(0)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007114 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00007115 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007116 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00007117 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00007118}
7119
Dan Gohmand858e902010-04-17 15:26:15 +00007120SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007121 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00007122 SDValue Op0 = Op.getOperand(0);
7123 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007124 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007125 EVT VT = Op.getValueType();
7126 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00007127
7128 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007129 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007130 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00007131 SrcVT = VT;
7132 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007133 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007134 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007135 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007136 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007137 }
7138
7139 // At this point the operands and the result should have the same
7140 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00007141
Evan Cheng68c47cb2007-01-05 07:55:56 +00007142 // First get the sign bit of second operand.
7143 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007144 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007145 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
7146 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007147 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007148 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
7149 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7150 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7151 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007152 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007153 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007154 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007155 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007156 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007157 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007158 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00007159
7160 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007161 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007162 // Op0 is MVT::f32, Op1 is MVT::f64.
7163 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
7164 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
7165 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007166 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00007167 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00007168 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007169 }
7170
Evan Cheng73d6cf12007-01-05 21:37:56 +00007171 // Clear first operand sign bit.
7172 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00007173 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007174 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
7175 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00007176 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007177 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
7178 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7179 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7180 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00007181 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007182 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007183 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007184 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007185 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007186 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007187 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00007188
7189 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00007190 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00007191}
7192
Dan Gohman076aee32009-03-04 19:44:21 +00007193/// Emit nodes that will be selected as "test Op0,Op0", or something
7194/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00007195SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00007196 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00007197 DebugLoc dl = Op.getDebugLoc();
7198
Dan Gohman31125812009-03-07 01:58:32 +00007199 // CF and OF aren't always set the way we want. Determine which
7200 // of these we need.
7201 bool NeedCF = false;
7202 bool NeedOF = false;
7203 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007204 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00007205 case X86::COND_A: case X86::COND_AE:
7206 case X86::COND_B: case X86::COND_BE:
7207 NeedCF = true;
7208 break;
7209 case X86::COND_G: case X86::COND_GE:
7210 case X86::COND_L: case X86::COND_LE:
7211 case X86::COND_O: case X86::COND_NO:
7212 NeedOF = true;
7213 break;
Dan Gohman31125812009-03-07 01:58:32 +00007214 }
7215
Dan Gohman076aee32009-03-04 19:44:21 +00007216 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00007217 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
7218 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007219 if (Op.getResNo() != 0 || NeedOF || NeedCF)
7220 // Emit a CMP with 0, which is the TEST pattern.
7221 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
7222 DAG.getConstant(0, Op.getValueType()));
7223
7224 unsigned Opcode = 0;
7225 unsigned NumOperands = 0;
7226 switch (Op.getNode()->getOpcode()) {
7227 case ISD::ADD:
7228 // Due to an isel shortcoming, be conservative if this add is likely to be
7229 // selected as part of a load-modify-store instruction. When the root node
7230 // in a match is a store, isel doesn't know how to remap non-chain non-flag
7231 // uses of other nodes in the match, such as the ADD in this case. This
7232 // leads to the ADD being left around and reselected, with the result being
7233 // two adds in the output. Alas, even if none our users are stores, that
7234 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
7235 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
7236 // climbing the DAG back to the root, and it doesn't seem to be worth the
7237 // effort.
7238 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Dan Gohman076aee32009-03-04 19:44:21 +00007239 UE = Op.getNode()->use_end(); UI != UE; ++UI)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007240 if (UI->getOpcode() != ISD::CopyToReg && UI->getOpcode() != ISD::SETCC)
7241 goto default_case;
7242
7243 if (ConstantSDNode *C =
7244 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
7245 // An add of one will be selected as an INC.
7246 if (C->getAPIntValue() == 1) {
7247 Opcode = X86ISD::INC;
7248 NumOperands = 1;
7249 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00007250 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007251
7252 // An add of negative one (subtract of one) will be selected as a DEC.
7253 if (C->getAPIntValue().isAllOnesValue()) {
7254 Opcode = X86ISD::DEC;
7255 NumOperands = 1;
7256 break;
7257 }
Dan Gohman076aee32009-03-04 19:44:21 +00007258 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007259
7260 // Otherwise use a regular EFLAGS-setting add.
7261 Opcode = X86ISD::ADD;
7262 NumOperands = 2;
7263 break;
7264 case ISD::AND: {
7265 // If the primary and result isn't used, don't bother using X86ISD::AND,
7266 // because a TEST instruction will be better.
7267 bool NonFlagUse = false;
7268 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
7269 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
7270 SDNode *User = *UI;
7271 unsigned UOpNo = UI.getOperandNo();
7272 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
7273 // Look pass truncate.
7274 UOpNo = User->use_begin().getOperandNo();
7275 User = *User->use_begin();
7276 }
7277
7278 if (User->getOpcode() != ISD::BRCOND &&
7279 User->getOpcode() != ISD::SETCC &&
7280 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
7281 NonFlagUse = true;
7282 break;
7283 }
Dan Gohman076aee32009-03-04 19:44:21 +00007284 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007285
7286 if (!NonFlagUse)
7287 break;
7288 }
7289 // FALL THROUGH
7290 case ISD::SUB:
7291 case ISD::OR:
7292 case ISD::XOR:
7293 // Due to the ISEL shortcoming noted above, be conservative if this op is
7294 // likely to be selected as part of a load-modify-store instruction.
7295 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
7296 UE = Op.getNode()->use_end(); UI != UE; ++UI)
7297 if (UI->getOpcode() == ISD::STORE)
7298 goto default_case;
7299
7300 // Otherwise use a regular EFLAGS-setting instruction.
7301 switch (Op.getNode()->getOpcode()) {
7302 default: llvm_unreachable("unexpected operator!");
7303 case ISD::SUB: Opcode = X86ISD::SUB; break;
7304 case ISD::OR: Opcode = X86ISD::OR; break;
7305 case ISD::XOR: Opcode = X86ISD::XOR; break;
7306 case ISD::AND: Opcode = X86ISD::AND; break;
7307 }
7308
7309 NumOperands = 2;
7310 break;
7311 case X86ISD::ADD:
7312 case X86ISD::SUB:
7313 case X86ISD::INC:
7314 case X86ISD::DEC:
7315 case X86ISD::OR:
7316 case X86ISD::XOR:
7317 case X86ISD::AND:
7318 return SDValue(Op.getNode(), 1);
7319 default:
7320 default_case:
7321 break;
Dan Gohman076aee32009-03-04 19:44:21 +00007322 }
7323
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007324 if (Opcode == 0)
7325 // Emit a CMP with 0, which is the TEST pattern.
7326 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
7327 DAG.getConstant(0, Op.getValueType()));
7328
7329 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
7330 SmallVector<SDValue, 4> Ops;
7331 for (unsigned i = 0; i != NumOperands; ++i)
7332 Ops.push_back(Op.getOperand(i));
7333
7334 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
7335 DAG.ReplaceAllUsesWith(Op, New);
7336 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00007337}
7338
7339/// Emit nodes that will be selected as "cmp Op0,Op1", or something
7340/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00007341SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00007342 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00007343 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
7344 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00007345 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00007346
7347 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00007348 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00007349}
7350
Evan Chengd40d03e2010-01-06 19:38:29 +00007351/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
7352/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00007353SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
7354 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00007355 SDValue Op0 = And.getOperand(0);
7356 SDValue Op1 = And.getOperand(1);
7357 if (Op0.getOpcode() == ISD::TRUNCATE)
7358 Op0 = Op0.getOperand(0);
7359 if (Op1.getOpcode() == ISD::TRUNCATE)
7360 Op1 = Op1.getOperand(0);
7361
Evan Chengd40d03e2010-01-06 19:38:29 +00007362 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00007363 if (Op1.getOpcode() == ISD::SHL)
7364 std::swap(Op0, Op1);
7365 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00007366 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
7367 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00007368 // If we looked past a truncate, check that it's only truncating away
7369 // known zeros.
7370 unsigned BitWidth = Op0.getValueSizeInBits();
7371 unsigned AndBitWidth = And.getValueSizeInBits();
7372 if (BitWidth > AndBitWidth) {
7373 APInt Mask = APInt::getAllOnesValue(BitWidth), Zeros, Ones;
7374 DAG.ComputeMaskedBits(Op0, Mask, Zeros, Ones);
7375 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
7376 return SDValue();
7377 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007378 LHS = Op1;
7379 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00007380 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007381 } else if (Op1.getOpcode() == ISD::Constant) {
7382 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
7383 SDValue AndLHS = Op0;
Evan Chengd40d03e2010-01-06 19:38:29 +00007384 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
7385 LHS = AndLHS.getOperand(0);
7386 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00007387 }
Evan Chengd40d03e2010-01-06 19:38:29 +00007388 }
Evan Cheng0488db92007-09-25 01:57:46 +00007389
Evan Chengd40d03e2010-01-06 19:38:29 +00007390 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00007391 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00007392 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00007393 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00007394 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00007395 // Also promote i16 to i32 for performance / code size reason.
7396 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00007397 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00007398 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00007399
Evan Chengd40d03e2010-01-06 19:38:29 +00007400 // If the operand types disagree, extend the shift amount to match. Since
7401 // BT ignores high bits (like shifts) we can use anyextend.
7402 if (LHS.getValueType() != RHS.getValueType())
7403 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00007404
Evan Chengd40d03e2010-01-06 19:38:29 +00007405 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
7406 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
7407 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7408 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00007409 }
7410
Evan Cheng54de3ea2010-01-05 06:52:31 +00007411 return SDValue();
7412}
7413
Dan Gohmand858e902010-04-17 15:26:15 +00007414SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng54de3ea2010-01-05 06:52:31 +00007415 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
7416 SDValue Op0 = Op.getOperand(0);
7417 SDValue Op1 = Op.getOperand(1);
7418 DebugLoc dl = Op.getDebugLoc();
7419 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
7420
7421 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00007422 // Lower (X & (1 << N)) == 0 to BT(X, N).
7423 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
7424 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Chris Lattner481eebc2010-12-19 21:23:48 +00007425 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00007426 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00007427 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00007428 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
7429 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
7430 if (NewSetCC.getNode())
7431 return NewSetCC;
7432 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00007433
Chris Lattner481eebc2010-12-19 21:23:48 +00007434 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
7435 // these.
7436 if (Op1.getOpcode() == ISD::Constant &&
Evan Cheng2c755ba2010-02-27 07:36:59 +00007437 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
7438 cast<ConstantSDNode>(Op1)->isNullValue()) &&
7439 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007440
Chris Lattner481eebc2010-12-19 21:23:48 +00007441 // If the input is a setcc, then reuse the input setcc or use a new one with
7442 // the inverted condition.
7443 if (Op0.getOpcode() == X86ISD::SETCC) {
7444 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
7445 bool Invert = (CC == ISD::SETNE) ^
7446 cast<ConstantSDNode>(Op1)->isNullValue();
7447 if (!Invert) return Op0;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007448
Evan Cheng2c755ba2010-02-27 07:36:59 +00007449 CCode = X86::GetOppositeBranchCondition(CCode);
Chris Lattner481eebc2010-12-19 21:23:48 +00007450 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7451 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
7452 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007453 }
7454
Evan Chenge5b51ac2010-04-17 06:13:15 +00007455 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00007456 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00007457 if (X86CC == X86::COND_INVALID)
7458 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00007459
Chris Lattnerc19d1c32010-12-19 22:08:31 +00007460 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00007461 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnerc19d1c32010-12-19 22:08:31 +00007462 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
Evan Cheng0488db92007-09-25 01:57:46 +00007463}
7464
Dan Gohmand858e902010-04-17 15:26:15 +00007465SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00007466 SDValue Cond;
7467 SDValue Op0 = Op.getOperand(0);
7468 SDValue Op1 = Op.getOperand(1);
7469 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00007470 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00007471 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
7472 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007473 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00007474
7475 if (isFP) {
7476 unsigned SSECC = 8;
Owen Andersone50ed302009-08-10 22:56:29 +00007477 EVT VT0 = Op0.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00007478 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
7479 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00007480 bool Swap = false;
7481
7482 switch (SetCCOpcode) {
7483 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00007484 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00007485 case ISD::SETEQ: SSECC = 0; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00007486 case ISD::SETOGT:
Nate Begeman30a0de92008-07-17 16:51:19 +00007487 case ISD::SETGT: Swap = true; // Fallthrough
7488 case ISD::SETLT:
7489 case ISD::SETOLT: SSECC = 1; break;
7490 case ISD::SETOGE:
7491 case ISD::SETGE: Swap = true; // Fallthrough
7492 case ISD::SETLE:
7493 case ISD::SETOLE: SSECC = 2; break;
7494 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00007495 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00007496 case ISD::SETNE: SSECC = 4; break;
7497 case ISD::SETULE: Swap = true;
7498 case ISD::SETUGE: SSECC = 5; break;
7499 case ISD::SETULT: Swap = true;
7500 case ISD::SETUGT: SSECC = 6; break;
7501 case ISD::SETO: SSECC = 7; break;
7502 }
7503 if (Swap)
7504 std::swap(Op0, Op1);
7505
Nate Begemanfb8ead02008-07-25 19:05:58 +00007506 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00007507 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00007508 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00007509 SDValue UNORD, EQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00007510 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
7511 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00007512 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00007513 }
7514 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00007515 SDValue ORD, NEQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00007516 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
7517 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00007518 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00007519 }
Torok Edwinc23197a2009-07-14 16:55:14 +00007520 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00007521 }
7522 // Handle all other FP comparisons here.
Owen Anderson825b72b2009-08-11 20:47:22 +00007523 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00007524 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007525
Nate Begeman30a0de92008-07-17 16:51:19 +00007526 // We are handling one of the integer comparisons here. Since SSE only has
7527 // GT and EQ comparisons for integer, swapping operands and multiple
7528 // operations may be required for some comparisons.
7529 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
7530 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007531
Owen Anderson825b72b2009-08-11 20:47:22 +00007532 switch (VT.getSimpleVT().SimpleTy) {
Nate Begeman30a0de92008-07-17 16:51:19 +00007533 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007534 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007535 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007536 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
7537 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00007538 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007539
Nate Begeman30a0de92008-07-17 16:51:19 +00007540 switch (SetCCOpcode) {
7541 default: break;
7542 case ISD::SETNE: Invert = true;
7543 case ISD::SETEQ: Opc = EQOpc; break;
7544 case ISD::SETLT: Swap = true;
7545 case ISD::SETGT: Opc = GTOpc; break;
7546 case ISD::SETGE: Swap = true;
7547 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
7548 case ISD::SETULT: Swap = true;
7549 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
7550 case ISD::SETUGE: Swap = true;
7551 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
7552 }
7553 if (Swap)
7554 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007555
Nate Begeman30a0de92008-07-17 16:51:19 +00007556 // Since SSE has no unsigned integer comparisons, we need to flip the sign
7557 // bits of the inputs before performing those operations.
7558 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00007559 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00007560 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
7561 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00007562 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00007563 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
7564 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00007565 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
7566 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00007567 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007568
Dale Johannesenace16102009-02-03 19:33:06 +00007569 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00007570
7571 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00007572 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00007573 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00007574
Nate Begeman30a0de92008-07-17 16:51:19 +00007575 return Result;
7576}
Evan Cheng0488db92007-09-25 01:57:46 +00007577
Evan Cheng370e5342008-12-03 08:38:43 +00007578// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00007579static bool isX86LogicalCmp(SDValue Op) {
7580 unsigned Opc = Op.getNode()->getOpcode();
7581 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
7582 return true;
7583 if (Op.getResNo() == 1 &&
7584 (Opc == X86ISD::ADD ||
7585 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00007586 Opc == X86ISD::ADC ||
7587 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00007588 Opc == X86ISD::SMUL ||
7589 Opc == X86ISD::UMUL ||
7590 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00007591 Opc == X86ISD::DEC ||
7592 Opc == X86ISD::OR ||
7593 Opc == X86ISD::XOR ||
7594 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00007595 return true;
7596
Chris Lattner9637d5b2010-12-05 07:49:54 +00007597 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
7598 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007599
Dan Gohman076aee32009-03-04 19:44:21 +00007600 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00007601}
7602
Chris Lattnera2b56002010-12-05 01:23:24 +00007603static bool isZero(SDValue V) {
7604 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
7605 return C && C->isNullValue();
7606}
7607
Chris Lattner96908b12010-12-05 02:00:51 +00007608static bool isAllOnes(SDValue V) {
7609 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
7610 return C && C->isAllOnesValue();
7611}
7612
Dan Gohmand858e902010-04-17 15:26:15 +00007613SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00007614 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007615 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00007616 SDValue Op1 = Op.getOperand(1);
7617 SDValue Op2 = Op.getOperand(2);
7618 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00007619 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00007620
Dan Gohman1a492952009-10-20 16:22:37 +00007621 if (Cond.getOpcode() == ISD::SETCC) {
7622 SDValue NewCond = LowerSETCC(Cond, DAG);
7623 if (NewCond.getNode())
7624 Cond = NewCond;
7625 }
Evan Cheng734503b2006-09-11 02:19:56 +00007626
Chris Lattnera2b56002010-12-05 01:23:24 +00007627 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00007628 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00007629 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00007630 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007631 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00007632 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
7633 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007634 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007635
Chris Lattnera2b56002010-12-05 01:23:24 +00007636 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007637
7638 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00007639 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
7640 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00007641
7642 SDValue CmpOp0 = Cmp.getOperand(0);
7643 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
7644 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007645
Chris Lattner96908b12010-12-05 02:00:51 +00007646 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00007647 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
7648 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007649
Chris Lattner96908b12010-12-05 02:00:51 +00007650 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
7651 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007652
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007653 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00007654 if (N2C == 0 || !N2C->isNullValue())
7655 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
7656 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007657 }
7658 }
7659
Chris Lattnera2b56002010-12-05 01:23:24 +00007660 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00007661 if (Cond.getOpcode() == ISD::AND &&
7662 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
7663 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00007664 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00007665 Cond = Cond.getOperand(0);
7666 }
7667
Evan Cheng3f41d662007-10-08 22:16:29 +00007668 // If condition flag is set by a X86ISD::CMP, then use it as the condition
7669 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00007670 if (Cond.getOpcode() == X86ISD::SETCC ||
7671 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00007672 CC = Cond.getOperand(0);
7673
Dan Gohman475871a2008-07-27 21:46:04 +00007674 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00007675 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00007676 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00007677
Evan Cheng3f41d662007-10-08 22:16:29 +00007678 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007679 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00007680 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00007681 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00007682
Chris Lattnerd1980a52009-03-12 06:52:53 +00007683 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
7684 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00007685 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00007686 addTest = false;
7687 }
7688 }
7689
7690 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007691 // Look pass the truncate.
7692 if (Cond.getOpcode() == ISD::TRUNCATE)
7693 Cond = Cond.getOperand(0);
7694
7695 // We know the result of AND is compared against zero. Try to match
7696 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00007697 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00007698 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00007699 if (NewSetCC.getNode()) {
7700 CC = NewSetCC.getOperand(0);
7701 Cond = NewSetCC.getOperand(1);
7702 addTest = false;
7703 }
7704 }
7705 }
7706
7707 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007708 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00007709 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00007710 }
7711
Benjamin Kramere915ff32010-12-22 23:09:28 +00007712 // a < b ? -1 : 0 -> RES = ~setcc_carry
7713 // a < b ? 0 : -1 -> RES = setcc_carry
7714 // a >= b ? -1 : 0 -> RES = setcc_carry
7715 // a >= b ? 0 : -1 -> RES = ~setcc_carry
7716 if (Cond.getOpcode() == X86ISD::CMP) {
7717 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
7718
7719 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
7720 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
7721 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
7722 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
7723 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
7724 return DAG.getNOT(DL, Res, Res.getValueType());
7725 return Res;
7726 }
7727 }
7728
Evan Cheng0488db92007-09-25 01:57:46 +00007729 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
7730 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007731 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007732 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00007733 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00007734}
7735
Evan Cheng370e5342008-12-03 08:38:43 +00007736// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
7737// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
7738// from the AND / OR.
7739static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
7740 Opc = Op.getOpcode();
7741 if (Opc != ISD::OR && Opc != ISD::AND)
7742 return false;
7743 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
7744 Op.getOperand(0).hasOneUse() &&
7745 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
7746 Op.getOperand(1).hasOneUse());
7747}
7748
Evan Cheng961d6d42009-02-02 08:19:07 +00007749// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
7750// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00007751static bool isXor1OfSetCC(SDValue Op) {
7752 if (Op.getOpcode() != ISD::XOR)
7753 return false;
7754 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
7755 if (N1C && N1C->getAPIntValue() == 1) {
7756 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
7757 Op.getOperand(0).hasOneUse();
7758 }
7759 return false;
7760}
7761
Dan Gohmand858e902010-04-17 15:26:15 +00007762SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00007763 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007764 SDValue Chain = Op.getOperand(0);
7765 SDValue Cond = Op.getOperand(1);
7766 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007767 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00007768 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00007769
Dan Gohman1a492952009-10-20 16:22:37 +00007770 if (Cond.getOpcode() == ISD::SETCC) {
7771 SDValue NewCond = LowerSETCC(Cond, DAG);
7772 if (NewCond.getNode())
7773 Cond = NewCond;
7774 }
Chris Lattnere55484e2008-12-25 05:34:37 +00007775#if 0
7776 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00007777 else if (Cond.getOpcode() == X86ISD::ADD ||
7778 Cond.getOpcode() == X86ISD::SUB ||
7779 Cond.getOpcode() == X86ISD::SMUL ||
7780 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00007781 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00007782#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00007783
Evan Chengad9c0a32009-12-15 00:53:42 +00007784 // Look pass (and (setcc_carry (cmp ...)), 1).
7785 if (Cond.getOpcode() == ISD::AND &&
7786 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
7787 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00007788 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00007789 Cond = Cond.getOperand(0);
7790 }
7791
Evan Cheng3f41d662007-10-08 22:16:29 +00007792 // If condition flag is set by a X86ISD::CMP, then use it as the condition
7793 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00007794 if (Cond.getOpcode() == X86ISD::SETCC ||
7795 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00007796 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007797
Dan Gohman475871a2008-07-27 21:46:04 +00007798 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00007799 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00007800 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00007801 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00007802 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00007803 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00007804 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00007805 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00007806 default: break;
7807 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00007808 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00007809 // These can only come from an arithmetic instruction with overflow,
7810 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00007811 Cond = Cond.getNode()->getOperand(1);
7812 addTest = false;
7813 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00007814 }
Evan Cheng0488db92007-09-25 01:57:46 +00007815 }
Evan Cheng370e5342008-12-03 08:38:43 +00007816 } else {
7817 unsigned CondOpc;
7818 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
7819 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00007820 if (CondOpc == ISD::OR) {
7821 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
7822 // two branches instead of an explicit OR instruction with a
7823 // separate test.
7824 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00007825 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00007826 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007827 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00007828 Chain, Dest, CC, Cmp);
7829 CC = Cond.getOperand(1).getOperand(0);
7830 Cond = Cmp;
7831 addTest = false;
7832 }
7833 } else { // ISD::AND
7834 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
7835 // two branches instead of an explicit AND instruction with a
7836 // separate test. However, we only do this if this block doesn't
7837 // have a fall-through edge, because this requires an explicit
7838 // jmp when the condition is false.
7839 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00007840 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00007841 Op.getNode()->hasOneUse()) {
7842 X86::CondCode CCode =
7843 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
7844 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007845 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00007846 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00007847 // Look for an unconditional branch following this conditional branch.
7848 // We need this because we need to reverse the successors in order
7849 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00007850 if (User->getOpcode() == ISD::BR) {
7851 SDValue FalseBB = User->getOperand(1);
7852 SDNode *NewBR =
7853 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00007854 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00007855 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00007856 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00007857
Dale Johannesene4d209d2009-02-03 20:21:25 +00007858 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00007859 Chain, Dest, CC, Cmp);
7860 X86::CondCode CCode =
7861 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
7862 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007863 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00007864 Cond = Cmp;
7865 addTest = false;
7866 }
7867 }
Dan Gohman279c22e2008-10-21 03:29:32 +00007868 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00007869 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
7870 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
7871 // It should be transformed during dag combiner except when the condition
7872 // is set by a arithmetics with overflow node.
7873 X86::CondCode CCode =
7874 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
7875 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007876 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00007877 Cond = Cond.getOperand(0).getOperand(1);
7878 addTest = false;
Dan Gohman279c22e2008-10-21 03:29:32 +00007879 }
Evan Cheng0488db92007-09-25 01:57:46 +00007880 }
7881
7882 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007883 // Look pass the truncate.
7884 if (Cond.getOpcode() == ISD::TRUNCATE)
7885 Cond = Cond.getOperand(0);
7886
7887 // We know the result of AND is compared against zero. Try to match
7888 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00007889 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007890 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
7891 if (NewSetCC.getNode()) {
7892 CC = NewSetCC.getOperand(0);
7893 Cond = NewSetCC.getOperand(1);
7894 addTest = false;
7895 }
7896 }
7897 }
7898
7899 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007900 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00007901 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00007902 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00007903 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00007904 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00007905}
7906
Anton Korobeynikove060b532007-04-17 19:34:00 +00007907
7908// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
7909// Calls to _alloca is needed to probe the stack when allocating more than 4k
7910// bytes in one go. Touching the stack at 4K increments is necessary to ensure
7911// that the guard pages used by the OS virtual memory manager are allocated in
7912// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00007913SDValue
7914X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00007915 SelectionDAG &DAG) const {
Duncan Sands1e1ca0b2010-10-21 16:02:12 +00007916 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows()) &&
Michael J. Spencere9c253e2010-10-21 01:41:01 +00007917 "This should be used only on Windows targets");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007918 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007919
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007920 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00007921 SDValue Chain = Op.getOperand(0);
7922 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007923 // FIXME: Ensure alignment here
7924
Dan Gohman475871a2008-07-27 21:46:04 +00007925 SDValue Flag;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007926
Owen Anderson825b72b2009-08-11 20:47:22 +00007927 EVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007928
Dale Johannesendd64c412009-02-04 00:33:20 +00007929 Chain = DAG.getCopyToReg(Chain, dl, X86::EAX, Size, Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00007930 Flag = Chain.getValue(1);
7931
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007932 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00007933
Michael J. Spencere9c253e2010-10-21 01:41:01 +00007934 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00007935 Flag = Chain.getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007936
Dale Johannesendd64c412009-02-04 00:33:20 +00007937 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007938
Dan Gohman475871a2008-07-27 21:46:04 +00007939 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007940 return DAG.getMergeValues(Ops1, 2, dl);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007941}
7942
Dan Gohmand858e902010-04-17 15:26:15 +00007943SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00007944 MachineFunction &MF = DAG.getMachineFunction();
7945 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
7946
Dan Gohman69de1932008-02-06 22:27:42 +00007947 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00007948 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00007949
Anton Korobeynikove7beda12010-10-03 22:52:07 +00007950 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00007951 // vastart just stores the address of the VarArgsFrameIndex slot into the
7952 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00007953 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
7954 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00007955 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
7956 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007957 }
7958
7959 // __va_list_tag:
7960 // gp_offset (0 - 6 * 8)
7961 // fp_offset (48 - 48 + 8 * 16)
7962 // overflow_arg_area (point to parameters coming in memory).
7963 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00007964 SmallVector<SDValue, 8> MemOps;
7965 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00007966 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00007967 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00007968 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
7969 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007970 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007971 MemOps.push_back(Store);
7972
7973 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00007974 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007975 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +00007976 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00007977 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
7978 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007979 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007980 MemOps.push_back(Store);
7981
7982 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +00007983 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007984 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00007985 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
7986 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00007987 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
7988 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +00007989 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007990 MemOps.push_back(Store);
7991
7992 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +00007993 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007994 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00007995 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
7996 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00007997 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
7998 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007999 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +00008000 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00008001 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00008002}
8003
Dan Gohmand858e902010-04-17 15:26:15 +00008004SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +00008005 assert(Subtarget->is64Bit() &&
8006 "LowerVAARG only handles 64-bit va_arg!");
8007 assert((Subtarget->isTargetLinux() ||
8008 Subtarget->isTargetDarwin()) &&
8009 "Unhandled target in LowerVAARG");
8010 assert(Op.getNode()->getNumOperands() == 4);
8011 SDValue Chain = Op.getOperand(0);
8012 SDValue SrcPtr = Op.getOperand(1);
8013 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
8014 unsigned Align = Op.getConstantOperandVal(3);
8015 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +00008016
Dan Gohman320afb82010-10-12 18:00:49 +00008017 EVT ArgVT = Op.getNode()->getValueType(0);
8018 const Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
8019 uint32_t ArgSize = getTargetData()->getTypeAllocSize(ArgTy);
8020 uint8_t ArgMode;
8021
8022 // Decide which area this value should be read from.
8023 // TODO: Implement the AMD64 ABI in its entirety. This simple
8024 // selection mechanism works only for the basic types.
8025 if (ArgVT == MVT::f80) {
8026 llvm_unreachable("va_arg for f80 not yet implemented");
8027 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
8028 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
8029 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
8030 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
8031 } else {
8032 llvm_unreachable("Unhandled argument type in LowerVAARG");
8033 }
8034
8035 if (ArgMode == 2) {
8036 // Sanity Check: Make sure using fp_offset makes sense.
Michael J. Spencer87b86652010-10-19 07:32:42 +00008037 assert(!UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +00008038 !(DAG.getMachineFunction()
8039 .getFunction()->hasFnAttr(Attribute::NoImplicitFloat)) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00008040 Subtarget->hasXMM());
Dan Gohman320afb82010-10-12 18:00:49 +00008041 }
8042
8043 // Insert VAARG_64 node into the DAG
8044 // VAARG_64 returns two values: Variable Argument Address, Chain
8045 SmallVector<SDValue, 11> InstOps;
8046 InstOps.push_back(Chain);
8047 InstOps.push_back(SrcPtr);
8048 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
8049 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
8050 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
8051 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
8052 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
8053 VTs, &InstOps[0], InstOps.size(),
8054 MVT::i64,
8055 MachinePointerInfo(SV),
8056 /*Align=*/0,
8057 /*Volatile=*/false,
8058 /*ReadMem=*/true,
8059 /*WriteMem=*/true);
8060 Chain = VAARG.getValue(1);
8061
8062 // Load the next argument and return it
8063 return DAG.getLoad(ArgVT, dl,
8064 Chain,
8065 VAARG,
8066 MachinePointerInfo(),
8067 false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +00008068}
8069
Dan Gohmand858e902010-04-17 15:26:15 +00008070SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) const {
Evan Chengae642192007-03-02 23:16:35 +00008071 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00008072 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00008073 SDValue Chain = Op.getOperand(0);
8074 SDValue DstPtr = Op.getOperand(1);
8075 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00008076 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
8077 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +00008078 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00008079
Chris Lattnere72f2022010-09-21 05:40:29 +00008080 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00008081 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +00008082 false,
Chris Lattnere72f2022010-09-21 05:40:29 +00008083 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +00008084}
8085
Dan Gohman475871a2008-07-27 21:46:04 +00008086SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00008087X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008088 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00008089 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00008090 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00008091 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00008092 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00008093 case Intrinsic::x86_sse_comieq_ss:
8094 case Intrinsic::x86_sse_comilt_ss:
8095 case Intrinsic::x86_sse_comile_ss:
8096 case Intrinsic::x86_sse_comigt_ss:
8097 case Intrinsic::x86_sse_comige_ss:
8098 case Intrinsic::x86_sse_comineq_ss:
8099 case Intrinsic::x86_sse_ucomieq_ss:
8100 case Intrinsic::x86_sse_ucomilt_ss:
8101 case Intrinsic::x86_sse_ucomile_ss:
8102 case Intrinsic::x86_sse_ucomigt_ss:
8103 case Intrinsic::x86_sse_ucomige_ss:
8104 case Intrinsic::x86_sse_ucomineq_ss:
8105 case Intrinsic::x86_sse2_comieq_sd:
8106 case Intrinsic::x86_sse2_comilt_sd:
8107 case Intrinsic::x86_sse2_comile_sd:
8108 case Intrinsic::x86_sse2_comigt_sd:
8109 case Intrinsic::x86_sse2_comige_sd:
8110 case Intrinsic::x86_sse2_comineq_sd:
8111 case Intrinsic::x86_sse2_ucomieq_sd:
8112 case Intrinsic::x86_sse2_ucomilt_sd:
8113 case Intrinsic::x86_sse2_ucomile_sd:
8114 case Intrinsic::x86_sse2_ucomigt_sd:
8115 case Intrinsic::x86_sse2_ucomige_sd:
8116 case Intrinsic::x86_sse2_ucomineq_sd: {
8117 unsigned Opc = 0;
8118 ISD::CondCode CC = ISD::SETCC_INVALID;
8119 switch (IntNo) {
8120 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008121 case Intrinsic::x86_sse_comieq_ss:
8122 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008123 Opc = X86ISD::COMI;
8124 CC = ISD::SETEQ;
8125 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00008126 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008127 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008128 Opc = X86ISD::COMI;
8129 CC = ISD::SETLT;
8130 break;
8131 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008132 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008133 Opc = X86ISD::COMI;
8134 CC = ISD::SETLE;
8135 break;
8136 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008137 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008138 Opc = X86ISD::COMI;
8139 CC = ISD::SETGT;
8140 break;
8141 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008142 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008143 Opc = X86ISD::COMI;
8144 CC = ISD::SETGE;
8145 break;
8146 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008147 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008148 Opc = X86ISD::COMI;
8149 CC = ISD::SETNE;
8150 break;
8151 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008152 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008153 Opc = X86ISD::UCOMI;
8154 CC = ISD::SETEQ;
8155 break;
8156 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008157 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008158 Opc = X86ISD::UCOMI;
8159 CC = ISD::SETLT;
8160 break;
8161 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008162 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008163 Opc = X86ISD::UCOMI;
8164 CC = ISD::SETLE;
8165 break;
8166 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008167 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008168 Opc = X86ISD::UCOMI;
8169 CC = ISD::SETGT;
8170 break;
8171 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008172 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008173 Opc = X86ISD::UCOMI;
8174 CC = ISD::SETGE;
8175 break;
8176 case Intrinsic::x86_sse_ucomineq_ss:
8177 case Intrinsic::x86_sse2_ucomineq_sd:
8178 Opc = X86ISD::UCOMI;
8179 CC = ISD::SETNE;
8180 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00008181 }
Evan Cheng734503b2006-09-11 02:19:56 +00008182
Dan Gohman475871a2008-07-27 21:46:04 +00008183 SDValue LHS = Op.getOperand(1);
8184 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00008185 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00008186 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00008187 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
8188 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8189 DAG.getConstant(X86CC, MVT::i8), Cond);
8190 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00008191 }
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008192 // ptest and testp intrinsics. The intrinsic these come from are designed to
8193 // return an integer value, not just an instruction so lower it to the ptest
8194 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00008195 case Intrinsic::x86_sse41_ptestz:
8196 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008197 case Intrinsic::x86_sse41_ptestnzc:
8198 case Intrinsic::x86_avx_ptestz_256:
8199 case Intrinsic::x86_avx_ptestc_256:
8200 case Intrinsic::x86_avx_ptestnzc_256:
8201 case Intrinsic::x86_avx_vtestz_ps:
8202 case Intrinsic::x86_avx_vtestc_ps:
8203 case Intrinsic::x86_avx_vtestnzc_ps:
8204 case Intrinsic::x86_avx_vtestz_pd:
8205 case Intrinsic::x86_avx_vtestc_pd:
8206 case Intrinsic::x86_avx_vtestnzc_pd:
8207 case Intrinsic::x86_avx_vtestz_ps_256:
8208 case Intrinsic::x86_avx_vtestc_ps_256:
8209 case Intrinsic::x86_avx_vtestnzc_ps_256:
8210 case Intrinsic::x86_avx_vtestz_pd_256:
8211 case Intrinsic::x86_avx_vtestc_pd_256:
8212 case Intrinsic::x86_avx_vtestnzc_pd_256: {
8213 bool IsTestPacked = false;
Eric Christopher71c67532009-07-29 00:28:05 +00008214 unsigned X86CC = 0;
8215 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00008216 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008217 case Intrinsic::x86_avx_vtestz_ps:
8218 case Intrinsic::x86_avx_vtestz_pd:
8219 case Intrinsic::x86_avx_vtestz_ps_256:
8220 case Intrinsic::x86_avx_vtestz_pd_256:
8221 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00008222 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008223 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +00008224 // ZF = 1
8225 X86CC = X86::COND_E;
8226 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008227 case Intrinsic::x86_avx_vtestc_ps:
8228 case Intrinsic::x86_avx_vtestc_pd:
8229 case Intrinsic::x86_avx_vtestc_ps_256:
8230 case Intrinsic::x86_avx_vtestc_pd_256:
8231 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00008232 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008233 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00008234 // CF = 1
8235 X86CC = X86::COND_B;
8236 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008237 case Intrinsic::x86_avx_vtestnzc_ps:
8238 case Intrinsic::x86_avx_vtestnzc_pd:
8239 case Intrinsic::x86_avx_vtestnzc_ps_256:
8240 case Intrinsic::x86_avx_vtestnzc_pd_256:
8241 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +00008242 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008243 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00008244 // ZF and CF = 0
8245 X86CC = X86::COND_A;
8246 break;
8247 }
Eric Christopherfd179292009-08-27 18:07:15 +00008248
Eric Christopher71c67532009-07-29 00:28:05 +00008249 SDValue LHS = Op.getOperand(1);
8250 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008251 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
8252 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00008253 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
8254 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
8255 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00008256 }
Evan Cheng5759f972008-05-04 09:15:50 +00008257
8258 // Fix vector shift instructions where the last operand is a non-immediate
8259 // i32 value.
8260 case Intrinsic::x86_sse2_pslli_w:
8261 case Intrinsic::x86_sse2_pslli_d:
8262 case Intrinsic::x86_sse2_pslli_q:
8263 case Intrinsic::x86_sse2_psrli_w:
8264 case Intrinsic::x86_sse2_psrli_d:
8265 case Intrinsic::x86_sse2_psrli_q:
8266 case Intrinsic::x86_sse2_psrai_w:
8267 case Intrinsic::x86_sse2_psrai_d:
8268 case Intrinsic::x86_mmx_pslli_w:
8269 case Intrinsic::x86_mmx_pslli_d:
8270 case Intrinsic::x86_mmx_pslli_q:
8271 case Intrinsic::x86_mmx_psrli_w:
8272 case Intrinsic::x86_mmx_psrli_d:
8273 case Intrinsic::x86_mmx_psrli_q:
8274 case Intrinsic::x86_mmx_psrai_w:
8275 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00008276 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00008277 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00008278 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00008279
8280 unsigned NewIntNo = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00008281 EVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00008282 switch (IntNo) {
8283 case Intrinsic::x86_sse2_pslli_w:
8284 NewIntNo = Intrinsic::x86_sse2_psll_w;
8285 break;
8286 case Intrinsic::x86_sse2_pslli_d:
8287 NewIntNo = Intrinsic::x86_sse2_psll_d;
8288 break;
8289 case Intrinsic::x86_sse2_pslli_q:
8290 NewIntNo = Intrinsic::x86_sse2_psll_q;
8291 break;
8292 case Intrinsic::x86_sse2_psrli_w:
8293 NewIntNo = Intrinsic::x86_sse2_psrl_w;
8294 break;
8295 case Intrinsic::x86_sse2_psrli_d:
8296 NewIntNo = Intrinsic::x86_sse2_psrl_d;
8297 break;
8298 case Intrinsic::x86_sse2_psrli_q:
8299 NewIntNo = Intrinsic::x86_sse2_psrl_q;
8300 break;
8301 case Intrinsic::x86_sse2_psrai_w:
8302 NewIntNo = Intrinsic::x86_sse2_psra_w;
8303 break;
8304 case Intrinsic::x86_sse2_psrai_d:
8305 NewIntNo = Intrinsic::x86_sse2_psra_d;
8306 break;
8307 default: {
Owen Anderson825b72b2009-08-11 20:47:22 +00008308 ShAmtVT = MVT::v2i32;
Evan Cheng5759f972008-05-04 09:15:50 +00008309 switch (IntNo) {
8310 case Intrinsic::x86_mmx_pslli_w:
8311 NewIntNo = Intrinsic::x86_mmx_psll_w;
8312 break;
8313 case Intrinsic::x86_mmx_pslli_d:
8314 NewIntNo = Intrinsic::x86_mmx_psll_d;
8315 break;
8316 case Intrinsic::x86_mmx_pslli_q:
8317 NewIntNo = Intrinsic::x86_mmx_psll_q;
8318 break;
8319 case Intrinsic::x86_mmx_psrli_w:
8320 NewIntNo = Intrinsic::x86_mmx_psrl_w;
8321 break;
8322 case Intrinsic::x86_mmx_psrli_d:
8323 NewIntNo = Intrinsic::x86_mmx_psrl_d;
8324 break;
8325 case Intrinsic::x86_mmx_psrli_q:
8326 NewIntNo = Intrinsic::x86_mmx_psrl_q;
8327 break;
8328 case Intrinsic::x86_mmx_psrai_w:
8329 NewIntNo = Intrinsic::x86_mmx_psra_w;
8330 break;
8331 case Intrinsic::x86_mmx_psrai_d:
8332 NewIntNo = Intrinsic::x86_mmx_psra_d;
8333 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00008334 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00008335 }
8336 break;
8337 }
8338 }
Mon P Wangefa42202009-09-03 19:56:25 +00008339
8340 // The vector shift intrinsics with scalars uses 32b shift amounts but
8341 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
8342 // to be zero.
8343 SDValue ShOps[4];
8344 ShOps[0] = ShAmt;
8345 ShOps[1] = DAG.getConstant(0, MVT::i32);
8346 if (ShAmtVT == MVT::v4i32) {
8347 ShOps[2] = DAG.getUNDEF(MVT::i32);
8348 ShOps[3] = DAG.getUNDEF(MVT::i32);
8349 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
8350 } else {
8351 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
Dale Johannesen0488fb62010-09-30 23:57:10 +00008352// FIXME this must be lowered to get rid of the invalid type.
Mon P Wangefa42202009-09-03 19:56:25 +00008353 }
8354
Owen Andersone50ed302009-08-10 22:56:29 +00008355 EVT VT = Op.getValueType();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008356 ShAmt = DAG.getNode(ISD::BITCAST, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008357 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008358 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00008359 Op.getOperand(1), ShAmt);
8360 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00008361 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008362}
Evan Cheng72261582005-12-20 06:22:03 +00008363
Dan Gohmand858e902010-04-17 15:26:15 +00008364SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
8365 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +00008366 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
8367 MFI->setReturnAddressIsTaken(true);
8368
Bill Wendling64e87322009-01-16 19:25:27 +00008369 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008370 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00008371
8372 if (Depth > 0) {
8373 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
8374 SDValue Offset =
8375 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00008376 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008377 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00008378 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008379 FrameAddr, Offset),
Chris Lattner51abfe42010-09-21 06:02:19 +00008380 MachinePointerInfo(), false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +00008381 }
8382
8383 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00008384 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00008385 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Chris Lattner51abfe42010-09-21 06:02:19 +00008386 RetAddrFI, MachinePointerInfo(), false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00008387}
8388
Dan Gohmand858e902010-04-17 15:26:15 +00008389SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +00008390 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
8391 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00008392
Owen Andersone50ed302009-08-10 22:56:29 +00008393 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008394 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00008395 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
8396 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00008397 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00008398 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +00008399 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
8400 MachinePointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +00008401 false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00008402 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00008403}
8404
Dan Gohman475871a2008-07-27 21:46:04 +00008405SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008406 SelectionDAG &DAG) const {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00008407 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008408}
8409
Dan Gohmand858e902010-04-17 15:26:15 +00008410SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008411 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00008412 SDValue Chain = Op.getOperand(0);
8413 SDValue Offset = Op.getOperand(1);
8414 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008415 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008416
Dan Gohmand8816272010-08-11 18:14:00 +00008417 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
8418 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
8419 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008420 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008421
Dan Gohmand8816272010-08-11 18:14:00 +00008422 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
8423 DAG.getIntPtrConstant(TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008424 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +00008425 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
8426 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00008427 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008428 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008429
Dale Johannesene4d209d2009-02-03 20:21:25 +00008430 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008431 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008432 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008433}
8434
Dan Gohman475871a2008-07-27 21:46:04 +00008435SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008436 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008437 SDValue Root = Op.getOperand(0);
8438 SDValue Trmp = Op.getOperand(1); // trampoline
8439 SDValue FPtr = Op.getOperand(2); // nested function
8440 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008441 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008442
Dan Gohman69de1932008-02-06 22:27:42 +00008443 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008444
8445 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00008446 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00008447
8448 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +00008449 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
8450 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +00008451
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00008452 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
8453 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00008454
8455 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
8456
8457 // Load the pointer to the nested function into R11.
8458 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00008459 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00008460 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008461 Addr, MachinePointerInfo(TrmpAddr),
8462 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008463
Owen Anderson825b72b2009-08-11 20:47:22 +00008464 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8465 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008466 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
8467 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +00008468 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00008469
8470 // Load the 'nest' parameter value into R10.
8471 // R10 is specified in X86CallingConv.td
8472 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +00008473 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8474 DAG.getConstant(10, MVT::i64));
8475 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008476 Addr, MachinePointerInfo(TrmpAddr, 10),
8477 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008478
Owen Anderson825b72b2009-08-11 20:47:22 +00008479 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8480 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008481 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
8482 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +00008483 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00008484
8485 // Jump to the nested function.
8486 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +00008487 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8488 DAG.getConstant(20, MVT::i64));
8489 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008490 Addr, MachinePointerInfo(TrmpAddr, 20),
8491 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008492
8493 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +00008494 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8495 DAG.getConstant(22, MVT::i64));
8496 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00008497 MachinePointerInfo(TrmpAddr, 22),
8498 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008499
Dan Gohman475871a2008-07-27 21:46:04 +00008500 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00008501 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008502 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008503 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00008504 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00008505 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00008506 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00008507 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008508
8509 switch (CC) {
8510 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00008511 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00008512 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00008513 case CallingConv::X86_StdCall: {
8514 // Pass 'nest' parameter in ECX.
8515 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00008516 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008517
8518 // Check that ECX wasn't needed by an 'inreg' parameter.
8519 const FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00008520 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008521
Chris Lattner58d74912008-03-12 17:45:29 +00008522 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00008523 unsigned InRegCount = 0;
8524 unsigned Idx = 1;
8525
8526 for (FunctionType::param_iterator I = FTy->param_begin(),
8527 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00008528 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00008529 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00008530 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008531
8532 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +00008533 report_fatal_error("Nest register in use - reduce number of inreg"
8534 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00008535 }
8536 }
8537 break;
8538 }
8539 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +00008540 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00008541 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00008542 // Pass 'nest' parameter in EAX.
8543 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00008544 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008545 break;
8546 }
8547
Dan Gohman475871a2008-07-27 21:46:04 +00008548 SDValue OutChains[4];
8549 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008550
Owen Anderson825b72b2009-08-11 20:47:22 +00008551 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8552 DAG.getConstant(10, MVT::i32));
8553 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008554
Chris Lattnera62fe662010-02-05 19:20:30 +00008555 // This is storing the opcode for MOV32ri.
8556 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00008557 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00008558 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008559 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008560 Trmp, MachinePointerInfo(TrmpAddr),
8561 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008562
Owen Anderson825b72b2009-08-11 20:47:22 +00008563 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8564 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008565 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
8566 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +00008567 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008568
Chris Lattnera62fe662010-02-05 19:20:30 +00008569 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +00008570 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8571 DAG.getConstant(5, MVT::i32));
8572 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00008573 MachinePointerInfo(TrmpAddr, 5),
8574 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008575
Owen Anderson825b72b2009-08-11 20:47:22 +00008576 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8577 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008578 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
8579 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +00008580 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008581
Dan Gohman475871a2008-07-27 21:46:04 +00008582 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00008583 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008584 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008585 }
8586}
8587
Dan Gohmand858e902010-04-17 15:26:15 +00008588SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
8589 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008590 /*
8591 The rounding mode is in bits 11:10 of FPSR, and has the following
8592 settings:
8593 00 Round to nearest
8594 01 Round to -inf
8595 10 Round to +inf
8596 11 Round to 0
8597
8598 FLT_ROUNDS, on the other hand, expects the following:
8599 -1 Undefined
8600 0 Round to 0
8601 1 Round to nearest
8602 2 Round to +inf
8603 3 Round to -inf
8604
8605 To perform the conversion, we do:
8606 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
8607 */
8608
8609 MachineFunction &MF = DAG.getMachineFunction();
8610 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00008611 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008612 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00008613 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +00008614 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008615
8616 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00008617 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +00008618 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008619
Michael J. Spencerec38de22010-10-10 22:04:20 +00008620
Chris Lattner2156b792010-09-22 01:11:26 +00008621 MachineMemOperand *MMO =
8622 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8623 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008624
Chris Lattner2156b792010-09-22 01:11:26 +00008625 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
8626 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
8627 DAG.getVTList(MVT::Other),
8628 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008629
8630 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +00008631 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Chris Lattner51abfe42010-09-21 06:02:19 +00008632 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008633
8634 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00008635 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +00008636 DAG.getNode(ISD::SRL, DL, MVT::i16,
8637 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00008638 CWD, DAG.getConstant(0x800, MVT::i16)),
8639 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00008640 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +00008641 DAG.getNode(ISD::SRL, DL, MVT::i16,
8642 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00008643 CWD, DAG.getConstant(0x400, MVT::i16)),
8644 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008645
Dan Gohman475871a2008-07-27 21:46:04 +00008646 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +00008647 DAG.getNode(ISD::AND, DL, MVT::i16,
8648 DAG.getNode(ISD::ADD, DL, MVT::i16,
8649 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +00008650 DAG.getConstant(1, MVT::i16)),
8651 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008652
8653
Duncan Sands83ec4b62008-06-06 12:08:01 +00008654 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +00008655 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008656}
8657
Dan Gohmand858e902010-04-17 15:26:15 +00008658SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008659 EVT VT = Op.getValueType();
8660 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008661 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008662 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00008663
8664 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008665 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00008666 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +00008667 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00008668 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008669 }
Evan Cheng18efe262007-12-14 02:13:44 +00008670
Evan Cheng152804e2007-12-14 08:30:15 +00008671 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008672 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008673 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00008674
8675 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00008676 SDValue Ops[] = {
8677 Op,
8678 DAG.getConstant(NumBits+NumBits-1, OpVT),
8679 DAG.getConstant(X86::COND_E, MVT::i8),
8680 Op.getValue(1)
8681 };
8682 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00008683
8684 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00008685 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00008686
Owen Anderson825b72b2009-08-11 20:47:22 +00008687 if (VT == MVT::i8)
8688 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008689 return Op;
8690}
8691
Dan Gohmand858e902010-04-17 15:26:15 +00008692SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008693 EVT VT = Op.getValueType();
8694 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008695 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008696 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00008697
8698 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008699 if (VT == MVT::i8) {
8700 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00008701 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008702 }
Evan Cheng152804e2007-12-14 08:30:15 +00008703
8704 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008705 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008706 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00008707
8708 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00008709 SDValue Ops[] = {
8710 Op,
8711 DAG.getConstant(NumBits, OpVT),
8712 DAG.getConstant(X86::COND_E, MVT::i8),
8713 Op.getValue(1)
8714 };
8715 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00008716
Owen Anderson825b72b2009-08-11 20:47:22 +00008717 if (VT == MVT::i8)
8718 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008719 return Op;
8720}
8721
Dan Gohmand858e902010-04-17 15:26:15 +00008722SDValue X86TargetLowering::LowerMUL_V2I64(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008723 EVT VT = Op.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00008724 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008725 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00008726
Mon P Wangaf9b9522008-12-18 21:42:19 +00008727 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
8728 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
8729 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
8730 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
8731 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
8732 //
8733 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
8734 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
8735 // return AloBlo + AloBhi + AhiBlo;
8736
8737 SDValue A = Op.getOperand(0);
8738 SDValue B = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008739
Dale Johannesene4d209d2009-02-03 20:21:25 +00008740 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008741 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8742 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008743 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008744 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8745 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008746 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008747 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008748 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008749 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008750 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008751 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008752 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008753 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008754 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008755 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008756 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8757 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008758 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008759 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8760 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008761 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
8762 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +00008763 return Res;
8764}
8765
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008766SDValue X86TargetLowering::LowerSHL(SDValue Op, SelectionDAG &DAG) const {
8767 EVT VT = Op.getValueType();
8768 DebugLoc dl = Op.getDebugLoc();
8769 SDValue R = Op.getOperand(0);
8770
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008771 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008772
Nate Begeman51409212010-07-28 00:21:48 +00008773 assert(Subtarget->hasSSE41() && "Cannot lower SHL without SSE4.1 or later");
8774
8775 if (VT == MVT::v4i32) {
8776 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8777 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
8778 Op.getOperand(1), DAG.getConstant(23, MVT::i32));
8779
8780 ConstantInt *CI = ConstantInt::get(*Context, APInt(32, 0x3f800000U));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008781
Nate Begeman51409212010-07-28 00:21:48 +00008782 std::vector<Constant*> CV(4, CI);
8783 Constant *C = ConstantVector::get(CV);
8784 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8785 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008786 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00008787 false, false, 16);
8788
8789 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008790 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +00008791 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
8792 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
8793 }
8794 if (VT == MVT::v16i8) {
8795 // a = a << 5;
8796 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8797 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
8798 Op.getOperand(1), DAG.getConstant(5, MVT::i32));
8799
8800 ConstantInt *CM1 = ConstantInt::get(*Context, APInt(8, 15));
8801 ConstantInt *CM2 = ConstantInt::get(*Context, APInt(8, 63));
8802
8803 std::vector<Constant*> CVM1(16, CM1);
8804 std::vector<Constant*> CVM2(16, CM2);
8805 Constant *C = ConstantVector::get(CVM1);
8806 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8807 SDValue M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008808 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00008809 false, false, 16);
8810
8811 // r = pblendv(r, psllw(r & (char16)15, 4), a);
8812 M = DAG.getNode(ISD::AND, dl, VT, R, M);
8813 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8814 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
8815 DAG.getConstant(4, MVT::i32));
Nate Begeman672fb622010-12-20 22:04:24 +00008816 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT, R, M, Op);
Nate Begeman51409212010-07-28 00:21:48 +00008817 // a += a
8818 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008819
Nate Begeman51409212010-07-28 00:21:48 +00008820 C = ConstantVector::get(CVM2);
8821 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8822 M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008823 MachinePointerInfo::getConstantPool(),
Chris Lattner51abfe42010-09-21 06:02:19 +00008824 false, false, 16);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008825
Nate Begeman51409212010-07-28 00:21:48 +00008826 // r = pblendv(r, psllw(r & (char16)63, 2), a);
8827 M = DAG.getNode(ISD::AND, dl, VT, R, M);
8828 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8829 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
8830 DAG.getConstant(2, MVT::i32));
Nate Begeman672fb622010-12-20 22:04:24 +00008831 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT, R, M, Op);
Nate Begeman51409212010-07-28 00:21:48 +00008832 // a += a
8833 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008834
Nate Begeman51409212010-07-28 00:21:48 +00008835 // return pblendv(r, r+r, a);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008836 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT,
Nate Begeman51409212010-07-28 00:21:48 +00008837 R, DAG.getNode(ISD::ADD, dl, VT, R, R), Op);
8838 return R;
8839 }
8840 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008841}
Mon P Wangaf9b9522008-12-18 21:42:19 +00008842
Dan Gohmand858e902010-04-17 15:26:15 +00008843SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
Bill Wendling74c37652008-12-09 22:08:41 +00008844 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
8845 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +00008846 // looks for this combo and may remove the "setcc" instruction if the "setcc"
8847 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +00008848 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +00008849 SDValue LHS = N->getOperand(0);
8850 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +00008851 unsigned BaseOp = 0;
8852 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008853 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +00008854 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008855 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +00008856 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +00008857 // A subtract of one will be selected as a INC. Note that INC doesn't
8858 // set CF, so we can't do this for UADDO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +00008859 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
8860 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +00008861 BaseOp = X86ISD::INC;
8862 Cond = X86::COND_O;
8863 break;
8864 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008865 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +00008866 Cond = X86::COND_O;
8867 break;
8868 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008869 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +00008870 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00008871 break;
8872 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +00008873 // A subtract of one will be selected as a DEC. Note that DEC doesn't
8874 // set CF, so we can't do this for USUBO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +00008875 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
8876 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +00008877 BaseOp = X86ISD::DEC;
8878 Cond = X86::COND_O;
8879 break;
8880 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008881 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +00008882 Cond = X86::COND_O;
8883 break;
8884 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008885 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +00008886 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00008887 break;
8888 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00008889 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +00008890 Cond = X86::COND_O;
8891 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008892 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
8893 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
8894 MVT::i32);
8895 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008896
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008897 SDValue SetCC =
8898 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
8899 DAG.getConstant(X86::COND_O, MVT::i32),
8900 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008901
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008902 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
8903 return Sum;
8904 }
Bill Wendling74c37652008-12-09 22:08:41 +00008905 }
Bill Wendling3fafd932008-11-26 22:37:40 +00008906
Bill Wendling61edeb52008-12-02 01:06:39 +00008907 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008908 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008909 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +00008910
Bill Wendling61edeb52008-12-02 01:06:39 +00008911 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008912 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
8913 DAG.getConstant(Cond, MVT::i32),
8914 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +00008915
Bill Wendling61edeb52008-12-02 01:06:39 +00008916 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
8917 return Sum;
Bill Wendling41ea7e72008-11-24 19:21:46 +00008918}
8919
Eric Christopher9a9d2752010-07-22 02:48:34 +00008920SDValue X86TargetLowering::LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG) const{
8921 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00008922
Eric Christopherb6729dc2010-08-04 23:03:04 +00008923 if (!Subtarget->hasSSE2()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +00008924 SDValue Chain = Op.getOperand(0);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008925 SDValue Zero = DAG.getConstant(0,
Eric Christopherb6729dc2010-08-04 23:03:04 +00008926 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +00008927 SDValue Ops[] = {
8928 DAG.getRegister(X86::ESP, MVT::i32), // Base
8929 DAG.getTargetConstant(1, MVT::i8), // Scale
8930 DAG.getRegister(0, MVT::i32), // Index
8931 DAG.getTargetConstant(0, MVT::i32), // Disp
8932 DAG.getRegister(0, MVT::i32), // Segment.
8933 Zero,
8934 Chain
8935 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00008936 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +00008937 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
8938 array_lengthof(Ops));
8939 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +00008940 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00008941
Eric Christopher9a9d2752010-07-22 02:48:34 +00008942 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +00008943 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +00008944 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008945
Chris Lattner132929a2010-08-14 17:26:09 +00008946 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
8947 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
8948 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
8949 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +00008950
Chris Lattner132929a2010-08-14 17:26:09 +00008951 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
8952 if (!Op1 && !Op2 && !Op3 && Op4)
8953 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008954
Chris Lattner132929a2010-08-14 17:26:09 +00008955 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
8956 if (Op1 && !Op2 && !Op3 && !Op4)
8957 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008958
8959 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +00008960 // (MFENCE)>;
8961 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +00008962}
8963
Dan Gohmand858e902010-04-17 15:26:15 +00008964SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008965 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008966 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +00008967 unsigned Reg = 0;
8968 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00008969 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008970 default:
8971 assert(false && "Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +00008972 case MVT::i8: Reg = X86::AL; size = 1; break;
8973 case MVT::i16: Reg = X86::AX; size = 2; break;
8974 case MVT::i32: Reg = X86::EAX; size = 4; break;
8975 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +00008976 assert(Subtarget->is64Bit() && "Node not type legal!");
8977 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00008978 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00008979 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008980 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +00008981 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +00008982 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +00008983 Op.getOperand(1),
8984 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +00008985 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +00008986 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008987 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008988 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
8989 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
8990 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +00008991 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008992 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +00008993 return cpOut;
8994}
8995
Duncan Sands1607f052008-12-01 11:39:25 +00008996SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008997 SelectionDAG &DAG) const {
Duncan Sands1607f052008-12-01 11:39:25 +00008998 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008999 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +00009000 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009001 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +00009002 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00009003 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
9004 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +00009005 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +00009006 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
9007 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +00009008 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +00009009 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +00009010 rdx.getValue(1)
9011 };
Dale Johannesene4d209d2009-02-03 20:21:25 +00009012 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009013}
9014
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009015SDValue X86TargetLowering::LowerBITCAST(SDValue Op,
Dale Johannesen7d07b482010-05-21 00:52:33 +00009016 SelectionDAG &DAG) const {
9017 EVT SrcVT = Op.getOperand(0).getValueType();
9018 EVT DstVT = Op.getValueType();
Chris Lattner2a786eb2010-12-19 20:19:20 +00009019 assert(Subtarget->is64Bit() && !Subtarget->hasSSE2() &&
9020 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +00009021 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +00009022 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009023 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +00009024 // i64 <=> MMX conversions are Legal.
9025 if (SrcVT==MVT::i64 && DstVT.isVector())
9026 return Op;
9027 if (DstVT==MVT::i64 && SrcVT.isVector())
9028 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +00009029 // MMX <=> MMX conversions are Legal.
9030 if (SrcVT.isVector() && DstVT.isVector())
9031 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +00009032 // All other conversions need to be expanded.
9033 return SDValue();
9034}
Chris Lattner5b856542010-12-20 00:59:46 +00009035
Dan Gohmand858e902010-04-17 15:26:15 +00009036SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen71d1bf52008-09-29 22:25:26 +00009037 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +00009038 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00009039 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009040 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +00009041 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009042 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009043 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +00009044 Node->getOperand(0),
9045 Node->getOperand(1), negOp,
9046 cast<AtomicSDNode>(Node)->getSrcValue(),
9047 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang63307c32008-05-05 19:05:59 +00009048}
9049
Chris Lattner5b856542010-12-20 00:59:46 +00009050static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
9051 EVT VT = Op.getNode()->getValueType(0);
9052
9053 // Let legalize expand this if it isn't a legal type yet.
9054 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
9055 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009056
Chris Lattner5b856542010-12-20 00:59:46 +00009057 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009058
Chris Lattner5b856542010-12-20 00:59:46 +00009059 unsigned Opc;
9060 bool ExtraOp = false;
9061 switch (Op.getOpcode()) {
9062 default: assert(0 && "Invalid code");
9063 case ISD::ADDC: Opc = X86ISD::ADD; break;
9064 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
9065 case ISD::SUBC: Opc = X86ISD::SUB; break;
9066 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
9067 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009068
Chris Lattner5b856542010-12-20 00:59:46 +00009069 if (!ExtraOp)
9070 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
9071 Op.getOperand(1));
9072 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
9073 Op.getOperand(1), Op.getOperand(2));
9074}
9075
Evan Cheng0db9fe62006-04-25 20:13:52 +00009076/// LowerOperation - Provide custom lowering hooks for some operations.
9077///
Dan Gohmand858e902010-04-17 15:26:15 +00009078SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00009079 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00009080 default: llvm_unreachable("Should not custom lower this!");
Eric Christopher9a9d2752010-07-22 02:48:34 +00009081 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op,DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009082 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
9083 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009084 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00009085 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009086 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
9087 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
9088 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
David Greene91585092011-01-26 15:38:49 +00009089 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
David Greenecfe33c42011-01-26 19:13:22 +00009090 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009091 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
9092 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
9093 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00009094 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +00009095 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +00009096 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009097 case ISD::SHL_PARTS:
9098 case ISD::SRA_PARTS:
9099 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
9100 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00009101 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009102 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00009103 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009104 case ISD::FABS: return LowerFABS(Op, DAG);
9105 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00009106 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00009107 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman30a0de92008-07-17 16:51:19 +00009108 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00009109 case ISD::SELECT: return LowerSELECT(Op, DAG);
9110 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009111 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009112 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +00009113 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +00009114 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009115 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00009116 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
9117 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009118 case ISD::FRAME_TO_ARGS_OFFSET:
9119 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009120 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009121 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009122 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00009123 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +00009124 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
9125 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Mon P Wangaf9b9522008-12-18 21:42:19 +00009126 case ISD::MUL: return LowerMUL_V2I64(Op, DAG);
Nate Begemanbdcb5af2010-07-27 22:37:06 +00009127 case ISD::SHL: return LowerSHL(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +00009128 case ISD::SADDO:
9129 case ISD::UADDO:
9130 case ISD::SSUBO:
9131 case ISD::USUBO:
9132 case ISD::SMULO:
9133 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00009134 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009135 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +00009136 case ISD::ADDC:
9137 case ISD::ADDE:
9138 case ISD::SUBC:
9139 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009140 }
Chris Lattner27a6c732007-11-24 07:07:01 +00009141}
9142
Duncan Sands1607f052008-12-01 11:39:25 +00009143void X86TargetLowering::
9144ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00009145 SelectionDAG &DAG, unsigned NewOp) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009146 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009147 DebugLoc dl = Node->getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00009148 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +00009149
9150 SDValue Chain = Node->getOperand(0);
9151 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00009152 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00009153 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00009154 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00009155 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +00009156 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +00009157 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +00009158 SDValue Result =
9159 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
9160 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +00009161 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +00009162 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00009163 Results.push_back(Result.getValue(2));
9164}
9165
Duncan Sands126d9072008-07-04 11:47:58 +00009166/// ReplaceNodeResults - Replace a node with an illegal result type
9167/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00009168void X86TargetLowering::ReplaceNodeResults(SDNode *N,
9169 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00009170 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +00009171 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +00009172 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +00009173 default:
Duncan Sands1607f052008-12-01 11:39:25 +00009174 assert(false && "Do not know how to custom type legalize this operation!");
9175 return;
Chris Lattner5b856542010-12-20 00:59:46 +00009176 case ISD::ADDC:
9177 case ISD::ADDE:
9178 case ISD::SUBC:
9179 case ISD::SUBE:
9180 // We don't want to expand or promote these.
9181 return;
Duncan Sands1607f052008-12-01 11:39:25 +00009182 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +00009183 std::pair<SDValue,SDValue> Vals =
9184 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +00009185 SDValue FIST = Vals.first, StackSlot = Vals.second;
9186 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +00009187 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +00009188 // Return a load from the stack slot.
Chris Lattner51abfe42010-09-21 06:02:19 +00009189 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
9190 MachinePointerInfo(), false, false, 0));
Duncan Sands1607f052008-12-01 11:39:25 +00009191 }
9192 return;
9193 }
9194 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009195 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +00009196 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009197 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00009198 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +00009199 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00009200 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00009201 eax.getValue(2));
9202 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
9203 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +00009204 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00009205 Results.push_back(edx.getValue(1));
9206 return;
9207 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009208 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +00009209 EVT T = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009210 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Duncan Sands1607f052008-12-01 11:39:25 +00009211 SDValue cpInL, cpInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00009212 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
9213 DAG.getConstant(0, MVT::i32));
9214 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
9215 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00009216 cpInL = DAG.getCopyToReg(N->getOperand(0), dl, X86::EAX, cpInL, SDValue());
9217 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl, X86::EDX, cpInH,
Duncan Sands1607f052008-12-01 11:39:25 +00009218 cpInL.getValue(1));
9219 SDValue swapInL, swapInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00009220 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
9221 DAG.getConstant(0, MVT::i32));
9222 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
9223 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00009224 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl, X86::EBX, swapInL,
Duncan Sands1607f052008-12-01 11:39:25 +00009225 cpInH.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00009226 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl, X86::ECX, swapInH,
Duncan Sands1607f052008-12-01 11:39:25 +00009227 swapInL.getValue(1));
9228 SDValue Ops[] = { swapInH.getValue(0),
9229 N->getOperand(1),
9230 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009231 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +00009232 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
9233 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG8_DAG, dl, Tys,
9234 Ops, 3, T, MMO);
Dale Johannesendd64c412009-02-04 00:33:20 +00009235 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl, X86::EAX,
Owen Anderson825b72b2009-08-11 20:47:22 +00009236 MVT::i32, Result.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00009237 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl, X86::EDX,
Owen Anderson825b72b2009-08-11 20:47:22 +00009238 MVT::i32, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +00009239 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Owen Anderson825b72b2009-08-11 20:47:22 +00009240 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00009241 Results.push_back(cpOutH.getValue(1));
9242 return;
9243 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009244 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +00009245 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
9246 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009247 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +00009248 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
9249 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009250 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +00009251 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
9252 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009253 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +00009254 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
9255 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009256 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +00009257 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
9258 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009259 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +00009260 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
9261 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009262 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +00009263 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
9264 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00009265 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00009266}
9267
Evan Cheng72261582005-12-20 06:22:03 +00009268const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
9269 switch (Opcode) {
9270 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +00009271 case X86ISD::BSF: return "X86ISD::BSF";
9272 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +00009273 case X86ISD::SHLD: return "X86ISD::SHLD";
9274 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00009275 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00009276 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00009277 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00009278 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00009279 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00009280 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00009281 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
9282 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
9283 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00009284 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00009285 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +00009286 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +00009287 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +00009288 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +00009289 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00009290 case X86ISD::COMI: return "X86ISD::COMI";
9291 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00009292 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +00009293 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Evan Cheng72261582005-12-20 06:22:03 +00009294 case X86ISD::CMOV: return "X86ISD::CMOV";
9295 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00009296 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00009297 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
9298 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +00009299 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00009300 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +00009301 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +00009302 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +00009303 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +00009304 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
9305 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +00009306 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +00009307 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Nate Begemanb65c1752010-12-17 22:55:37 +00009308 case X86ISD::PANDN: return "X86ISD::PANDN";
9309 case X86ISD::PSIGNB: return "X86ISD::PSIGNB";
9310 case X86ISD::PSIGNW: return "X86ISD::PSIGNW";
9311 case X86ISD::PSIGND: return "X86ISD::PSIGND";
Nate Begeman672fb622010-12-20 22:04:24 +00009312 case X86ISD::PBLENDVB: return "X86ISD::PBLENDVB";
Evan Cheng8ca29322006-11-10 21:43:37 +00009313 case X86ISD::FMAX: return "X86ISD::FMAX";
9314 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +00009315 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
9316 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00009317 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +00009318 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009319 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00009320 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009321 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +00009322 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
9323 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009324 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
9325 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
9326 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
9327 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
9328 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
9329 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +00009330 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
9331 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +00009332 case X86ISD::VSHL: return "X86ISD::VSHL";
9333 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +00009334 case X86ISD::CMPPD: return "X86ISD::CMPPD";
9335 case X86ISD::CMPPS: return "X86ISD::CMPPS";
9336 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
9337 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
9338 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
9339 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
9340 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
9341 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
9342 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
9343 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +00009344 case X86ISD::ADD: return "X86ISD::ADD";
9345 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +00009346 case X86ISD::ADC: return "X86ISD::ADC";
9347 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +00009348 case X86ISD::SMUL: return "X86ISD::SMUL";
9349 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +00009350 case X86ISD::INC: return "X86ISD::INC";
9351 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +00009352 case X86ISD::OR: return "X86ISD::OR";
9353 case X86ISD::XOR: return "X86ISD::XOR";
9354 case X86ISD::AND: return "X86ISD::AND";
Evan Cheng73f24c92009-03-30 21:36:47 +00009355 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +00009356 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009357 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009358 case X86ISD::PALIGN: return "X86ISD::PALIGN";
9359 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
9360 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
9361 case X86ISD::PSHUFHW_LD: return "X86ISD::PSHUFHW_LD";
9362 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
9363 case X86ISD::PSHUFLW_LD: return "X86ISD::PSHUFLW_LD";
9364 case X86ISD::SHUFPS: return "X86ISD::SHUFPS";
9365 case X86ISD::SHUFPD: return "X86ISD::SHUFPD";
9366 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009367 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00009368 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009369 case X86ISD::MOVHLPD: return "X86ISD::MOVHLPD";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00009370 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
9371 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009372 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
9373 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
9374 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
9375 case X86ISD::MOVSHDUP_LD: return "X86ISD::MOVSHDUP_LD";
9376 case X86ISD::MOVSLDUP_LD: return "X86ISD::MOVSLDUP_LD";
9377 case X86ISD::MOVSD: return "X86ISD::MOVSD";
9378 case X86ISD::MOVSS: return "X86ISD::MOVSS";
9379 case X86ISD::UNPCKLPS: return "X86ISD::UNPCKLPS";
9380 case X86ISD::UNPCKLPD: return "X86ISD::UNPCKLPD";
David Greenefbf05d32011-02-22 23:31:46 +00009381 case X86ISD::VUNPCKLPS: return "X86ISD::VUNPCKLPS";
9382 case X86ISD::VUNPCKLPD: return "X86ISD::VUNPCKLPD";
9383 case X86ISD::VUNPCKLPSY: return "X86ISD::VUNPCKLPSY";
9384 case X86ISD::VUNPCKLPDY: return "X86ISD::VUNPCKLPDY";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009385 case X86ISD::UNPCKHPS: return "X86ISD::UNPCKHPS";
9386 case X86ISD::UNPCKHPD: return "X86ISD::UNPCKHPD";
9387 case X86ISD::PUNPCKLBW: return "X86ISD::PUNPCKLBW";
9388 case X86ISD::PUNPCKLWD: return "X86ISD::PUNPCKLWD";
9389 case X86ISD::PUNPCKLDQ: return "X86ISD::PUNPCKLDQ";
9390 case X86ISD::PUNPCKLQDQ: return "X86ISD::PUNPCKLQDQ";
9391 case X86ISD::PUNPCKHBW: return "X86ISD::PUNPCKHBW";
9392 case X86ISD::PUNPCKHWD: return "X86ISD::PUNPCKHWD";
9393 case X86ISD::PUNPCKHDQ: return "X86ISD::PUNPCKHDQ";
9394 case X86ISD::PUNPCKHQDQ: return "X86ISD::PUNPCKHQDQ";
Dan Gohmand6708ea2009-08-15 01:38:56 +00009395 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +00009396 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +00009397 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Evan Cheng72261582005-12-20 06:22:03 +00009398 }
9399}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00009400
Chris Lattnerc9addb72007-03-30 23:15:24 +00009401// isLegalAddressingMode - Return true if the addressing mode represented
9402// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +00009403bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerc9addb72007-03-30 23:15:24 +00009404 const Type *Ty) const {
9405 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009406 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +00009407 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +00009408
Chris Lattnerc9addb72007-03-30 23:15:24 +00009409 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009410 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +00009411 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00009412
Chris Lattnerc9addb72007-03-30 23:15:24 +00009413 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +00009414 unsigned GVFlags =
9415 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009416
Chris Lattnerdfed4132009-07-10 07:38:24 +00009417 // If a reference to this global requires an extra load, we can't fold it.
9418 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +00009419 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009420
Chris Lattnerdfed4132009-07-10 07:38:24 +00009421 // If BaseGV requires a register for the PIC base, we cannot also have a
9422 // BaseReg specified.
9423 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +00009424 return false;
Evan Cheng52787842007-08-01 23:46:47 +00009425
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009426 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +00009427 if ((M != CodeModel::Small || R != Reloc::Static) &&
9428 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009429 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +00009430 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009431
Chris Lattnerc9addb72007-03-30 23:15:24 +00009432 switch (AM.Scale) {
9433 case 0:
9434 case 1:
9435 case 2:
9436 case 4:
9437 case 8:
9438 // These scales always work.
9439 break;
9440 case 3:
9441 case 5:
9442 case 9:
9443 // These scales are formed with basereg+scalereg. Only accept if there is
9444 // no basereg yet.
9445 if (AM.HasBaseReg)
9446 return false;
9447 break;
9448 default: // Other stuff never works.
9449 return false;
9450 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009451
Chris Lattnerc9addb72007-03-30 23:15:24 +00009452 return true;
9453}
9454
9455
Evan Cheng2bd122c2007-10-26 01:56:11 +00009456bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009457 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +00009458 return false;
Evan Chenge127a732007-10-29 07:57:50 +00009459 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
9460 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00009461 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +00009462 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00009463 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +00009464}
9465
Owen Andersone50ed302009-08-10 22:56:29 +00009466bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +00009467 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009468 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009469 unsigned NumBits1 = VT1.getSizeInBits();
9470 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00009471 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009472 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00009473 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009474}
Evan Cheng2bd122c2007-10-26 01:56:11 +00009475
Dan Gohman97121ba2009-04-08 00:15:30 +00009476bool X86TargetLowering::isZExtFree(const Type *Ty1, const Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00009477 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009478 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00009479}
9480
Owen Andersone50ed302009-08-10 22:56:29 +00009481bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00009482 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00009483 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00009484}
9485
Owen Andersone50ed302009-08-10 22:56:29 +00009486bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +00009487 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +00009488 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +00009489}
9490
Evan Cheng60c07e12006-07-05 22:17:51 +00009491/// isShuffleMaskLegal - Targets can use this to indicate that they only
9492/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
9493/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
9494/// are assumed to be legal.
9495bool
Eric Christopherfd179292009-08-27 18:07:15 +00009496X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +00009497 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +00009498 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +00009499 if (VT.getSizeInBits() == 64)
Eric Christophercff6f852010-04-15 01:40:20 +00009500 return isPALIGNRMask(M, VT, Subtarget->hasSSSE3());
Nate Begeman9008ca62009-04-27 18:41:29 +00009501
Nate Begemana09008b2009-10-19 02:17:23 +00009502 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +00009503 return (VT.getVectorNumElements() == 2 ||
9504 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
9505 isMOVLMask(M, VT) ||
9506 isSHUFPMask(M, VT) ||
9507 isPSHUFDMask(M, VT) ||
9508 isPSHUFHWMask(M, VT) ||
9509 isPSHUFLWMask(M, VT) ||
Nate Begemana09008b2009-10-19 02:17:23 +00009510 isPALIGNRMask(M, VT, Subtarget->hasSSSE3()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +00009511 isUNPCKLMask(M, VT) ||
9512 isUNPCKHMask(M, VT) ||
9513 isUNPCKL_v_undef_Mask(M, VT) ||
9514 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00009515}
9516
Dan Gohman7d8143f2008-04-09 20:09:42 +00009517bool
Nate Begeman5a5ca152009-04-29 05:20:52 +00009518X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +00009519 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00009520 unsigned NumElts = VT.getVectorNumElements();
9521 // FIXME: This collection of masks seems suspect.
9522 if (NumElts == 2)
9523 return true;
9524 if (NumElts == 4 && VT.getSizeInBits() == 128) {
9525 return (isMOVLMask(Mask, VT) ||
9526 isCommutedMOVLMask(Mask, VT, true) ||
9527 isSHUFPMask(Mask, VT) ||
9528 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00009529 }
9530 return false;
9531}
9532
9533//===----------------------------------------------------------------------===//
9534// X86 Scheduler Hooks
9535//===----------------------------------------------------------------------===//
9536
Mon P Wang63307c32008-05-05 19:05:59 +00009537// private utility function
9538MachineBasicBlock *
9539X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
9540 MachineBasicBlock *MBB,
9541 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009542 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009543 unsigned LoadOpc,
9544 unsigned CXchgOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009545 unsigned notOpc,
9546 unsigned EAXreg,
9547 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009548 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00009549 // For the atomic bitwise operator, we generate
9550 // thisMBB:
9551 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00009552 // ld t1 = [bitinstr.addr]
9553 // op t2 = t1, [bitinstr.val]
9554 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00009555 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
9556 // bz newMBB
9557 // fallthrough -->nextMBB
9558 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9559 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009560 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00009561 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009562
Mon P Wang63307c32008-05-05 19:05:59 +00009563 /// First build the CFG
9564 MachineFunction *F = MBB->getParent();
9565 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009566 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9567 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9568 F->insert(MBBIter, newMBB);
9569 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009570
Dan Gohman14152b42010-07-06 20:24:04 +00009571 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9572 nextMBB->splice(nextMBB->begin(), thisMBB,
9573 llvm::next(MachineBasicBlock::iterator(bInstr)),
9574 thisMBB->end());
9575 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009576
Mon P Wang63307c32008-05-05 19:05:59 +00009577 // Update thisMBB to fall through to newMBB
9578 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009579
Mon P Wang63307c32008-05-05 19:05:59 +00009580 // newMBB jumps to itself and fall through to nextMBB
9581 newMBB->addSuccessor(nextMBB);
9582 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009583
Mon P Wang63307c32008-05-05 19:05:59 +00009584 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009585 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009586 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +00009587 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00009588 MachineOperand& destOper = bInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009589 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00009590 int numArgs = bInstr->getNumOperands() - 1;
9591 for (int i=0; i < numArgs; ++i)
9592 argOpers[i] = &bInstr->getOperand(i+1);
9593
9594 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009595 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009596 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00009597
Dale Johannesen140be2d2008-08-19 18:47:28 +00009598 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009599 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00009600 for (int i=0; i <= lastAddrIndx; ++i)
9601 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009602
Dale Johannesen140be2d2008-08-19 18:47:28 +00009603 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009604 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00009605 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009606 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009607 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009608 tt = t1;
9609
Dale Johannesen140be2d2008-08-19 18:47:28 +00009610 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +00009611 assert((argOpers[valArgIndx]->isReg() ||
9612 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00009613 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +00009614 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009615 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00009616 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009617 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009618 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +00009619 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009620
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009621 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +00009622 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009623
Dale Johannesene4d209d2009-02-03 20:21:25 +00009624 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +00009625 for (int i=0; i <= lastAddrIndx; ++i)
9626 (*MIB).addOperand(*argOpers[i]);
9627 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +00009628 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009629 (*MIB).setMemRefs(bInstr->memoperands_begin(),
9630 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +00009631
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009632 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +00009633 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +00009634
Mon P Wang63307c32008-05-05 19:05:59 +00009635 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009636 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00009637
Dan Gohman14152b42010-07-06 20:24:04 +00009638 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00009639 return nextMBB;
9640}
9641
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00009642// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +00009643MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009644X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
9645 MachineBasicBlock *MBB,
9646 unsigned regOpcL,
9647 unsigned regOpcH,
9648 unsigned immOpcL,
9649 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009650 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009651 // For the atomic bitwise operator, we generate
9652 // thisMBB (instructions are in pairs, except cmpxchg8b)
9653 // ld t1,t2 = [bitinstr.addr]
9654 // newMBB:
9655 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
9656 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +00009657 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009658 // mov ECX, EBX <- t5, t6
9659 // mov EAX, EDX <- t1, t2
9660 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
9661 // mov t3, t4 <- EAX, EDX
9662 // bz newMBB
9663 // result in out1, out2
9664 // fallthrough -->nextMBB
9665
9666 const TargetRegisterClass *RC = X86::GR32RegisterClass;
9667 const unsigned LoadOpc = X86::MOV32rm;
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009668 const unsigned NotOpc = X86::NOT32r;
9669 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9670 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
9671 MachineFunction::iterator MBBIter = MBB;
9672 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009673
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009674 /// First build the CFG
9675 MachineFunction *F = MBB->getParent();
9676 MachineBasicBlock *thisMBB = MBB;
9677 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9678 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9679 F->insert(MBBIter, newMBB);
9680 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009681
Dan Gohman14152b42010-07-06 20:24:04 +00009682 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9683 nextMBB->splice(nextMBB->begin(), thisMBB,
9684 llvm::next(MachineBasicBlock::iterator(bInstr)),
9685 thisMBB->end());
9686 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009687
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009688 // Update thisMBB to fall through to newMBB
9689 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009690
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009691 // newMBB jumps to itself and fall through to nextMBB
9692 newMBB->addSuccessor(nextMBB);
9693 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009694
Dale Johannesene4d209d2009-02-03 20:21:25 +00009695 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009696 // Insert instructions into newMBB based on incoming instruction
9697 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009698 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009699 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009700 MachineOperand& dest1Oper = bInstr->getOperand(0);
9701 MachineOperand& dest2Oper = bInstr->getOperand(1);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009702 MachineOperand* argOpers[2 + X86::AddrNumOperands];
9703 for (int i=0; i < 2 + X86::AddrNumOperands; ++i) {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009704 argOpers[i] = &bInstr->getOperand(i+2);
9705
Dan Gohman71ea4e52010-05-14 21:01:44 +00009706 // We use some of the operands multiple times, so conservatively just
9707 // clear any kill flags that might be present.
9708 if (argOpers[i]->isReg() && argOpers[i]->isUse())
9709 argOpers[i]->setIsKill(false);
9710 }
9711
Evan Chengad5b52f2010-01-08 19:14:57 +00009712 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009713 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +00009714
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009715 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009716 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009717 for (int i=0; i <= lastAddrIndx; ++i)
9718 (*MIB).addOperand(*argOpers[i]);
9719 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009720 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +00009721 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +00009722 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009723 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +00009724 MachineOperand newOp3 = *(argOpers[3]);
9725 if (newOp3.isImm())
9726 newOp3.setImm(newOp3.getImm()+4);
9727 else
9728 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009729 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +00009730 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009731
9732 // t3/4 are defined later, at the bottom of the loop
9733 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
9734 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009735 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009736 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009737 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009738 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
9739
Evan Cheng306b4ca2010-01-08 23:41:50 +00009740 // The subsequent operations should be using the destination registers of
9741 //the PHI instructions.
Scott Michelfdc40a02009-02-17 22:15:04 +00009742 if (invSrc) {
Evan Cheng306b4ca2010-01-08 23:41:50 +00009743 t1 = F->getRegInfo().createVirtualRegister(RC);
9744 t2 = F->getRegInfo().createVirtualRegister(RC);
9745 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
9746 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009747 } else {
Evan Cheng306b4ca2010-01-08 23:41:50 +00009748 t1 = dest1Oper.getReg();
9749 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009750 }
9751
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009752 int valArgIndx = lastAddrIndx + 1;
9753 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +00009754 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009755 "invalid operand");
9756 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
9757 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009758 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009759 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009760 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009761 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +00009762 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00009763 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009764 (*MIB).addOperand(*argOpers[valArgIndx]);
9765 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00009766 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009767 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00009768 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009769 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009770 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009771 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009772 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +00009773 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00009774 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009775 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009776
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009777 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009778 MIB.addReg(t1);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009779 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009780 MIB.addReg(t2);
9781
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009782 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009783 MIB.addReg(t5);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009784 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009785 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +00009786
Dale Johannesene4d209d2009-02-03 20:21:25 +00009787 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009788 for (int i=0; i <= lastAddrIndx; ++i)
9789 (*MIB).addOperand(*argOpers[i]);
9790
9791 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009792 (*MIB).setMemRefs(bInstr->memoperands_begin(),
9793 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009794
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009795 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009796 MIB.addReg(X86::EAX);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009797 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009798 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +00009799
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009800 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009801 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009802
Dan Gohman14152b42010-07-06 20:24:04 +00009803 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009804 return nextMBB;
9805}
9806
9807// private utility function
9808MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +00009809X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
9810 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009811 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00009812 // For the atomic min/max operator, we generate
9813 // thisMBB:
9814 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00009815 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +00009816 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +00009817 // cmp t1, t2
9818 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +00009819 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00009820 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
9821 // bz newMBB
9822 // fallthrough -->nextMBB
9823 //
9824 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9825 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009826 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00009827 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009828
Mon P Wang63307c32008-05-05 19:05:59 +00009829 /// First build the CFG
9830 MachineFunction *F = MBB->getParent();
9831 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009832 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9833 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9834 F->insert(MBBIter, newMBB);
9835 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009836
Dan Gohman14152b42010-07-06 20:24:04 +00009837 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9838 nextMBB->splice(nextMBB->begin(), thisMBB,
9839 llvm::next(MachineBasicBlock::iterator(mInstr)),
9840 thisMBB->end());
9841 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009842
Mon P Wang63307c32008-05-05 19:05:59 +00009843 // Update thisMBB to fall through to newMBB
9844 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009845
Mon P Wang63307c32008-05-05 19:05:59 +00009846 // newMBB jumps to newMBB and fall through to nextMBB
9847 newMBB->addSuccessor(nextMBB);
9848 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009849
Dale Johannesene4d209d2009-02-03 20:21:25 +00009850 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00009851 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009852 assert(mInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009853 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +00009854 MachineOperand& destOper = mInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009855 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00009856 int numArgs = mInstr->getNumOperands() - 1;
9857 for (int i=0; i < numArgs; ++i)
9858 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009859
Mon P Wang63307c32008-05-05 19:05:59 +00009860 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009861 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009862 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00009863
Mon P Wangab3e7472008-05-05 22:56:23 +00009864 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009865 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00009866 for (int i=0; i <= lastAddrIndx; ++i)
9867 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +00009868
Mon P Wang63307c32008-05-05 19:05:59 +00009869 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +00009870 assert((argOpers[valArgIndx]->isReg() ||
9871 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00009872 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +00009873
9874 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +00009875 if (argOpers[valArgIndx]->isReg())
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009876 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +00009877 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009878 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00009879 (*MIB).addOperand(*argOpers[valArgIndx]);
9880
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009881 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +00009882 MIB.addReg(t1);
9883
Dale Johannesene4d209d2009-02-03 20:21:25 +00009884 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +00009885 MIB.addReg(t1);
9886 MIB.addReg(t2);
9887
9888 // Generate movc
9889 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009890 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +00009891 MIB.addReg(t2);
9892 MIB.addReg(t1);
9893
9894 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +00009895 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +00009896 for (int i=0; i <= lastAddrIndx; ++i)
9897 (*MIB).addOperand(*argOpers[i]);
9898 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +00009899 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009900 (*MIB).setMemRefs(mInstr->memoperands_begin(),
9901 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +00009902
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009903 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +00009904 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +00009905
Mon P Wang63307c32008-05-05 19:05:59 +00009906 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009907 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00009908
Dan Gohman14152b42010-07-06 20:24:04 +00009909 mInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00009910 return nextMBB;
9911}
9912
Eric Christopherf83a5de2009-08-27 18:08:16 +00009913// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009914// or XMM0_V32I8 in AVX all of this code can be replaced with that
9915// in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +00009916MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +00009917X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +00009918 unsigned numArgs, bool memArg) const {
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009919 assert((Subtarget->hasSSE42() || Subtarget->hasAVX()) &&
9920 "Target must have SSE4.2 or AVX features enabled");
9921
Eric Christopherb120ab42009-08-18 22:50:32 +00009922 DebugLoc dl = MI->getDebugLoc();
9923 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Eric Christopherb120ab42009-08-18 22:50:32 +00009924 unsigned Opc;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009925 if (!Subtarget->hasAVX()) {
9926 if (memArg)
9927 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
9928 else
9929 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
9930 } else {
9931 if (memArg)
9932 Opc = numArgs == 3 ? X86::VPCMPISTRM128rm : X86::VPCMPESTRM128rm;
9933 else
9934 Opc = numArgs == 3 ? X86::VPCMPISTRM128rr : X86::VPCMPESTRM128rr;
9935 }
Eric Christopherb120ab42009-08-18 22:50:32 +00009936
Eric Christopher41c902f2010-11-30 08:20:21 +00009937 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Eric Christopherb120ab42009-08-18 22:50:32 +00009938 for (unsigned i = 0; i < numArgs; ++i) {
9939 MachineOperand &Op = MI->getOperand(i+1);
Eric Christopherb120ab42009-08-18 22:50:32 +00009940 if (!(Op.isReg() && Op.isImplicit()))
9941 MIB.addOperand(Op);
9942 }
Eric Christopher41c902f2010-11-30 08:20:21 +00009943 BuildMI(*BB, MI, dl, TII->get(X86::MOVAPSrr), MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +00009944 .addReg(X86::XMM0);
9945
Dan Gohman14152b42010-07-06 20:24:04 +00009946 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +00009947 return BB;
9948}
9949
9950MachineBasicBlock *
Eric Christopher228232b2010-11-30 07:20:12 +00009951X86TargetLowering::EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +00009952 DebugLoc dl = MI->getDebugLoc();
9953 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009954
Eric Christopher228232b2010-11-30 07:20:12 +00009955 // Address into RAX/EAX, other two args into ECX, EDX.
9956 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
9957 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
9958 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
9959 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +00009960 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009961
Eric Christopher228232b2010-11-30 07:20:12 +00009962 unsigned ValOps = X86::AddrNumOperands;
9963 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
9964 .addReg(MI->getOperand(ValOps).getReg());
9965 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
9966 .addReg(MI->getOperand(ValOps+1).getReg());
9967
9968 // The instruction doesn't actually take any operands though.
9969 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009970
Eric Christopher228232b2010-11-30 07:20:12 +00009971 MI->eraseFromParent(); // The pseudo is gone now.
9972 return BB;
9973}
9974
9975MachineBasicBlock *
9976X86TargetLowering::EmitMwait(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +00009977 DebugLoc dl = MI->getDebugLoc();
9978 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009979
Eric Christopher228232b2010-11-30 07:20:12 +00009980 // First arg in ECX, the second in EAX.
9981 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
9982 .addReg(MI->getOperand(0).getReg());
9983 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EAX)
9984 .addReg(MI->getOperand(1).getReg());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009985
Eric Christopher228232b2010-11-30 07:20:12 +00009986 // The instruction doesn't actually take any operands though.
9987 BuildMI(*BB, MI, dl, TII->get(X86::MWAITrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009988
Eric Christopher228232b2010-11-30 07:20:12 +00009989 MI->eraseFromParent(); // The pseudo is gone now.
9990 return BB;
9991}
9992
9993MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +00009994X86TargetLowering::EmitVAARG64WithCustomInserter(
9995 MachineInstr *MI,
9996 MachineBasicBlock *MBB) const {
9997 // Emit va_arg instruction on X86-64.
9998
9999 // Operands to this pseudo-instruction:
10000 // 0 ) Output : destination address (reg)
10001 // 1-5) Input : va_list address (addr, i64mem)
10002 // 6 ) ArgSize : Size (in bytes) of vararg type
10003 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
10004 // 8 ) Align : Alignment of type
10005 // 9 ) EFLAGS (implicit-def)
10006
10007 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
10008 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
10009
10010 unsigned DestReg = MI->getOperand(0).getReg();
10011 MachineOperand &Base = MI->getOperand(1);
10012 MachineOperand &Scale = MI->getOperand(2);
10013 MachineOperand &Index = MI->getOperand(3);
10014 MachineOperand &Disp = MI->getOperand(4);
10015 MachineOperand &Segment = MI->getOperand(5);
10016 unsigned ArgSize = MI->getOperand(6).getImm();
10017 unsigned ArgMode = MI->getOperand(7).getImm();
10018 unsigned Align = MI->getOperand(8).getImm();
10019
10020 // Memory Reference
10021 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
10022 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
10023 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
10024
10025 // Machine Information
10026 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10027 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
10028 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
10029 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
10030 DebugLoc DL = MI->getDebugLoc();
10031
10032 // struct va_list {
10033 // i32 gp_offset
10034 // i32 fp_offset
10035 // i64 overflow_area (address)
10036 // i64 reg_save_area (address)
10037 // }
10038 // sizeof(va_list) = 24
10039 // alignment(va_list) = 8
10040
10041 unsigned TotalNumIntRegs = 6;
10042 unsigned TotalNumXMMRegs = 8;
10043 bool UseGPOffset = (ArgMode == 1);
10044 bool UseFPOffset = (ArgMode == 2);
10045 unsigned MaxOffset = TotalNumIntRegs * 8 +
10046 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
10047
10048 /* Align ArgSize to a multiple of 8 */
10049 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
10050 bool NeedsAlign = (Align > 8);
10051
10052 MachineBasicBlock *thisMBB = MBB;
10053 MachineBasicBlock *overflowMBB;
10054 MachineBasicBlock *offsetMBB;
10055 MachineBasicBlock *endMBB;
10056
10057 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
10058 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
10059 unsigned OffsetReg = 0;
10060
10061 if (!UseGPOffset && !UseFPOffset) {
10062 // If we only pull from the overflow region, we don't create a branch.
10063 // We don't need to alter control flow.
10064 OffsetDestReg = 0; // unused
10065 OverflowDestReg = DestReg;
10066
10067 offsetMBB = NULL;
10068 overflowMBB = thisMBB;
10069 endMBB = thisMBB;
10070 } else {
10071 // First emit code to check if gp_offset (or fp_offset) is below the bound.
10072 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
10073 // If not, pull from overflow_area. (branch to overflowMBB)
10074 //
10075 // thisMBB
10076 // | .
10077 // | .
10078 // offsetMBB overflowMBB
10079 // | .
10080 // | .
10081 // endMBB
10082
10083 // Registers for the PHI in endMBB
10084 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
10085 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
10086
10087 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
10088 MachineFunction *MF = MBB->getParent();
10089 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
10090 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
10091 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
10092
10093 MachineFunction::iterator MBBIter = MBB;
10094 ++MBBIter;
10095
10096 // Insert the new basic blocks
10097 MF->insert(MBBIter, offsetMBB);
10098 MF->insert(MBBIter, overflowMBB);
10099 MF->insert(MBBIter, endMBB);
10100
10101 // Transfer the remainder of MBB and its successor edges to endMBB.
10102 endMBB->splice(endMBB->begin(), thisMBB,
10103 llvm::next(MachineBasicBlock::iterator(MI)),
10104 thisMBB->end());
10105 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
10106
10107 // Make offsetMBB and overflowMBB successors of thisMBB
10108 thisMBB->addSuccessor(offsetMBB);
10109 thisMBB->addSuccessor(overflowMBB);
10110
10111 // endMBB is a successor of both offsetMBB and overflowMBB
10112 offsetMBB->addSuccessor(endMBB);
10113 overflowMBB->addSuccessor(endMBB);
10114
10115 // Load the offset value into a register
10116 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
10117 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
10118 .addOperand(Base)
10119 .addOperand(Scale)
10120 .addOperand(Index)
10121 .addDisp(Disp, UseFPOffset ? 4 : 0)
10122 .addOperand(Segment)
10123 .setMemRefs(MMOBegin, MMOEnd);
10124
10125 // Check if there is enough room left to pull this argument.
10126 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
10127 .addReg(OffsetReg)
10128 .addImm(MaxOffset + 8 - ArgSizeA8);
10129
10130 // Branch to "overflowMBB" if offset >= max
10131 // Fall through to "offsetMBB" otherwise
10132 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
10133 .addMBB(overflowMBB);
10134 }
10135
10136 // In offsetMBB, emit code to use the reg_save_area.
10137 if (offsetMBB) {
10138 assert(OffsetReg != 0);
10139
10140 // Read the reg_save_area address.
10141 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
10142 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
10143 .addOperand(Base)
10144 .addOperand(Scale)
10145 .addOperand(Index)
10146 .addDisp(Disp, 16)
10147 .addOperand(Segment)
10148 .setMemRefs(MMOBegin, MMOEnd);
10149
10150 // Zero-extend the offset
10151 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
10152 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
10153 .addImm(0)
10154 .addReg(OffsetReg)
10155 .addImm(X86::sub_32bit);
10156
10157 // Add the offset to the reg_save_area to get the final address.
10158 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
10159 .addReg(OffsetReg64)
10160 .addReg(RegSaveReg);
10161
10162 // Compute the offset for the next argument
10163 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
10164 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
10165 .addReg(OffsetReg)
10166 .addImm(UseFPOffset ? 16 : 8);
10167
10168 // Store it back into the va_list.
10169 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
10170 .addOperand(Base)
10171 .addOperand(Scale)
10172 .addOperand(Index)
10173 .addDisp(Disp, UseFPOffset ? 4 : 0)
10174 .addOperand(Segment)
10175 .addReg(NextOffsetReg)
10176 .setMemRefs(MMOBegin, MMOEnd);
10177
10178 // Jump to endMBB
10179 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
10180 .addMBB(endMBB);
10181 }
10182
10183 //
10184 // Emit code to use overflow area
10185 //
10186
10187 // Load the overflow_area address into a register.
10188 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
10189 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
10190 .addOperand(Base)
10191 .addOperand(Scale)
10192 .addOperand(Index)
10193 .addDisp(Disp, 8)
10194 .addOperand(Segment)
10195 .setMemRefs(MMOBegin, MMOEnd);
10196
10197 // If we need to align it, do so. Otherwise, just copy the address
10198 // to OverflowDestReg.
10199 if (NeedsAlign) {
10200 // Align the overflow address
10201 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
10202 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
10203
10204 // aligned_addr = (addr + (align-1)) & ~(align-1)
10205 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
10206 .addReg(OverflowAddrReg)
10207 .addImm(Align-1);
10208
10209 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
10210 .addReg(TmpReg)
10211 .addImm(~(uint64_t)(Align-1));
10212 } else {
10213 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
10214 .addReg(OverflowAddrReg);
10215 }
10216
10217 // Compute the next overflow address after this argument.
10218 // (the overflow address should be kept 8-byte aligned)
10219 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
10220 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
10221 .addReg(OverflowDestReg)
10222 .addImm(ArgSizeA8);
10223
10224 // Store the new overflow address.
10225 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
10226 .addOperand(Base)
10227 .addOperand(Scale)
10228 .addOperand(Index)
10229 .addDisp(Disp, 8)
10230 .addOperand(Segment)
10231 .addReg(NextAddrReg)
10232 .setMemRefs(MMOBegin, MMOEnd);
10233
10234 // If we branched, emit the PHI to the front of endMBB.
10235 if (offsetMBB) {
10236 BuildMI(*endMBB, endMBB->begin(), DL,
10237 TII->get(X86::PHI), DestReg)
10238 .addReg(OffsetDestReg).addMBB(offsetMBB)
10239 .addReg(OverflowDestReg).addMBB(overflowMBB);
10240 }
10241
10242 // Erase the pseudo instruction
10243 MI->eraseFromParent();
10244
10245 return endMBB;
10246}
10247
10248MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +000010249X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
10250 MachineInstr *MI,
10251 MachineBasicBlock *MBB) const {
10252 // Emit code to save XMM registers to the stack. The ABI says that the
10253 // number of registers to save is given in %al, so it's theoretically
10254 // possible to do an indirect jump trick to avoid saving all of them,
10255 // however this code takes a simpler approach and just executes all
10256 // of the stores if %al is non-zero. It's less code, and it's probably
10257 // easier on the hardware branch predictor, and stores aren't all that
10258 // expensive anyway.
10259
10260 // Create the new basic blocks. One block contains all the XMM stores,
10261 // and one block is the final destination regardless of whether any
10262 // stores were performed.
10263 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
10264 MachineFunction *F = MBB->getParent();
10265 MachineFunction::iterator MBBIter = MBB;
10266 ++MBBIter;
10267 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
10268 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
10269 F->insert(MBBIter, XMMSaveMBB);
10270 F->insert(MBBIter, EndMBB);
10271
Dan Gohman14152b42010-07-06 20:24:04 +000010272 // Transfer the remainder of MBB and its successor edges to EndMBB.
10273 EndMBB->splice(EndMBB->begin(), MBB,
10274 llvm::next(MachineBasicBlock::iterator(MI)),
10275 MBB->end());
10276 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
10277
Dan Gohmand6708ea2009-08-15 01:38:56 +000010278 // The original block will now fall through to the XMM save block.
10279 MBB->addSuccessor(XMMSaveMBB);
10280 // The XMMSaveMBB will fall through to the end block.
10281 XMMSaveMBB->addSuccessor(EndMBB);
10282
10283 // Now add the instructions.
10284 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10285 DebugLoc DL = MI->getDebugLoc();
10286
10287 unsigned CountReg = MI->getOperand(0).getReg();
10288 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
10289 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
10290
10291 if (!Subtarget->isTargetWin64()) {
10292 // If %al is 0, branch around the XMM save block.
10293 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +000010294 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010295 MBB->addSuccessor(EndMBB);
10296 }
10297
10298 // In the XMM save block, save all the XMM argument registers.
10299 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
10300 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +000010301 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +000010302 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +000010303 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +000010304 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +000010305 /*Size=*/16, /*Align=*/16);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010306 BuildMI(XMMSaveMBB, DL, TII->get(X86::MOVAPSmr))
10307 .addFrameIndex(RegSaveFrameIndex)
10308 .addImm(/*Scale=*/1)
10309 .addReg(/*IndexReg=*/0)
10310 .addImm(/*Disp=*/Offset)
10311 .addReg(/*Segment=*/0)
10312 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +000010313 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010314 }
10315
Dan Gohman14152b42010-07-06 20:24:04 +000010316 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +000010317
10318 return EndMBB;
10319}
Mon P Wang63307c32008-05-05 19:05:59 +000010320
Evan Cheng60c07e12006-07-05 22:17:51 +000010321MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +000010322X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010323 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +000010324 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10325 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +000010326
Chris Lattner52600972009-09-02 05:57:00 +000010327 // To "insert" a SELECT_CC instruction, we actually have to insert the
10328 // diamond control-flow pattern. The incoming instruction knows the
10329 // destination vreg to set, the condition code register to branch on, the
10330 // true/false values to select between, and a branch opcode to use.
10331 const BasicBlock *LLVM_BB = BB->getBasicBlock();
10332 MachineFunction::iterator It = BB;
10333 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +000010334
Chris Lattner52600972009-09-02 05:57:00 +000010335 // thisMBB:
10336 // ...
10337 // TrueVal = ...
10338 // cmpTY ccX, r1, r2
10339 // bCC copy1MBB
10340 // fallthrough --> copy0MBB
10341 MachineBasicBlock *thisMBB = BB;
10342 MachineFunction *F = BB->getParent();
10343 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
10344 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +000010345 F->insert(It, copy0MBB);
10346 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +000010347
Bill Wendling730c07e2010-06-25 20:48:10 +000010348 // If the EFLAGS register isn't dead in the terminator, then claim that it's
10349 // live into the sink and copy blocks.
10350 const MachineFunction *MF = BB->getParent();
10351 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
10352 BitVector ReservedRegs = TRI->getReservedRegs(*MF);
Bill Wendling730c07e2010-06-25 20:48:10 +000010353
Dan Gohman14152b42010-07-06 20:24:04 +000010354 for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) {
10355 const MachineOperand &MO = MI->getOperand(I);
10356 if (!MO.isReg() || !MO.isUse() || MO.isKill()) continue;
Bill Wendling730c07e2010-06-25 20:48:10 +000010357 unsigned Reg = MO.getReg();
10358 if (Reg != X86::EFLAGS) continue;
10359 copy0MBB->addLiveIn(Reg);
10360 sinkMBB->addLiveIn(Reg);
10361 }
10362
Dan Gohman14152b42010-07-06 20:24:04 +000010363 // Transfer the remainder of BB and its successor edges to sinkMBB.
10364 sinkMBB->splice(sinkMBB->begin(), BB,
10365 llvm::next(MachineBasicBlock::iterator(MI)),
10366 BB->end());
10367 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
10368
10369 // Add the true and fallthrough blocks as its successors.
10370 BB->addSuccessor(copy0MBB);
10371 BB->addSuccessor(sinkMBB);
10372
10373 // Create the conditional branch instruction.
10374 unsigned Opc =
10375 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
10376 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
10377
Chris Lattner52600972009-09-02 05:57:00 +000010378 // copy0MBB:
10379 // %FalseValue = ...
10380 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000010381 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000010382
Chris Lattner52600972009-09-02 05:57:00 +000010383 // sinkMBB:
10384 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
10385 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000010386 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
10387 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000010388 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
10389 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
10390
Dan Gohman14152b42010-07-06 20:24:04 +000010391 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000010392 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000010393}
10394
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010395MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010396X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010397 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010398 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10399 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010400
10401 // The lowering is pretty easy: we're just emitting the call to _alloca. The
10402 // non-trivial part is impdef of ESP.
10403 // FIXME: The code should be tweaked as soon as we'll try to do codegen for
10404 // mingw-w64.
10405
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010406 const char *StackProbeSymbol =
10407 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
10408
Dan Gohman14152b42010-07-06 20:24:04 +000010409 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010410 .addExternalSymbol(StackProbeSymbol)
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010411 .addReg(X86::EAX, RegState::Implicit)
10412 .addReg(X86::ESP, RegState::Implicit)
10413 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
Anton Korobeynikov9f7f83b2010-08-25 07:50:11 +000010414 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
10415 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010416
Dan Gohman14152b42010-07-06 20:24:04 +000010417 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010418 return BB;
10419}
Chris Lattner52600972009-09-02 05:57:00 +000010420
10421MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000010422X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
10423 MachineBasicBlock *BB) const {
10424 // This is pretty easy. We're taking the value that we received from
10425 // our load from the relocation, sticking it in either RDI (x86-64)
10426 // or EAX and doing an indirect call. The return value will then
10427 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000010428 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000010429 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000010430 DebugLoc DL = MI->getDebugLoc();
10431 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000010432
10433 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000010434 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000010435
Eric Christopher30ef0e52010-06-03 04:07:48 +000010436 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000010437 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10438 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000010439 .addReg(X86::RIP)
10440 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010441 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000010442 MI->getOperand(3).getTargetFlags())
10443 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000010444 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000010445 addDirectMem(MIB, X86::RDI);
Eric Christopher61025492010-06-15 23:08:42 +000010446 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000010447 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10448 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000010449 .addReg(0)
10450 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010451 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000010452 MI->getOperand(3).getTargetFlags())
10453 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000010454 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000010455 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010456 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000010457 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10458 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000010459 .addReg(TII->getGlobalBaseReg(F))
10460 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010461 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000010462 MI->getOperand(3).getTargetFlags())
10463 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000010464 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000010465 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010466 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000010467
Dan Gohman14152b42010-07-06 20:24:04 +000010468 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000010469 return BB;
10470}
10471
10472MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000010473X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010474 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000010475 switch (MI->getOpcode()) {
10476 default: assert(false && "Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000010477 case X86::TAILJMPd64:
10478 case X86::TAILJMPr64:
10479 case X86::TAILJMPm64:
10480 assert(!"TAILJMP64 would not be touched here.");
10481 case X86::TCRETURNdi64:
10482 case X86::TCRETURNri64:
10483 case X86::TCRETURNmi64:
10484 // Defs of TCRETURNxx64 has Win64's callee-saved registers, as subset.
10485 // On AMD64, additional defs should be added before register allocation.
10486 if (!Subtarget->isTargetWin64()) {
10487 MI->addRegisterDefined(X86::RSI);
10488 MI->addRegisterDefined(X86::RDI);
10489 MI->addRegisterDefined(X86::XMM6);
10490 MI->addRegisterDefined(X86::XMM7);
10491 MI->addRegisterDefined(X86::XMM8);
10492 MI->addRegisterDefined(X86::XMM9);
10493 MI->addRegisterDefined(X86::XMM10);
10494 MI->addRegisterDefined(X86::XMM11);
10495 MI->addRegisterDefined(X86::XMM12);
10496 MI->addRegisterDefined(X86::XMM13);
10497 MI->addRegisterDefined(X86::XMM14);
10498 MI->addRegisterDefined(X86::XMM15);
10499 }
10500 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010501 case X86::WIN_ALLOCA:
10502 return EmitLoweredWinAlloca(MI, BB);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010503 case X86::TLSCall_32:
10504 case X86::TLSCall_64:
10505 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000010506 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000010507 case X86::CMOV_FR32:
10508 case X86::CMOV_FR64:
10509 case X86::CMOV_V4F32:
10510 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000010511 case X86::CMOV_V2I64:
Chris Lattner314a1132010-03-14 18:31:44 +000010512 case X86::CMOV_GR16:
10513 case X86::CMOV_GR32:
10514 case X86::CMOV_RFP32:
10515 case X86::CMOV_RFP64:
10516 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010517 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000010518
Dale Johannesen849f2142007-07-03 00:53:03 +000010519 case X86::FP32_TO_INT16_IN_MEM:
10520 case X86::FP32_TO_INT32_IN_MEM:
10521 case X86::FP32_TO_INT64_IN_MEM:
10522 case X86::FP64_TO_INT16_IN_MEM:
10523 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000010524 case X86::FP64_TO_INT64_IN_MEM:
10525 case X86::FP80_TO_INT16_IN_MEM:
10526 case X86::FP80_TO_INT32_IN_MEM:
10527 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000010528 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10529 DebugLoc DL = MI->getDebugLoc();
10530
Evan Cheng60c07e12006-07-05 22:17:51 +000010531 // Change the floating point control register to use "round towards zero"
10532 // mode when truncating to an integer value.
10533 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000010534 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000010535 addFrameReference(BuildMI(*BB, MI, DL,
10536 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010537
10538 // Load the old value of the high byte of the control word...
10539 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +000010540 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohman14152b42010-07-06 20:24:04 +000010541 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000010542 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010543
10544 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000010545 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000010546 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000010547
10548 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000010549 addFrameReference(BuildMI(*BB, MI, DL,
10550 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010551
10552 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000010553 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000010554 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000010555
10556 // Get the X86 opcode to use.
10557 unsigned Opc;
10558 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010559 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000010560 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
10561 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
10562 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
10563 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
10564 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
10565 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000010566 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
10567 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
10568 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000010569 }
10570
10571 X86AddressMode AM;
10572 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000010573 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000010574 AM.BaseType = X86AddressMode::RegBase;
10575 AM.Base.Reg = Op.getReg();
10576 } else {
10577 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000010578 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000010579 }
10580 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000010581 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000010582 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010583 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000010584 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000010585 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010586 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000010587 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000010588 AM.GV = Op.getGlobal();
10589 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000010590 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010591 }
Dan Gohman14152b42010-07-06 20:24:04 +000010592 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010593 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000010594
10595 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000010596 addFrameReference(BuildMI(*BB, MI, DL,
10597 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010598
Dan Gohman14152b42010-07-06 20:24:04 +000010599 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000010600 return BB;
10601 }
Eric Christopherb120ab42009-08-18 22:50:32 +000010602 // String/text processing lowering.
10603 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010604 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000010605 return EmitPCMP(MI, BB, 3, false /* in-mem */);
10606 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010607 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000010608 return EmitPCMP(MI, BB, 3, true /* in-mem */);
10609 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010610 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000010611 return EmitPCMP(MI, BB, 5, false /* in mem */);
10612 case X86::PCMPESTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010613 case X86::VPCMPESTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000010614 return EmitPCMP(MI, BB, 5, true /* in mem */);
10615
Eric Christopher228232b2010-11-30 07:20:12 +000010616 // Thread synchronization.
10617 case X86::MONITOR:
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010618 return EmitMonitor(MI, BB);
Eric Christopher228232b2010-11-30 07:20:12 +000010619 case X86::MWAIT:
10620 return EmitMwait(MI, BB);
10621
Eric Christopherb120ab42009-08-18 22:50:32 +000010622 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +000010623 case X86::ATOMAND32:
10624 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010625 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010626 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010627 X86::NOT32r, X86::EAX,
10628 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000010629 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +000010630 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
10631 X86::OR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010632 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010633 X86::NOT32r, X86::EAX,
10634 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000010635 case X86::ATOMXOR32:
10636 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010637 X86::XOR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010638 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010639 X86::NOT32r, X86::EAX,
10640 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010641 case X86::ATOMNAND32:
10642 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010643 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010644 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010645 X86::NOT32r, X86::EAX,
10646 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +000010647 case X86::ATOMMIN32:
10648 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
10649 case X86::ATOMMAX32:
10650 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
10651 case X86::ATOMUMIN32:
10652 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
10653 case X86::ATOMUMAX32:
10654 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +000010655
10656 case X86::ATOMAND16:
10657 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
10658 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010659 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010660 X86::NOT16r, X86::AX,
10661 X86::GR16RegisterClass);
10662 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +000010663 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010664 X86::OR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010665 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010666 X86::NOT16r, X86::AX,
10667 X86::GR16RegisterClass);
10668 case X86::ATOMXOR16:
10669 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
10670 X86::XOR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010671 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010672 X86::NOT16r, X86::AX,
10673 X86::GR16RegisterClass);
10674 case X86::ATOMNAND16:
10675 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
10676 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010677 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010678 X86::NOT16r, X86::AX,
10679 X86::GR16RegisterClass, true);
10680 case X86::ATOMMIN16:
10681 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
10682 case X86::ATOMMAX16:
10683 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
10684 case X86::ATOMUMIN16:
10685 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
10686 case X86::ATOMUMAX16:
10687 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
10688
10689 case X86::ATOMAND8:
10690 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
10691 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010692 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010693 X86::NOT8r, X86::AL,
10694 X86::GR8RegisterClass);
10695 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +000010696 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010697 X86::OR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010698 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010699 X86::NOT8r, X86::AL,
10700 X86::GR8RegisterClass);
10701 case X86::ATOMXOR8:
10702 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
10703 X86::XOR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010704 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010705 X86::NOT8r, X86::AL,
10706 X86::GR8RegisterClass);
10707 case X86::ATOMNAND8:
10708 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
10709 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010710 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010711 X86::NOT8r, X86::AL,
10712 X86::GR8RegisterClass, true);
10713 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010714 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +000010715 case X86::ATOMAND64:
10716 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010717 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010718 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010719 X86::NOT64r, X86::RAX,
10720 X86::GR64RegisterClass);
10721 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +000010722 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
10723 X86::OR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010724 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010725 X86::NOT64r, X86::RAX,
10726 X86::GR64RegisterClass);
10727 case X86::ATOMXOR64:
10728 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010729 X86::XOR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010730 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010731 X86::NOT64r, X86::RAX,
10732 X86::GR64RegisterClass);
10733 case X86::ATOMNAND64:
10734 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
10735 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010736 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010737 X86::NOT64r, X86::RAX,
10738 X86::GR64RegisterClass, true);
10739 case X86::ATOMMIN64:
10740 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
10741 case X86::ATOMMAX64:
10742 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
10743 case X86::ATOMUMIN64:
10744 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
10745 case X86::ATOMUMAX64:
10746 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010747
10748 // This group does 64-bit operations on a 32-bit host.
10749 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010750 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010751 X86::AND32rr, X86::AND32rr,
10752 X86::AND32ri, X86::AND32ri,
10753 false);
10754 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010755 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010756 X86::OR32rr, X86::OR32rr,
10757 X86::OR32ri, X86::OR32ri,
10758 false);
10759 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010760 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010761 X86::XOR32rr, X86::XOR32rr,
10762 X86::XOR32ri, X86::XOR32ri,
10763 false);
10764 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010765 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010766 X86::AND32rr, X86::AND32rr,
10767 X86::AND32ri, X86::AND32ri,
10768 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010769 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010770 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010771 X86::ADD32rr, X86::ADC32rr,
10772 X86::ADD32ri, X86::ADC32ri,
10773 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010774 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010775 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010776 X86::SUB32rr, X86::SBB32rr,
10777 X86::SUB32ri, X86::SBB32ri,
10778 false);
Dale Johannesen880ae362008-10-03 22:25:52 +000010779 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010780 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +000010781 X86::MOV32rr, X86::MOV32rr,
10782 X86::MOV32ri, X86::MOV32ri,
10783 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010784 case X86::VASTART_SAVE_XMM_REGS:
10785 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000010786
10787 case X86::VAARG_64:
10788 return EmitVAARG64WithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000010789 }
10790}
10791
10792//===----------------------------------------------------------------------===//
10793// X86 Optimization Hooks
10794//===----------------------------------------------------------------------===//
10795
Dan Gohman475871a2008-07-27 21:46:04 +000010796void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +000010797 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000010798 APInt &KnownZero,
10799 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000010800 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000010801 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010802 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000010803 assert((Opc >= ISD::BUILTIN_OP_END ||
10804 Opc == ISD::INTRINSIC_WO_CHAIN ||
10805 Opc == ISD::INTRINSIC_W_CHAIN ||
10806 Opc == ISD::INTRINSIC_VOID) &&
10807 "Should use MaskedValueIsZero if you don't know whether Op"
10808 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010809
Dan Gohmanf4f92f52008-02-13 23:07:24 +000010810 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010811 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000010812 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000010813 case X86ISD::ADD:
10814 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000010815 case X86ISD::ADC:
10816 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000010817 case X86ISD::SMUL:
10818 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000010819 case X86ISD::INC:
10820 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000010821 case X86ISD::OR:
10822 case X86ISD::XOR:
10823 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000010824 // These nodes' second result is a boolean.
10825 if (Op.getResNo() == 0)
10826 break;
10827 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010828 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000010829 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
10830 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000010831 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010832 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010833}
Chris Lattner259e97c2006-01-31 19:43:35 +000010834
Owen Andersonbc146b02010-09-21 20:42:50 +000010835unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
10836 unsigned Depth) const {
10837 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
10838 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
10839 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000010840
Owen Andersonbc146b02010-09-21 20:42:50 +000010841 // Fallback case.
10842 return 1;
10843}
10844
Evan Cheng206ee9d2006-07-07 08:33:52 +000010845/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000010846/// node is a GlobalAddress + offset.
10847bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000010848 const GlobalValue* &GA,
10849 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000010850 if (N->getOpcode() == X86ISD::Wrapper) {
10851 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000010852 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000010853 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000010854 return true;
10855 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000010856 }
Evan Chengad4196b2008-05-12 19:56:52 +000010857 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000010858}
10859
Evan Cheng206ee9d2006-07-07 08:33:52 +000010860/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
10861/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
10862/// if the load addresses are consecutive, non-overlapping, and in the right
Nate Begemanfdea31a2010-03-24 20:49:50 +000010863/// order.
Dan Gohman475871a2008-07-27 21:46:04 +000010864static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Mon P Wanga0fd0d52010-12-19 23:55:53 +000010865 TargetLowering::DAGCombinerInfo &DCI) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000010866 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000010867 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000010868
Eli Friedman7a5e5552009-06-07 06:52:44 +000010869 if (VT.getSizeInBits() != 128)
10870 return SDValue();
10871
Mon P Wanga0fd0d52010-12-19 23:55:53 +000010872 // Don't create instructions with illegal types after legalize types has run.
10873 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10874 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
10875 return SDValue();
10876
Nate Begemanfdea31a2010-03-24 20:49:50 +000010877 SmallVector<SDValue, 16> Elts;
10878 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000010879 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000010880
Nate Begemanfdea31a2010-03-24 20:49:50 +000010881 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000010882}
Evan Chengd880b972008-05-09 21:53:03 +000010883
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000010884/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
10885/// generation and convert it from being a bunch of shuffles and extracts
10886/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010887static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
10888 const TargetLowering &TLI) {
10889 SDValue InputVector = N->getOperand(0);
10890
10891 // Only operate on vectors of 4 elements, where the alternative shuffling
10892 // gets to be more expensive.
10893 if (InputVector.getValueType() != MVT::v4i32)
10894 return SDValue();
10895
10896 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
10897 // single use which is a sign-extend or zero-extend, and all elements are
10898 // used.
10899 SmallVector<SDNode *, 4> Uses;
10900 unsigned ExtractedElements = 0;
10901 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
10902 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
10903 if (UI.getUse().getResNo() != InputVector.getResNo())
10904 return SDValue();
10905
10906 SDNode *Extract = *UI;
10907 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
10908 return SDValue();
10909
10910 if (Extract->getValueType(0) != MVT::i32)
10911 return SDValue();
10912 if (!Extract->hasOneUse())
10913 return SDValue();
10914 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
10915 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
10916 return SDValue();
10917 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
10918 return SDValue();
10919
10920 // Record which element was extracted.
10921 ExtractedElements |=
10922 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
10923
10924 Uses.push_back(Extract);
10925 }
10926
10927 // If not all the elements were used, this may not be worthwhile.
10928 if (ExtractedElements != 15)
10929 return SDValue();
10930
10931 // Ok, we've now decided to do the transformation.
10932 DebugLoc dl = InputVector.getDebugLoc();
10933
10934 // Store the value to a temporary stack slot.
10935 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000010936 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
10937 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010938
10939 // Replace each use (extract) with a load of the appropriate element.
10940 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
10941 UE = Uses.end(); UI != UE; ++UI) {
10942 SDNode *Extract = *UI;
10943
10944 // Compute the element's address.
10945 SDValue Idx = Extract->getOperand(1);
10946 unsigned EltSize =
10947 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
10948 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
10949 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
10950
Eric Christopher90eb4022010-07-22 00:26:08 +000010951 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(),
Chris Lattner51abfe42010-09-21 06:02:19 +000010952 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010953
10954 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000010955 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000010956 ScalarAddr, MachinePointerInfo(),
10957 false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010958
10959 // Replace the exact with the load.
10960 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
10961 }
10962
10963 // The replacement was made in place; don't return anything.
10964 return SDValue();
10965}
10966
Chris Lattner83e6c992006-10-04 06:57:07 +000010967/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000010968static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +000010969 const X86Subtarget *Subtarget) {
10970 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000010971 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000010972 // Get the LHS/RHS of the select.
10973 SDValue LHS = N->getOperand(1);
10974 SDValue RHS = N->getOperand(2);
Eric Christopherfd179292009-08-27 18:07:15 +000010975
Dan Gohman670e5392009-09-21 18:03:22 +000010976 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000010977 // instructions match the semantics of the common C idiom x<y?x:y but not
10978 // x<=y?x:y, because of how they handle negative zero (which can be
10979 // ignored in unsafe-math mode).
Chris Lattner83e6c992006-10-04 06:57:07 +000010980 if (Subtarget->hasSSE2() &&
Owen Anderson825b72b2009-08-11 20:47:22 +000010981 (LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64) &&
Chris Lattner47b4ce82009-03-11 05:48:52 +000010982 Cond.getOpcode() == ISD::SETCC) {
10983 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010984
Chris Lattner47b4ce82009-03-11 05:48:52 +000010985 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000010986 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000010987 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
10988 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000010989 switch (CC) {
10990 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000010991 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000010992 // Converting this to a min would handle NaNs incorrectly, and swapping
10993 // the operands would cause it to handle comparisons between positive
10994 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000010995 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000010996 if (!UnsafeFPMath &&
10997 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
10998 break;
10999 std::swap(LHS, RHS);
11000 }
Dan Gohman670e5392009-09-21 18:03:22 +000011001 Opcode = X86ISD::FMIN;
11002 break;
11003 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000011004 // Converting this to a min would handle comparisons between positive
11005 // and negative zero incorrectly.
11006 if (!UnsafeFPMath &&
11007 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
11008 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011009 Opcode = X86ISD::FMIN;
11010 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000011011 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000011012 // Converting this to a min would handle both negative zeros and NaNs
11013 // incorrectly, but we can swap the operands to fix both.
11014 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000011015 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011016 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000011017 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011018 Opcode = X86ISD::FMIN;
11019 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011020
Dan Gohman670e5392009-09-21 18:03:22 +000011021 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000011022 // Converting this to a max would handle comparisons between positive
11023 // and negative zero incorrectly.
11024 if (!UnsafeFPMath &&
11025 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(LHS))
11026 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011027 Opcode = X86ISD::FMAX;
11028 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000011029 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000011030 // Converting this to a max would handle NaNs incorrectly, and swapping
11031 // the operands would cause it to handle comparisons between positive
11032 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000011033 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000011034 if (!UnsafeFPMath &&
11035 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
11036 break;
11037 std::swap(LHS, RHS);
11038 }
Dan Gohman670e5392009-09-21 18:03:22 +000011039 Opcode = X86ISD::FMAX;
11040 break;
11041 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000011042 // Converting this to a max would handle both negative zeros and NaNs
11043 // incorrectly, but we can swap the operands to fix both.
11044 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000011045 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011046 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011047 case ISD::SETGE:
11048 Opcode = X86ISD::FMAX;
11049 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000011050 }
Dan Gohman670e5392009-09-21 18:03:22 +000011051 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000011052 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
11053 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000011054 switch (CC) {
11055 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000011056 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000011057 // Converting this to a min would handle comparisons between positive
11058 // and negative zero incorrectly, and swapping the operands would
11059 // cause it to handle NaNs incorrectly.
11060 if (!UnsafeFPMath &&
11061 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000011062 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000011063 break;
11064 std::swap(LHS, RHS);
11065 }
Dan Gohman670e5392009-09-21 18:03:22 +000011066 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000011067 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011068 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000011069 // Converting this to a min would handle NaNs incorrectly.
11070 if (!UnsafeFPMath &&
11071 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
11072 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011073 Opcode = X86ISD::FMIN;
11074 break;
11075 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000011076 // Converting this to a min would handle both negative zeros and NaNs
11077 // incorrectly, but we can swap the operands to fix both.
11078 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000011079 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011080 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011081 case ISD::SETGE:
11082 Opcode = X86ISD::FMIN;
11083 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011084
Dan Gohman670e5392009-09-21 18:03:22 +000011085 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000011086 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000011087 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000011088 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011089 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000011090 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011091 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000011092 // Converting this to a max would handle comparisons between positive
11093 // and negative zero incorrectly, and swapping the operands would
11094 // cause it to handle NaNs incorrectly.
11095 if (!UnsafeFPMath &&
11096 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000011097 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000011098 break;
11099 std::swap(LHS, RHS);
11100 }
Dan Gohman670e5392009-09-21 18:03:22 +000011101 Opcode = X86ISD::FMAX;
11102 break;
11103 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000011104 // Converting this to a max would handle both negative zeros and NaNs
11105 // incorrectly, but we can swap the operands to fix both.
11106 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000011107 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011108 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000011109 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011110 Opcode = X86ISD::FMAX;
11111 break;
11112 }
Chris Lattner83e6c992006-10-04 06:57:07 +000011113 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011114
Chris Lattner47b4ce82009-03-11 05:48:52 +000011115 if (Opcode)
11116 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000011117 }
Eric Christopherfd179292009-08-27 18:07:15 +000011118
Chris Lattnerd1980a52009-03-12 06:52:53 +000011119 // If this is a select between two integer constants, try to do some
11120 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000011121 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
11122 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000011123 // Don't do this for crazy integer types.
11124 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
11125 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000011126 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000011127 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000011128
Chris Lattnercee56e72009-03-13 05:53:31 +000011129 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000011130 // Efficiently invertible.
11131 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
11132 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
11133 isa<ConstantSDNode>(Cond.getOperand(1))))) {
11134 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000011135 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000011136 }
Eric Christopherfd179292009-08-27 18:07:15 +000011137
Chris Lattnerd1980a52009-03-12 06:52:53 +000011138 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000011139 if (FalseC->getAPIntValue() == 0 &&
11140 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000011141 if (NeedsCondInvert) // Invert the condition if needed.
11142 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
11143 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011144
Chris Lattnerd1980a52009-03-12 06:52:53 +000011145 // Zero extend the condition if needed.
11146 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011147
Chris Lattnercee56e72009-03-13 05:53:31 +000011148 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000011149 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000011150 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000011151 }
Eric Christopherfd179292009-08-27 18:07:15 +000011152
Chris Lattner97a29a52009-03-13 05:22:11 +000011153 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000011154 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000011155 if (NeedsCondInvert) // Invert the condition if needed.
11156 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
11157 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011158
Chris Lattner97a29a52009-03-13 05:22:11 +000011159 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000011160 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
11161 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000011162 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000011163 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000011164 }
Eric Christopherfd179292009-08-27 18:07:15 +000011165
Chris Lattnercee56e72009-03-13 05:53:31 +000011166 // Optimize cases that will turn into an LEA instruction. This requires
11167 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000011168 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000011169 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000011170 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000011171
Chris Lattnercee56e72009-03-13 05:53:31 +000011172 bool isFastMultiplier = false;
11173 if (Diff < 10) {
11174 switch ((unsigned char)Diff) {
11175 default: break;
11176 case 1: // result = add base, cond
11177 case 2: // result = lea base( , cond*2)
11178 case 3: // result = lea base(cond, cond*2)
11179 case 4: // result = lea base( , cond*4)
11180 case 5: // result = lea base(cond, cond*4)
11181 case 8: // result = lea base( , cond*8)
11182 case 9: // result = lea base(cond, cond*8)
11183 isFastMultiplier = true;
11184 break;
11185 }
11186 }
Eric Christopherfd179292009-08-27 18:07:15 +000011187
Chris Lattnercee56e72009-03-13 05:53:31 +000011188 if (isFastMultiplier) {
11189 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
11190 if (NeedsCondInvert) // Invert the condition if needed.
11191 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
11192 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011193
Chris Lattnercee56e72009-03-13 05:53:31 +000011194 // Zero extend the condition if needed.
11195 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
11196 Cond);
11197 // Scale the condition by the difference.
11198 if (Diff != 1)
11199 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
11200 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011201
Chris Lattnercee56e72009-03-13 05:53:31 +000011202 // Add the base if non-zero.
11203 if (FalseC->getAPIntValue() != 0)
11204 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
11205 SDValue(FalseC, 0));
11206 return Cond;
11207 }
Eric Christopherfd179292009-08-27 18:07:15 +000011208 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000011209 }
11210 }
Eric Christopherfd179292009-08-27 18:07:15 +000011211
Dan Gohman475871a2008-07-27 21:46:04 +000011212 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000011213}
11214
Chris Lattnerd1980a52009-03-12 06:52:53 +000011215/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
11216static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
11217 TargetLowering::DAGCombinerInfo &DCI) {
11218 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000011219
Chris Lattnerd1980a52009-03-12 06:52:53 +000011220 // If the flag operand isn't dead, don't touch this CMOV.
11221 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
11222 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000011223
Chris Lattnerd1980a52009-03-12 06:52:53 +000011224 // If this is a select between two integer constants, try to do some
11225 // optimizations. Note that the operands are ordered the opposite of SELECT
11226 // operands.
11227 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
11228 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
11229 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
11230 // larger than FalseC (the false value).
11231 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
Eric Christopherfd179292009-08-27 18:07:15 +000011232
Chris Lattnerd1980a52009-03-12 06:52:53 +000011233 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
11234 CC = X86::GetOppositeBranchCondition(CC);
11235 std::swap(TrueC, FalseC);
11236 }
Eric Christopherfd179292009-08-27 18:07:15 +000011237
Chris Lattnerd1980a52009-03-12 06:52:53 +000011238 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000011239 // This is efficient for any integer data type (including i8/i16) and
11240 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000011241 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
11242 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +000011243 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11244 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011245
Chris Lattnerd1980a52009-03-12 06:52:53 +000011246 // Zero extend the condition if needed.
11247 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011248
Chris Lattnerd1980a52009-03-12 06:52:53 +000011249 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
11250 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000011251 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000011252 if (N->getNumValues() == 2) // Dead flag value?
11253 return DCI.CombineTo(N, Cond, SDValue());
11254 return Cond;
11255 }
Eric Christopherfd179292009-08-27 18:07:15 +000011256
Chris Lattnercee56e72009-03-13 05:53:31 +000011257 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
11258 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000011259 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
11260 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +000011261 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11262 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011263
Chris Lattner97a29a52009-03-13 05:22:11 +000011264 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000011265 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
11266 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000011267 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
11268 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000011269
Chris Lattner97a29a52009-03-13 05:22:11 +000011270 if (N->getNumValues() == 2) // Dead flag value?
11271 return DCI.CombineTo(N, Cond, SDValue());
11272 return Cond;
11273 }
Eric Christopherfd179292009-08-27 18:07:15 +000011274
Chris Lattnercee56e72009-03-13 05:53:31 +000011275 // Optimize cases that will turn into an LEA instruction. This requires
11276 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000011277 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000011278 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000011279 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000011280
Chris Lattnercee56e72009-03-13 05:53:31 +000011281 bool isFastMultiplier = false;
11282 if (Diff < 10) {
11283 switch ((unsigned char)Diff) {
11284 default: break;
11285 case 1: // result = add base, cond
11286 case 2: // result = lea base( , cond*2)
11287 case 3: // result = lea base(cond, cond*2)
11288 case 4: // result = lea base( , cond*4)
11289 case 5: // result = lea base(cond, cond*4)
11290 case 8: // result = lea base( , cond*8)
11291 case 9: // result = lea base(cond, cond*8)
11292 isFastMultiplier = true;
11293 break;
11294 }
11295 }
Eric Christopherfd179292009-08-27 18:07:15 +000011296
Chris Lattnercee56e72009-03-13 05:53:31 +000011297 if (isFastMultiplier) {
11298 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
11299 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +000011300 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11301 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000011302 // Zero extend the condition if needed.
11303 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
11304 Cond);
11305 // Scale the condition by the difference.
11306 if (Diff != 1)
11307 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
11308 DAG.getConstant(Diff, Cond.getValueType()));
11309
11310 // Add the base if non-zero.
11311 if (FalseC->getAPIntValue() != 0)
11312 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
11313 SDValue(FalseC, 0));
11314 if (N->getNumValues() == 2) // Dead flag value?
11315 return DCI.CombineTo(N, Cond, SDValue());
11316 return Cond;
11317 }
Eric Christopherfd179292009-08-27 18:07:15 +000011318 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000011319 }
11320 }
11321 return SDValue();
11322}
11323
11324
Evan Cheng0b0cd912009-03-28 05:57:29 +000011325/// PerformMulCombine - Optimize a single multiply with constant into two
11326/// in order to implement it with two cheaper instructions, e.g.
11327/// LEA + SHL, LEA + LEA.
11328static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
11329 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000011330 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
11331 return SDValue();
11332
Owen Andersone50ed302009-08-10 22:56:29 +000011333 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000011334 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000011335 return SDValue();
11336
11337 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
11338 if (!C)
11339 return SDValue();
11340 uint64_t MulAmt = C->getZExtValue();
11341 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
11342 return SDValue();
11343
11344 uint64_t MulAmt1 = 0;
11345 uint64_t MulAmt2 = 0;
11346 if ((MulAmt % 9) == 0) {
11347 MulAmt1 = 9;
11348 MulAmt2 = MulAmt / 9;
11349 } else if ((MulAmt % 5) == 0) {
11350 MulAmt1 = 5;
11351 MulAmt2 = MulAmt / 5;
11352 } else if ((MulAmt % 3) == 0) {
11353 MulAmt1 = 3;
11354 MulAmt2 = MulAmt / 3;
11355 }
11356 if (MulAmt2 &&
11357 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
11358 DebugLoc DL = N->getDebugLoc();
11359
11360 if (isPowerOf2_64(MulAmt2) &&
11361 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
11362 // If second multiplifer is pow2, issue it first. We want the multiply by
11363 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
11364 // is an add.
11365 std::swap(MulAmt1, MulAmt2);
11366
11367 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000011368 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000011369 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000011370 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000011371 else
Evan Cheng73f24c92009-03-30 21:36:47 +000011372 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000011373 DAG.getConstant(MulAmt1, VT));
11374
Eric Christopherfd179292009-08-27 18:07:15 +000011375 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000011376 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000011377 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000011378 else
Evan Cheng73f24c92009-03-30 21:36:47 +000011379 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000011380 DAG.getConstant(MulAmt2, VT));
11381
11382 // Do not add new nodes to DAG combiner worklist.
11383 DCI.CombineTo(N, NewMul, false);
11384 }
11385 return SDValue();
11386}
11387
Evan Chengad9c0a32009-12-15 00:53:42 +000011388static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
11389 SDValue N0 = N->getOperand(0);
11390 SDValue N1 = N->getOperand(1);
11391 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
11392 EVT VT = N0.getValueType();
11393
11394 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
11395 // since the result of setcc_c is all zero's or all ones.
11396 if (N1C && N0.getOpcode() == ISD::AND &&
11397 N0.getOperand(1).getOpcode() == ISD::Constant) {
11398 SDValue N00 = N0.getOperand(0);
11399 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
11400 ((N00.getOpcode() == ISD::ANY_EXTEND ||
11401 N00.getOpcode() == ISD::ZERO_EXTEND) &&
11402 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
11403 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
11404 APInt ShAmt = N1C->getAPIntValue();
11405 Mask = Mask.shl(ShAmt);
11406 if (Mask != 0)
11407 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
11408 N00, DAG.getConstant(Mask, VT));
11409 }
11410 }
11411
11412 return SDValue();
11413}
Evan Cheng0b0cd912009-03-28 05:57:29 +000011414
Nate Begeman740ab032009-01-26 00:52:55 +000011415/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
11416/// when possible.
11417static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
11418 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000011419 EVT VT = N->getValueType(0);
11420 if (!VT.isVector() && VT.isInteger() &&
11421 N->getOpcode() == ISD::SHL)
11422 return PerformSHLCombine(N, DAG);
11423
Nate Begeman740ab032009-01-26 00:52:55 +000011424 // On X86 with SSE2 support, we can transform this to a vector shift if
11425 // all elements are shifted by the same amount. We can't do this in legalize
11426 // because the a constant vector is typically transformed to a constant pool
11427 // so we have no knowledge of the shift amount.
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011428 if (!Subtarget->hasSSE2())
11429 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000011430
Owen Anderson825b72b2009-08-11 20:47:22 +000011431 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011432 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000011433
Mon P Wang3becd092009-01-28 08:12:05 +000011434 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000011435 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000011436 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000011437 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000011438 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
11439 unsigned NumElts = VT.getVectorNumElements();
11440 unsigned i = 0;
11441 for (; i != NumElts; ++i) {
11442 SDValue Arg = ShAmtOp.getOperand(i);
11443 if (Arg.getOpcode() == ISD::UNDEF) continue;
11444 BaseShAmt = Arg;
11445 break;
11446 }
11447 for (; i != NumElts; ++i) {
11448 SDValue Arg = ShAmtOp.getOperand(i);
11449 if (Arg.getOpcode() == ISD::UNDEF) continue;
11450 if (Arg != BaseShAmt) {
11451 return SDValue();
11452 }
11453 }
11454 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000011455 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000011456 SDValue InVec = ShAmtOp.getOperand(0);
11457 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
11458 unsigned NumElts = InVec.getValueType().getVectorNumElements();
11459 unsigned i = 0;
11460 for (; i != NumElts; ++i) {
11461 SDValue Arg = InVec.getOperand(i);
11462 if (Arg.getOpcode() == ISD::UNDEF) continue;
11463 BaseShAmt = Arg;
11464 break;
11465 }
11466 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
11467 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000011468 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000011469 if (C->getZExtValue() == SplatIdx)
11470 BaseShAmt = InVec.getOperand(1);
11471 }
11472 }
11473 if (BaseShAmt.getNode() == 0)
11474 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
11475 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +000011476 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011477 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000011478
Mon P Wangefa42202009-09-03 19:56:25 +000011479 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000011480 if (EltVT.bitsGT(MVT::i32))
11481 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
11482 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000011483 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000011484
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011485 // The shift amount is identical so we can do a vector shift.
11486 SDValue ValOp = N->getOperand(0);
11487 switch (N->getOpcode()) {
11488 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000011489 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011490 break;
11491 case ISD::SHL:
Owen Anderson825b72b2009-08-11 20:47:22 +000011492 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011493 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011494 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011495 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011496 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011497 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011498 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011499 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011500 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011501 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011502 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011503 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011504 break;
11505 case ISD::SRA:
Owen Anderson825b72b2009-08-11 20:47:22 +000011506 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011507 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011508 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011509 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011510 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011511 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011512 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011513 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011514 break;
11515 case ISD::SRL:
Owen Anderson825b72b2009-08-11 20:47:22 +000011516 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011517 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011518 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011519 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011520 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011521 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011522 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011523 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011524 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011525 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011526 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011527 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011528 break;
Nate Begeman740ab032009-01-26 00:52:55 +000011529 }
11530 return SDValue();
11531}
11532
Nate Begemanb65c1752010-12-17 22:55:37 +000011533
11534static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
11535 TargetLowering::DAGCombinerInfo &DCI,
11536 const X86Subtarget *Subtarget) {
11537 if (DCI.isBeforeLegalizeOps())
11538 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011539
Nate Begemanb65c1752010-12-17 22:55:37 +000011540 // Want to form PANDN nodes, in the hopes of then easily combining them with
11541 // OR and AND nodes to form PBLEND/PSIGN.
11542 EVT VT = N->getValueType(0);
11543 if (VT != MVT::v2i64)
11544 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011545
Nate Begemanb65c1752010-12-17 22:55:37 +000011546 SDValue N0 = N->getOperand(0);
11547 SDValue N1 = N->getOperand(1);
11548 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011549
Nate Begemanb65c1752010-12-17 22:55:37 +000011550 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011551 if (N0.getOpcode() == ISD::XOR &&
Nate Begemanb65c1752010-12-17 22:55:37 +000011552 ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
11553 return DAG.getNode(X86ISD::PANDN, DL, VT, N0.getOperand(0), N1);
11554
11555 // Check RHS for vnot
11556 if (N1.getOpcode() == ISD::XOR &&
11557 ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
11558 return DAG.getNode(X86ISD::PANDN, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011559
Nate Begemanb65c1752010-12-17 22:55:37 +000011560 return SDValue();
11561}
11562
Evan Cheng760d1942010-01-04 21:22:48 +000011563static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000011564 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000011565 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +000011566 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000011567 return SDValue();
11568
Evan Cheng760d1942010-01-04 21:22:48 +000011569 EVT VT = N->getValueType(0);
Nate Begemanb65c1752010-12-17 22:55:37 +000011570 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64 && VT != MVT::v2i64)
Evan Cheng760d1942010-01-04 21:22:48 +000011571 return SDValue();
11572
Evan Cheng760d1942010-01-04 21:22:48 +000011573 SDValue N0 = N->getOperand(0);
11574 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011575
Nate Begemanb65c1752010-12-17 22:55:37 +000011576 // look for psign/blend
11577 if (Subtarget->hasSSSE3()) {
11578 if (VT == MVT::v2i64) {
11579 // Canonicalize pandn to RHS
11580 if (N0.getOpcode() == X86ISD::PANDN)
11581 std::swap(N0, N1);
11582 // or (and (m, x), (pandn m, y))
11583 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::PANDN) {
11584 SDValue Mask = N1.getOperand(0);
11585 SDValue X = N1.getOperand(1);
11586 SDValue Y;
11587 if (N0.getOperand(0) == Mask)
11588 Y = N0.getOperand(1);
11589 if (N0.getOperand(1) == Mask)
11590 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011591
Nate Begemanb65c1752010-12-17 22:55:37 +000011592 // Check to see if the mask appeared in both the AND and PANDN and
11593 if (!Y.getNode())
11594 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011595
Nate Begemanb65c1752010-12-17 22:55:37 +000011596 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
11597 if (Mask.getOpcode() != ISD::BITCAST ||
11598 X.getOpcode() != ISD::BITCAST ||
11599 Y.getOpcode() != ISD::BITCAST)
11600 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011601
Nate Begemanb65c1752010-12-17 22:55:37 +000011602 // Look through mask bitcast.
11603 Mask = Mask.getOperand(0);
11604 EVT MaskVT = Mask.getValueType();
11605
11606 // Validate that the Mask operand is a vector sra node. The sra node
11607 // will be an intrinsic.
11608 if (Mask.getOpcode() != ISD::INTRINSIC_WO_CHAIN)
11609 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011610
Nate Begemanb65c1752010-12-17 22:55:37 +000011611 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
11612 // there is no psrai.b
11613 switch (cast<ConstantSDNode>(Mask.getOperand(0))->getZExtValue()) {
11614 case Intrinsic::x86_sse2_psrai_w:
11615 case Intrinsic::x86_sse2_psrai_d:
11616 break;
11617 default: return SDValue();
11618 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011619
Nate Begemanb65c1752010-12-17 22:55:37 +000011620 // Check that the SRA is all signbits.
11621 SDValue SraC = Mask.getOperand(2);
11622 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
11623 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
11624 if ((SraAmt + 1) != EltBits)
11625 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011626
Nate Begemanb65c1752010-12-17 22:55:37 +000011627 DebugLoc DL = N->getDebugLoc();
11628
11629 // Now we know we at least have a plendvb with the mask val. See if
11630 // we can form a psignb/w/d.
11631 // psign = x.type == y.type == mask.type && y = sub(0, x);
11632 X = X.getOperand(0);
11633 Y = Y.getOperand(0);
11634 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
11635 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
11636 X.getValueType() == MaskVT && X.getValueType() == Y.getValueType()){
11637 unsigned Opc = 0;
11638 switch (EltBits) {
11639 case 8: Opc = X86ISD::PSIGNB; break;
11640 case 16: Opc = X86ISD::PSIGNW; break;
11641 case 32: Opc = X86ISD::PSIGND; break;
11642 default: break;
11643 }
11644 if (Opc) {
11645 SDValue Sign = DAG.getNode(Opc, DL, MaskVT, X, Mask.getOperand(1));
11646 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Sign);
11647 }
11648 }
11649 // PBLENDVB only available on SSE 4.1
11650 if (!Subtarget->hasSSE41())
11651 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011652
Nate Begemanb65c1752010-12-17 22:55:37 +000011653 X = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, X);
11654 Y = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Y);
11655 Mask = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Mask);
Nate Begeman672fb622010-12-20 22:04:24 +000011656 Mask = DAG.getNode(X86ISD::PBLENDVB, DL, MVT::v16i8, X, Y, Mask);
Nate Begemanb65c1752010-12-17 22:55:37 +000011657 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Mask);
11658 }
11659 }
11660 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011661
Nate Begemanb65c1752010-12-17 22:55:37 +000011662 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000011663 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
11664 std::swap(N0, N1);
11665 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
11666 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000011667 if (!N0.hasOneUse() || !N1.hasOneUse())
11668 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000011669
11670 SDValue ShAmt0 = N0.getOperand(1);
11671 if (ShAmt0.getValueType() != MVT::i8)
11672 return SDValue();
11673 SDValue ShAmt1 = N1.getOperand(1);
11674 if (ShAmt1.getValueType() != MVT::i8)
11675 return SDValue();
11676 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
11677 ShAmt0 = ShAmt0.getOperand(0);
11678 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
11679 ShAmt1 = ShAmt1.getOperand(0);
11680
11681 DebugLoc DL = N->getDebugLoc();
11682 unsigned Opc = X86ISD::SHLD;
11683 SDValue Op0 = N0.getOperand(0);
11684 SDValue Op1 = N1.getOperand(0);
11685 if (ShAmt0.getOpcode() == ISD::SUB) {
11686 Opc = X86ISD::SHRD;
11687 std::swap(Op0, Op1);
11688 std::swap(ShAmt0, ShAmt1);
11689 }
11690
Evan Cheng8b1190a2010-04-28 01:18:01 +000011691 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000011692 if (ShAmt1.getOpcode() == ISD::SUB) {
11693 SDValue Sum = ShAmt1.getOperand(0);
11694 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000011695 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
11696 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
11697 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
11698 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000011699 return DAG.getNode(Opc, DL, VT,
11700 Op0, Op1,
11701 DAG.getNode(ISD::TRUNCATE, DL,
11702 MVT::i8, ShAmt0));
11703 }
11704 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
11705 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
11706 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000011707 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000011708 return DAG.getNode(Opc, DL, VT,
11709 N0.getOperand(0), N1.getOperand(0),
11710 DAG.getNode(ISD::TRUNCATE, DL,
11711 MVT::i8, ShAmt0));
11712 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011713
Evan Cheng760d1942010-01-04 21:22:48 +000011714 return SDValue();
11715}
11716
Chris Lattner149a4e52008-02-22 02:09:43 +000011717/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000011718static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000011719 const X86Subtarget *Subtarget) {
Chris Lattner149a4e52008-02-22 02:09:43 +000011720 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
11721 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000011722 // A preferable solution to the general problem is to figure out the right
11723 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000011724
11725 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng7e2ff772008-05-08 00:57:18 +000011726 StoreSDNode *St = cast<StoreSDNode>(N);
Owen Andersone50ed302009-08-10 22:56:29 +000011727 EVT VT = St->getValue().getValueType();
Evan Cheng536e6672009-03-12 05:59:15 +000011728 if (VT.getSizeInBits() != 64)
11729 return SDValue();
11730
Devang Patel578efa92009-06-05 21:57:13 +000011731 const Function *F = DAG.getMachineFunction().getFunction();
11732 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Eric Christopherfd179292009-08-27 18:07:15 +000011733 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
Devang Patel578efa92009-06-05 21:57:13 +000011734 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +000011735 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000011736 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000011737 isa<LoadSDNode>(St->getValue()) &&
11738 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
11739 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000011740 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011741 LoadSDNode *Ld = 0;
11742 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000011743 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000011744 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011745 // Must be a store of a load. We currently handle two cases: the load
11746 // is a direct child, and it's under an intervening TokenFactor. It is
11747 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000011748 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000011749 Ld = cast<LoadSDNode>(St->getChain());
11750 else if (St->getValue().hasOneUse() &&
11751 ChainVal->getOpcode() == ISD::TokenFactor) {
11752 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000011753 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000011754 TokenFactorIndex = i;
11755 Ld = cast<LoadSDNode>(St->getValue());
11756 } else
11757 Ops.push_back(ChainVal->getOperand(i));
11758 }
11759 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000011760
Evan Cheng536e6672009-03-12 05:59:15 +000011761 if (!Ld || !ISD::isNormalLoad(Ld))
11762 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011763
Evan Cheng536e6672009-03-12 05:59:15 +000011764 // If this is not the MMX case, i.e. we are just turning i64 load/store
11765 // into f64 load/store, avoid the transformation if there are multiple
11766 // uses of the loaded value.
11767 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
11768 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011769
Evan Cheng536e6672009-03-12 05:59:15 +000011770 DebugLoc LdDL = Ld->getDebugLoc();
11771 DebugLoc StDL = N->getDebugLoc();
11772 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
11773 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
11774 // pair instead.
11775 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000011776 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000011777 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
11778 Ld->getPointerInfo(), Ld->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000011779 Ld->isNonTemporal(), Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000011780 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000011781 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000011782 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000011783 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000011784 Ops.size());
11785 }
Evan Cheng536e6672009-03-12 05:59:15 +000011786 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000011787 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000011788 St->isVolatile(), St->isNonTemporal(),
11789 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000011790 }
Evan Cheng536e6672009-03-12 05:59:15 +000011791
11792 // Otherwise, lower to two pairs of 32-bit loads / stores.
11793 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000011794 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
11795 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000011796
Owen Anderson825b72b2009-08-11 20:47:22 +000011797 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000011798 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000011799 Ld->isVolatile(), Ld->isNonTemporal(),
11800 Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000011801 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000011802 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000011803 Ld->isVolatile(), Ld->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000011804 MinAlign(Ld->getAlignment(), 4));
11805
11806 SDValue NewChain = LoLd.getValue(1);
11807 if (TokenFactorIndex != -1) {
11808 Ops.push_back(LoLd);
11809 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000011810 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000011811 Ops.size());
11812 }
11813
11814 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000011815 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
11816 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000011817
11818 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000011819 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000011820 St->isVolatile(), St->isNonTemporal(),
11821 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000011822 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000011823 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000011824 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000011825 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000011826 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000011827 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000011828 }
Dan Gohman475871a2008-07-27 21:46:04 +000011829 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000011830}
11831
Chris Lattner6cf73262008-01-25 06:14:17 +000011832/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
11833/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000011834static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000011835 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
11836 // F[X]OR(0.0, x) -> x
11837 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000011838 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
11839 if (C->getValueAPF().isPosZero())
11840 return N->getOperand(1);
11841 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
11842 if (C->getValueAPF().isPosZero())
11843 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000011844 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000011845}
11846
11847/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000011848static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000011849 // FAND(0.0, x) -> 0.0
11850 // FAND(x, 0.0) -> 0.0
11851 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
11852 if (C->getValueAPF().isPosZero())
11853 return N->getOperand(0);
11854 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
11855 if (C->getValueAPF().isPosZero())
11856 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000011857 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000011858}
11859
Dan Gohmane5af2d32009-01-29 01:59:02 +000011860static SDValue PerformBTCombine(SDNode *N,
11861 SelectionDAG &DAG,
11862 TargetLowering::DAGCombinerInfo &DCI) {
11863 // BT ignores high bits in the bit index operand.
11864 SDValue Op1 = N->getOperand(1);
11865 if (Op1.hasOneUse()) {
11866 unsigned BitWidth = Op1.getValueSizeInBits();
11867 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
11868 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000011869 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
11870 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000011871 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000011872 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
11873 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
11874 DCI.CommitTargetLoweringOpt(TLO);
11875 }
11876 return SDValue();
11877}
Chris Lattner83e6c992006-10-04 06:57:07 +000011878
Eli Friedman7a5e5552009-06-07 06:52:44 +000011879static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
11880 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011881 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000011882 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000011883 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000011884 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000011885 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000011886 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011887 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000011888 }
11889 return SDValue();
11890}
11891
Evan Cheng2e489c42009-12-16 00:53:11 +000011892static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
11893 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
11894 // (and (i32 x86isd::setcc_carry), 1)
11895 // This eliminates the zext. This transformation is necessary because
11896 // ISD::SETCC is always legalized to i8.
11897 DebugLoc dl = N->getDebugLoc();
11898 SDValue N0 = N->getOperand(0);
11899 EVT VT = N->getValueType(0);
11900 if (N0.getOpcode() == ISD::AND &&
11901 N0.hasOneUse() &&
11902 N0.getOperand(0).hasOneUse()) {
11903 SDValue N00 = N0.getOperand(0);
11904 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
11905 return SDValue();
11906 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
11907 if (!C || C->getZExtValue() != 1)
11908 return SDValue();
11909 return DAG.getNode(ISD::AND, dl, VT,
11910 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
11911 N00.getOperand(0), N00.getOperand(1)),
11912 DAG.getConstant(1, VT));
11913 }
11914
11915 return SDValue();
11916}
11917
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011918// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
11919static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG) {
11920 unsigned X86CC = N->getConstantOperandVal(0);
11921 SDValue EFLAG = N->getOperand(1);
11922 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011923
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011924 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
11925 // a zext and produces an all-ones bit which is more useful than 0/1 in some
11926 // cases.
11927 if (X86CC == X86::COND_B)
11928 return DAG.getNode(ISD::AND, DL, MVT::i8,
11929 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
11930 DAG.getConstant(X86CC, MVT::i8), EFLAG),
11931 DAG.getConstant(1, MVT::i8));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011932
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011933 return SDValue();
11934}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011935
Chris Lattner23a01992010-12-20 01:37:09 +000011936// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
11937static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
11938 X86TargetLowering::DAGCombinerInfo &DCI) {
11939 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
11940 // the result is either zero or one (depending on the input carry bit).
11941 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
11942 if (X86::isZeroNode(N->getOperand(0)) &&
11943 X86::isZeroNode(N->getOperand(1)) &&
11944 // We don't have a good way to replace an EFLAGS use, so only do this when
11945 // dead right now.
11946 SDValue(N, 1).use_empty()) {
11947 DebugLoc DL = N->getDebugLoc();
11948 EVT VT = N->getValueType(0);
11949 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
11950 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
11951 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
11952 DAG.getConstant(X86::COND_B,MVT::i8),
11953 N->getOperand(2)),
11954 DAG.getConstant(1, VT));
11955 return DCI.CombineTo(N, Res1, CarryOut);
11956 }
11957
11958 return SDValue();
11959}
11960
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000011961// fold (add Y, (sete X, 0)) -> adc 0, Y
11962// (add Y, (setne X, 0)) -> sbb -1, Y
11963// (sub (sete X, 0), Y) -> sbb 0, Y
11964// (sub (setne X, 0), Y) -> adc -1, Y
11965static SDValue OptimizeConditonalInDecrement(SDNode *N, SelectionDAG &DAG) {
11966 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011967
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000011968 // Look through ZExts.
11969 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
11970 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
11971 return SDValue();
11972
11973 SDValue SetCC = Ext.getOperand(0);
11974 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
11975 return SDValue();
11976
11977 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
11978 if (CC != X86::COND_E && CC != X86::COND_NE)
11979 return SDValue();
11980
11981 SDValue Cmp = SetCC.getOperand(1);
11982 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000011983 !X86::isZeroNode(Cmp.getOperand(1)) ||
11984 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000011985 return SDValue();
11986
11987 SDValue CmpOp0 = Cmp.getOperand(0);
11988 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
11989 DAG.getConstant(1, CmpOp0.getValueType()));
11990
11991 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
11992 if (CC == X86::COND_NE)
11993 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
11994 DL, OtherVal.getValueType(), OtherVal,
11995 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
11996 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
11997 DL, OtherVal.getValueType(), OtherVal,
11998 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
11999}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000012000
Dan Gohman475871a2008-07-27 21:46:04 +000012001SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000012002 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000012003 SelectionDAG &DAG = DCI.DAG;
12004 switch (N->getOpcode()) {
12005 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012006 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerc19d1c32010-12-19 22:08:31 +000012007 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, *this);
Chris Lattneraf723b92008-01-25 05:46:26 +000012008 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +000012009 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000012010 case ISD::ADD:
12011 case ISD::SUB: return OptimizeConditonalInDecrement(N, DAG);
Chris Lattner23a01992010-12-20 01:37:09 +000012012 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000012013 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000012014 case ISD::SHL:
12015 case ISD::SRA:
12016 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000012017 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000012018 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000012019 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +000012020 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000012021 case X86ISD::FOR: return PerformFORCombine(N, DAG);
12022 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000012023 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000012024 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Evan Cheng2e489c42009-12-16 00:53:11 +000012025 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG);
Chris Lattnerc19d1c32010-12-19 22:08:31 +000012026 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000012027 case X86ISD::SHUFPS: // Handle all target specific shuffles
12028 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000012029 case X86ISD::PALIGN:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000012030 case X86ISD::PUNPCKHBW:
12031 case X86ISD::PUNPCKHWD:
12032 case X86ISD::PUNPCKHDQ:
12033 case X86ISD::PUNPCKHQDQ:
12034 case X86ISD::UNPCKHPS:
12035 case X86ISD::UNPCKHPD:
12036 case X86ISD::PUNPCKLBW:
12037 case X86ISD::PUNPCKLWD:
12038 case X86ISD::PUNPCKLDQ:
12039 case X86ISD::PUNPCKLQDQ:
12040 case X86ISD::UNPCKLPS:
12041 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +000012042 case X86ISD::VUNPCKLPS:
12043 case X86ISD::VUNPCKLPD:
12044 case X86ISD::VUNPCKLPSY:
12045 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000012046 case X86ISD::MOVHLPS:
12047 case X86ISD::MOVLHPS:
12048 case X86ISD::PSHUFD:
12049 case X86ISD::PSHUFHW:
12050 case X86ISD::PSHUFLW:
12051 case X86ISD::MOVSS:
12052 case X86ISD::MOVSD:
Mon P Wanga0fd0d52010-12-19 23:55:53 +000012053 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI);
Evan Cheng206ee9d2006-07-07 08:33:52 +000012054 }
12055
Dan Gohman475871a2008-07-27 21:46:04 +000012056 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000012057}
12058
Evan Chenge5b51ac2010-04-17 06:13:15 +000012059/// isTypeDesirableForOp - Return true if the target has native support for
12060/// the specified value type and it is 'desirable' to use the type for the
12061/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
12062/// instruction encodings are longer and some i16 instructions are slow.
12063bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
12064 if (!isTypeLegal(VT))
12065 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000012066 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000012067 return true;
12068
12069 switch (Opc) {
12070 default:
12071 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000012072 case ISD::LOAD:
12073 case ISD::SIGN_EXTEND:
12074 case ISD::ZERO_EXTEND:
12075 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000012076 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000012077 case ISD::SRL:
12078 case ISD::SUB:
12079 case ISD::ADD:
12080 case ISD::MUL:
12081 case ISD::AND:
12082 case ISD::OR:
12083 case ISD::XOR:
12084 return false;
12085 }
12086}
12087
12088/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000012089/// beneficial for dag combiner to promote the specified node. If true, it
12090/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000012091bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000012092 EVT VT = Op.getValueType();
12093 if (VT != MVT::i16)
12094 return false;
12095
Evan Cheng4c26e932010-04-19 19:29:22 +000012096 bool Promote = false;
12097 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000012098 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000012099 default: break;
12100 case ISD::LOAD: {
12101 LoadSDNode *LD = cast<LoadSDNode>(Op);
12102 // If the non-extending load has a single use and it's not live out, then it
12103 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000012104 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
12105 Op.hasOneUse()*/) {
12106 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
12107 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
12108 // The only case where we'd want to promote LOAD (rather then it being
12109 // promoted as an operand is when it's only use is liveout.
12110 if (UI->getOpcode() != ISD::CopyToReg)
12111 return false;
12112 }
12113 }
Evan Cheng4c26e932010-04-19 19:29:22 +000012114 Promote = true;
12115 break;
12116 }
12117 case ISD::SIGN_EXTEND:
12118 case ISD::ZERO_EXTEND:
12119 case ISD::ANY_EXTEND:
12120 Promote = true;
12121 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000012122 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000012123 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000012124 SDValue N0 = Op.getOperand(0);
12125 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000012126 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000012127 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000012128 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000012129 break;
12130 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000012131 case ISD::ADD:
12132 case ISD::MUL:
12133 case ISD::AND:
12134 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000012135 case ISD::XOR:
12136 Commute = true;
12137 // fallthrough
12138 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000012139 SDValue N0 = Op.getOperand(0);
12140 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000012141 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000012142 return false;
12143 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000012144 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000012145 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000012146 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000012147 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000012148 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000012149 }
12150 }
12151
12152 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000012153 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000012154}
12155
Evan Cheng60c07e12006-07-05 22:17:51 +000012156//===----------------------------------------------------------------------===//
12157// X86 Inline Assembly Support
12158//===----------------------------------------------------------------------===//
12159
Chris Lattnerb8105652009-07-20 17:51:36 +000012160bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
12161 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000012162
12163 std::string AsmStr = IA->getAsmString();
12164
12165 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000012166 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000012167 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000012168
12169 switch (AsmPieces.size()) {
12170 default: return false;
12171 case 1:
12172 AsmStr = AsmPieces[0];
12173 AsmPieces.clear();
12174 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
12175
Evan Cheng55d42002011-01-08 01:24:27 +000012176 // FIXME: this should verify that we are targetting a 486 or better. If not,
12177 // we will turn this bswap into something that will be lowered to logical ops
12178 // instead of emitting the bswap asm. For now, we don't support 486 or lower
12179 // so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000012180 // bswap $0
12181 if (AsmPieces.size() == 2 &&
12182 (AsmPieces[0] == "bswap" ||
12183 AsmPieces[0] == "bswapq" ||
12184 AsmPieces[0] == "bswapl") &&
12185 (AsmPieces[1] == "$0" ||
12186 AsmPieces[1] == "${0:q}")) {
12187 // No need to check constraints, nothing other than the equivalent of
12188 // "=r,0" would be valid here.
Evan Cheng55d42002011-01-08 01:24:27 +000012189 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
12190 if (!Ty || Ty->getBitWidth() % 16 != 0)
12191 return false;
12192 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000012193 }
12194 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000012195 if (CI->getType()->isIntegerTy(16) &&
Chris Lattnerb8105652009-07-20 17:51:36 +000012196 AsmPieces.size() == 3 &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000012197 (AsmPieces[0] == "rorw" || AsmPieces[0] == "rolw") &&
Chris Lattnerb8105652009-07-20 17:51:36 +000012198 AsmPieces[1] == "$$8," &&
12199 AsmPieces[2] == "${0:w}" &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000012200 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
12201 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000012202 const std::string &ConstraintsStr = IA->getConstraintString();
12203 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000012204 std::sort(AsmPieces.begin(), AsmPieces.end());
12205 if (AsmPieces.size() == 4 &&
12206 AsmPieces[0] == "~{cc}" &&
12207 AsmPieces[1] == "~{dirflag}" &&
12208 AsmPieces[2] == "~{flags}" &&
12209 AsmPieces[3] == "~{fpsr}") {
Evan Cheng55d42002011-01-08 01:24:27 +000012210 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
12211 if (!Ty || Ty->getBitWidth() % 16 != 0)
12212 return false;
12213 return IntrinsicLowering::LowerToByteSwap(CI);
Dan Gohman0ef701e2010-03-04 19:58:08 +000012214 }
Chris Lattnerb8105652009-07-20 17:51:36 +000012215 }
12216 break;
12217 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000012218 if (CI->getType()->isIntegerTy(32) &&
12219 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
12220 SmallVector<StringRef, 4> Words;
12221 SplitString(AsmPieces[0], Words, " \t,");
12222 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
12223 Words[2] == "${0:w}") {
12224 Words.clear();
12225 SplitString(AsmPieces[1], Words, " \t,");
12226 if (Words.size() == 3 && Words[0] == "rorl" && Words[1] == "$$16" &&
12227 Words[2] == "$0") {
12228 Words.clear();
12229 SplitString(AsmPieces[2], Words, " \t,");
12230 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
12231 Words[2] == "${0:w}") {
12232 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000012233 const std::string &ConstraintsStr = IA->getConstraintString();
12234 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Peter Collingbourne948cf022010-11-13 19:54:30 +000012235 std::sort(AsmPieces.begin(), AsmPieces.end());
12236 if (AsmPieces.size() == 4 &&
12237 AsmPieces[0] == "~{cc}" &&
12238 AsmPieces[1] == "~{dirflag}" &&
12239 AsmPieces[2] == "~{flags}" &&
12240 AsmPieces[3] == "~{fpsr}") {
Evan Cheng55d42002011-01-08 01:24:27 +000012241 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
12242 if (!Ty || Ty->getBitWidth() % 16 != 0)
12243 return false;
12244 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000012245 }
12246 }
12247 }
12248 }
12249 }
Evan Cheng55d42002011-01-08 01:24:27 +000012250
12251 if (CI->getType()->isIntegerTy(64)) {
12252 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
12253 if (Constraints.size() >= 2 &&
12254 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
12255 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
12256 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
12257 SmallVector<StringRef, 4> Words;
12258 SplitString(AsmPieces[0], Words, " \t");
12259 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
Chris Lattnerb8105652009-07-20 17:51:36 +000012260 Words.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000012261 SplitString(AsmPieces[1], Words, " \t");
12262 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
12263 Words.clear();
12264 SplitString(AsmPieces[2], Words, " \t,");
12265 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
12266 Words[2] == "%edx") {
12267 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
12268 if (!Ty || Ty->getBitWidth() % 16 != 0)
12269 return false;
12270 return IntrinsicLowering::LowerToByteSwap(CI);
12271 }
Chris Lattnerb8105652009-07-20 17:51:36 +000012272 }
12273 }
12274 }
12275 }
12276 break;
12277 }
12278 return false;
12279}
12280
12281
12282
Chris Lattnerf4dff842006-07-11 02:54:03 +000012283/// getConstraintType - Given a constraint letter, return the type of
12284/// constraint it is for this target.
12285X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000012286X86TargetLowering::getConstraintType(const std::string &Constraint) const {
12287 if (Constraint.size() == 1) {
12288 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000012289 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000012290 case 'q':
12291 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000012292 case 'f':
12293 case 't':
12294 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000012295 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000012296 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000012297 case 'Y':
12298 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000012299 case 'a':
12300 case 'b':
12301 case 'c':
12302 case 'd':
12303 case 'S':
12304 case 'D':
12305 case 'A':
12306 return C_Register;
12307 case 'I':
12308 case 'J':
12309 case 'K':
12310 case 'L':
12311 case 'M':
12312 case 'N':
12313 case 'G':
12314 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000012315 case 'e':
12316 case 'Z':
12317 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000012318 default:
12319 break;
12320 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000012321 }
Chris Lattner4234f572007-03-25 02:14:49 +000012322 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000012323}
12324
John Thompson44ab89e2010-10-29 17:29:13 +000012325/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000012326/// This object must already have been set up with the operand type
12327/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000012328TargetLowering::ConstraintWeight
12329 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000012330 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000012331 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012332 Value *CallOperandVal = info.CallOperandVal;
12333 // If we don't have a value, we can't do a match,
12334 // but allow it at the lowest weight.
12335 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000012336 return CW_Default;
12337 const Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000012338 // Look at the constraint type.
12339 switch (*constraint) {
12340 default:
John Thompson44ab89e2010-10-29 17:29:13 +000012341 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
12342 case 'R':
12343 case 'q':
12344 case 'Q':
12345 case 'a':
12346 case 'b':
12347 case 'c':
12348 case 'd':
12349 case 'S':
12350 case 'D':
12351 case 'A':
12352 if (CallOperandVal->getType()->isIntegerTy())
12353 weight = CW_SpecificReg;
12354 break;
12355 case 'f':
12356 case 't':
12357 case 'u':
12358 if (type->isFloatingPointTy())
12359 weight = CW_SpecificReg;
12360 break;
12361 case 'y':
Chris Lattner2a786eb2010-12-19 20:19:20 +000012362 if (type->isX86_MMXTy() && Subtarget->hasMMX())
John Thompson44ab89e2010-10-29 17:29:13 +000012363 weight = CW_SpecificReg;
12364 break;
12365 case 'x':
12366 case 'Y':
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012367 if ((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasXMM())
John Thompson44ab89e2010-10-29 17:29:13 +000012368 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012369 break;
12370 case 'I':
12371 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
12372 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000012373 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012374 }
12375 break;
John Thompson44ab89e2010-10-29 17:29:13 +000012376 case 'J':
12377 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12378 if (C->getZExtValue() <= 63)
12379 weight = CW_Constant;
12380 }
12381 break;
12382 case 'K':
12383 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12384 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
12385 weight = CW_Constant;
12386 }
12387 break;
12388 case 'L':
12389 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12390 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
12391 weight = CW_Constant;
12392 }
12393 break;
12394 case 'M':
12395 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12396 if (C->getZExtValue() <= 3)
12397 weight = CW_Constant;
12398 }
12399 break;
12400 case 'N':
12401 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12402 if (C->getZExtValue() <= 0xff)
12403 weight = CW_Constant;
12404 }
12405 break;
12406 case 'G':
12407 case 'C':
12408 if (dyn_cast<ConstantFP>(CallOperandVal)) {
12409 weight = CW_Constant;
12410 }
12411 break;
12412 case 'e':
12413 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12414 if ((C->getSExtValue() >= -0x80000000LL) &&
12415 (C->getSExtValue() <= 0x7fffffffLL))
12416 weight = CW_Constant;
12417 }
12418 break;
12419 case 'Z':
12420 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12421 if (C->getZExtValue() <= 0xffffffff)
12422 weight = CW_Constant;
12423 }
12424 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012425 }
12426 return weight;
12427}
12428
Dale Johannesenba2a0b92008-01-29 02:21:21 +000012429/// LowerXConstraint - try to replace an X constraint, which matches anything,
12430/// with another that has more specific requirements based on the type of the
12431/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000012432const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000012433LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000012434 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
12435 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000012436 if (ConstraintVT.isFloatingPoint()) {
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012437 if (Subtarget->hasXMMInt())
Chris Lattner5e764232008-04-26 23:02:14 +000012438 return "Y";
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012439 if (Subtarget->hasXMM())
Chris Lattner5e764232008-04-26 23:02:14 +000012440 return "x";
12441 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012442
Chris Lattner5e764232008-04-26 23:02:14 +000012443 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000012444}
12445
Chris Lattner48884cd2007-08-25 00:47:38 +000012446/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
12447/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000012448void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattner48884cd2007-08-25 00:47:38 +000012449 char Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000012450 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000012451 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000012452 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000012453
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012454 switch (Constraint) {
12455 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000012456 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000012457 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000012458 if (C->getZExtValue() <= 31) {
12459 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000012460 break;
12461 }
Devang Patel84f7fd22007-03-17 00:13:28 +000012462 }
Chris Lattner48884cd2007-08-25 00:47:38 +000012463 return;
Evan Cheng364091e2008-09-22 23:57:37 +000012464 case 'J':
12465 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000012466 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000012467 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
12468 break;
12469 }
12470 }
12471 return;
12472 case 'K':
12473 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000012474 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000012475 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
12476 break;
12477 }
12478 }
12479 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000012480 case 'N':
12481 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000012482 if (C->getZExtValue() <= 255) {
12483 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000012484 break;
12485 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000012486 }
Chris Lattner48884cd2007-08-25 00:47:38 +000012487 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000012488 case 'e': {
12489 // 32-bit signed value
12490 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000012491 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
12492 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000012493 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000012494 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000012495 break;
12496 }
12497 // FIXME gcc accepts some relocatable values here too, but only in certain
12498 // memory models; it's complicated.
12499 }
12500 return;
12501 }
12502 case 'Z': {
12503 // 32-bit unsigned value
12504 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000012505 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
12506 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000012507 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
12508 break;
12509 }
12510 }
12511 // FIXME gcc accepts some relocatable values here too, but only in certain
12512 // memory models; it's complicated.
12513 return;
12514 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012515 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012516 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000012517 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000012518 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000012519 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000012520 break;
12521 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012522
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000012523 // In any sort of PIC mode addresses need to be computed at runtime by
12524 // adding in a register or some sort of table lookup. These can't
12525 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000012526 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000012527 return;
12528
Chris Lattnerdc43a882007-05-03 16:52:29 +000012529 // If we are in non-pic codegen mode, we allow the address of a global (with
12530 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000012531 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000012532 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000012533
Chris Lattner49921962009-05-08 18:23:14 +000012534 // Match either (GA), (GA+C), (GA+C1+C2), etc.
12535 while (1) {
12536 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
12537 Offset += GA->getOffset();
12538 break;
12539 } else if (Op.getOpcode() == ISD::ADD) {
12540 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
12541 Offset += C->getZExtValue();
12542 Op = Op.getOperand(0);
12543 continue;
12544 }
12545 } else if (Op.getOpcode() == ISD::SUB) {
12546 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
12547 Offset += -C->getZExtValue();
12548 Op = Op.getOperand(0);
12549 continue;
12550 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012551 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012552
Chris Lattner49921962009-05-08 18:23:14 +000012553 // Otherwise, this isn't something we can handle, reject it.
12554 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000012555 }
Eric Christopherfd179292009-08-27 18:07:15 +000012556
Dan Gohman46510a72010-04-15 01:51:59 +000012557 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012558 // If we require an extra load to get this address, as in PIC mode, we
12559 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000012560 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
12561 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012562 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000012563
Devang Patel0d881da2010-07-06 22:08:15 +000012564 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
12565 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000012566 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012567 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012568 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012569
Gabor Greifba36cb52008-08-28 21:40:38 +000012570 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000012571 Ops.push_back(Result);
12572 return;
12573 }
Dale Johannesen1784d162010-06-25 21:55:36 +000012574 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012575}
12576
Chris Lattner259e97c2006-01-31 19:43:35 +000012577std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +000012578getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000012579 EVT VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +000012580 if (Constraint.size() == 1) {
12581 // FIXME: not handling fp-stack yet!
Chris Lattner259e97c2006-01-31 19:43:35 +000012582 switch (Constraint[0]) { // GCC X86 Constraint Letters
Chris Lattnerf4dff842006-07-11 02:54:03 +000012583 default: break; // Unknown constraint letter
Evan Cheng47e9fab2009-07-17 22:13:25 +000012584 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
12585 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000012586 if (VT == MVT::i32)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012587 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
12588 X86::ESI, X86::EDI, X86::R8D, X86::R9D,
12589 X86::R10D,X86::R11D,X86::R12D,
12590 X86::R13D,X86::R14D,X86::R15D,
12591 X86::EBP, X86::ESP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012592 else if (VT == MVT::i16)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012593 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
12594 X86::SI, X86::DI, X86::R8W,X86::R9W,
12595 X86::R10W,X86::R11W,X86::R12W,
12596 X86::R13W,X86::R14W,X86::R15W,
12597 X86::BP, X86::SP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012598 else if (VT == MVT::i8)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012599 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL,
12600 X86::SIL, X86::DIL, X86::R8B,X86::R9B,
12601 X86::R10B,X86::R11B,X86::R12B,
12602 X86::R13B,X86::R14B,X86::R15B,
12603 X86::BPL, X86::SPL, 0);
12604
Owen Anderson825b72b2009-08-11 20:47:22 +000012605 else if (VT == MVT::i64)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012606 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX,
12607 X86::RSI, X86::RDI, X86::R8, X86::R9,
12608 X86::R10, X86::R11, X86::R12,
12609 X86::R13, X86::R14, X86::R15,
12610 X86::RBP, X86::RSP, 0);
12611
12612 break;
12613 }
Eric Christopherfd179292009-08-27 18:07:15 +000012614 // 32-bit fallthrough
Chris Lattner259e97c2006-01-31 19:43:35 +000012615 case 'Q': // Q_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000012616 if (VT == MVT::i32)
Chris Lattner80a7ecc2006-05-06 00:29:37 +000012617 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012618 else if (VT == MVT::i16)
Chris Lattner80a7ecc2006-05-06 00:29:37 +000012619 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012620 else if (VT == MVT::i8)
Evan Cheng12914382007-08-13 23:27:11 +000012621 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012622 else if (VT == MVT::i64)
Chris Lattner03e6c702007-11-04 06:51:12 +000012623 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
12624 break;
Chris Lattner259e97c2006-01-31 19:43:35 +000012625 }
12626 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012627
Chris Lattner1efa40f2006-02-22 00:56:39 +000012628 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +000012629}
Chris Lattnerf76d1802006-07-31 23:26:50 +000012630
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012631std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000012632X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000012633 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000012634 // First, see if this is a constraint that directly corresponds to an LLVM
12635 // register class.
12636 if (Constraint.size() == 1) {
12637 // GCC Constraint Letters
12638 switch (Constraint[0]) {
12639 default: break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000012640 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000012641 case 'l': // INDEX_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000012642 if (VT == MVT::i8)
Chris Lattner0f65cad2007-04-09 05:49:22 +000012643 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012644 if (VT == MVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +000012645 return std::make_pair(0U, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012646 if (VT == MVT::i32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +000012647 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +000012648 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000012649 case 'R': // LEGACY_REGS
12650 if (VT == MVT::i8)
12651 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
12652 if (VT == MVT::i16)
12653 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
12654 if (VT == MVT::i32 || !Subtarget->is64Bit())
12655 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
12656 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000012657 case 'f': // FP Stack registers.
12658 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
12659 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000012660 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000012661 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012662 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000012663 return std::make_pair(0U, X86::RFP64RegisterClass);
12664 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000012665 case 'y': // MMX_REGS if MMX allowed.
12666 if (!Subtarget->hasMMX()) break;
12667 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000012668 case 'Y': // SSE_REGS if SSE2 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012669 if (!Subtarget->hasXMMInt()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000012670 // FALL THROUGH.
12671 case 'x': // SSE_REGS if SSE1 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012672 if (!Subtarget->hasXMM()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000012673
Owen Anderson825b72b2009-08-11 20:47:22 +000012674 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000012675 default: break;
12676 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000012677 case MVT::f32:
12678 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +000012679 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012680 case MVT::f64:
12681 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +000012682 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000012683 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000012684 case MVT::v16i8:
12685 case MVT::v8i16:
12686 case MVT::v4i32:
12687 case MVT::v2i64:
12688 case MVT::v4f32:
12689 case MVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +000012690 return std::make_pair(0U, X86::VR128RegisterClass);
12691 }
Chris Lattnerad043e82007-04-09 05:11:28 +000012692 break;
12693 }
12694 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012695
Chris Lattnerf76d1802006-07-31 23:26:50 +000012696 // Use the default implementation in TargetLowering to convert the register
12697 // constraint into a member of a register class.
12698 std::pair<unsigned, const TargetRegisterClass*> Res;
12699 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000012700
12701 // Not found as a standard register?
12702 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000012703 // Map st(0) -> st(7) -> ST0
12704 if (Constraint.size() == 7 && Constraint[0] == '{' &&
12705 tolower(Constraint[1]) == 's' &&
12706 tolower(Constraint[2]) == 't' &&
12707 Constraint[3] == '(' &&
12708 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
12709 Constraint[5] == ')' &&
12710 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000012711
Chris Lattner56d77c72009-09-13 22:41:48 +000012712 Res.first = X86::ST0+Constraint[4]-'0';
12713 Res.second = X86::RFP80RegisterClass;
12714 return Res;
12715 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000012716
Chris Lattner56d77c72009-09-13 22:41:48 +000012717 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000012718 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000012719 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +000012720 Res.second = X86::RFP80RegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000012721 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000012722 }
Chris Lattner56d77c72009-09-13 22:41:48 +000012723
12724 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000012725 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000012726 Res.first = X86::EFLAGS;
12727 Res.second = X86::CCRRegisterClass;
12728 return Res;
12729 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000012730
Dale Johannesen330169f2008-11-13 21:52:36 +000012731 // 'A' means EAX + EDX.
12732 if (Constraint == "A") {
12733 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +000012734 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000012735 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000012736 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000012737 return Res;
12738 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012739
Chris Lattnerf76d1802006-07-31 23:26:50 +000012740 // Otherwise, check to see if this is a register class of the wrong value
12741 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
12742 // turn into {ax},{dx}.
12743 if (Res.second->hasType(VT))
12744 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012745
Chris Lattnerf76d1802006-07-31 23:26:50 +000012746 // All of the single-register GCC register classes map their values onto
12747 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
12748 // really want an 8-bit or 32-bit register, map to the appropriate register
12749 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +000012750 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000012751 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000012752 unsigned DestReg = 0;
12753 switch (Res.first) {
12754 default: break;
12755 case X86::AX: DestReg = X86::AL; break;
12756 case X86::DX: DestReg = X86::DL; break;
12757 case X86::CX: DestReg = X86::CL; break;
12758 case X86::BX: DestReg = X86::BL; break;
12759 }
12760 if (DestReg) {
12761 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000012762 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000012763 }
Owen Anderson825b72b2009-08-11 20:47:22 +000012764 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000012765 unsigned DestReg = 0;
12766 switch (Res.first) {
12767 default: break;
12768 case X86::AX: DestReg = X86::EAX; break;
12769 case X86::DX: DestReg = X86::EDX; break;
12770 case X86::CX: DestReg = X86::ECX; break;
12771 case X86::BX: DestReg = X86::EBX; break;
12772 case X86::SI: DestReg = X86::ESI; break;
12773 case X86::DI: DestReg = X86::EDI; break;
12774 case X86::BP: DestReg = X86::EBP; break;
12775 case X86::SP: DestReg = X86::ESP; break;
12776 }
12777 if (DestReg) {
12778 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000012779 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000012780 }
Owen Anderson825b72b2009-08-11 20:47:22 +000012781 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000012782 unsigned DestReg = 0;
12783 switch (Res.first) {
12784 default: break;
12785 case X86::AX: DestReg = X86::RAX; break;
12786 case X86::DX: DestReg = X86::RDX; break;
12787 case X86::CX: DestReg = X86::RCX; break;
12788 case X86::BX: DestReg = X86::RBX; break;
12789 case X86::SI: DestReg = X86::RSI; break;
12790 case X86::DI: DestReg = X86::RDI; break;
12791 case X86::BP: DestReg = X86::RBP; break;
12792 case X86::SP: DestReg = X86::RSP; break;
12793 }
12794 if (DestReg) {
12795 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000012796 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000012797 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000012798 }
Chris Lattner6ba50a92008-08-26 06:19:02 +000012799 } else if (Res.second == X86::FR32RegisterClass ||
12800 Res.second == X86::FR64RegisterClass ||
12801 Res.second == X86::VR128RegisterClass) {
12802 // Handle references to XMM physical registers that got mapped into the
12803 // wrong class. This can happen with constraints like {xmm0} where the
12804 // target independent register mapper will just pick the first match it can
12805 // find, ignoring the required type.
Owen Anderson825b72b2009-08-11 20:47:22 +000012806 if (VT == MVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +000012807 Res.second = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +000012808 else if (VT == MVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +000012809 Res.second = X86::FR64RegisterClass;
12810 else if (X86::VR128RegisterClass->hasType(VT))
12811 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000012812 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012813
Chris Lattnerf76d1802006-07-31 23:26:50 +000012814 return Res;
12815}