blob: 910ed2abd1f93f94157c7c5dbd202543a57dc31a [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
Dan Gohman98ca4f22009-08-05 01:29:28 +00001451/// LowerCallResult - Lower the result values of a call into the
1452/// appropriate copies out of appropriate physical registers.
1453///
1454SDValue
1455X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001456 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001457 const SmallVectorImpl<ISD::InputArg> &Ins,
1458 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001459 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001460
Chris Lattnere32bbf62007-02-28 07:09:55 +00001461 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001462 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001463 bool Is64Bit = Subtarget->is64Bit();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001464 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +00001465 RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001466 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001467
Chris Lattner3085e152007-02-25 08:59:22 +00001468 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001469 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001470 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001471 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001472
Torok Edwin3f142c32009-02-01 18:15:56 +00001473 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001474 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001475 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasXMM())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001476 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001477 }
1478
Evan Cheng79fb3b42009-02-20 20:43:02 +00001479 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001480
1481 // If this is a call to a function that returns an fp value on the floating
1482 // point stack, we must guarantee the the value is popped from the stack, so
1483 // a CopyFromReg is not good enough - the copy instruction may be eliminated
1484 // if the return value is not used. We use the FpGET_ST0 instructions
1485 // instead.
1486 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1487 // If we prefer to use the value in xmm registers, copy it out as f80 and
1488 // use a truncate to move it from fp stack reg to xmm reg.
1489 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
1490 bool isST0 = VA.getLocReg() == X86::ST0;
1491 unsigned Opc = 0;
1492 if (CopyVT == MVT::f32) Opc = isST0 ? X86::FpGET_ST0_32:X86::FpGET_ST1_32;
1493 if (CopyVT == MVT::f64) Opc = isST0 ? X86::FpGET_ST0_64:X86::FpGET_ST1_64;
1494 if (CopyVT == MVT::f80) Opc = isST0 ? X86::FpGET_ST0_80:X86::FpGET_ST1_80;
1495 SDValue Ops[] = { Chain, InFlag };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001496 Chain = SDValue(DAG.getMachineNode(Opc, dl, CopyVT, MVT::Other, MVT::Glue,
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001497 Ops, 2), 1);
1498 Val = Chain.getValue(0);
1499
1500 // Round the f80 to the right size, which also moves it to the appropriate
1501 // xmm register.
1502 if (CopyVT != VA.getValVT())
1503 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1504 // This truncation won't change the value.
1505 DAG.getIntPtrConstant(1));
1506 } else if (Is64Bit && CopyVT.isVector() && CopyVT.getSizeInBits() == 64) {
Evan Cheng242b38b2009-02-23 09:03:22 +00001507 // For x86-64, MMX values are returned in XMM0 / XMM1 except for v1i64.
1508 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
1509 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001510 MVT::v2i64, InFlag).getValue(1);
Evan Cheng242b38b2009-02-23 09:03:22 +00001511 Val = Chain.getValue(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00001512 Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1513 Val, DAG.getConstant(0, MVT::i64));
Evan Cheng242b38b2009-02-23 09:03:22 +00001514 } else {
1515 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001516 MVT::i64, InFlag).getValue(1);
Evan Cheng242b38b2009-02-23 09:03:22 +00001517 Val = Chain.getValue(0);
1518 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001519 Val = DAG.getNode(ISD::BITCAST, dl, CopyVT, Val);
Evan Cheng79fb3b42009-02-20 20:43:02 +00001520 } else {
1521 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1522 CopyVT, InFlag).getValue(1);
1523 Val = Chain.getValue(0);
1524 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001525 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001526 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001527 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001528
Dan Gohman98ca4f22009-08-05 01:29:28 +00001529 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001530}
1531
1532
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001533//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001534// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001535//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001536// StdCall calling convention seems to be standard for many Windows' API
1537// routines and around. It differs from C calling convention just a little:
1538// callee should clean up the stack, not caller. Symbols should be also
1539// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001540// For info on fast calling convention see Fast Calling Convention (tail call)
1541// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001542
Dan Gohman98ca4f22009-08-05 01:29:28 +00001543/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001544/// semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001545static bool CallIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
1546 if (Outs.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001547 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001548
Dan Gohman98ca4f22009-08-05 01:29:28 +00001549 return Outs[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001550}
1551
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001552/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001553/// return semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001554static bool
1555ArgsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
1556 if (Ins.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001557 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001558
Dan Gohman98ca4f22009-08-05 01:29:28 +00001559 return Ins[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001560}
1561
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001562/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1563/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001564/// the specific parameter attribute. The copy will be passed as a byval
1565/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001566static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001567CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001568 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1569 DebugLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00001570 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Michael J. Spencerec38de22010-10-10 22:04:20 +00001571
Dale Johannesendd64c412009-02-04 00:33:20 +00001572 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Stuart Hastings03d58262011-03-10 00:25:53 +00001573 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001574 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001575}
1576
Chris Lattner29689432010-03-11 00:22:57 +00001577/// IsTailCallConvention - Return true if the calling convention is one that
1578/// supports tail call optimization.
1579static bool IsTailCallConvention(CallingConv::ID CC) {
1580 return (CC == CallingConv::Fast || CC == CallingConv::GHC);
1581}
1582
Evan Cheng0c439eb2010-01-27 00:07:07 +00001583/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1584/// a tailcall target by changing its ABI.
1585static bool FuncIsMadeTailCallSafe(CallingConv::ID CC) {
Chris Lattner29689432010-03-11 00:22:57 +00001586 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001587}
1588
Dan Gohman98ca4f22009-08-05 01:29:28 +00001589SDValue
1590X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001591 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001592 const SmallVectorImpl<ISD::InputArg> &Ins,
1593 DebugLoc dl, SelectionDAG &DAG,
1594 const CCValAssign &VA,
1595 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001596 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001597 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001598 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Evan Cheng0c439eb2010-01-27 00:07:07 +00001599 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001600 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001601 EVT ValVT;
1602
1603 // If value is passed by pointer we have address passed instead of the value
1604 // itself.
1605 if (VA.getLocInfo() == CCValAssign::Indirect)
1606 ValVT = VA.getLocVT();
1607 else
1608 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001609
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001610 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001611 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001612 // In case of tail call optimization mark all arguments mutable. Since they
1613 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001614 if (Flags.isByVal()) {
1615 int FI = MFI->CreateFixedObject(Flags.getByValSize(),
Evan Chenged2ae132010-07-03 00:40:23 +00001616 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001617 return DAG.getFrameIndex(FI, getPointerTy());
1618 } else {
1619 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001620 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001621 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1622 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001623 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00001624 false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001625 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001626}
1627
Dan Gohman475871a2008-07-27 21:46:04 +00001628SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001629X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001630 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001631 bool isVarArg,
1632 const SmallVectorImpl<ISD::InputArg> &Ins,
1633 DebugLoc dl,
1634 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001635 SmallVectorImpl<SDValue> &InVals)
1636 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001637 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001638 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001639
Gordon Henriksen86737662008-01-05 16:56:59 +00001640 const Function* Fn = MF.getFunction();
1641 if (Fn->hasExternalLinkage() &&
1642 Subtarget->isTargetCygMing() &&
1643 Fn->getName() == "main")
1644 FuncInfo->setForceFramePointer(true);
1645
Evan Cheng1bc78042006-04-26 01:20:17 +00001646 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001647 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001648 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001649
Chris Lattner29689432010-03-11 00:22:57 +00001650 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1651 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001652
Chris Lattner638402b2007-02-28 07:00:42 +00001653 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001654 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001655 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1656 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001657
1658 // Allocate shadow area for Win64
1659 if (IsWin64) {
1660 CCInfo.AllocateStack(32, 8);
1661 }
1662
Duncan Sands45907662010-10-31 13:21:44 +00001663 CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001664
Chris Lattnerf39f7712007-02-28 05:46:49 +00001665 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001666 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001667 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1668 CCValAssign &VA = ArgLocs[i];
1669 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1670 // places.
1671 assert(VA.getValNo() != LastVal &&
1672 "Don't support value assigned to multiple locs yet");
1673 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001674
Chris Lattnerf39f7712007-02-28 05:46:49 +00001675 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001676 EVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001677 TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001678 if (RegVT == MVT::i32)
Chris Lattnerf39f7712007-02-28 05:46:49 +00001679 RC = X86::GR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001680 else if (Is64Bit && RegVT == MVT::i64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001681 RC = X86::GR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001682 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001683 RC = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001684 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001685 RC = X86::FR64RegisterClass;
Bruno Cardoso Lopesac098352010-08-05 23:35:51 +00001686 else if (RegVT.isVector() && RegVT.getSizeInBits() == 256)
1687 RC = X86::VR256RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001688 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001689 RC = X86::VR128RegisterClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001690 else if (RegVT == MVT::x86mmx)
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001691 RC = X86::VR64RegisterClass;
1692 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001693 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001694
Devang Patel68e6bee2011-02-21 23:21:26 +00001695 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001696 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001697
Chris Lattnerf39f7712007-02-28 05:46:49 +00001698 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1699 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1700 // right size.
1701 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001702 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001703 DAG.getValueType(VA.getValVT()));
1704 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001705 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001706 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001707 else if (VA.getLocInfo() == CCValAssign::BCvt)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001708 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001709
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001710 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001711 // Handle MMX values passed in XMM regs.
1712 if (RegVT.isVector()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001713 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(),
1714 ArgValue);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001715 } else
1716 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001717 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001718 } else {
1719 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001720 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001721 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001722
1723 // If value is passed via pointer - do a load.
1724 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00001725 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
1726 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001727
Dan Gohman98ca4f22009-08-05 01:29:28 +00001728 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001729 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001730
Dan Gohman61a92132008-04-21 23:59:07 +00001731 // The x86-64 ABI for returning structs by value requires that we copy
1732 // the sret argument into %rax for the return. Save the argument into
1733 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001734 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001735 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1736 unsigned Reg = FuncInfo->getSRetReturnReg();
1737 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001738 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001739 FuncInfo->setSRetReturnReg(Reg);
1740 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001741 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001742 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001743 }
1744
Chris Lattnerf39f7712007-02-28 05:46:49 +00001745 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001746 // Align stack specially for tail calls.
1747 if (FuncIsMadeTailCallSafe(CallConv))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001748 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001749
Evan Cheng1bc78042006-04-26 01:20:17 +00001750 // If the function takes variable number of arguments, make a frame index for
1751 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001752 if (isVarArg) {
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001753 if (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
1754 CallConv != CallingConv::X86_ThisCall)) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00001755 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00001756 }
1757 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001758 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1759
1760 // FIXME: We should really autogenerate these arrays
1761 static const unsigned GPR64ArgRegsWin64[] = {
1762 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001763 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001764 static const unsigned GPR64ArgRegs64Bit[] = {
1765 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1766 };
1767 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001768 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1769 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1770 };
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001771 const unsigned *GPR64ArgRegs;
1772 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001773
1774 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001775 // The XMM registers which might contain var arg parameters are shadowed
1776 // in their paired GPR. So we only need to save the GPR to their home
1777 // slots.
1778 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001779 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001780 } else {
1781 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1782 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001783
1784 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit, TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001785 }
1786 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1787 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001788
Devang Patel578efa92009-06-05 21:57:13 +00001789 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001790 assert(!(NumXMMRegs && !Subtarget->hasXMM()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001791 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001792 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001793 "SSE register cannot be used when SSE is disabled!");
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001794 if (UseSoftFloat || NoImplicitFloatOps || !Subtarget->hasXMM())
Torok Edwin3f142c32009-02-01 18:15:56 +00001795 // Kernel mode asks for SSE to be disabled, so don't push them
1796 // on the stack.
1797 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001798
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001799 if (IsWin64) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001800 const TargetFrameLowering &TFI = *getTargetMachine().getFrameLowering();
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001801 // Get to the caller-allocated home save location. Add 8 to account
1802 // for the return address.
1803 int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001804 FuncInfo->setRegSaveFrameIndex(
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001805 MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001806 // Fixup to set vararg frame on shadow area (4 x i64).
1807 if (NumIntRegs < 4)
1808 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001809 } else {
1810 // For X86-64, if there are vararg parameters that are passed via
1811 // registers, then we must store them to their spots on the stack so they
1812 // may be loaded by deferencing the result of va_next.
1813 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
1814 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
1815 FuncInfo->setRegSaveFrameIndex(
1816 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00001817 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001818 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001819
Gordon Henriksen86737662008-01-05 16:56:59 +00001820 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001821 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00001822 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
1823 getPointerTy());
1824 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001825 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00001826 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
1827 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00001828 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00001829 X86::GR64RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00001830 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001831 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001832 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001833 MachinePointerInfo::getFixedStack(
1834 FuncInfo->getRegSaveFrameIndex(), Offset),
1835 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001836 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001837 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001838 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001839
Dan Gohmanface41a2009-08-16 21:24:25 +00001840 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
1841 // Now store the XMM (fp + vector) parameter registers.
1842 SmallVector<SDValue, 11> SaveXMMOps;
1843 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001844
Devang Patel68e6bee2011-02-21 23:21:26 +00001845 unsigned AL = MF.addLiveIn(X86::AL, X86::GR8RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00001846 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
1847 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001848
Dan Gohman1e93df62010-04-17 14:41:14 +00001849 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1850 FuncInfo->getRegSaveFrameIndex()));
1851 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1852 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00001853
Dan Gohmanface41a2009-08-16 21:24:25 +00001854 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001855 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00001856 X86::VR128RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00001857 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
1858 SaveXMMOps.push_back(Val);
1859 }
1860 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
1861 MVT::Other,
1862 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00001863 }
Dan Gohmanface41a2009-08-16 21:24:25 +00001864
1865 if (!MemOps.empty())
1866 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1867 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001868 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001869 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001870
Gordon Henriksen86737662008-01-05 16:56:59 +00001871 // Some CCs need callee pop.
Dan Gohman4d3d6e12010-05-27 18:43:40 +00001872 if (Subtarget->IsCalleePop(isVarArg, CallConv)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001873 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001874 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00001875 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001876 // If this is an sret function, the return should pop the hidden pointer.
Chris Lattner29689432010-03-11 00:22:57 +00001877 if (!Is64Bit && !IsTailCallConvention(CallConv) && ArgsAreStructReturn(Ins))
Dan Gohman1e93df62010-04-17 14:41:14 +00001878 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001879 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001880
Gordon Henriksen86737662008-01-05 16:56:59 +00001881 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001882 // RegSaveFrameIndex is X86-64 only.
1883 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001884 if (CallConv == CallingConv::X86_FastCall ||
1885 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00001886 // fastcc functions can't have varargs.
1887 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00001888 }
Evan Cheng25caf632006-05-23 21:06:34 +00001889
Dan Gohman98ca4f22009-08-05 01:29:28 +00001890 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001891}
1892
Dan Gohman475871a2008-07-27 21:46:04 +00001893SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001894X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
1895 SDValue StackPtr, SDValue Arg,
1896 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00001897 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001898 ISD::ArgFlagsTy Flags) const {
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001899 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00001900 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00001901 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001902 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00001903 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001904
1905 return DAG.getStore(Chain, dl, Arg, PtrOff,
1906 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00001907 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00001908}
1909
Bill Wendling64e87322009-01-16 19:25:27 +00001910/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001911/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00001912SDValue
1913X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00001914 SDValue &OutRetAddr, SDValue Chain,
1915 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00001916 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001917 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00001918 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001919 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00001920
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001921 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00001922 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
1923 false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001924 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001925}
1926
1927/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1928/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00001929static SDValue
1930EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00001931 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00001932 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001933 // Store the return address to the appropriate stack slot.
1934 if (!FPDiff) return Chain;
1935 // Calculate the new stack slot for the return address.
1936 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00001937 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00001938 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00001939 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00001940 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001941 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00001942 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00001943 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001944 return Chain;
1945}
1946
Dan Gohman98ca4f22009-08-05 01:29:28 +00001947SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001948X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001949 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001950 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001951 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001952 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001953 const SmallVectorImpl<ISD::InputArg> &Ins,
1954 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001955 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001956 MachineFunction &MF = DAG.getMachineFunction();
1957 bool Is64Bit = Subtarget->is64Bit();
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00001958 bool IsWin64 = Subtarget->isTargetWin64();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001959 bool IsStructRet = CallIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00001960 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001961
Evan Cheng5f941932010-02-05 02:21:12 +00001962 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00001963 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00001964 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1965 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001966 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00001967
1968 // Sibcalls are automatically detected tailcalls which do not require
1969 // ABI changes.
Dan Gohman1797ed52010-02-08 20:27:50 +00001970 if (!GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00001971 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00001972
1973 if (isTailCall)
1974 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00001975 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00001976
Chris Lattner29689432010-03-11 00:22:57 +00001977 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1978 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001979
Chris Lattner638402b2007-02-28 07:00:42 +00001980 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00001981 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001982 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1983 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001984
1985 // Allocate shadow area for Win64
1986 if (IsWin64) {
1987 CCInfo.AllocateStack(32, 8);
1988 }
1989
Duncan Sands45907662010-10-31 13:21:44 +00001990 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001991
Chris Lattner423c5f42007-02-28 05:31:48 +00001992 // Get a count of how many bytes are to be pushed on the stack.
1993 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00001994 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00001995 // This is a sibcall. The memory operands are available in caller's
1996 // own caller's stack.
1997 NumBytes = 0;
Chris Lattner29689432010-03-11 00:22:57 +00001998 else if (GuaranteedTailCallOpt && IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00001999 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002000
Gordon Henriksen86737662008-01-05 16:56:59 +00002001 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00002002 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002003 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00002004 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00002005 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
2006 FPDiff = NumBytesCallerPushed - NumBytes;
2007
2008 // Set the delta of movement of the returnaddr stackslot.
2009 // But only set if delta is greater than previous delta.
2010 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
2011 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
2012 }
2013
Evan Chengf22f9b32010-02-06 03:28:46 +00002014 if (!IsSibcall)
2015 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002016
Dan Gohman475871a2008-07-27 21:46:04 +00002017 SDValue RetAddrFrIdx;
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002018 // Load return adress for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00002019 if (isTailCall && FPDiff)
2020 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
2021 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002022
Dan Gohman475871a2008-07-27 21:46:04 +00002023 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
2024 SmallVector<SDValue, 8> MemOpChains;
2025 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00002026
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002027 // Walk the register/memloc assignments, inserting copies/loads. In the case
2028 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00002029 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2030 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00002031 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00002032 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002033 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00002034 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00002035
Chris Lattner423c5f42007-02-28 05:31:48 +00002036 // Promote the value if needed.
2037 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002038 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00002039 case CCValAssign::Full: break;
2040 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002041 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002042 break;
2043 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002044 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002045 break;
2046 case CCValAssign::AExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002047 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
2048 // Special case: passing MMX values in XMM registers.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002049 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00002050 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
2051 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002052 } else
2053 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
2054 break;
2055 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002056 Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002057 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002058 case CCValAssign::Indirect: {
2059 // Store the argument.
2060 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00002061 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002062 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00002063 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002064 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002065 Arg = SpillSlot;
2066 break;
2067 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00002068 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002069
Chris Lattner423c5f42007-02-28 05:31:48 +00002070 if (VA.isRegLoc()) {
2071 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002072 if (isVarArg && IsWin64) {
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002073 // Win64 ABI requires argument XMM reg to be copied to the corresponding
2074 // shadow reg if callee is a varargs function.
2075 unsigned ShadowReg = 0;
2076 switch (VA.getLocReg()) {
2077 case X86::XMM0: ShadowReg = X86::RCX; break;
2078 case X86::XMM1: ShadowReg = X86::RDX; break;
2079 case X86::XMM2: ShadowReg = X86::R8; break;
2080 case X86::XMM3: ShadowReg = X86::R9; break;
2081 }
2082 if (ShadowReg)
2083 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
2084 }
Evan Chengf22f9b32010-02-06 03:28:46 +00002085 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00002086 assert(VA.isMemLoc());
2087 if (StackPtr.getNode() == 0)
2088 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
2089 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
2090 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002091 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002092 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002093
Evan Cheng32fe1032006-05-25 00:59:30 +00002094 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002095 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002096 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002097
Evan Cheng347d5f72006-04-28 21:29:37 +00002098 // Build a sequence of copy-to-reg nodes chained together with token chain
2099 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002100 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002101 // Tail call byval lowering might overwrite argument registers so in case of
2102 // tail call optimization the copies to registers are lowered later.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002103 if (!isTailCall)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002104 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002105 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002106 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002107 InFlag = Chain.getValue(1);
2108 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002109
Chris Lattner88e1fd52009-07-09 04:24:46 +00002110 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002111 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2112 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002113 if (!isTailCall) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002114 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
2115 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00002116 DebugLoc(), getPointerTy()),
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002117 InFlag);
2118 InFlag = Chain.getValue(1);
2119 } else {
2120 // If we are tail calling and generating PIC/GOT style code load the
2121 // address of the callee into ECX. The value in ecx is used as target of
2122 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2123 // for tail calls on PIC/GOT architectures. Normally we would just put the
2124 // address of GOT into ebx and then call target@PLT. But for tail calls
2125 // ebx would be restored (since ebx is callee saved) before jumping to the
2126 // target@PLT.
2127
2128 // Note: The actual moving to ECX is done further down.
2129 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2130 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2131 !G->getGlobal()->hasProtectedVisibility())
2132 Callee = LowerGlobalAddress(Callee, DAG);
2133 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002134 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002135 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002136 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002137
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002138 if (Is64Bit && isVarArg && !IsWin64) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002139 // From AMD64 ABI document:
2140 // For calls that may call functions that use varargs or stdargs
2141 // (prototype-less calls or calls to functions containing ellipsis (...) in
2142 // the declaration) %al is used as hidden argument to specify the number
2143 // of SSE registers used. The contents of %al do not need to match exactly
2144 // the number of registers, but must be an ubound on the number of SSE
2145 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002146
Gordon Henriksen86737662008-01-05 16:56:59 +00002147 // Count the number of XMM registers allocated.
2148 static const unsigned XMMArgRegs[] = {
2149 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2150 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2151 };
2152 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00002153 assert((Subtarget->hasXMM() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002154 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002155
Dale Johannesendd64c412009-02-04 00:33:20 +00002156 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Owen Anderson825b72b2009-08-11 20:47:22 +00002157 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002158 InFlag = Chain.getValue(1);
2159 }
2160
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002161
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002162 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002163 if (isTailCall) {
2164 // Force all the incoming stack arguments to be loaded from the stack
2165 // before any new outgoing arguments are stored to the stack, because the
2166 // outgoing stack slots may alias the incoming argument stack slots, and
2167 // the alias isn't otherwise explicit. This is slightly more conservative
2168 // than necessary, because it means that each store effectively depends
2169 // on every argument instead of just those arguments it would clobber.
2170 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2171
Dan Gohman475871a2008-07-27 21:46:04 +00002172 SmallVector<SDValue, 8> MemOpChains2;
2173 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002174 int FI = 0;
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002175 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00002176 InFlag = SDValue();
Dan Gohman1797ed52010-02-08 20:27:50 +00002177 if (GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002178 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2179 CCValAssign &VA = ArgLocs[i];
2180 if (VA.isRegLoc())
2181 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002182 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002183 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002184 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002185 // Create frame index.
2186 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002187 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002188 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002189 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002190
Duncan Sands276dcbd2008-03-21 09:14:45 +00002191 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002192 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002193 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002194 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002195 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00002196 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002197 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002198
Dan Gohman98ca4f22009-08-05 01:29:28 +00002199 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2200 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002201 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002202 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002203 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002204 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002205 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002206 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002207 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002208 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002209 }
2210 }
2211
2212 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002213 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002214 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002215
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002216 // Copy arguments to their registers.
2217 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002218 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002219 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002220 InFlag = Chain.getValue(1);
2221 }
Dan Gohman475871a2008-07-27 21:46:04 +00002222 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002223
Gordon Henriksen86737662008-01-05 16:56:59 +00002224 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002225 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002226 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002227 }
2228
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002229 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2230 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2231 // In the 64-bit large code model, we have to make all calls
2232 // through a register, since the call instruction's 32-bit
2233 // pc-relative offset may not be large enough to hold the whole
2234 // address.
2235 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002236 // If the callee is a GlobalAddress node (quite common, every direct call
2237 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2238 // it.
2239
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002240 // We should use extra load for direct calls to dllimported functions in
2241 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002242 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002243 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002244 unsigned char OpFlags = 0;
Eric Christopherfd179292009-08-27 18:07:15 +00002245
Chris Lattner48a7d022009-07-09 05:02:21 +00002246 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2247 // external symbols most go through the PLT in PIC mode. If the symbol
2248 // has hidden or protected visibility, or if it is static or local, then
2249 // we don't need to use the PLT - we can directly call it.
2250 if (Subtarget->isTargetELF() &&
2251 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002252 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002253 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002254 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002255 (GV->isDeclaration() || GV->isWeakForLinker()) &&
2256 Subtarget->getDarwinVers() < 9) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002257 // PC-relative references to external symbols should go through $stub,
2258 // unless we're building with the leopard linker or later, which
2259 // automatically synthesizes these stubs.
2260 OpFlags = X86II::MO_DARWIN_STUB;
2261 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002262
Devang Patel0d881da2010-07-06 22:08:15 +00002263 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002264 G->getOffset(), OpFlags);
2265 }
Bill Wendling056292f2008-09-16 21:48:12 +00002266 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002267 unsigned char OpFlags = 0;
2268
Evan Cheng1bf891a2010-12-01 22:59:46 +00002269 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2270 // external symbols should go through the PLT.
2271 if (Subtarget->isTargetELF() &&
2272 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2273 OpFlags = X86II::MO_PLT;
2274 } else if (Subtarget->isPICStyleStubAny() &&
2275 Subtarget->getDarwinVers() < 9) {
2276 // PC-relative references to external symbols should go through $stub,
2277 // unless we're building with the leopard linker or later, which
2278 // automatically synthesizes these stubs.
2279 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002280 }
Eric Christopherfd179292009-08-27 18:07:15 +00002281
Chris Lattner48a7d022009-07-09 05:02:21 +00002282 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2283 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002284 }
2285
Chris Lattnerd96d0722007-02-25 06:40:16 +00002286 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002287 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002288 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002289
Evan Chengf22f9b32010-02-06 03:28:46 +00002290 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002291 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2292 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002293 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002294 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002295
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002296 Ops.push_back(Chain);
2297 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002298
Dan Gohman98ca4f22009-08-05 01:29:28 +00002299 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002300 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002301
Gordon Henriksen86737662008-01-05 16:56:59 +00002302 // Add argument registers to the end of the list so that they are known live
2303 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002304 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2305 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2306 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002307
Evan Cheng586ccac2008-03-18 23:36:35 +00002308 // Add an implicit use GOT pointer in EBX.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002309 if (!isTailCall && Subtarget->isPICStyleGOT())
Evan Cheng586ccac2008-03-18 23:36:35 +00002310 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
2311
Anton Korobeynikov3a1e54a2010-08-17 21:06:07 +00002312 // Add an implicit use of AL for non-Windows x86 64-bit vararg functions.
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002313 if (Is64Bit && isVarArg && !IsWin64)
Owen Anderson825b72b2009-08-11 20:47:22 +00002314 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
Evan Cheng586ccac2008-03-18 23:36:35 +00002315
Gabor Greifba36cb52008-08-28 21:40:38 +00002316 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002317 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002318
Dan Gohman98ca4f22009-08-05 01:29:28 +00002319 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002320 // We used to do:
2321 //// If this is the first return lowered for this function, add the regs
2322 //// to the liveout set for the function.
2323 // This isn't right, although it's probably harmless on x86; liveouts
2324 // should be computed from returns not tail calls. Consider a void
2325 // function making a tail call to a function returning int.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002326 return DAG.getNode(X86ISD::TC_RETURN, dl,
2327 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002328 }
2329
Dale Johannesenace16102009-02-03 19:33:06 +00002330 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002331 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002332
Chris Lattner2d297092006-05-23 18:50:38 +00002333 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002334 unsigned NumBytesForCalleeToPush;
Dan Gohman4d3d6e12010-05-27 18:43:40 +00002335 if (Subtarget->IsCalleePop(isVarArg, CallConv))
Gordon Henriksen86737662008-01-05 16:56:59 +00002336 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Chris Lattner29689432010-03-11 00:22:57 +00002337 else if (!Is64Bit && !IsTailCallConvention(CallConv) && IsStructRet)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002338 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002339 // pops the hidden struct pointer, so we have to push it back.
2340 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002341 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002342 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002343 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002344
Gordon Henriksenae636f82008-01-03 16:47:34 +00002345 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002346 if (!IsSibcall) {
2347 Chain = DAG.getCALLSEQ_END(Chain,
2348 DAG.getIntPtrConstant(NumBytes, true),
2349 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2350 true),
2351 InFlag);
2352 InFlag = Chain.getValue(1);
2353 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002354
Chris Lattner3085e152007-02-25 08:59:22 +00002355 // Handle result values, copying them out of physregs into vregs that we
2356 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002357 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2358 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002359}
2360
Evan Cheng25ab6902006-09-08 06:48:29 +00002361
2362//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002363// Fast Calling Convention (tail call) implementation
2364//===----------------------------------------------------------------------===//
2365
2366// Like std call, callee cleans arguments, convention except that ECX is
2367// reserved for storing the tail called function address. Only 2 registers are
2368// free for argument passing (inreg). Tail call optimization is performed
2369// provided:
2370// * tailcallopt is enabled
2371// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002372// On X86_64 architecture with GOT-style position independent code only local
2373// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002374// To keep the stack aligned according to platform abi the function
2375// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2376// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002377// If a tail called function callee has more arguments than the caller the
2378// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002379// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002380// original REtADDR, but before the saved framepointer or the spilled registers
2381// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2382// stack layout:
2383// arg1
2384// arg2
2385// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002386// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002387// move area ]
2388// (possible EBP)
2389// ESI
2390// EDI
2391// local1 ..
2392
2393/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2394/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002395unsigned
2396X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2397 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002398 MachineFunction &MF = DAG.getMachineFunction();
2399 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002400 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002401 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002402 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002403 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002404 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002405 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2406 // Number smaller than 12 so just add the difference.
2407 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2408 } else {
2409 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002410 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002411 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002412 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002413 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002414}
2415
Evan Cheng5f941932010-02-05 02:21:12 +00002416/// MatchingStackOffset - Return true if the given stack call argument is
2417/// already available in the same position (relatively) of the caller's
2418/// incoming argument stack.
2419static
2420bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2421 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2422 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002423 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2424 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002425 if (Arg.getOpcode() == ISD::CopyFromReg) {
2426 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00002427 if (!TargetRegisterInfo::isVirtualRegister(VR))
Evan Cheng5f941932010-02-05 02:21:12 +00002428 return false;
2429 MachineInstr *Def = MRI->getVRegDef(VR);
2430 if (!Def)
2431 return false;
2432 if (!Flags.isByVal()) {
2433 if (!TII->isLoadFromStackSlot(Def, FI))
2434 return false;
2435 } else {
2436 unsigned Opcode = Def->getOpcode();
2437 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2438 Def->getOperand(1).isFI()) {
2439 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002440 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002441 } else
2442 return false;
2443 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002444 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2445 if (Flags.isByVal())
2446 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002447 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002448 // define @foo(%struct.X* %A) {
2449 // tail call @bar(%struct.X* byval %A)
2450 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002451 return false;
2452 SDValue Ptr = Ld->getBasePtr();
2453 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2454 if (!FINode)
2455 return false;
2456 FI = FINode->getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002457 } else
2458 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002459
Evan Cheng4cae1332010-03-05 08:38:04 +00002460 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002461 if (!MFI->isFixedObjectIndex(FI))
2462 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002463 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002464}
2465
Dan Gohman98ca4f22009-08-05 01:29:28 +00002466/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2467/// for tail call optimization. Targets which want to do tail call
2468/// optimization should implement this function.
2469bool
2470X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002471 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002472 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002473 bool isCalleeStructRet,
2474 bool isCallerStructRet,
Evan Chengb1712452010-01-27 06:25:16 +00002475 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002476 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002477 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002478 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002479 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002480 CalleeCC != CallingConv::C)
2481 return false;
2482
Evan Cheng7096ae42010-01-29 06:45:59 +00002483 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002484 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002485 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Cheng13617962010-04-30 01:12:32 +00002486 CallingConv::ID CallerCC = CallerF->getCallingConv();
2487 bool CCMatch = CallerCC == CalleeCC;
2488
Dan Gohman1797ed52010-02-08 20:27:50 +00002489 if (GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002490 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002491 return true;
2492 return false;
2493 }
2494
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002495 // Look for obvious safe cases to perform tail call optimization that do not
2496 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002497
Evan Cheng2c12cb42010-03-26 16:26:03 +00002498 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2499 // emit a special epilogue.
2500 if (RegInfo->needsStackRealignment(MF))
2501 return false;
2502
Eric Christopher90eb4022010-07-22 00:26:08 +00002503 // Do not sibcall optimize vararg calls unless the call site is not passing
2504 // any arguments.
Evan Cheng3c262ee2010-03-26 02:13:13 +00002505 if (isVarArg && !Outs.empty())
Evan Cheng843bd692010-01-31 06:44:49 +00002506 return false;
2507
Evan Chenga375d472010-03-15 18:54:48 +00002508 // Also avoid sibcall optimization if either caller or callee uses struct
2509 // return semantics.
2510 if (isCalleeStructRet || isCallerStructRet)
2511 return false;
2512
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002513 // If the call result is in ST0 / ST1, it needs to be popped off the x87 stack.
2514 // Therefore if it's not used by the call it is not safe to optimize this into
2515 // a sibcall.
2516 bool Unused = false;
2517 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2518 if (!Ins[i].Used) {
2519 Unused = true;
2520 break;
2521 }
2522 }
2523 if (Unused) {
2524 SmallVector<CCValAssign, 16> RVLocs;
2525 CCState CCInfo(CalleeCC, false, getTargetMachine(),
2526 RVLocs, *DAG.getContext());
2527 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002528 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002529 CCValAssign &VA = RVLocs[i];
2530 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2531 return false;
2532 }
2533 }
2534
Evan Cheng13617962010-04-30 01:12:32 +00002535 // If the calling conventions do not match, then we'd better make sure the
2536 // results are returned in the same way as what the caller expects.
2537 if (!CCMatch) {
2538 SmallVector<CCValAssign, 16> RVLocs1;
2539 CCState CCInfo1(CalleeCC, false, getTargetMachine(),
2540 RVLocs1, *DAG.getContext());
2541 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2542
2543 SmallVector<CCValAssign, 16> RVLocs2;
2544 CCState CCInfo2(CallerCC, false, getTargetMachine(),
2545 RVLocs2, *DAG.getContext());
2546 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2547
2548 if (RVLocs1.size() != RVLocs2.size())
2549 return false;
2550 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2551 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2552 return false;
2553 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2554 return false;
2555 if (RVLocs1[i].isRegLoc()) {
2556 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2557 return false;
2558 } else {
2559 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2560 return false;
2561 }
2562 }
2563 }
2564
Evan Chenga6bff982010-01-30 01:22:00 +00002565 // If the callee takes no arguments then go on to check the results of the
2566 // call.
2567 if (!Outs.empty()) {
2568 // Check if stack adjustment is needed. For now, do not do this if any
2569 // argument is passed on the stack.
2570 SmallVector<CCValAssign, 16> ArgLocs;
2571 CCState CCInfo(CalleeCC, isVarArg, getTargetMachine(),
2572 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002573
2574 // Allocate shadow area for Win64
2575 if (Subtarget->isTargetWin64()) {
2576 CCInfo.AllocateStack(32, 8);
2577 }
2578
Duncan Sands45907662010-10-31 13:21:44 +00002579 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Evan Chengb2c92902010-02-02 02:22:50 +00002580 if (CCInfo.getNextStackOffset()) {
2581 MachineFunction &MF = DAG.getMachineFunction();
2582 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2583 return false;
Evan Chengb2c92902010-02-02 02:22:50 +00002584
2585 // Check if the arguments are already laid out in the right way as
2586 // the caller's fixed stack objects.
2587 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002588 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2589 const X86InstrInfo *TII =
2590 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002591 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2592 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002593 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002594 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002595 if (VA.getLocInfo() == CCValAssign::Indirect)
2596 return false;
2597 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002598 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2599 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002600 return false;
2601 }
2602 }
2603 }
Evan Cheng9c044672010-05-29 01:35:22 +00002604
2605 // If the tailcall address may be in a register, then make sure it's
2606 // possible to register allocate for it. In 32-bit, the call address can
2607 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002608 // callee-saved registers are restored. These happen to be the same
2609 // registers used to pass 'inreg' arguments so watch out for those.
2610 if (!Subtarget->is64Bit() &&
2611 !isa<GlobalAddressSDNode>(Callee) &&
Evan Cheng9c044672010-05-29 01:35:22 +00002612 !isa<ExternalSymbolSDNode>(Callee)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002613 unsigned NumInRegs = 0;
2614 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2615 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002616 if (!VA.isRegLoc())
2617 continue;
2618 unsigned Reg = VA.getLocReg();
2619 switch (Reg) {
2620 default: break;
2621 case X86::EAX: case X86::EDX: case X86::ECX:
2622 if (++NumInRegs == 3)
Evan Cheng9c044672010-05-29 01:35:22 +00002623 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002624 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002625 }
2626 }
2627 }
Evan Chenga6bff982010-01-30 01:22:00 +00002628 }
Evan Chengb1712452010-01-27 06:25:16 +00002629
Dale Johannesend155d7e2010-10-25 22:17:05 +00002630 // An stdcall caller is expected to clean up its arguments; the callee
Dale Johannesen0e034562010-11-12 00:43:18 +00002631 // isn't going to do that.
Dale Johannesend155d7e2010-10-25 22:17:05 +00002632 if (!CCMatch && CallerCC==CallingConv::X86_StdCall)
2633 return false;
2634
Evan Cheng86809cc2010-02-03 03:28:02 +00002635 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002636}
2637
Dan Gohman3df24e62008-09-03 23:12:08 +00002638FastISel *
Dan Gohmana4160c32010-07-07 16:29:44 +00002639X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
2640 return X86::createFastISel(funcInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00002641}
2642
2643
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002644//===----------------------------------------------------------------------===//
2645// Other Lowering Hooks
2646//===----------------------------------------------------------------------===//
2647
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00002648static bool MayFoldLoad(SDValue Op) {
2649 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
2650}
2651
2652static bool MayFoldIntoStore(SDValue Op) {
2653 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
2654}
2655
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002656static bool isTargetShuffle(unsigned Opcode) {
2657 switch(Opcode) {
2658 default: return false;
2659 case X86ISD::PSHUFD:
2660 case X86ISD::PSHUFHW:
2661 case X86ISD::PSHUFLW:
2662 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002663 case X86ISD::PALIGN:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002664 case X86ISD::SHUFPS:
2665 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002666 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002667 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002668 case X86ISD::MOVLPS:
2669 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002670 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002671 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002672 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002673 case X86ISD::MOVSS:
2674 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002675 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002676 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +00002677 case X86ISD::VUNPCKLPS:
2678 case X86ISD::VUNPCKLPD:
2679 case X86ISD::VUNPCKLPSY:
2680 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002681 case X86ISD::PUNPCKLWD:
2682 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002683 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002684 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002685 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002686 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002687 case X86ISD::PUNPCKHWD:
2688 case X86ISD::PUNPCKHBW:
2689 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002690 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002691 return true;
2692 }
2693 return false;
2694}
2695
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002696static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002697 SDValue V1, SelectionDAG &DAG) {
2698 switch(Opc) {
2699 default: llvm_unreachable("Unknown x86 shuffle node");
2700 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002701 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002702 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002703 return DAG.getNode(Opc, dl, VT, V1);
2704 }
2705
2706 return SDValue();
2707}
2708
2709static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00002710 SDValue V1, unsigned TargetMask, SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002711 switch(Opc) {
2712 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002713 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002714 case X86ISD::PSHUFHW:
2715 case X86ISD::PSHUFLW:
2716 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
2717 }
2718
2719 return SDValue();
2720}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002721
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002722static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2723 SDValue V1, SDValue V2, unsigned TargetMask, SelectionDAG &DAG) {
2724 switch(Opc) {
2725 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002726 case X86ISD::PALIGN:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002727 case X86ISD::SHUFPD:
2728 case X86ISD::SHUFPS:
2729 return DAG.getNode(Opc, dl, VT, V1, V2,
2730 DAG.getConstant(TargetMask, MVT::i8));
2731 }
2732 return SDValue();
2733}
2734
2735static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2736 SDValue V1, SDValue V2, SelectionDAG &DAG) {
2737 switch(Opc) {
2738 default: llvm_unreachable("Unknown x86 shuffle node");
2739 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00002740 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002741 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002742 case X86ISD::MOVLPS:
2743 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002744 case X86ISD::MOVSS:
2745 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002746 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002747 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +00002748 case X86ISD::VUNPCKLPS:
2749 case X86ISD::VUNPCKLPD:
2750 case X86ISD::VUNPCKLPSY:
2751 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002752 case X86ISD::PUNPCKLWD:
2753 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002754 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002755 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002756 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002757 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002758 case X86ISD::PUNPCKHWD:
2759 case X86ISD::PUNPCKHBW:
2760 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002761 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002762 return DAG.getNode(Opc, dl, VT, V1, V2);
2763 }
2764 return SDValue();
2765}
2766
Dan Gohmand858e902010-04-17 15:26:15 +00002767SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002768 MachineFunction &MF = DAG.getMachineFunction();
2769 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2770 int ReturnAddrIndex = FuncInfo->getRAIndex();
2771
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002772 if (ReturnAddrIndex == 0) {
2773 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002774 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002775 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00002776 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002777 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002778 }
2779
Evan Cheng25ab6902006-09-08 06:48:29 +00002780 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002781}
2782
2783
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002784bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2785 bool hasSymbolicDisplacement) {
2786 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00002787 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002788 return false;
2789
2790 // If we don't have a symbolic displacement - we don't have any extra
2791 // restrictions.
2792 if (!hasSymbolicDisplacement)
2793 return true;
2794
2795 // FIXME: Some tweaks might be needed for medium code model.
2796 if (M != CodeModel::Small && M != CodeModel::Kernel)
2797 return false;
2798
2799 // For small code model we assume that latest object is 16MB before end of 31
2800 // bits boundary. We may also accept pretty large negative constants knowing
2801 // that all objects are in the positive half of address space.
2802 if (M == CodeModel::Small && Offset < 16*1024*1024)
2803 return true;
2804
2805 // For kernel code model we know that all object resist in the negative half
2806 // of 32bits address space. We may not accept negative offsets, since they may
2807 // be just off and we may accept pretty large positive ones.
2808 if (M == CodeModel::Kernel && Offset > 0)
2809 return true;
2810
2811 return false;
2812}
2813
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002814/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2815/// specific condition code, returning the condition code and the LHS/RHS of the
2816/// comparison to make.
2817static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2818 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00002819 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002820 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2821 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2822 // X > -1 -> X == 0, jump !sign.
2823 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002824 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002825 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2826 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002827 return X86::COND_S;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002828 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00002829 // X < 1 -> X <= 0
2830 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002831 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002832 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002833 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002834
Evan Chengd9558e02006-01-06 00:43:03 +00002835 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002836 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002837 case ISD::SETEQ: return X86::COND_E;
2838 case ISD::SETGT: return X86::COND_G;
2839 case ISD::SETGE: return X86::COND_GE;
2840 case ISD::SETLT: return X86::COND_L;
2841 case ISD::SETLE: return X86::COND_LE;
2842 case ISD::SETNE: return X86::COND_NE;
2843 case ISD::SETULT: return X86::COND_B;
2844 case ISD::SETUGT: return X86::COND_A;
2845 case ISD::SETULE: return X86::COND_BE;
2846 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00002847 }
Chris Lattner4c78e022008-12-23 23:42:27 +00002848 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002849
Chris Lattner4c78e022008-12-23 23:42:27 +00002850 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00002851
Chris Lattner4c78e022008-12-23 23:42:27 +00002852 // If LHS is a foldable load, but RHS is not, flip the condition.
Rafael Espindolaf297c932011-02-03 03:58:05 +00002853 if (ISD::isNON_EXTLoad(LHS.getNode()) &&
2854 !ISD::isNON_EXTLoad(RHS.getNode())) {
Chris Lattner4c78e022008-12-23 23:42:27 +00002855 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
2856 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00002857 }
2858
Chris Lattner4c78e022008-12-23 23:42:27 +00002859 switch (SetCCOpcode) {
2860 default: break;
2861 case ISD::SETOLT:
2862 case ISD::SETOLE:
2863 case ISD::SETUGT:
2864 case ISD::SETUGE:
2865 std::swap(LHS, RHS);
2866 break;
2867 }
2868
2869 // On a floating point condition, the flags are set as follows:
2870 // ZF PF CF op
2871 // 0 | 0 | 0 | X > Y
2872 // 0 | 0 | 1 | X < Y
2873 // 1 | 0 | 0 | X == Y
2874 // 1 | 1 | 1 | unordered
2875 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002876 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00002877 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002878 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00002879 case ISD::SETOLT: // flipped
2880 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002881 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00002882 case ISD::SETOLE: // flipped
2883 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002884 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002885 case ISD::SETUGT: // flipped
2886 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002887 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00002888 case ISD::SETUGE: // flipped
2889 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002890 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002891 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002892 case ISD::SETNE: return X86::COND_NE;
2893 case ISD::SETUO: return X86::COND_P;
2894 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00002895 case ISD::SETOEQ:
2896 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00002897 }
Evan Chengd9558e02006-01-06 00:43:03 +00002898}
2899
Evan Cheng4a460802006-01-11 00:33:36 +00002900/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2901/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00002902/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00002903static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00002904 switch (X86CC) {
2905 default:
2906 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00002907 case X86::COND_B:
2908 case X86::COND_BE:
2909 case X86::COND_E:
2910 case X86::COND_P:
2911 case X86::COND_A:
2912 case X86::COND_AE:
2913 case X86::COND_NE:
2914 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00002915 return true;
2916 }
2917}
2918
Evan Chengeb2f9692009-10-27 19:56:55 +00002919/// isFPImmLegal - Returns true if the target can instruction select the
2920/// specified FP immediate natively. If false, the legalizer will
2921/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00002922bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00002923 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
2924 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
2925 return true;
2926 }
2927 return false;
2928}
2929
Nate Begeman9008ca62009-04-27 18:41:29 +00002930/// isUndefOrInRange - Return true if Val is undef or if its value falls within
2931/// the specified range (L, H].
2932static bool isUndefOrInRange(int Val, int Low, int Hi) {
2933 return (Val < 0) || (Val >= Low && Val < Hi);
2934}
2935
2936/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
2937/// specified value.
2938static bool isUndefOrEqual(int Val, int CmpVal) {
2939 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002940 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00002941 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00002942}
2943
Nate Begeman9008ca62009-04-27 18:41:29 +00002944/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
2945/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
2946/// the second operand.
Owen Andersone50ed302009-08-10 22:56:29 +00002947static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00002948 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00002949 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002950 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00002951 return (Mask[0] < 2 && Mask[1] < 2);
2952 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002953}
2954
Nate Begeman9008ca62009-04-27 18:41:29 +00002955bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002956 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002957 N->getMask(M);
2958 return ::isPSHUFDMask(M, N->getValueType(0));
2959}
Evan Cheng0188ecb2006-03-22 18:59:22 +00002960
Nate Begeman9008ca62009-04-27 18:41:29 +00002961/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
2962/// is suitable for input to PSHUFHW.
Owen Andersone50ed302009-08-10 22:56:29 +00002963static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002964 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00002965 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002966
Nate Begeman9008ca62009-04-27 18:41:29 +00002967 // Lower quadword copied in order or undef.
2968 for (int i = 0; i != 4; ++i)
2969 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00002970 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002971
Evan Cheng506d3df2006-03-29 23:07:14 +00002972 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002973 for (int i = 4; i != 8; ++i)
2974 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00002975 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002976
Evan Cheng506d3df2006-03-29 23:07:14 +00002977 return true;
2978}
2979
Nate Begeman9008ca62009-04-27 18:41:29 +00002980bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002981 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002982 N->getMask(M);
2983 return ::isPSHUFHWMask(M, N->getValueType(0));
2984}
Evan Cheng506d3df2006-03-29 23:07:14 +00002985
Nate Begeman9008ca62009-04-27 18:41:29 +00002986/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
2987/// is suitable for input to PSHUFLW.
Owen Andersone50ed302009-08-10 22:56:29 +00002988static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002989 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00002990 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002991
Rafael Espindola15684b22009-04-24 12:40:33 +00002992 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00002993 for (int i = 4; i != 8; ++i)
2994 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00002995 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002996
Rafael Espindola15684b22009-04-24 12:40:33 +00002997 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002998 for (int i = 0; i != 4; ++i)
2999 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00003000 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003001
Rafael Espindola15684b22009-04-24 12:40:33 +00003002 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003003}
3004
Nate Begeman9008ca62009-04-27 18:41:29 +00003005bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003006 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003007 N->getMask(M);
3008 return ::isPSHUFLWMask(M, N->getValueType(0));
3009}
3010
Nate Begemana09008b2009-10-19 02:17:23 +00003011/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
3012/// is suitable for input to PALIGNR.
3013static bool isPALIGNRMask(const SmallVectorImpl<int> &Mask, EVT VT,
3014 bool hasSSSE3) {
3015 int i, e = VT.getVectorNumElements();
Michael J. Spencerec38de22010-10-10 22:04:20 +00003016
Nate Begemana09008b2009-10-19 02:17:23 +00003017 // Do not handle v2i64 / v2f64 shuffles with palignr.
3018 if (e < 4 || !hasSSSE3)
3019 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003020
Nate Begemana09008b2009-10-19 02:17:23 +00003021 for (i = 0; i != e; ++i)
3022 if (Mask[i] >= 0)
3023 break;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003024
Nate Begemana09008b2009-10-19 02:17:23 +00003025 // All undef, not a palignr.
3026 if (i == e)
3027 return false;
3028
3029 // Determine if it's ok to perform a palignr with only the LHS, since we
3030 // don't have access to the actual shuffle elements to see if RHS is undef.
3031 bool Unary = Mask[i] < (int)e;
3032 bool NeedsUnary = false;
3033
3034 int s = Mask[i] - i;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003035
Nate Begemana09008b2009-10-19 02:17:23 +00003036 // Check the rest of the elements to see if they are consecutive.
3037 for (++i; i != e; ++i) {
3038 int m = Mask[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00003039 if (m < 0)
Nate Begemana09008b2009-10-19 02:17:23 +00003040 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003041
Nate Begemana09008b2009-10-19 02:17:23 +00003042 Unary = Unary && (m < (int)e);
3043 NeedsUnary = NeedsUnary || (m < s);
3044
3045 if (NeedsUnary && !Unary)
3046 return false;
3047 if (Unary && m != ((s+i) & (e-1)))
3048 return false;
3049 if (!Unary && m != (s+i))
3050 return false;
3051 }
3052 return true;
3053}
3054
3055bool X86::isPALIGNRMask(ShuffleVectorSDNode *N) {
3056 SmallVector<int, 8> M;
3057 N->getMask(M);
3058 return ::isPALIGNRMask(M, N->getValueType(0), true);
3059}
3060
Evan Cheng14aed5e2006-03-24 01:18:28 +00003061/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
3062/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Owen Andersone50ed302009-08-10 22:56:29 +00003063static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003064 int NumElems = VT.getVectorNumElements();
3065 if (NumElems != 2 && NumElems != 4)
3066 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003067
Nate Begeman9008ca62009-04-27 18:41:29 +00003068 int Half = NumElems / 2;
3069 for (int i = 0; i < Half; ++i)
3070 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003071 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003072 for (int i = Half; i < NumElems; ++i)
3073 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00003074 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003075
Evan Cheng14aed5e2006-03-24 01:18:28 +00003076 return true;
3077}
3078
Nate Begeman9008ca62009-04-27 18:41:29 +00003079bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
3080 SmallVector<int, 8> M;
3081 N->getMask(M);
3082 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003083}
3084
Evan Cheng213d2cf2007-05-17 18:45:50 +00003085/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00003086/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
3087/// half elements to come from vector 1 (which would equal the dest.) and
3088/// the upper half to come from vector 2.
Owen Andersone50ed302009-08-10 22:56:29 +00003089static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003090 int NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003091
3092 if (NumElems != 2 && NumElems != 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00003093 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003094
Nate Begeman9008ca62009-04-27 18:41:29 +00003095 int Half = NumElems / 2;
3096 for (int i = 0; i < Half; ++i)
3097 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00003098 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003099 for (int i = Half; i < NumElems; ++i)
3100 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003101 return false;
3102 return true;
3103}
3104
Nate Begeman9008ca62009-04-27 18:41:29 +00003105static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
3106 SmallVector<int, 8> M;
3107 N->getMask(M);
3108 return isCommutedSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003109}
3110
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003111/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
3112/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00003113bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
3114 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003115 return false;
3116
Evan Cheng2064a2b2006-03-28 06:50:32 +00003117 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00003118 return isUndefOrEqual(N->getMaskElt(0), 6) &&
3119 isUndefOrEqual(N->getMaskElt(1), 7) &&
3120 isUndefOrEqual(N->getMaskElt(2), 2) &&
3121 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003122}
3123
Nate Begeman0b10b912009-11-07 23:17:15 +00003124/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3125/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3126/// <2, 3, 2, 3>
3127bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
3128 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Michael J. Spencerec38de22010-10-10 22:04:20 +00003129
Nate Begeman0b10b912009-11-07 23:17:15 +00003130 if (NumElems != 4)
3131 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003132
Nate Begeman0b10b912009-11-07 23:17:15 +00003133 return isUndefOrEqual(N->getMaskElt(0), 2) &&
3134 isUndefOrEqual(N->getMaskElt(1), 3) &&
3135 isUndefOrEqual(N->getMaskElt(2), 2) &&
3136 isUndefOrEqual(N->getMaskElt(3), 3);
3137}
3138
Evan Cheng5ced1d82006-04-06 23:23:56 +00003139/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3140/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00003141bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
3142 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003143
Evan Cheng5ced1d82006-04-06 23:23:56 +00003144 if (NumElems != 2 && NumElems != 4)
3145 return false;
3146
Evan Chengc5cdff22006-04-07 21:53:05 +00003147 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003148 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003149 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003150
Evan Chengc5cdff22006-04-07 21:53:05 +00003151 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003152 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003153 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003154
3155 return true;
3156}
3157
Nate Begeman0b10b912009-11-07 23:17:15 +00003158/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3159/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
3160bool X86::isMOVLHPSMask(ShuffleVectorSDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003161 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003162
David Greenea20244d2011-03-02 17:23:43 +00003163 if ((NumElems != 2 && NumElems != 4)
3164 || N->getValueType(0).getSizeInBits() > 128)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003165 return false;
3166
Evan Chengc5cdff22006-04-07 21:53:05 +00003167 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003168 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003169 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003170
Nate Begeman9008ca62009-04-27 18:41:29 +00003171 for (unsigned i = 0; i < NumElems/2; ++i)
3172 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003173 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003174
3175 return true;
3176}
3177
Evan Cheng0038e592006-03-28 00:39:58 +00003178/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3179/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Owen Andersone50ed302009-08-10 22:56:29 +00003180static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003181 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003182 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003183 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng0038e592006-03-28 00:39:58 +00003184 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003185
David Greenea20244d2011-03-02 17:23:43 +00003186 // Handle vector lengths > 128 bits. Define a "section" as a set of
3187 // 128 bits. AVX defines UNPCK* to operate independently on 128-bit
3188 // sections.
3189 unsigned NumSections = VT.getSizeInBits() / 128;
3190 if (NumSections == 0 ) NumSections = 1; // Handle MMX
3191 unsigned NumSectionElts = NumElts / NumSections;
3192
3193 unsigned Start = 0;
3194 unsigned End = NumSectionElts;
3195 for (unsigned s = 0; s < NumSections; ++s) {
3196 for (unsigned i = Start, j = s * NumSectionElts;
3197 i != End;
3198 i += 2, ++j) {
3199 int BitI = Mask[i];
3200 int BitI1 = Mask[i+1];
3201 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003202 return false;
David Greenea20244d2011-03-02 17:23:43 +00003203 if (V2IsSplat) {
3204 if (!isUndefOrEqual(BitI1, NumElts))
3205 return false;
3206 } else {
3207 if (!isUndefOrEqual(BitI1, j + NumElts))
3208 return false;
3209 }
Evan Cheng39623da2006-04-20 08:58:49 +00003210 }
David Greenea20244d2011-03-02 17:23:43 +00003211 // Process the next 128 bits.
3212 Start += NumSectionElts;
3213 End += NumSectionElts;
Evan Cheng0038e592006-03-28 00:39:58 +00003214 }
David Greenea20244d2011-03-02 17:23:43 +00003215
Evan Cheng0038e592006-03-28 00:39:58 +00003216 return true;
3217}
3218
Nate Begeman9008ca62009-04-27 18:41:29 +00003219bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3220 SmallVector<int, 8> M;
3221 N->getMask(M);
3222 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003223}
3224
Evan Cheng4fcb9222006-03-28 02:43:26 +00003225/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3226/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Eric Christopherfd179292009-08-27 18:07:15 +00003227static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003228 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003229 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003230 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng4fcb9222006-03-28 02:43:26 +00003231 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003232
Nate Begeman9008ca62009-04-27 18:41:29 +00003233 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
3234 int BitI = Mask[i];
3235 int BitI1 = Mask[i+1];
Chris Lattner5a88b832007-02-25 07:10:00 +00003236 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengc5cdff22006-04-07 21:53:05 +00003237 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00003238 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00003239 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003240 return false;
3241 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00003242 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003243 return false;
3244 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003245 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003246 return true;
3247}
3248
Nate Begeman9008ca62009-04-27 18:41:29 +00003249bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3250 SmallVector<int, 8> M;
3251 N->getMask(M);
3252 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003253}
3254
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003255/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3256/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3257/// <0, 0, 1, 1>
Owen Andersone50ed302009-08-10 22:56:29 +00003258static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003259 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003260 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003261 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003262
David Greenea20244d2011-03-02 17:23:43 +00003263 // Handle vector lengths > 128 bits. Define a "section" as a set of
3264 // 128 bits. AVX defines UNPCK* to operate independently on 128-bit
3265 // sections.
3266 unsigned NumSections = VT.getSizeInBits() / 128;
3267 if (NumSections == 0 ) NumSections = 1; // Handle MMX
3268 unsigned NumSectionElts = NumElems / NumSections;
3269
3270 for (unsigned s = 0; s < NumSections; ++s) {
3271 for (unsigned i = s * NumSectionElts, j = s * NumSectionElts;
3272 i != NumSectionElts * (s + 1);
3273 i += 2, ++j) {
3274 int BitI = Mask[i];
3275 int BitI1 = Mask[i+1];
3276
3277 if (!isUndefOrEqual(BitI, j))
3278 return false;
3279 if (!isUndefOrEqual(BitI1, j))
3280 return false;
3281 }
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003282 }
David Greenea20244d2011-03-02 17:23:43 +00003283
Rafael Espindola15684b22009-04-24 12:40:33 +00003284 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003285}
3286
Nate Begeman9008ca62009-04-27 18:41:29 +00003287bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
3288 SmallVector<int, 8> M;
3289 N->getMask(M);
3290 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
3291}
3292
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003293/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3294/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3295/// <2, 2, 3, 3>
Owen Andersone50ed302009-08-10 22:56:29 +00003296static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003297 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003298 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
3299 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003300
Nate Begeman9008ca62009-04-27 18:41:29 +00003301 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
3302 int BitI = Mask[i];
3303 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003304 if (!isUndefOrEqual(BitI, j))
3305 return false;
3306 if (!isUndefOrEqual(BitI1, j))
3307 return false;
3308 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003309 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003310}
3311
Nate Begeman9008ca62009-04-27 18:41:29 +00003312bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
3313 SmallVector<int, 8> M;
3314 N->getMask(M);
3315 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
3316}
3317
Evan Cheng017dcc62006-04-21 01:05:10 +00003318/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3319/// specifies a shuffle of elements that is suitable for input to MOVSS,
3320/// MOVSD, and MOVD, i.e. setting the lowest element.
Owen Andersone50ed302009-08-10 22:56:29 +00003321static bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003322 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003323 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003324
3325 int NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003326
Nate Begeman9008ca62009-04-27 18:41:29 +00003327 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003328 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003329
Nate Begeman9008ca62009-04-27 18:41:29 +00003330 for (int i = 1; i < NumElts; ++i)
3331 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003332 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003333
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003334 return true;
3335}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003336
Nate Begeman9008ca62009-04-27 18:41:29 +00003337bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
3338 SmallVector<int, 8> M;
3339 N->getMask(M);
3340 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003341}
3342
Evan Cheng017dcc62006-04-21 01:05:10 +00003343/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
3344/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003345/// element of vector 2 and the other elements to come from vector 1 in order.
Owen Andersone50ed302009-08-10 22:56:29 +00003346static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003347 bool V2IsSplat = false, bool V2IsUndef = false) {
3348 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003349 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003350 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003351
Nate Begeman9008ca62009-04-27 18:41:29 +00003352 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003353 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003354
Nate Begeman9008ca62009-04-27 18:41:29 +00003355 for (int i = 1; i < NumOps; ++i)
3356 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3357 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3358 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003359 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003360
Evan Cheng39623da2006-04-20 08:58:49 +00003361 return true;
3362}
3363
Nate Begeman9008ca62009-04-27 18:41:29 +00003364static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00003365 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003366 SmallVector<int, 8> M;
3367 N->getMask(M);
3368 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00003369}
3370
Evan Chengd9539472006-04-14 21:59:03 +00003371/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3372/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003373bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N) {
3374 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003375 return false;
3376
3377 // Expect 1, 1, 3, 3
Rafael Espindola15684b22009-04-24 12:40:33 +00003378 for (unsigned i = 0; i < 2; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003379 int Elt = N->getMaskElt(i);
3380 if (Elt >= 0 && Elt != 1)
3381 return false;
Rafael Espindola15684b22009-04-24 12:40:33 +00003382 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003383
3384 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003385 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003386 int Elt = N->getMaskElt(i);
3387 if (Elt >= 0 && Elt != 3)
3388 return false;
3389 if (Elt == 3)
3390 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003391 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003392 // Don't use movshdup if it can be done with a shufps.
Nate Begeman9008ca62009-04-27 18:41:29 +00003393 // FIXME: verify that matching u, u, 3, 3 is what we want.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003394 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003395}
3396
3397/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3398/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003399bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N) {
3400 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003401 return false;
3402
3403 // Expect 0, 0, 2, 2
Nate Begeman9008ca62009-04-27 18:41:29 +00003404 for (unsigned i = 0; i < 2; ++i)
3405 if (N->getMaskElt(i) > 0)
3406 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003407
3408 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003409 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003410 int Elt = N->getMaskElt(i);
3411 if (Elt >= 0 && Elt != 2)
3412 return false;
3413 if (Elt == 2)
3414 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003415 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003416 // Don't use movsldup if it can be done with a shufps.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003417 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003418}
3419
Evan Cheng0b457f02008-09-25 20:50:48 +00003420/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3421/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003422bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
3423 int e = N->getValueType(0).getVectorNumElements() / 2;
Eric Christopherfd179292009-08-27 18:07:15 +00003424
Nate Begeman9008ca62009-04-27 18:41:29 +00003425 for (int i = 0; i < e; ++i)
3426 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003427 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003428 for (int i = 0; i < e; ++i)
3429 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003430 return false;
3431 return true;
3432}
3433
David Greenec38a03e2011-02-03 15:50:00 +00003434/// isVEXTRACTF128Index - Return true if the specified
3435/// EXTRACT_SUBVECTOR operand specifies a vector extract that is
3436/// suitable for input to VEXTRACTF128.
3437bool X86::isVEXTRACTF128Index(SDNode *N) {
3438 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3439 return false;
3440
3441 // The index should be aligned on a 128-bit boundary.
3442 uint64_t Index =
3443 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
3444
3445 unsigned VL = N->getValueType(0).getVectorNumElements();
3446 unsigned VBits = N->getValueType(0).getSizeInBits();
3447 unsigned ElSize = VBits / VL;
3448 bool Result = (Index * ElSize) % 128 == 0;
3449
3450 return Result;
3451}
3452
David Greeneccacdc12011-02-04 16:08:29 +00003453/// isVINSERTF128Index - Return true if the specified INSERT_SUBVECTOR
3454/// operand specifies a subvector insert that is suitable for input to
3455/// VINSERTF128.
3456bool X86::isVINSERTF128Index(SDNode *N) {
3457 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
3458 return false;
3459
3460 // The index should be aligned on a 128-bit boundary.
3461 uint64_t Index =
3462 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
3463
3464 unsigned VL = N->getValueType(0).getVectorNumElements();
3465 unsigned VBits = N->getValueType(0).getSizeInBits();
3466 unsigned ElSize = VBits / VL;
3467 bool Result = (Index * ElSize) % 128 == 0;
3468
3469 return Result;
3470}
3471
Evan Cheng63d33002006-03-22 08:01:21 +00003472/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003473/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Evan Cheng63d33002006-03-22 08:01:21 +00003474unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003475 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3476 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
3477
Evan Chengb9df0ca2006-03-22 02:53:00 +00003478 unsigned Shift = (NumOperands == 4) ? 2 : 1;
3479 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00003480 for (int i = 0; i < NumOperands; ++i) {
3481 int Val = SVOp->getMaskElt(NumOperands-i-1);
3482 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00003483 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00003484 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00003485 if (i != NumOperands - 1)
3486 Mask <<= Shift;
3487 }
Evan Cheng63d33002006-03-22 08:01:21 +00003488 return Mask;
3489}
3490
Evan Cheng506d3df2006-03-29 23:07:14 +00003491/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003492/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003493unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003494 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003495 unsigned Mask = 0;
3496 // 8 nodes, but we only care about the last 4.
3497 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003498 int Val = SVOp->getMaskElt(i);
3499 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00003500 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00003501 if (i != 4)
3502 Mask <<= 2;
3503 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003504 return Mask;
3505}
3506
3507/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003508/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003509unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003510 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003511 unsigned Mask = 0;
3512 // 8 nodes, but we only care about the first 4.
3513 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003514 int Val = SVOp->getMaskElt(i);
3515 if (Val >= 0)
3516 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00003517 if (i != 0)
3518 Mask <<= 2;
3519 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003520 return Mask;
3521}
3522
Nate Begemana09008b2009-10-19 02:17:23 +00003523/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
3524/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
3525unsigned X86::getShufflePALIGNRImmediate(SDNode *N) {
3526 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3527 EVT VVT = N->getValueType(0);
3528 unsigned EltSize = VVT.getVectorElementType().getSizeInBits() >> 3;
3529 int Val = 0;
3530
3531 unsigned i, e;
3532 for (i = 0, e = VVT.getVectorNumElements(); i != e; ++i) {
3533 Val = SVOp->getMaskElt(i);
3534 if (Val >= 0)
3535 break;
3536 }
3537 return (Val - i) * EltSize;
3538}
3539
David Greenec38a03e2011-02-03 15:50:00 +00003540/// getExtractVEXTRACTF128Immediate - Return the appropriate immediate
3541/// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF128
3542/// instructions.
3543unsigned X86::getExtractVEXTRACTF128Immediate(SDNode *N) {
3544 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3545 llvm_unreachable("Illegal extract subvector for VEXTRACTF128");
3546
3547 uint64_t Index =
3548 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
3549
3550 EVT VecVT = N->getOperand(0).getValueType();
3551 EVT ElVT = VecVT.getVectorElementType();
3552
3553 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
3554
3555 return Index / NumElemsPerChunk;
3556}
3557
David Greeneccacdc12011-02-04 16:08:29 +00003558/// getInsertVINSERTF128Immediate - Return the appropriate immediate
3559/// to insert at the specified INSERT_SUBVECTOR index with VINSERTF128
3560/// instructions.
3561unsigned X86::getInsertVINSERTF128Immediate(SDNode *N) {
3562 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
3563 llvm_unreachable("Illegal insert subvector for VINSERTF128");
3564
3565 uint64_t Index =
NAKAMURA Takumi27635382011-02-05 15:10:54 +00003566 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
David Greeneccacdc12011-02-04 16:08:29 +00003567
3568 EVT VecVT = N->getValueType(0);
3569 EVT ElVT = VecVT.getVectorElementType();
3570
3571 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
3572
3573 return Index / NumElemsPerChunk;
3574}
3575
Evan Cheng37b73872009-07-30 08:33:02 +00003576/// isZeroNode - Returns true if Elt is a constant zero or a floating point
3577/// constant +0.0.
3578bool X86::isZeroNode(SDValue Elt) {
3579 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00003580 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00003581 (isa<ConstantFPSDNode>(Elt) &&
3582 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
3583}
3584
Nate Begeman9008ca62009-04-27 18:41:29 +00003585/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
3586/// their permute mask.
3587static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
3588 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003589 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003590 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00003591 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00003592
Nate Begeman5a5ca152009-04-29 05:20:52 +00003593 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003594 int idx = SVOp->getMaskElt(i);
3595 if (idx < 0)
3596 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003597 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003598 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003599 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003600 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003601 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003602 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
3603 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003604}
3605
Evan Cheng779ccea2007-12-07 21:30:01 +00003606/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3607/// the two vector operands have swapped position.
Owen Andersone50ed302009-08-10 22:56:29 +00003608static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00003609 unsigned NumElems = VT.getVectorNumElements();
3610 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003611 int idx = Mask[i];
3612 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003613 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003614 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003615 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003616 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003617 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003618 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003619}
3620
Evan Cheng533a0aa2006-04-19 20:35:22 +00003621/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
3622/// match movhlps. The lower half elements should come from upper half of
3623/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003624/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00003625static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
3626 if (Op->getValueType(0).getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00003627 return false;
3628 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003629 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003630 return false;
3631 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003632 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003633 return false;
3634 return true;
3635}
3636
Evan Cheng5ced1d82006-04-06 23:23:56 +00003637/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00003638/// is promoted to a vector. It also returns the LoadSDNode by reference if
3639/// required.
3640static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00003641 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
3642 return false;
3643 N = N->getOperand(0).getNode();
3644 if (!ISD::isNON_EXTLoad(N))
3645 return false;
3646 if (LD)
3647 *LD = cast<LoadSDNode>(N);
3648 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003649}
3650
Evan Cheng533a0aa2006-04-19 20:35:22 +00003651/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
3652/// match movlp{s|d}. The lower half elements should come from lower half of
3653/// V1 (and in order), and the upper half elements should come from the upper
3654/// half of V2 (and in order). And since V1 will become the source of the
3655/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003656static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
3657 ShuffleVectorSDNode *Op) {
Evan Cheng466685d2006-10-09 20:57:25 +00003658 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003659 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00003660 // Is V2 is a vector load, don't do this transformation. We will try to use
3661 // load folding shufps op.
3662 if (ISD::isNON_EXTLoad(V2))
3663 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003664
Nate Begeman5a5ca152009-04-29 05:20:52 +00003665 unsigned NumElems = Op->getValueType(0).getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003666
Evan Cheng533a0aa2006-04-19 20:35:22 +00003667 if (NumElems != 2 && NumElems != 4)
3668 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003669 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003670 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003671 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003672 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003673 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003674 return false;
3675 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003676}
3677
Evan Cheng39623da2006-04-20 08:58:49 +00003678/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
3679/// all the same.
3680static bool isSplatVector(SDNode *N) {
3681 if (N->getOpcode() != ISD::BUILD_VECTOR)
3682 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003683
Dan Gohman475871a2008-07-27 21:46:04 +00003684 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00003685 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
3686 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003687 return false;
3688 return true;
3689}
3690
Evan Cheng213d2cf2007-05-17 18:45:50 +00003691/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00003692/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00003693/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00003694static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00003695 SDValue V1 = N->getOperand(0);
3696 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003697 unsigned NumElems = N->getValueType(0).getVectorNumElements();
3698 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003699 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003700 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003701 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00003702 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
3703 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003704 if (Opc != ISD::BUILD_VECTOR ||
3705 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00003706 return false;
3707 } else if (Idx >= 0) {
3708 unsigned Opc = V1.getOpcode();
3709 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
3710 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003711 if (Opc != ISD::BUILD_VECTOR ||
3712 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00003713 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00003714 }
3715 }
3716 return true;
3717}
3718
3719/// getZeroVector - Returns a vector of specified type with all zero elements.
3720///
Owen Andersone50ed302009-08-10 22:56:29 +00003721static SDValue getZeroVector(EVT VT, bool HasSSE2, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00003722 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003723 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003724
Dale Johannesen0488fb62010-09-30 23:57:10 +00003725 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003726 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00003727 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00003728 if (VT.getSizeInBits() == 128) { // SSE
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003729 if (HasSSE2) { // SSE2
3730 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3731 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
3732 } else { // SSE1
3733 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
3734 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
3735 }
3736 } else if (VT.getSizeInBits() == 256) { // AVX
3737 // 256-bit logic and arithmetic instructions in AVX are
3738 // all floating-point, no support for integer ops. Default
3739 // to emitting fp zeroed vectors then.
Owen Anderson825b72b2009-08-11 20:47:22 +00003740 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003741 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
3742 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
Evan Chengf0df0312008-05-15 08:39:06 +00003743 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003744 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00003745}
3746
Chris Lattner8a594482007-11-25 00:24:49 +00003747/// getOnesVector - Returns a vector of specified type with all bits set.
3748///
Owen Andersone50ed302009-08-10 22:56:29 +00003749static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003750 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003751
Chris Lattner8a594482007-11-25 00:24:49 +00003752 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
3753 // type. This ensures they get CSE'd.
Owen Anderson825b72b2009-08-11 20:47:22 +00003754 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00003755 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00003756 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003757 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00003758}
3759
3760
Evan Cheng39623da2006-04-20 08:58:49 +00003761/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
3762/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00003763static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003764 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003765 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003766
Evan Cheng39623da2006-04-20 08:58:49 +00003767 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003768 SmallVector<int, 8> MaskVec;
3769 SVOp->getMask(MaskVec);
Eric Christopherfd179292009-08-27 18:07:15 +00003770
Nate Begeman5a5ca152009-04-29 05:20:52 +00003771 for (unsigned i = 0; i != NumElems; ++i) {
3772 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003773 MaskVec[i] = NumElems;
3774 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00003775 }
Evan Cheng39623da2006-04-20 08:58:49 +00003776 }
Evan Cheng39623da2006-04-20 08:58:49 +00003777 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00003778 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
3779 SVOp->getOperand(1), &MaskVec[0]);
3780 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00003781}
3782
Evan Cheng017dcc62006-04-21 01:05:10 +00003783/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
3784/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00003785static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003786 SDValue V2) {
3787 unsigned NumElems = VT.getVectorNumElements();
3788 SmallVector<int, 8> Mask;
3789 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00003790 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003791 Mask.push_back(i);
3792 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00003793}
3794
Nate Begeman9008ca62009-04-27 18:41:29 +00003795/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003796static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003797 SDValue V2) {
3798 unsigned NumElems = VT.getVectorNumElements();
3799 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00003800 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003801 Mask.push_back(i);
3802 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00003803 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003804 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00003805}
3806
Nate Begeman9008ca62009-04-27 18:41:29 +00003807/// getUnpackhMask - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003808static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003809 SDValue V2) {
3810 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00003811 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00003812 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00003813 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003814 Mask.push_back(i + Half);
3815 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00003816 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003817 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003818}
3819
Bruno Cardoso Lopesbb0a9482010-08-13 17:50:47 +00003820/// PromoteSplat - Promote a splat of v4i32, v8i16 or v16i8 to v4f32.
3821static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003822 EVT PVT = MVT::v4f32;
Owen Andersone50ed302009-08-10 22:56:29 +00003823 EVT VT = SV->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00003824 DebugLoc dl = SV->getDebugLoc();
3825 SDValue V1 = SV->getOperand(0);
3826 int NumElems = VT.getVectorNumElements();
3827 int EltNo = SV->getSplatIndex();
Rafael Espindola15684b22009-04-24 12:40:33 +00003828
Nate Begeman9008ca62009-04-27 18:41:29 +00003829 // unpack elements to the correct location
3830 while (NumElems > 4) {
3831 if (EltNo < NumElems/2) {
3832 V1 = getUnpackl(DAG, dl, VT, V1, V1);
3833 } else {
3834 V1 = getUnpackh(DAG, dl, VT, V1, V1);
3835 EltNo -= NumElems/2;
3836 }
3837 NumElems >>= 1;
3838 }
Eric Christopherfd179292009-08-27 18:07:15 +00003839
Nate Begeman9008ca62009-04-27 18:41:29 +00003840 // Perform the splat.
3841 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003842 V1 = DAG.getNode(ISD::BITCAST, dl, PVT, V1);
Nate Begeman9008ca62009-04-27 18:41:29 +00003843 V1 = DAG.getVectorShuffle(PVT, dl, V1, DAG.getUNDEF(PVT), &SplatMask[0]);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003844 return DAG.getNode(ISD::BITCAST, dl, VT, V1);
Evan Chengc575ca22006-04-17 20:43:08 +00003845}
3846
Evan Chengba05f722006-04-21 23:03:30 +00003847/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00003848/// vector of zero or undef vector. This produces a shuffle where the low
3849/// element of V2 is swizzled into the zero/undef vector, landing at element
3850/// 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 +00003851static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Chengf0df0312008-05-15 08:39:06 +00003852 bool isZero, bool HasSSE2,
3853 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003854 EVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00003855 SDValue V1 = isZero
Nate Begeman9008ca62009-04-27 18:41:29 +00003856 ? getZeroVector(VT, HasSSE2, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
3857 unsigned NumElems = VT.getVectorNumElements();
3858 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00003859 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003860 // If this is the insertion idx, put the low elt of V2 here.
3861 MaskVec.push_back(i == Idx ? NumElems : i);
3862 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00003863}
3864
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003865/// getShuffleScalarElt - Returns the scalar element that will make up the ith
3866/// element of the result of the vector shuffle.
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003867SDValue getShuffleScalarElt(SDNode *N, int Index, SelectionDAG &DAG,
3868 unsigned Depth) {
3869 if (Depth == 6)
3870 return SDValue(); // Limit search depth.
3871
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003872 SDValue V = SDValue(N, 0);
3873 EVT VT = V.getValueType();
3874 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003875
3876 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
3877 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
3878 Index = SV->getMaskElt(Index);
3879
3880 if (Index < 0)
3881 return DAG.getUNDEF(VT.getVectorElementType());
3882
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003883 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003884 SDValue NewV = (Index < NumElems) ? SV->getOperand(0) : SV->getOperand(1);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003885 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00003886 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003887
3888 // Recurse into target specific vector shuffles to find scalars.
3889 if (isTargetShuffle(Opcode)) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003890 int NumElems = VT.getVectorNumElements();
3891 SmallVector<unsigned, 16> ShuffleMask;
3892 SDValue ImmN;
3893
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003894 switch(Opcode) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003895 case X86ISD::SHUFPS:
3896 case X86ISD::SHUFPD:
3897 ImmN = N->getOperand(N->getNumOperands()-1);
3898 DecodeSHUFPSMask(NumElems,
3899 cast<ConstantSDNode>(ImmN)->getZExtValue(),
3900 ShuffleMask);
3901 break;
3902 case X86ISD::PUNPCKHBW:
3903 case X86ISD::PUNPCKHWD:
3904 case X86ISD::PUNPCKHDQ:
3905 case X86ISD::PUNPCKHQDQ:
3906 DecodePUNPCKHMask(NumElems, ShuffleMask);
3907 break;
3908 case X86ISD::UNPCKHPS:
3909 case X86ISD::UNPCKHPD:
3910 DecodeUNPCKHPMask(NumElems, ShuffleMask);
3911 break;
3912 case X86ISD::PUNPCKLBW:
3913 case X86ISD::PUNPCKLWD:
3914 case X86ISD::PUNPCKLDQ:
3915 case X86ISD::PUNPCKLQDQ:
David Greenec4db4e52011-02-28 19:06:56 +00003916 DecodePUNPCKLMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003917 break;
3918 case X86ISD::UNPCKLPS:
3919 case X86ISD::UNPCKLPD:
David Greenec4db4e52011-02-28 19:06:56 +00003920 case X86ISD::VUNPCKLPS:
3921 case X86ISD::VUNPCKLPD:
3922 case X86ISD::VUNPCKLPSY:
3923 case X86ISD::VUNPCKLPDY:
3924 DecodeUNPCKLPMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003925 break;
3926 case X86ISD::MOVHLPS:
3927 DecodeMOVHLPSMask(NumElems, ShuffleMask);
3928 break;
3929 case X86ISD::MOVLHPS:
3930 DecodeMOVLHPSMask(NumElems, ShuffleMask);
3931 break;
3932 case X86ISD::PSHUFD:
3933 ImmN = N->getOperand(N->getNumOperands()-1);
3934 DecodePSHUFMask(NumElems,
3935 cast<ConstantSDNode>(ImmN)->getZExtValue(),
3936 ShuffleMask);
3937 break;
3938 case X86ISD::PSHUFHW:
3939 ImmN = N->getOperand(N->getNumOperands()-1);
3940 DecodePSHUFHWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
3941 ShuffleMask);
3942 break;
3943 case X86ISD::PSHUFLW:
3944 ImmN = N->getOperand(N->getNumOperands()-1);
3945 DecodePSHUFLWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
3946 ShuffleMask);
3947 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003948 case X86ISD::MOVSS:
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00003949 case X86ISD::MOVSD: {
3950 // The index 0 always comes from the first element of the second source,
3951 // this is why MOVSS and MOVSD are used in the first place. The other
3952 // elements come from the other positions of the first source vector.
3953 unsigned OpNum = (Index == 0) ? 1 : 0;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003954 return getShuffleScalarElt(V.getOperand(OpNum).getNode(), Index, DAG,
3955 Depth+1);
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00003956 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003957 default:
3958 assert("not implemented for target shuffle node");
3959 return SDValue();
3960 }
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003961
3962 Index = ShuffleMask[Index];
3963 if (Index < 0)
3964 return DAG.getUNDEF(VT.getVectorElementType());
3965
3966 SDValue NewV = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
3967 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG,
3968 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003969 }
3970
3971 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003972 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003973 V = V.getOperand(0);
3974 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003975 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003976
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003977 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003978 return SDValue();
3979 }
3980
3981 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
3982 return (Index == 0) ? V.getOperand(0)
3983 : DAG.getUNDEF(VT.getVectorElementType());
3984
3985 if (V.getOpcode() == ISD::BUILD_VECTOR)
3986 return V.getOperand(Index);
3987
3988 return SDValue();
3989}
3990
3991/// getNumOfConsecutiveZeros - Return the number of elements of a vector
3992/// shuffle operation which come from a consecutively from a zero. The
3993/// search can start in two diferent directions, from left or right.
3994static
3995unsigned getNumOfConsecutiveZeros(SDNode *N, int NumElems,
3996 bool ZerosFromLeft, SelectionDAG &DAG) {
3997 int i = 0;
3998
3999 while (i < NumElems) {
4000 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004001 SDValue Elt = getShuffleScalarElt(N, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004002 if (!(Elt.getNode() &&
4003 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
4004 break;
4005 ++i;
4006 }
4007
4008 return i;
4009}
4010
4011/// isShuffleMaskConsecutive - Check if the shuffle mask indicies from MaskI to
4012/// MaskE correspond consecutively to elements from one of the vector operands,
4013/// starting from its index OpIdx. Also tell OpNum which source vector operand.
4014static
4015bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp, int MaskI, int MaskE,
4016 int OpIdx, int NumElems, unsigned &OpNum) {
4017 bool SeenV1 = false;
4018 bool SeenV2 = false;
4019
4020 for (int i = MaskI; i <= MaskE; ++i, ++OpIdx) {
4021 int Idx = SVOp->getMaskElt(i);
4022 // Ignore undef indicies
4023 if (Idx < 0)
4024 continue;
4025
4026 if (Idx < NumElems)
4027 SeenV1 = true;
4028 else
4029 SeenV2 = true;
4030
4031 // Only accept consecutive elements from the same vector
4032 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
4033 return false;
4034 }
4035
4036 OpNum = SeenV1 ? 0 : 1;
4037 return true;
4038}
4039
4040/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
4041/// logical left shift of a vector.
4042static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4043 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4044 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4045 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4046 false /* check zeros from right */, DAG);
4047 unsigned OpSrc;
4048
4049 if (!NumZeros)
4050 return false;
4051
4052 // Considering the elements in the mask that are not consecutive zeros,
4053 // check if they consecutively come from only one of the source vectors.
4054 //
4055 // V1 = {X, A, B, C} 0
4056 // \ \ \ /
4057 // vector_shuffle V1, V2 <1, 2, 3, X>
4058 //
4059 if (!isShuffleMaskConsecutive(SVOp,
4060 0, // Mask Start Index
4061 NumElems-NumZeros-1, // Mask End Index
4062 NumZeros, // Where to start looking in the src vector
4063 NumElems, // Number of elements in vector
4064 OpSrc)) // Which source operand ?
4065 return false;
4066
4067 isLeft = false;
4068 ShAmt = NumZeros;
4069 ShVal = SVOp->getOperand(OpSrc);
4070 return true;
4071}
4072
4073/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
4074/// logical left shift of a vector.
4075static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4076 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4077 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4078 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4079 true /* check zeros from left */, DAG);
4080 unsigned OpSrc;
4081
4082 if (!NumZeros)
4083 return false;
4084
4085 // Considering the elements in the mask that are not consecutive zeros,
4086 // check if they consecutively come from only one of the source vectors.
4087 //
4088 // 0 { A, B, X, X } = V2
4089 // / \ / /
4090 // vector_shuffle V1, V2 <X, X, 4, 5>
4091 //
4092 if (!isShuffleMaskConsecutive(SVOp,
4093 NumZeros, // Mask Start Index
4094 NumElems-1, // Mask End Index
4095 0, // Where to start looking in the src vector
4096 NumElems, // Number of elements in vector
4097 OpSrc)) // Which source operand ?
4098 return false;
4099
4100 isLeft = true;
4101 ShAmt = NumZeros;
4102 ShVal = SVOp->getOperand(OpSrc);
4103 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004104}
4105
4106/// isVectorShift - Returns true if the shuffle can be implemented as a
4107/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004108static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00004109 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004110 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
4111 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
4112 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004113
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004114 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00004115}
4116
Evan Chengc78d3b42006-04-24 18:01:45 +00004117/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
4118///
Dan Gohman475871a2008-07-27 21:46:04 +00004119static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00004120 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00004121 SelectionDAG &DAG,
4122 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004123 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00004124 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004125
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004126 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004127 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004128 bool First = true;
4129 for (unsigned i = 0; i < 16; ++i) {
4130 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
4131 if (ThisIsNonZero && First) {
4132 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004133 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004134 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004135 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004136 First = false;
4137 }
4138
4139 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00004140 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004141 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
4142 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004143 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004144 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00004145 }
4146 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004147 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
4148 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
4149 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00004150 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004151 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00004152 } else
4153 ThisElt = LastElt;
4154
Gabor Greifba36cb52008-08-28 21:40:38 +00004155 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00004156 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00004157 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00004158 }
4159 }
4160
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004161 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00004162}
4163
Bill Wendlinga348c562007-03-22 18:42:45 +00004164/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00004165///
Dan Gohman475871a2008-07-27 21:46:04 +00004166static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00004167 unsigned NumNonZero, unsigned NumZero,
4168 SelectionDAG &DAG,
4169 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004170 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00004171 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004172
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004173 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004174 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004175 bool First = true;
4176 for (unsigned i = 0; i < 8; ++i) {
4177 bool isNonZero = (NonZeros & (1 << i)) != 0;
4178 if (isNonZero) {
4179 if (First) {
4180 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004181 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004182 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004183 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004184 First = false;
4185 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004186 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004187 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00004188 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00004189 }
4190 }
4191
4192 return V;
4193}
4194
Evan Chengf26ffe92008-05-29 08:22:04 +00004195/// getVShift - Return a vector logical shift node.
4196///
Owen Andersone50ed302009-08-10 22:56:29 +00004197static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00004198 unsigned NumBits, SelectionDAG &DAG,
4199 const TargetLowering &TLI, DebugLoc dl) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004200 EVT ShVT = MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00004201 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004202 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
4203 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004204 DAG.getNode(Opc, dl, ShVT, SrcOp,
Owen Anderson95771af2011-02-25 21:41:48 +00004205 DAG.getConstant(NumBits,
4206 TLI.getShiftAmountTy(SrcOp.getValueType()))));
Evan Chengf26ffe92008-05-29 08:22:04 +00004207}
4208
Dan Gohman475871a2008-07-27 21:46:04 +00004209SDValue
Evan Chengc3630942009-12-09 21:00:30 +00004210X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00004211 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00004212
Evan Chengc3630942009-12-09 21:00:30 +00004213 // Check if the scalar load can be widened into a vector load. And if
4214 // the address is "base + cst" see if the cst can be "absorbed" into
4215 // the shuffle mask.
4216 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
4217 SDValue Ptr = LD->getBasePtr();
4218 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
4219 return SDValue();
4220 EVT PVT = LD->getValueType(0);
4221 if (PVT != MVT::i32 && PVT != MVT::f32)
4222 return SDValue();
4223
4224 int FI = -1;
4225 int64_t Offset = 0;
4226 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
4227 FI = FINode->getIndex();
4228 Offset = 0;
Chris Lattner0a9481f2011-02-13 22:25:43 +00004229 } else if (DAG.isBaseWithConstantOffset(Ptr) &&
Evan Chengc3630942009-12-09 21:00:30 +00004230 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4231 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4232 Offset = Ptr.getConstantOperandVal(1);
4233 Ptr = Ptr.getOperand(0);
4234 } else {
4235 return SDValue();
4236 }
4237
4238 SDValue Chain = LD->getChain();
4239 // Make sure the stack object alignment is at least 16.
4240 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
4241 if (DAG.InferPtrAlignment(Ptr) < 16) {
4242 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00004243 // Can't change the alignment. FIXME: It's possible to compute
4244 // the exact stack offset and reference FI + adjust offset instead.
4245 // If someone *really* cares about this. That's the way to implement it.
4246 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004247 } else {
4248 MFI->setObjectAlignment(FI, 16);
4249 }
4250 }
4251
4252 // (Offset % 16) must be multiple of 4. Then address is then
4253 // Ptr + (Offset & ~15).
4254 if (Offset < 0)
4255 return SDValue();
4256 if ((Offset % 16) & 3)
4257 return SDValue();
4258 int64_t StartOffset = Offset & ~15;
4259 if (StartOffset)
4260 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
4261 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
4262
4263 int EltNo = (Offset - StartOffset) >> 2;
4264 int Mask[4] = { EltNo, EltNo, EltNo, EltNo };
4265 EVT VT = (PVT == MVT::i32) ? MVT::v4i32 : MVT::v4f32;
Chris Lattner51abfe42010-09-21 06:02:19 +00004266 SDValue V1 = DAG.getLoad(VT, dl, Chain, Ptr,
4267 LD->getPointerInfo().getWithOffset(StartOffset),
David Greene67c9d422010-02-15 16:53:33 +00004268 false, false, 0);
Evan Chengc3630942009-12-09 21:00:30 +00004269 // Canonicalize it to a v4i32 shuffle.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004270 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, V1);
4271 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Chengc3630942009-12-09 21:00:30 +00004272 DAG.getVectorShuffle(MVT::v4i32, dl, V1,
Chris Lattner51abfe42010-09-21 06:02:19 +00004273 DAG.getUNDEF(MVT::v4i32),&Mask[0]));
Evan Chengc3630942009-12-09 21:00:30 +00004274 }
4275
4276 return SDValue();
4277}
4278
Michael J. Spencerec38de22010-10-10 22:04:20 +00004279/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
4280/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00004281/// load which has the same value as a build_vector whose operands are 'elts'.
4282///
4283/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00004284///
Nate Begeman1449f292010-03-24 22:19:06 +00004285/// FIXME: we'd also like to handle the case where the last elements are zero
4286/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
4287/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004288static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00004289 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004290 EVT EltVT = VT.getVectorElementType();
4291 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00004292
Nate Begemanfdea31a2010-03-24 20:49:50 +00004293 LoadSDNode *LDBase = NULL;
4294 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004295
Nate Begeman1449f292010-03-24 22:19:06 +00004296 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00004297 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00004298 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004299 for (unsigned i = 0; i < NumElems; ++i) {
4300 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00004301
Nate Begemanfdea31a2010-03-24 20:49:50 +00004302 if (!Elt.getNode() ||
4303 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
4304 return SDValue();
4305 if (!LDBase) {
4306 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
4307 return SDValue();
4308 LDBase = cast<LoadSDNode>(Elt.getNode());
4309 LastLoadedElt = i;
4310 continue;
4311 }
4312 if (Elt.getOpcode() == ISD::UNDEF)
4313 continue;
4314
4315 LoadSDNode *LD = cast<LoadSDNode>(Elt);
4316 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
4317 return SDValue();
4318 LastLoadedElt = i;
4319 }
Nate Begeman1449f292010-03-24 22:19:06 +00004320
4321 // If we have found an entire vector of loads and undefs, then return a large
4322 // load of the entire vector width starting at the base pointer. If we found
4323 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004324 if (LastLoadedElt == NumElems - 1) {
4325 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00004326 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004327 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004328 LDBase->isVolatile(), LDBase->isNonTemporal(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00004329 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004330 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004331 LDBase->isVolatile(), LDBase->isNonTemporal(),
4332 LDBase->getAlignment());
4333 } else if (NumElems == 4 && LastLoadedElt == 1) {
4334 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
4335 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Chris Lattner88641552010-09-22 00:34:38 +00004336 SDValue ResNode = DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys,
4337 Ops, 2, MVT::i32,
4338 LDBase->getMemOperand());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004339 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00004340 }
4341 return SDValue();
4342}
4343
Evan Chengc3630942009-12-09 21:00:30 +00004344SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004345X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004346 DebugLoc dl = Op.getDebugLoc();
David Greenea5f26012011-02-07 19:36:54 +00004347
David Greenef125a292011-02-08 19:04:41 +00004348 EVT VT = Op.getValueType();
4349 EVT ExtVT = VT.getVectorElementType();
4350
4351 unsigned NumElems = Op.getNumOperands();
4352
4353 // For AVX-length vectors, build the individual 128-bit pieces and
4354 // use shuffles to put them in place.
Owen Anderson95771af2011-02-25 21:41:48 +00004355 if (VT.getSizeInBits() > 256 &&
4356 Subtarget->hasAVX() &&
David Greenef125a292011-02-08 19:04:41 +00004357 !ISD::isBuildVectorAllZeros(Op.getNode())) {
4358 SmallVector<SDValue, 8> V;
4359 V.resize(NumElems);
4360 for (unsigned i = 0; i < NumElems; ++i) {
4361 V[i] = Op.getOperand(i);
4362 }
Owen Anderson95771af2011-02-25 21:41:48 +00004363
David Greenef125a292011-02-08 19:04:41 +00004364 EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElems/2);
4365
4366 // Build the lower subvector.
4367 SDValue Lower = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[0], NumElems/2);
4368 // Build the upper subvector.
4369 SDValue Upper = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[NumElems / 2],
4370 NumElems/2);
4371
4372 return ConcatVectors(Lower, Upper, DAG);
4373 }
4374
Chris Lattner6e80e442010-08-28 17:15:43 +00004375 // All zero's are handled with pxor in SSE2 and above, xorps in SSE1.
4376 // All one's are handled with pcmpeqd. In AVX, zero's are handled with
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004377 // vpxor in 128-bit and xor{pd,ps} in 256-bit, but no 256 version of pcmpeqd
4378 // is present, so AllOnes is ignored.
4379 if (ISD::isBuildVectorAllZeros(Op.getNode()) ||
4380 (Op.getValueType().getSizeInBits() != 256 &&
4381 ISD::isBuildVectorAllOnes(Op.getNode()))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004382 // Canonicalize this to <4 x i32> (SSE) to
Chris Lattner8a594482007-11-25 00:24:49 +00004383 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
4384 // eliminated on x86-32 hosts.
Dale Johannesen0488fb62010-09-30 23:57:10 +00004385 if (Op.getValueType() == MVT::v4i32)
Chris Lattner8a594482007-11-25 00:24:49 +00004386 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004387
Gabor Greifba36cb52008-08-28 21:40:38 +00004388 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00004389 return getOnesVector(Op.getValueType(), DAG, dl);
4390 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00004391 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004392
Owen Andersone50ed302009-08-10 22:56:29 +00004393 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004394
Evan Cheng0db9fe62006-04-25 20:13:52 +00004395 unsigned NumZero = 0;
4396 unsigned NumNonZero = 0;
4397 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004398 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00004399 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004400 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00004401 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00004402 if (Elt.getOpcode() == ISD::UNDEF)
4403 continue;
4404 Values.insert(Elt);
4405 if (Elt.getOpcode() != ISD::Constant &&
4406 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004407 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00004408 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00004409 NumZero++;
4410 else {
4411 NonZeros |= (1 << i);
4412 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004413 }
4414 }
4415
Chris Lattner97a2a562010-08-26 05:24:29 +00004416 // All undef vector. Return an UNDEF. All zero vectors were handled above.
4417 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00004418 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004419
Chris Lattner67f453a2008-03-09 05:42:06 +00004420 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00004421 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004422 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00004423 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00004424
Chris Lattner62098042008-03-09 01:05:04 +00004425 // If this is an insertion of an i64 value on x86-32, and if the top bits of
4426 // the value are obviously zero, truncate the value to i32 and do the
4427 // insertion that way. Only do this if the value is non-constant or if the
4428 // value is a constant being inserted into element 0. It is cheaper to do
4429 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00004430 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00004431 (!IsAllConstants || Idx == 0)) {
4432 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004433 // Handle SSE only.
4434 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
4435 EVT VecVT = MVT::v4i32;
4436 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00004437
Chris Lattner62098042008-03-09 01:05:04 +00004438 // Truncate the value (which may itself be a constant) to i32, and
4439 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00004440 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00004441 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00004442 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
4443 Subtarget->hasSSE2(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00004444
Chris Lattner62098042008-03-09 01:05:04 +00004445 // Now we have our 32-bit value zero extended in the low element of
4446 // a vector. If Idx != 0, swizzle it into place.
4447 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004448 SmallVector<int, 4> Mask;
4449 Mask.push_back(Idx);
4450 for (unsigned i = 1; i != VecElts; ++i)
4451 Mask.push_back(i);
4452 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopherfd179292009-08-27 18:07:15 +00004453 DAG.getUNDEF(Item.getValueType()),
Nate Begeman9008ca62009-04-27 18:41:29 +00004454 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004455 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004456 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00004457 }
4458 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004459
Chris Lattner19f79692008-03-08 22:59:52 +00004460 // If we have a constant or non-constant insertion into the low element of
4461 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
4462 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00004463 // depending on what the source datatype is.
4464 if (Idx == 0) {
4465 if (NumZero == 0) {
4466 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Owen Anderson825b72b2009-08-11 20:47:22 +00004467 } else if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
4468 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Eli Friedman10415532009-06-06 06:05:10 +00004469 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
4470 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
4471 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget->hasSSE2(),
4472 DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00004473 } else if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
4474 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Dale Johannesen0488fb62010-09-30 23:57:10 +00004475 assert(VT.getSizeInBits() == 128 && "Expected an SSE value type!");
4476 EVT MiddleVT = MVT::v4i32;
Eli Friedman10415532009-06-06 06:05:10 +00004477 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
4478 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
4479 Subtarget->hasSSE2(), DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004480 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00004481 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004482 }
Evan Chengf26ffe92008-05-29 08:22:04 +00004483
4484 // Is it a vector logical left shift?
4485 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00004486 X86::isZeroNode(Op.getOperand(0)) &&
4487 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004488 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00004489 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00004490 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004491 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00004492 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004493 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004494
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004495 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00004496 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004497
Chris Lattner19f79692008-03-08 22:59:52 +00004498 // Otherwise, if this is a vector with i32 or f32 elements, and the element
4499 // is a non-constant being inserted into an element other than the low one,
4500 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
4501 // movd/movss) to move this into the low element, then shuffle it into
4502 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004503 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00004504 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00004505
Evan Cheng0db9fe62006-04-25 20:13:52 +00004506 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00004507 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
4508 Subtarget->hasSSE2(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00004509 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004510 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00004511 MaskVec.push_back(i == Idx ? 0 : 1);
4512 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004513 }
4514 }
4515
Chris Lattner67f453a2008-03-09 05:42:06 +00004516 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00004517 if (Values.size() == 1) {
4518 if (EVTBits == 32) {
4519 // Instead of a shuffle like this:
4520 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
4521 // Check if it's possible to issue this instead.
4522 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
4523 unsigned Idx = CountTrailingZeros_32(NonZeros);
4524 SDValue Item = Op.getOperand(Idx);
4525 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
4526 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
4527 }
Dan Gohman475871a2008-07-27 21:46:04 +00004528 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004529 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004530
Dan Gohmana3941172007-07-24 22:55:08 +00004531 // A vector full of immediates; various special cases are already
4532 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004533 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00004534 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00004535
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00004536 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00004537 if (EVTBits == 64) {
4538 if (NumNonZero == 1) {
4539 // One half is zero or undef.
4540 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00004541 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00004542 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00004543 return getShuffleVectorZeroOrUndef(V2, Idx, true,
4544 Subtarget->hasSSE2(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00004545 }
Dan Gohman475871a2008-07-27 21:46:04 +00004546 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00004547 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004548
4549 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00004550 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00004551 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00004552 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004553 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004554 }
4555
Bill Wendling826f36f2007-03-28 00:57:11 +00004556 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00004557 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Chris Lattner97a2a562010-08-26 05:24:29 +00004558 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004559 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004560 }
4561
4562 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00004563 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00004564 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004565 if (NumElems == 4 && NumZero > 0) {
4566 for (unsigned i = 0; i < 4; ++i) {
4567 bool isZero = !(NonZeros & (1 << i));
4568 if (isZero)
Dale Johannesenace16102009-02-03 19:33:06 +00004569 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004570 else
Dale Johannesenace16102009-02-03 19:33:06 +00004571 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004572 }
4573
4574 for (unsigned i = 0; i < 2; ++i) {
4575 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
4576 default: break;
4577 case 0:
4578 V[i] = V[i*2]; // Must be a zero vector.
4579 break;
4580 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00004581 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004582 break;
4583 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00004584 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004585 break;
4586 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00004587 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004588 break;
4589 }
4590 }
4591
Nate Begeman9008ca62009-04-27 18:41:29 +00004592 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004593 bool Reverse = (NonZeros & 0x3) == 2;
4594 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004595 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004596 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
4597 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004598 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
4599 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004600 }
4601
Nate Begemanfdea31a2010-03-24 20:49:50 +00004602 if (Values.size() > 1 && VT.getSizeInBits() == 128) {
4603 // Check for a build vector of consecutive loads.
4604 for (unsigned i = 0; i < NumElems; ++i)
4605 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00004606
Nate Begemanfdea31a2010-03-24 20:49:50 +00004607 // Check for elements which are consecutive loads.
4608 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
4609 if (LD.getNode())
4610 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004611
4612 // For SSE 4.1, use insertps to put the high elements into the low element.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004613 if (getSubtarget()->hasSSE41()) {
Chris Lattner24faf612010-08-28 17:59:08 +00004614 SDValue Result;
4615 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
4616 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
4617 else
4618 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00004619
Chris Lattner24faf612010-08-28 17:59:08 +00004620 for (unsigned i = 1; i < NumElems; ++i) {
4621 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
4622 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00004623 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00004624 }
4625 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00004626 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00004627
Chris Lattner6e80e442010-08-28 17:15:43 +00004628 // Otherwise, expand into a number of unpckl*, start by extending each of
4629 // our (non-undef) elements to the full vector width with the element in the
4630 // bottom slot of the vector (which generates no code for SSE).
4631 for (unsigned i = 0; i < NumElems; ++i) {
4632 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
4633 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
4634 else
4635 V[i] = DAG.getUNDEF(VT);
4636 }
4637
4638 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004639 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
4640 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
4641 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00004642 unsigned EltStride = NumElems >> 1;
4643 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00004644 for (unsigned i = 0; i < EltStride; ++i) {
4645 // If V[i+EltStride] is undef and this is the first round of mixing,
4646 // then it is safe to just drop this shuffle: V[i] is already in the
4647 // right place, the one element (since it's the first round) being
4648 // inserted as undef can be dropped. This isn't safe for successive
4649 // rounds because they will permute elements within both vectors.
4650 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
4651 EltStride == NumElems/2)
4652 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004653
Chris Lattner6e80e442010-08-28 17:15:43 +00004654 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00004655 }
Chris Lattner6e80e442010-08-28 17:15:43 +00004656 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004657 }
4658 return V[0];
4659 }
Dan Gohman475871a2008-07-27 21:46:04 +00004660 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004661}
4662
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004663SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004664X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const {
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004665 // We support concatenate two MMX registers and place them in a MMX
4666 // register. This is better than doing a stack convert.
4667 DebugLoc dl = Op.getDebugLoc();
4668 EVT ResVT = Op.getValueType();
4669 assert(Op.getNumOperands() == 2);
4670 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
4671 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
4672 int Mask[2];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004673 SDValue InVec = DAG.getNode(ISD::BITCAST,dl, MVT::v1i64, Op.getOperand(0));
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004674 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4675 InVec = Op.getOperand(1);
4676 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4677 unsigned NumElts = ResVT.getVectorNumElements();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004678 VecOp = DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004679 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
4680 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
4681 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004682 InVec = DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, InVec);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004683 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4684 Mask[0] = 0; Mask[1] = 2;
4685 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
4686 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004687 return DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004688}
4689
Nate Begemanb9a47b82009-02-23 08:49:38 +00004690// v8i16 shuffles - Prefer shuffles in the following order:
4691// 1. [all] pshuflw, pshufhw, optional move
4692// 2. [ssse3] 1 x pshufb
4693// 3. [ssse3] 2 x pshufb + 1 x por
4694// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004695SDValue
4696X86TargetLowering::LowerVECTOR_SHUFFLEv8i16(SDValue Op,
4697 SelectionDAG &DAG) const {
4698 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00004699 SDValue V1 = SVOp->getOperand(0);
4700 SDValue V2 = SVOp->getOperand(1);
4701 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004702 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00004703
Nate Begemanb9a47b82009-02-23 08:49:38 +00004704 // Determine if more than 1 of the words in each of the low and high quadwords
4705 // of the result come from the same quadword of one of the two inputs. Undef
4706 // mask values count as coming from any quadword, for better codegen.
4707 SmallVector<unsigned, 4> LoQuad(4);
4708 SmallVector<unsigned, 4> HiQuad(4);
4709 BitVector InputQuads(4);
4710 for (unsigned i = 0; i < 8; ++i) {
4711 SmallVectorImpl<unsigned> &Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00004712 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004713 MaskVals.push_back(EltIdx);
4714 if (EltIdx < 0) {
4715 ++Quad[0];
4716 ++Quad[1];
4717 ++Quad[2];
4718 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00004719 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004720 }
4721 ++Quad[EltIdx / 4];
4722 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00004723 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004724
Nate Begemanb9a47b82009-02-23 08:49:38 +00004725 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004726 unsigned MaxQuad = 1;
4727 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004728 if (LoQuad[i] > MaxQuad) {
4729 BestLoQuad = i;
4730 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004731 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004732 }
4733
Nate Begemanb9a47b82009-02-23 08:49:38 +00004734 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004735 MaxQuad = 1;
4736 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004737 if (HiQuad[i] > MaxQuad) {
4738 BestHiQuad = i;
4739 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004740 }
4741 }
4742
Nate Begemanb9a47b82009-02-23 08:49:38 +00004743 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00004744 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00004745 // single pshufb instruction is necessary. If There are more than 2 input
4746 // quads, disable the next transformation since it does not help SSSE3.
4747 bool V1Used = InputQuads[0] || InputQuads[1];
4748 bool V2Used = InputQuads[2] || InputQuads[3];
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004749 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004750 if (InputQuads.count() == 2 && V1Used && V2Used) {
4751 BestLoQuad = InputQuads.find_first();
4752 BestHiQuad = InputQuads.find_next(BestLoQuad);
4753 }
4754 if (InputQuads.count() > 2) {
4755 BestLoQuad = -1;
4756 BestHiQuad = -1;
4757 }
4758 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004759
Nate Begemanb9a47b82009-02-23 08:49:38 +00004760 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
4761 // the shuffle mask. If a quad is scored as -1, that means that it contains
4762 // words from all 4 input quadwords.
4763 SDValue NewV;
4764 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004765 SmallVector<int, 8> MaskV;
4766 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
4767 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Eric Christopherfd179292009-08-27 18:07:15 +00004768 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004769 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
4770 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
4771 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00004772
Nate Begemanb9a47b82009-02-23 08:49:38 +00004773 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
4774 // source words for the shuffle, to aid later transformations.
4775 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00004776 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00004777 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004778 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00004779 if (idx != (int)i)
4780 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004781 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00004782 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004783 AllWordsInNewV = false;
4784 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00004785 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004786
Nate Begemanb9a47b82009-02-23 08:49:38 +00004787 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
4788 if (AllWordsInNewV) {
4789 for (int i = 0; i != 8; ++i) {
4790 int idx = MaskVals[i];
4791 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00004792 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004793 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004794 if ((idx != i) && idx < 4)
4795 pshufhw = false;
4796 if ((idx != i) && idx > 3)
4797 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00004798 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00004799 V1 = NewV;
4800 V2Used = false;
4801 BestLoQuad = 0;
4802 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004803 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004804
Nate Begemanb9a47b82009-02-23 08:49:38 +00004805 // If we've eliminated the use of V2, and the new mask is a pshuflw or
4806 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00004807 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00004808 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
4809 unsigned TargetMask = 0;
4810 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00004811 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00004812 TargetMask = pshufhw ? X86::getShufflePSHUFHWImmediate(NewV.getNode()):
4813 X86::getShufflePSHUFLWImmediate(NewV.getNode());
4814 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004815 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00004816 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004817 }
Eric Christopherfd179292009-08-27 18:07:15 +00004818
Nate Begemanb9a47b82009-02-23 08:49:38 +00004819 // If we have SSSE3, and all words of the result are from 1 input vector,
4820 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
4821 // is present, fall back to case 4.
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004822 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004823 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004824
Nate Begemanb9a47b82009-02-23 08:49:38 +00004825 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00004826 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00004827 // mask, and elements that come from V1 in the V2 mask, so that the two
4828 // results can be OR'd together.
4829 bool TwoInputs = V1Used && V2Used;
4830 for (unsigned i = 0; i != 8; ++i) {
4831 int EltIdx = MaskVals[i] * 2;
4832 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004833 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4834 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004835 continue;
4836 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004837 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
4838 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004839 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004840 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004841 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004842 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004843 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004844 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004845 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004846
Nate Begemanb9a47b82009-02-23 08:49:38 +00004847 // Calculate the shuffle mask for the second input, shuffle it, and
4848 // OR it with the first shuffled input.
4849 pshufbMask.clear();
4850 for (unsigned i = 0; i != 8; ++i) {
4851 int EltIdx = MaskVals[i] * 2;
4852 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004853 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4854 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004855 continue;
4856 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004857 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
4858 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004859 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004860 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00004861 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00004862 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004863 MVT::v16i8, &pshufbMask[0], 16));
4864 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004865 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004866 }
4867
4868 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
4869 // and update MaskVals with new element order.
4870 BitVector InOrder(8);
4871 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004872 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004873 for (int i = 0; i != 4; ++i) {
4874 int idx = MaskVals[i];
4875 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004876 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004877 InOrder.set(i);
4878 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004879 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004880 InOrder.set(i);
4881 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004882 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004883 }
4884 }
4885 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004886 MaskV.push_back(i);
Owen Anderson825b72b2009-08-11 20:47:22 +00004887 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004888 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004889
4890 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
4891 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
4892 NewV.getOperand(0),
4893 X86::getShufflePSHUFLWImmediate(NewV.getNode()),
4894 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004895 }
Eric Christopherfd179292009-08-27 18:07:15 +00004896
Nate Begemanb9a47b82009-02-23 08:49:38 +00004897 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
4898 // and update MaskVals with the new element order.
4899 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004900 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004901 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004902 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004903 for (unsigned i = 4; i != 8; ++i) {
4904 int idx = MaskVals[i];
4905 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004906 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004907 InOrder.set(i);
4908 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004909 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004910 InOrder.set(i);
4911 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004912 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004913 }
4914 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004915 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004916 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004917
4918 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
4919 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
4920 NewV.getOperand(0),
4921 X86::getShufflePSHUFHWImmediate(NewV.getNode()),
4922 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004923 }
Eric Christopherfd179292009-08-27 18:07:15 +00004924
Nate Begemanb9a47b82009-02-23 08:49:38 +00004925 // In case BestHi & BestLo were both -1, which means each quadword has a word
4926 // from each of the four input quadwords, calculate the InOrder bitvector now
4927 // before falling through to the insert/extract cleanup.
4928 if (BestLoQuad == -1 && BestHiQuad == -1) {
4929 NewV = V1;
4930 for (int i = 0; i != 8; ++i)
4931 if (MaskVals[i] < 0 || MaskVals[i] == i)
4932 InOrder.set(i);
4933 }
Eric Christopherfd179292009-08-27 18:07:15 +00004934
Nate Begemanb9a47b82009-02-23 08:49:38 +00004935 // The other elements are put in the right place using pextrw and pinsrw.
4936 for (unsigned i = 0; i != 8; ++i) {
4937 if (InOrder[i])
4938 continue;
4939 int EltIdx = MaskVals[i];
4940 if (EltIdx < 0)
4941 continue;
4942 SDValue ExtOp = (EltIdx < 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00004943 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004944 DAG.getIntPtrConstant(EltIdx))
Owen Anderson825b72b2009-08-11 20:47:22 +00004945 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004946 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00004947 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004948 DAG.getIntPtrConstant(i));
4949 }
4950 return NewV;
4951}
4952
4953// v16i8 shuffles - Prefer shuffles in the following order:
4954// 1. [ssse3] 1 x pshufb
4955// 2. [ssse3] 2 x pshufb + 1 x por
4956// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
4957static
Nate Begeman9008ca62009-04-27 18:41:29 +00004958SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00004959 SelectionDAG &DAG,
4960 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004961 SDValue V1 = SVOp->getOperand(0);
4962 SDValue V2 = SVOp->getOperand(1);
4963 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004964 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00004965 SVOp->getMask(MaskVals);
Eric Christopherfd179292009-08-27 18:07:15 +00004966
Nate Begemanb9a47b82009-02-23 08:49:38 +00004967 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00004968 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00004969 // present, fall back to case 3.
4970 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
4971 bool V1Only = true;
4972 bool V2Only = true;
4973 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004974 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00004975 if (EltIdx < 0)
4976 continue;
4977 if (EltIdx < 16)
4978 V2Only = false;
4979 else
4980 V1Only = false;
4981 }
Eric Christopherfd179292009-08-27 18:07:15 +00004982
Nate Begemanb9a47b82009-02-23 08:49:38 +00004983 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
4984 if (TLI.getSubtarget()->hasSSSE3()) {
4985 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004986
Nate Begemanb9a47b82009-02-23 08:49:38 +00004987 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00004988 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00004989 //
4990 // Otherwise, we have elements from both input vectors, and must zero out
4991 // elements that come from V2 in the first mask, and V1 in the second mask
4992 // so that we can OR them together.
4993 bool TwoInputs = !(V1Only || V2Only);
4994 for (unsigned i = 0; i != 16; ++i) {
4995 int EltIdx = MaskVals[i];
4996 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004997 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004998 continue;
4999 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005000 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005001 }
5002 // If all the elements are from V2, assign it to V1 and return after
5003 // building the first pshufb.
5004 if (V2Only)
5005 V1 = V2;
Owen Anderson825b72b2009-08-11 20:47:22 +00005006 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005007 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005008 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005009 if (!TwoInputs)
5010 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00005011
Nate Begemanb9a47b82009-02-23 08:49:38 +00005012 // Calculate the shuffle mask for the second input, shuffle it, and
5013 // OR it with the first shuffled input.
5014 pshufbMask.clear();
5015 for (unsigned i = 0; i != 16; ++i) {
5016 int EltIdx = MaskVals[i];
5017 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005018 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005019 continue;
5020 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005021 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005022 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005023 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005024 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005025 MVT::v16i8, &pshufbMask[0], 16));
5026 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005027 }
Eric Christopherfd179292009-08-27 18:07:15 +00005028
Nate Begemanb9a47b82009-02-23 08:49:38 +00005029 // No SSSE3 - Calculate in place words and then fix all out of place words
5030 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
5031 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005032 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
5033 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005034 SDValue NewV = V2Only ? V2 : V1;
5035 for (int i = 0; i != 8; ++i) {
5036 int Elt0 = MaskVals[i*2];
5037 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00005038
Nate Begemanb9a47b82009-02-23 08:49:38 +00005039 // This word of the result is all undef, skip it.
5040 if (Elt0 < 0 && Elt1 < 0)
5041 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005042
Nate Begemanb9a47b82009-02-23 08:49:38 +00005043 // This word of the result is already in the correct place, skip it.
5044 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
5045 continue;
5046 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
5047 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005048
Nate Begemanb9a47b82009-02-23 08:49:38 +00005049 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
5050 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
5051 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00005052
5053 // If Elt0 and Elt1 are defined, are consecutive, and can be load
5054 // using a single extract together, load it and store it.
5055 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005056 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005057 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00005058 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005059 DAG.getIntPtrConstant(i));
5060 continue;
5061 }
5062
Nate Begemanb9a47b82009-02-23 08:49:38 +00005063 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00005064 // source byte is not also odd, shift the extracted word left 8 bits
5065 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005066 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005067 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005068 DAG.getIntPtrConstant(Elt1 / 2));
5069 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005070 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Owen Anderson95771af2011-02-25 21:41:48 +00005071 DAG.getConstant(8,
5072 TLI.getShiftAmountTy(InsElt.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005073 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005074 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
5075 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005076 }
5077 // If Elt0 is defined, extract it from the appropriate source. If the
5078 // source byte is not also even, shift the extracted word right 8 bits. If
5079 // Elt1 was also defined, OR the extracted values together before
5080 // inserting them in the result.
5081 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005082 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005083 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
5084 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005085 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Owen Anderson95771af2011-02-25 21:41:48 +00005086 DAG.getConstant(8,
5087 TLI.getShiftAmountTy(InsElt0.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005088 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005089 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
5090 DAG.getConstant(0x00FF, MVT::i16));
5091 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00005092 : InsElt0;
5093 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005094 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005095 DAG.getIntPtrConstant(i));
5096 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005097 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005098}
5099
Evan Cheng7a831ce2007-12-15 03:00:47 +00005100/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005101/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00005102/// done when every pair / quad of shuffle mask elements point to elements in
5103/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005104/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00005105static
Nate Begeman9008ca62009-04-27 18:41:29 +00005106SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005107 SelectionDAG &DAG, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00005108 EVT VT = SVOp->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00005109 SDValue V1 = SVOp->getOperand(0);
5110 SDValue V2 = SVOp->getOperand(1);
5111 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00005112 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005113 EVT NewVT;
Owen Anderson825b72b2009-08-11 20:47:22 +00005114 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005115 default: assert(false && "Unexpected!");
Owen Anderson825b72b2009-08-11 20:47:22 +00005116 case MVT::v4f32: NewVT = MVT::v2f64; break;
5117 case MVT::v4i32: NewVT = MVT::v2i64; break;
5118 case MVT::v8i16: NewVT = MVT::v4i32; break;
5119 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00005120 }
5121
Nate Begeman9008ca62009-04-27 18:41:29 +00005122 int Scale = NumElems / NewWidth;
5123 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00005124 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005125 int StartIdx = -1;
5126 for (int j = 0; j < Scale; ++j) {
5127 int EltIdx = SVOp->getMaskElt(i+j);
5128 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005129 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00005130 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00005131 StartIdx = EltIdx - (EltIdx % Scale);
5132 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00005133 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00005134 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005135 if (StartIdx == -1)
5136 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00005137 else
Nate Begeman9008ca62009-04-27 18:41:29 +00005138 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005139 }
5140
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005141 V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, V1);
5142 V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00005143 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005144}
5145
Evan Chengd880b972008-05-09 21:53:03 +00005146/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005147///
Owen Andersone50ed302009-08-10 22:56:29 +00005148static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00005149 SDValue SrcOp, SelectionDAG &DAG,
5150 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005151 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005152 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00005153 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00005154 LD = dyn_cast<LoadSDNode>(SrcOp);
5155 if (!LD) {
5156 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
5157 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00005158 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00005159 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00005160 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005161 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00005162 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005163 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00005164 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005165 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005166 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
5167 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
5168 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00005169 SrcOp.getOperand(0)
5170 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005171 }
5172 }
5173 }
5174
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005175 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005176 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005177 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00005178 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005179}
5180
Evan Chengace3c172008-07-22 21:13:36 +00005181/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
5182/// shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00005183static SDValue
Nate Begeman9008ca62009-04-27 18:41:29 +00005184LowerVECTOR_SHUFFLE_4wide(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
5185 SDValue V1 = SVOp->getOperand(0);
5186 SDValue V2 = SVOp->getOperand(1);
5187 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005188 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00005189
Evan Chengace3c172008-07-22 21:13:36 +00005190 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00005191 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00005192 SmallVector<int, 8> Mask1(4U, -1);
5193 SmallVector<int, 8> PermMask;
5194 SVOp->getMask(PermMask);
5195
Evan Chengace3c172008-07-22 21:13:36 +00005196 unsigned NumHi = 0;
5197 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00005198 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005199 int Idx = PermMask[i];
5200 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00005201 Locs[i] = std::make_pair(-1, -1);
5202 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005203 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
5204 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00005205 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00005206 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005207 NumLo++;
5208 } else {
5209 Locs[i] = std::make_pair(1, NumHi);
5210 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00005211 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005212 NumHi++;
5213 }
5214 }
5215 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005216
Evan Chengace3c172008-07-22 21:13:36 +00005217 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005218 // If no more than two elements come from either vector. This can be
5219 // implemented with two shuffles. First shuffle gather the elements.
5220 // The second shuffle, which takes the first shuffle as both of its
5221 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00005222 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005223
Nate Begeman9008ca62009-04-27 18:41:29 +00005224 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopherfd179292009-08-27 18:07:15 +00005225
Evan Chengace3c172008-07-22 21:13:36 +00005226 for (unsigned i = 0; i != 4; ++i) {
5227 if (Locs[i].first == -1)
5228 continue;
5229 else {
5230 unsigned Idx = (i < 2) ? 0 : 4;
5231 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00005232 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005233 }
5234 }
5235
Nate Begeman9008ca62009-04-27 18:41:29 +00005236 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005237 } else if (NumLo == 3 || NumHi == 3) {
5238 // Otherwise, we must have three elements from one vector, call it X, and
5239 // one element from the other, call it Y. First, use a shufps to build an
5240 // intermediate vector with the one element from Y and the element from X
5241 // that will be in the same half in the final destination (the indexes don't
5242 // matter). Then, use a shufps to build the final vector, taking the half
5243 // containing the element from Y from the intermediate, and the other half
5244 // from X.
5245 if (NumHi == 3) {
5246 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00005247 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005248 std::swap(V1, V2);
5249 }
5250
5251 // Find the element from V2.
5252 unsigned HiIndex;
5253 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005254 int Val = PermMask[HiIndex];
5255 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005256 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005257 if (Val >= 4)
5258 break;
5259 }
5260
Nate Begeman9008ca62009-04-27 18:41:29 +00005261 Mask1[0] = PermMask[HiIndex];
5262 Mask1[1] = -1;
5263 Mask1[2] = PermMask[HiIndex^1];
5264 Mask1[3] = -1;
5265 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005266
5267 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005268 Mask1[0] = PermMask[0];
5269 Mask1[1] = PermMask[1];
5270 Mask1[2] = HiIndex & 1 ? 6 : 4;
5271 Mask1[3] = HiIndex & 1 ? 4 : 6;
5272 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005273 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005274 Mask1[0] = HiIndex & 1 ? 2 : 0;
5275 Mask1[1] = HiIndex & 1 ? 0 : 2;
5276 Mask1[2] = PermMask[2];
5277 Mask1[3] = PermMask[3];
5278 if (Mask1[2] >= 0)
5279 Mask1[2] += 4;
5280 if (Mask1[3] >= 0)
5281 Mask1[3] += 4;
5282 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005283 }
Evan Chengace3c172008-07-22 21:13:36 +00005284 }
5285
5286 // Break it into (shuffle shuffle_hi, shuffle_lo).
5287 Locs.clear();
David Greenec4db4e52011-02-28 19:06:56 +00005288 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00005289 SmallVector<int,8> LoMask(4U, -1);
5290 SmallVector<int,8> HiMask(4U, -1);
5291
5292 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00005293 unsigned MaskIdx = 0;
5294 unsigned LoIdx = 0;
5295 unsigned HiIdx = 2;
5296 for (unsigned i = 0; i != 4; ++i) {
5297 if (i == 2) {
5298 MaskPtr = &HiMask;
5299 MaskIdx = 1;
5300 LoIdx = 0;
5301 HiIdx = 2;
5302 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005303 int Idx = PermMask[i];
5304 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00005305 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00005306 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00005307 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00005308 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005309 LoIdx++;
5310 } else {
5311 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00005312 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005313 HiIdx++;
5314 }
5315 }
5316
Nate Begeman9008ca62009-04-27 18:41:29 +00005317 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
5318 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
5319 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00005320 for (unsigned i = 0; i != 4; ++i) {
5321 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005322 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00005323 } else {
5324 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00005325 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00005326 }
5327 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005328 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00005329}
5330
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005331static bool MayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005332 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005333 V = V.getOperand(0);
5334 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
5335 V = V.getOperand(0);
5336 if (MayFoldLoad(V))
5337 return true;
5338 return false;
5339}
5340
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005341// FIXME: the version above should always be used. Since there's
5342// a bug where several vector shuffles can't be folded because the
5343// DAG is not updated during lowering and a node claims to have two
5344// uses while it only has one, use this version, and let isel match
5345// another instruction if the load really happens to have more than
5346// one use. Remove this version after this bug get fixed.
Evan Cheng835580f2010-10-07 20:50:20 +00005347// rdar://8434668, PR8156
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005348static bool RelaxedMayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005349 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005350 V = V.getOperand(0);
5351 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
5352 V = V.getOperand(0);
5353 if (ISD::isNormalLoad(V.getNode()))
5354 return true;
5355 return false;
5356}
5357
5358/// CanFoldShuffleIntoVExtract - Check if the current shuffle is used by
5359/// a vector extract, and if both can be later optimized into a single load.
5360/// This is done in visitEXTRACT_VECTOR_ELT and the conditions are checked
5361/// here because otherwise a target specific shuffle node is going to be
5362/// emitted for this shuffle, and the optimization not done.
5363/// FIXME: This is probably not the best approach, but fix the problem
5364/// until the right path is decided.
5365static
5366bool CanXFormVExtractWithShuffleIntoLoad(SDValue V, SelectionDAG &DAG,
5367 const TargetLowering &TLI) {
5368 EVT VT = V.getValueType();
5369 ShuffleVectorSDNode *SVOp = dyn_cast<ShuffleVectorSDNode>(V);
5370
5371 // Be sure that the vector shuffle is present in a pattern like this:
5372 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), c) -> (f32 load $addr)
5373 if (!V.hasOneUse())
5374 return false;
5375
5376 SDNode *N = *V.getNode()->use_begin();
5377 if (N->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
5378 return false;
5379
5380 SDValue EltNo = N->getOperand(1);
5381 if (!isa<ConstantSDNode>(EltNo))
5382 return false;
5383
5384 // If the bit convert changed the number of elements, it is unsafe
5385 // to examine the mask.
5386 bool HasShuffleIntoBitcast = false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005387 if (V.getOpcode() == ISD::BITCAST) {
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005388 EVT SrcVT = V.getOperand(0).getValueType();
5389 if (SrcVT.getVectorNumElements() != VT.getVectorNumElements())
5390 return false;
5391 V = V.getOperand(0);
5392 HasShuffleIntoBitcast = true;
5393 }
5394
5395 // Select the input vector, guarding against out of range extract vector.
5396 unsigned NumElems = VT.getVectorNumElements();
5397 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
5398 int Idx = (Elt > NumElems) ? -1 : SVOp->getMaskElt(Elt);
5399 V = (Idx < (int)NumElems) ? V.getOperand(0) : V.getOperand(1);
5400
5401 // Skip one more bit_convert if necessary
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005402 if (V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005403 V = V.getOperand(0);
5404
5405 if (ISD::isNormalLoad(V.getNode())) {
5406 // Is the original load suitable?
5407 LoadSDNode *LN0 = cast<LoadSDNode>(V);
5408
5409 // FIXME: avoid the multi-use bug that is preventing lots of
5410 // of foldings to be detected, this is still wrong of course, but
5411 // give the temporary desired behavior, and if it happens that
5412 // the load has real more uses, during isel it will not fold, and
5413 // will generate poor code.
5414 if (!LN0 || LN0->isVolatile()) // || !LN0->hasOneUse()
5415 return false;
5416
5417 if (!HasShuffleIntoBitcast)
5418 return true;
5419
5420 // If there's a bitcast before the shuffle, check if the load type and
5421 // alignment is valid.
5422 unsigned Align = LN0->getAlignment();
5423 unsigned NewAlign =
5424 TLI.getTargetData()->getABITypeAlignment(
5425 VT.getTypeForEVT(*DAG.getContext()));
5426
5427 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
5428 return false;
5429 }
5430
5431 return true;
5432}
5433
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005434static
Evan Cheng835580f2010-10-07 20:50:20 +00005435SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
5436 EVT VT = Op.getValueType();
5437
5438 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005439 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
5440 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00005441 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
5442 V1, DAG));
5443}
5444
5445static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005446SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
5447 bool HasSSE2) {
5448 SDValue V1 = Op.getOperand(0);
5449 SDValue V2 = Op.getOperand(1);
5450 EVT VT = Op.getValueType();
5451
5452 assert(VT != MVT::v2i64 && "unsupported shuffle type");
5453
5454 if (HasSSE2 && VT == MVT::v2f64)
5455 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
5456
5457 // v4f32 or v4i32
5458 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V2, DAG);
5459}
5460
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00005461static
5462SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
5463 SDValue V1 = Op.getOperand(0);
5464 SDValue V2 = Op.getOperand(1);
5465 EVT VT = Op.getValueType();
5466
5467 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
5468 "unsupported shuffle type");
5469
5470 if (V2.getOpcode() == ISD::UNDEF)
5471 V2 = V1;
5472
5473 // v4i32 or v4f32
5474 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
5475}
5476
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005477static
5478SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasSSE2) {
5479 SDValue V1 = Op.getOperand(0);
5480 SDValue V2 = Op.getOperand(1);
5481 EVT VT = Op.getValueType();
5482 unsigned NumElems = VT.getVectorNumElements();
5483
5484 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
5485 // operand of these instructions is only memory, so check if there's a
5486 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
5487 // same masks.
5488 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005489
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00005490 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005491 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005492 CanFoldLoad = true;
5493
5494 // When V1 is a load, it can be folded later into a store in isel, example:
5495 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
5496 // turns into:
5497 // (MOVLPSmr addr:$src1, VR128:$src2)
5498 // So, recognize this potential and also use MOVLPS or MOVLPD
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005499 if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005500 CanFoldLoad = true;
5501
Eric Christopher893a8822011-02-20 05:04:42 +00005502 // Both of them can't be memory operations though.
5503 if (MayFoldVectorLoad(V1) && MayFoldVectorLoad(V2))
5504 CanFoldLoad = false;
Owen Anderson95771af2011-02-25 21:41:48 +00005505
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005506 if (CanFoldLoad) {
5507 if (HasSSE2 && NumElems == 2)
5508 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
5509
5510 if (NumElems == 4)
5511 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
5512 }
5513
5514 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5515 // movl and movlp will both match v2i64, but v2i64 is never matched by
5516 // movl earlier because we make it strict to avoid messing with the movlp load
5517 // folding logic (see the code above getMOVLP call). Match it here then,
5518 // this is horrible, but will stay like this until we move all shuffle
5519 // matching to x86 specific nodes. Note that for the 1st condition all
5520 // types are matched with movsd.
5521 if ((HasSSE2 && NumElems == 2) || !X86::isMOVLMask(SVOp))
5522 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
5523 else if (HasSSE2)
5524 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
5525
5526
5527 assert(VT != MVT::v4i32 && "unsupported shuffle type");
5528
5529 // Invert the operand order and use SHUFPS to match it.
5530 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V2, V1,
5531 X86::getShuffleSHUFImmediate(SVOp), DAG);
5532}
5533
David Greenec4db4e52011-02-28 19:06:56 +00005534static inline unsigned getUNPCKLOpcode(EVT VT, const X86Subtarget *Subtarget) {
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005535 switch(VT.getSimpleVT().SimpleTy) {
5536 case MVT::v4i32: return X86ISD::PUNPCKLDQ;
5537 case MVT::v2i64: return X86ISD::PUNPCKLQDQ;
David Greenec4db4e52011-02-28 19:06:56 +00005538 case MVT::v4f32:
5539 return Subtarget->hasAVX() ? X86ISD::VUNPCKLPS : X86ISD::UNPCKLPS;
5540 case MVT::v2f64:
5541 return Subtarget->hasAVX() ? X86ISD::VUNPCKLPD : X86ISD::UNPCKLPD;
5542 case MVT::v8f32: return X86ISD::VUNPCKLPSY;
5543 case MVT::v4f64: return X86ISD::VUNPCKLPDY;
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005544 case MVT::v16i8: return X86ISD::PUNPCKLBW;
5545 case MVT::v8i16: return X86ISD::PUNPCKLWD;
5546 default:
Eric Christopherdd6e40a2011-02-19 03:19:09 +00005547 llvm_unreachable("Unknown type for unpckl");
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005548 }
5549 return 0;
5550}
5551
5552static inline unsigned getUNPCKHOpcode(EVT VT) {
5553 switch(VT.getSimpleVT().SimpleTy) {
5554 case MVT::v4i32: return X86ISD::PUNPCKHDQ;
5555 case MVT::v2i64: return X86ISD::PUNPCKHQDQ;
5556 case MVT::v4f32: return X86ISD::UNPCKHPS;
5557 case MVT::v2f64: return X86ISD::UNPCKHPD;
5558 case MVT::v16i8: return X86ISD::PUNPCKHBW;
5559 case MVT::v8i16: return X86ISD::PUNPCKHWD;
5560 default:
Eric Christopherdd6e40a2011-02-19 03:19:09 +00005561 llvm_unreachable("Unknown type for unpckh");
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005562 }
5563 return 0;
5564}
5565
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005566static
5567SDValue NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG,
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005568 const TargetLowering &TLI,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005569 const X86Subtarget *Subtarget) {
5570 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5571 EVT VT = Op.getValueType();
5572 DebugLoc dl = Op.getDebugLoc();
5573 SDValue V1 = Op.getOperand(0);
5574 SDValue V2 = Op.getOperand(1);
5575
5576 if (isZeroShuffle(SVOp))
5577 return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
5578
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005579 // Handle splat operations
5580 if (SVOp->isSplat()) {
5581 // Special case, this is the only place now where it's
5582 // allowed to return a vector_shuffle operation without
5583 // using a target specific node, because *hopefully* it
5584 // will be optimized away by the dag combiner.
5585 if (VT.getVectorNumElements() <= 4 &&
5586 CanXFormVExtractWithShuffleIntoLoad(Op, DAG, TLI))
5587 return Op;
5588
5589 // Handle splats by matching through known masks
5590 if (VT.getVectorNumElements() <= 4)
5591 return SDValue();
5592
Evan Cheng835580f2010-10-07 20:50:20 +00005593 // Canonicalize all of the remaining to v4f32.
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005594 return PromoteSplat(SVOp, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005595 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005596
5597 // If the shuffle can be profitably rewritten as a narrower shuffle, then
5598 // do it!
5599 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
5600 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5601 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005602 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005603 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
5604 // FIXME: Figure out a cleaner way to do this.
5605 // Try to make use of movq to zero out the top part.
5606 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
5607 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5608 if (NewOp.getNode()) {
5609 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
5610 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
5611 DAG, Subtarget, dl);
5612 }
5613 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
5614 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5615 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
5616 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
5617 DAG, Subtarget, dl);
5618 }
5619 }
5620 return SDValue();
5621}
5622
Dan Gohman475871a2008-07-27 21:46:04 +00005623SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005624X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00005625 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00005626 SDValue V1 = Op.getOperand(0);
5627 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00005628 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005629 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00005630 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005631 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005632 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
5633 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00005634 bool V1IsSplat = false;
5635 bool V2IsSplat = false;
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005636 bool HasSSE2 = Subtarget->hasSSE2() || Subtarget->hasAVX();
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00005637 bool HasSSE3 = Subtarget->hasSSE3() || Subtarget->hasAVX();
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00005638 bool HasSSSE3 = Subtarget->hasSSSE3() || Subtarget->hasAVX();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005639 MachineFunction &MF = DAG.getMachineFunction();
5640 bool OptForSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005641
Dale Johannesen0488fb62010-09-30 23:57:10 +00005642 // Shuffle operations on MMX not supported.
5643 if (isMMX)
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00005644 return Op;
5645
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005646 // Vector shuffle lowering takes 3 steps:
5647 //
5648 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
5649 // narrowing and commutation of operands should be handled.
5650 // 2) Matching of shuffles with known shuffle masks to x86 target specific
5651 // shuffle nodes.
5652 // 3) Rewriting of unmatched masks into new generic shuffle operations,
5653 // so the shuffle can be broken into other shuffles and the legalizer can
5654 // try the lowering again.
5655 //
5656 // The general ideia is that no vector_shuffle operation should be left to
5657 // be matched during isel, all of them must be converted to a target specific
5658 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00005659
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005660 // Normalize the input vectors. Here splats, zeroed vectors, profitable
5661 // narrowing and commutation of operands should be handled. The actual code
5662 // doesn't include all of those, work in progress...
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005663 SDValue NewOp = NormalizeVectorShuffle(Op, DAG, *this, Subtarget);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005664 if (NewOp.getNode())
5665 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00005666
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005667 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
5668 // unpckh_undef). Only use pshufd if speed is more important than size.
5669 if (OptForSize && X86::isUNPCKL_v_undef_Mask(SVOp))
5670 if (VT != MVT::v2i64 && VT != MVT::v2f64)
David Greenec4db4e52011-02-28 19:06:56 +00005671 return getTargetShuffleNode(getUNPCKLOpcode(VT, getSubtarget()), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005672 if (OptForSize && X86::isUNPCKH_v_undef_Mask(SVOp))
5673 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5674 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00005675
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005676 if (X86::isMOVDDUPMask(SVOp) && HasSSE3 && V2IsUndef &&
Dale Johannesen0488fb62010-09-30 23:57:10 +00005677 RelaxedMayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00005678 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005679
Dale Johannesen0488fb62010-09-30 23:57:10 +00005680 if (X86::isMOVHLPS_v_undef_Mask(SVOp))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005681 return getMOVHighToLow(Op, dl, DAG);
5682
5683 // Use to match splats
5684 if (HasSSE2 && X86::isUNPCKHMask(SVOp) && V2IsUndef &&
5685 (VT == MVT::v2f64 || VT == MVT::v2i64))
5686 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
5687
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005688 if (X86::isPSHUFDMask(SVOp)) {
5689 // The actual implementation will match the mask in the if above and then
5690 // during isel it can match several different instructions, not only pshufd
5691 // as its name says, sad but true, emulate the behavior for now...
5692 if (X86::isMOVDDUPMask(SVOp) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
5693 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
5694
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005695 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
5696
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005697 if (HasSSE2 && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005698 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
5699
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005700 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005701 return getTargetShuffleNode(X86ISD::SHUFPD, dl, VT, V1, V1,
5702 TargetMask, DAG);
5703
5704 if (VT == MVT::v4f32)
5705 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V1, V1,
5706 TargetMask, DAG);
5707 }
Eric Christopherfd179292009-08-27 18:07:15 +00005708
Evan Chengf26ffe92008-05-29 08:22:04 +00005709 // Check if this can be converted into a logical shift.
5710 bool isLeft = false;
5711 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00005712 SDValue ShVal;
Nate Begeman9008ca62009-04-27 18:41:29 +00005713 bool isShift = getSubtarget()->hasSSE2() &&
Evan Chengc3630942009-12-09 21:00:30 +00005714 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00005715 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00005716 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00005717 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00005718 EVT EltVT = VT.getVectorElementType();
5719 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00005720 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005721 }
Eric Christopherfd179292009-08-27 18:07:15 +00005722
Nate Begeman9008ca62009-04-27 18:41:29 +00005723 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005724 if (V1IsUndef)
5725 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00005726 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00005727 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Dale Johannesen0488fb62010-09-30 23:57:10 +00005728 if (!X86::isMOVLPMask(SVOp)) {
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005729 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00005730 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
5731
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005732 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00005733 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
5734 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00005735 }
Eric Christopherfd179292009-08-27 18:07:15 +00005736
Nate Begeman9008ca62009-04-27 18:41:29 +00005737 // FIXME: fold these into legal mask.
Dale Johannesen0488fb62010-09-30 23:57:10 +00005738 if (X86::isMOVLHPSMask(SVOp) && !X86::isUNPCKLMask(SVOp))
5739 return getMOVLowToHigh(Op, dl, DAG, HasSSE2);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005740
Dale Johannesen0488fb62010-09-30 23:57:10 +00005741 if (X86::isMOVHLPSMask(SVOp))
5742 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00005743
Dale Johannesen0488fb62010-09-30 23:57:10 +00005744 if (X86::isMOVSHDUPMask(SVOp) && HasSSE3 && V2IsUndef && NumElems == 4)
5745 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00005746
Dale Johannesen0488fb62010-09-30 23:57:10 +00005747 if (X86::isMOVSLDUPMask(SVOp) && HasSSE3 && V2IsUndef && NumElems == 4)
5748 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00005749
Dale Johannesen0488fb62010-09-30 23:57:10 +00005750 if (X86::isMOVLPMask(SVOp))
5751 return getMOVLP(Op, dl, DAG, HasSSE2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005752
Nate Begeman9008ca62009-04-27 18:41:29 +00005753 if (ShouldXformToMOVHLPS(SVOp) ||
5754 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
5755 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005756
Evan Chengf26ffe92008-05-29 08:22:04 +00005757 if (isShift) {
5758 // No better options. Use a vshl / vsrl.
Dan Gohman8a55ce42009-09-23 21:02:20 +00005759 EVT EltVT = VT.getVectorElementType();
5760 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00005761 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005762 }
Eric Christopherfd179292009-08-27 18:07:15 +00005763
Evan Cheng9eca5e82006-10-25 21:49:50 +00005764 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00005765 // FIXME: This should also accept a bitcast of a splat? Be careful, not
5766 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00005767 V1IsSplat = isSplatVector(V1.getNode());
5768 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00005769
Chris Lattner8a594482007-11-25 00:24:49 +00005770 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00005771 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005772 Op = CommuteVectorShuffle(SVOp, DAG);
5773 SVOp = cast<ShuffleVectorSDNode>(Op);
5774 V1 = SVOp->getOperand(0);
5775 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00005776 std::swap(V1IsSplat, V2IsSplat);
5777 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00005778 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00005779 }
5780
Nate Begeman9008ca62009-04-27 18:41:29 +00005781 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
5782 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00005783 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00005784 return V1;
5785 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
5786 // the instruction selector will not match, so get a canonical MOVL with
5787 // swapped operands to undo the commute.
5788 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00005789 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005790
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005791 if (X86::isUNPCKLMask(SVOp))
David Greenec4db4e52011-02-28 19:06:56 +00005792 return getTargetShuffleNode(getUNPCKLOpcode(VT, getSubtarget()),
5793 dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005794
5795 if (X86::isUNPCKHMask(SVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005796 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00005797
Evan Cheng9bbbb982006-10-25 20:48:19 +00005798 if (V2IsSplat) {
5799 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005800 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00005801 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00005802 SDValue NewMask = NormalizeMask(SVOp, DAG);
5803 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
5804 if (NSVOp != SVOp) {
5805 if (X86::isUNPCKLMask(NSVOp, true)) {
5806 return NewMask;
5807 } else if (X86::isUNPCKHMask(NSVOp, true)) {
5808 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005809 }
5810 }
5811 }
5812
Evan Cheng9eca5e82006-10-25 21:49:50 +00005813 if (Commuted) {
5814 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00005815 // FIXME: this seems wrong.
5816 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
5817 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005818
5819 if (X86::isUNPCKLMask(NewSVOp))
David Greenec4db4e52011-02-28 19:06:56 +00005820 return getTargetShuffleNode(getUNPCKLOpcode(VT, getSubtarget()),
5821 dl, VT, V2, V1, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005822
5823 if (X86::isUNPCKHMask(NewSVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005824 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V2, V1, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00005825 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005826
Nate Begeman9008ca62009-04-27 18:41:29 +00005827 // Normalize the node to match x86 shuffle ops if needed
Dale Johannesen0488fb62010-09-30 23:57:10 +00005828 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
Nate Begeman9008ca62009-04-27 18:41:29 +00005829 return CommuteVectorShuffle(SVOp, DAG);
5830
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00005831 // The checks below are all present in isShuffleMaskLegal, but they are
5832 // inlined here right now to enable us to directly emit target specific
5833 // nodes, and remove one by one until they don't return Op anymore.
5834 SmallVector<int, 16> M;
5835 SVOp->getMask(M);
5836
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00005837 if (isPALIGNRMask(M, VT, HasSSSE3))
5838 return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
5839 X86::getShufflePALIGNRImmediate(SVOp),
5840 DAG);
5841
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00005842 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
5843 SVOp->getSplatIndex() == 0 && V2IsUndef) {
David Greenec4db4e52011-02-28 19:06:56 +00005844 if (VT == MVT::v2f64) {
5845 X86ISD::NodeType Opcode =
5846 getSubtarget()->hasAVX() ? X86ISD::VUNPCKLPD : X86ISD::UNPCKLPD;
5847 return getTargetShuffleNode(Opcode, dl, VT, V1, V1, DAG);
5848 }
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00005849 if (VT == MVT::v2i64)
5850 return getTargetShuffleNode(X86ISD::PUNPCKLQDQ, dl, VT, V1, V1, DAG);
5851 }
5852
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00005853 if (isPSHUFHWMask(M, VT))
5854 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
5855 X86::getShufflePSHUFHWImmediate(SVOp),
5856 DAG);
5857
5858 if (isPSHUFLWMask(M, VT))
5859 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
5860 X86::getShufflePSHUFLWImmediate(SVOp),
5861 DAG);
5862
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00005863 if (isSHUFPMask(M, VT)) {
5864 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
5865 if (VT == MVT::v4f32 || VT == MVT::v4i32)
5866 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V1, V2,
5867 TargetMask, DAG);
5868 if (VT == MVT::v2f64 || VT == MVT::v2i64)
5869 return getTargetShuffleNode(X86ISD::SHUFPD, dl, VT, V1, V2,
5870 TargetMask, DAG);
5871 }
5872
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005873 if (X86::isUNPCKL_v_undef_Mask(SVOp))
5874 if (VT != MVT::v2i64 && VT != MVT::v2f64)
David Greenec4db4e52011-02-28 19:06:56 +00005875 return getTargetShuffleNode(getUNPCKLOpcode(VT, getSubtarget()),
5876 dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005877 if (X86::isUNPCKH_v_undef_Mask(SVOp))
5878 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5879 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
5880
Evan Cheng14b32e12007-12-11 01:46:18 +00005881 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
Owen Anderson825b72b2009-08-11 20:47:22 +00005882 if (VT == MVT::v8i16) {
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005883 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00005884 if (NewOp.getNode())
Evan Cheng14b32e12007-12-11 01:46:18 +00005885 return NewOp;
5886 }
5887
Owen Anderson825b72b2009-08-11 20:47:22 +00005888 if (VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005889 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005890 if (NewOp.getNode())
5891 return NewOp;
5892 }
Eric Christopherfd179292009-08-27 18:07:15 +00005893
Dale Johannesen0488fb62010-09-30 23:57:10 +00005894 // Handle all 4 wide cases with a number of shuffles.
5895 if (NumElems == 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00005896 return LowerVECTOR_SHUFFLE_4wide(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005897
Dan Gohman475871a2008-07-27 21:46:04 +00005898 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005899}
5900
Dan Gohman475871a2008-07-27 21:46:04 +00005901SDValue
5902X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00005903 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005904 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005905 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005906 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005907 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005908 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00005909 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005910 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00005911 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005912 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00005913 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
5914 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
5915 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005916 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
5917 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005918 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005919 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00005920 Op.getOperand(0)),
5921 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00005922 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005923 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00005924 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005925 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00005926 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Anderson825b72b2009-08-11 20:47:22 +00005927 } else if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00005928 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
5929 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00005930 // result has a single use which is a store or a bitcast to i32. And in
5931 // the case of a store, it's not worth it if the index is a constant 0,
5932 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00005933 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00005934 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00005935 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00005936 if ((User->getOpcode() != ISD::STORE ||
5937 (isa<ConstantSDNode>(Op.getOperand(1)) &&
5938 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005939 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00005940 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00005941 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00005942 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005943 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00005944 Op.getOperand(0)),
5945 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005946 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Owen Anderson825b72b2009-08-11 20:47:22 +00005947 } else if (VT == MVT::i32) {
Mon P Wangf0fcdd82009-01-15 21:10:20 +00005948 // ExtractPS works with constant index.
5949 if (isa<ConstantSDNode>(Op.getOperand(1)))
5950 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00005951 }
Dan Gohman475871a2008-07-27 21:46:04 +00005952 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005953}
5954
5955
Dan Gohman475871a2008-07-27 21:46:04 +00005956SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005957X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
5958 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005959 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00005960 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005961
David Greene74a579d2011-02-10 16:57:36 +00005962 SDValue Vec = Op.getOperand(0);
5963 EVT VecVT = Vec.getValueType();
5964
5965 // If this is a 256-bit vector result, first extract the 128-bit
5966 // vector and then extract from the 128-bit vector.
5967 if (VecVT.getSizeInBits() > 128) {
5968 DebugLoc dl = Op.getNode()->getDebugLoc();
5969 unsigned NumElems = VecVT.getVectorNumElements();
5970 SDValue Idx = Op.getOperand(1);
5971
5972 if (!isa<ConstantSDNode>(Idx))
5973 return SDValue();
5974
5975 unsigned ExtractNumElems = NumElems / (VecVT.getSizeInBits() / 128);
5976 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
5977
5978 // Get the 128-bit vector.
5979 bool Upper = IdxVal >= ExtractNumElems;
5980 Vec = Extract128BitVector(Vec, Idx, DAG, dl);
5981
5982 // Extract from it.
5983 SDValue ScaledIdx = Idx;
5984 if (Upper)
5985 ScaledIdx = DAG.getNode(ISD::SUB, dl, Idx.getValueType(), Idx,
5986 DAG.getConstant(ExtractNumElems,
5987 Idx.getValueType()));
5988 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, Op.getValueType(), Vec,
5989 ScaledIdx);
5990 }
5991
5992 assert(Vec.getValueSizeInBits() <= 128 && "Unexpected vector length");
5993
Evan Cheng62a3f152008-03-24 21:52:23 +00005994 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005995 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00005996 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00005997 return Res;
5998 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00005999
Owen Andersone50ed302009-08-10 22:56:29 +00006000 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006001 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006002 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006003 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00006004 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006005 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00006006 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006007 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6008 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006009 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006010 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00006011 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006012 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00006013 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00006014 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00006015 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00006016 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00006017 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006018 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006019 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006020 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006021 if (Idx == 0)
6022 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00006023
Evan Cheng0db9fe62006-04-25 20:13:52 +00006024 // SHUFPS the element to the lowest double word, then movss.
Nate Begeman9008ca62009-04-27 18:41:29 +00006025 int Mask[4] = { Idx, -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006026 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006027 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00006028 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00006029 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00006030 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00006031 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00006032 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
6033 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
6034 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006035 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006036 if (Idx == 0)
6037 return Op;
6038
6039 // UNPCKHPD the element to the lowest double word, then movsd.
6040 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
6041 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00006042 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006043 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006044 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00006045 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00006046 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00006047 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006048 }
6049
Dan Gohman475871a2008-07-27 21:46:04 +00006050 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006051}
6052
Dan Gohman475871a2008-07-27 21:46:04 +00006053SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006054X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
6055 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006056 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006057 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006058 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006059
Dan Gohman475871a2008-07-27 21:46:04 +00006060 SDValue N0 = Op.getOperand(0);
6061 SDValue N1 = Op.getOperand(1);
6062 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00006063
Dan Gohman8a55ce42009-09-23 21:02:20 +00006064 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00006065 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00006066 unsigned Opc;
6067 if (VT == MVT::v8i16)
6068 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00006069 else if (VT == MVT::v16i8)
6070 Opc = X86ISD::PINSRB;
6071 else
6072 Opc = X86ISD::PINSRB;
6073
Nate Begeman14d12ca2008-02-11 04:19:36 +00006074 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
6075 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00006076 if (N1.getValueType() != MVT::i32)
6077 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
6078 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006079 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00006080 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00006081 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00006082 // Bits [7:6] of the constant are the source select. This will always be
6083 // zero here. The DAG Combiner may combine an extract_elt index into these
6084 // bits. For example (insert (extract, 3), 2) could be matched by putting
6085 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00006086 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00006087 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00006088 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00006089 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006090 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00006091 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00006092 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00006093 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00006094 } else if (EltVT == MVT::i32 && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00006095 // PINSR* works with constant index.
6096 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00006097 }
Dan Gohman475871a2008-07-27 21:46:04 +00006098 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006099}
6100
Dan Gohman475871a2008-07-27 21:46:04 +00006101SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006102X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006103 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006104 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006105
David Greene6b381262011-02-09 15:32:06 +00006106 DebugLoc dl = Op.getDebugLoc();
6107 SDValue N0 = Op.getOperand(0);
6108 SDValue N1 = Op.getOperand(1);
6109 SDValue N2 = Op.getOperand(2);
6110
6111 // If this is a 256-bit vector result, first insert into a 128-bit
6112 // vector and then insert into the 256-bit vector.
6113 if (VT.getSizeInBits() > 128) {
6114 if (!isa<ConstantSDNode>(N2))
6115 return SDValue();
6116
6117 // Get the 128-bit vector.
6118 unsigned NumElems = VT.getVectorNumElements();
6119 unsigned IdxVal = cast<ConstantSDNode>(N2)->getZExtValue();
6120 bool Upper = IdxVal >= NumElems / 2;
6121
6122 SDValue SubN0 = Extract128BitVector(N0, N2, DAG, dl);
6123
6124 // Insert into it.
6125 SDValue ScaledN2 = N2;
6126 if (Upper)
6127 ScaledN2 = DAG.getNode(ISD::SUB, dl, N2.getValueType(), N2,
Owen Anderson95771af2011-02-25 21:41:48 +00006128 DAG.getConstant(NumElems /
David Greene6b381262011-02-09 15:32:06 +00006129 (VT.getSizeInBits() / 128),
6130 N2.getValueType()));
6131 Op = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, SubN0.getValueType(), SubN0,
6132 N1, ScaledN2);
6133
6134 // Insert the 128-bit vector
6135 // FIXME: Why UNDEF?
6136 return Insert128BitVector(N0, Op, N2, DAG, dl);
6137 }
6138
Nate Begeman14d12ca2008-02-11 04:19:36 +00006139 if (Subtarget->hasSSE41())
6140 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
6141
Dan Gohman8a55ce42009-09-23 21:02:20 +00006142 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00006143 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00006144
Dan Gohman8a55ce42009-09-23 21:02:20 +00006145 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00006146 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
6147 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00006148 if (N1.getValueType() != MVT::i32)
6149 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
6150 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006151 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00006152 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006153 }
Dan Gohman475871a2008-07-27 21:46:04 +00006154 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006155}
6156
Dan Gohman475871a2008-07-27 21:46:04 +00006157SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006158X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) const {
David Greene2fcdfb42011-02-10 23:11:29 +00006159 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006160 DebugLoc dl = Op.getDebugLoc();
David Greene2fcdfb42011-02-10 23:11:29 +00006161 EVT OpVT = Op.getValueType();
6162
6163 // If this is a 256-bit vector result, first insert into a 128-bit
6164 // vector and then insert into the 256-bit vector.
6165 if (OpVT.getSizeInBits() > 128) {
6166 // Insert into a 128-bit vector.
6167 EVT VT128 = EVT::getVectorVT(*Context,
6168 OpVT.getVectorElementType(),
6169 OpVT.getVectorNumElements() / 2);
6170
6171 Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT128, Op.getOperand(0));
6172
6173 // Insert the 128-bit vector.
6174 return Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, OpVT), Op,
6175 DAG.getConstant(0, MVT::i32),
6176 DAG, dl);
6177 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00006178
Chris Lattnerf172ecd2010-07-04 23:07:25 +00006179 if (Op.getValueType() == MVT::v1i64 &&
6180 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00006181 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00006182
Owen Anderson825b72b2009-08-11 20:47:22 +00006183 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Dale Johannesen0488fb62010-09-30 23:57:10 +00006184 assert(Op.getValueType().getSimpleVT().getSizeInBits() == 128 &&
6185 "Expected an SSE type!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006186 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(),
Dale Johannesen0488fb62010-09-30 23:57:10 +00006187 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006188}
6189
David Greene91585092011-01-26 15:38:49 +00006190// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
6191// a simple subregister reference or explicit instructions to grab
6192// upper bits of a vector.
6193SDValue
6194X86TargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
6195 if (Subtarget->hasAVX()) {
David Greenea5f26012011-02-07 19:36:54 +00006196 DebugLoc dl = Op.getNode()->getDebugLoc();
6197 SDValue Vec = Op.getNode()->getOperand(0);
6198 SDValue Idx = Op.getNode()->getOperand(1);
6199
6200 if (Op.getNode()->getValueType(0).getSizeInBits() == 128
6201 && Vec.getNode()->getValueType(0).getSizeInBits() == 256) {
6202 return Extract128BitVector(Vec, Idx, DAG, dl);
6203 }
David Greene91585092011-01-26 15:38:49 +00006204 }
6205 return SDValue();
6206}
6207
David Greenecfe33c42011-01-26 19:13:22 +00006208// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
6209// simple superregister reference or explicit instructions to insert
6210// the upper bits of a vector.
6211SDValue
6212X86TargetLowering::LowerINSERT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
6213 if (Subtarget->hasAVX()) {
6214 DebugLoc dl = Op.getNode()->getDebugLoc();
6215 SDValue Vec = Op.getNode()->getOperand(0);
6216 SDValue SubVec = Op.getNode()->getOperand(1);
6217 SDValue Idx = Op.getNode()->getOperand(2);
6218
6219 if (Op.getNode()->getValueType(0).getSizeInBits() == 256
6220 && SubVec.getNode()->getValueType(0).getSizeInBits() == 128) {
David Greenea5f26012011-02-07 19:36:54 +00006221 return Insert128BitVector(Vec, SubVec, Idx, DAG, dl);
David Greenecfe33c42011-01-26 19:13:22 +00006222 }
6223 }
6224 return SDValue();
6225}
6226
Bill Wendling056292f2008-09-16 21:48:12 +00006227// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
6228// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
6229// one of the above mentioned nodes. It has to be wrapped because otherwise
6230// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
6231// be used to form addressing mode. These wrapped nodes will be selected
6232// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00006233SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006234X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006235 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00006236
Chris Lattner41621a22009-06-26 19:22:52 +00006237 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6238 // global base reg.
6239 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006240 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006241 CodeModel::Model M = getTargetMachine().getCodeModel();
6242
Chris Lattner4f066492009-07-11 20:29:19 +00006243 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006244 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00006245 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00006246 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006247 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00006248 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006249 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00006250
Evan Cheng1606e8e2009-03-13 07:51:59 +00006251 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00006252 CP->getAlignment(),
6253 CP->getOffset(), OpFlag);
6254 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00006255 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006256 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00006257 if (OpFlag) {
6258 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00006259 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006260 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006261 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006262 }
6263
6264 return Result;
6265}
6266
Dan Gohmand858e902010-04-17 15:26:15 +00006267SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00006268 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00006269
Chris Lattner18c59872009-06-27 04:16:01 +00006270 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6271 // global base reg.
6272 unsigned char OpFlag = 0;
6273 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006274 CodeModel::Model M = getTargetMachine().getCodeModel();
6275
Chris Lattner4f066492009-07-11 20:29:19 +00006276 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006277 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00006278 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00006279 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006280 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00006281 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006282 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00006283
Chris Lattner18c59872009-06-27 04:16:01 +00006284 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
6285 OpFlag);
6286 DebugLoc DL = JT->getDebugLoc();
6287 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00006288
Chris Lattner18c59872009-06-27 04:16:01 +00006289 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00006290 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00006291 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6292 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006293 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00006294 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00006295
Chris Lattner18c59872009-06-27 04:16:01 +00006296 return Result;
6297}
6298
6299SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006300X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00006301 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00006302
Chris Lattner18c59872009-06-27 04:16:01 +00006303 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6304 // global base reg.
6305 unsigned char OpFlag = 0;
6306 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006307 CodeModel::Model M = getTargetMachine().getCodeModel();
6308
Chris Lattner4f066492009-07-11 20:29:19 +00006309 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006310 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00006311 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00006312 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006313 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00006314 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006315 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00006316
Chris Lattner18c59872009-06-27 04:16:01 +00006317 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00006318
Chris Lattner18c59872009-06-27 04:16:01 +00006319 DebugLoc DL = Op.getDebugLoc();
6320 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00006321
6322
Chris Lattner18c59872009-06-27 04:16:01 +00006323 // With PIC, the address is actually $g + Offset.
6324 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00006325 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00006326 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6327 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006328 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00006329 Result);
6330 }
Eric Christopherfd179292009-08-27 18:07:15 +00006331
Chris Lattner18c59872009-06-27 04:16:01 +00006332 return Result;
6333}
6334
Dan Gohman475871a2008-07-27 21:46:04 +00006335SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006336X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00006337 // Create the TargetBlockAddressAddress node.
6338 unsigned char OpFlags =
6339 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00006340 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00006341 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +00006342 DebugLoc dl = Op.getDebugLoc();
6343 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
6344 /*isTarget=*/true, OpFlags);
6345
Dan Gohmanf705adb2009-10-30 01:28:02 +00006346 if (Subtarget->isPICStyleRIPRel() &&
6347 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00006348 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
6349 else
6350 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00006351
Dan Gohman29cbade2009-11-20 23:18:13 +00006352 // With PIC, the address is actually $g + Offset.
6353 if (isGlobalRelativeToPICBase(OpFlags)) {
6354 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
6355 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
6356 Result);
6357 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00006358
6359 return Result;
6360}
6361
6362SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00006363X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00006364 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00006365 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00006366 // Create the TargetGlobalAddress node, folding in the constant
6367 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00006368 unsigned char OpFlags =
6369 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006370 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00006371 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006372 if (OpFlags == X86II::MO_NO_FLAG &&
6373 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00006374 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00006375 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00006376 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006377 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00006378 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00006379 }
Eric Christopherfd179292009-08-27 18:07:15 +00006380
Chris Lattner4f066492009-07-11 20:29:19 +00006381 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006382 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00006383 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
6384 else
6385 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00006386
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006387 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00006388 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00006389 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
6390 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006391 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006392 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006393
Chris Lattner36c25012009-07-10 07:34:39 +00006394 // For globals that require a load from a stub to get the address, emit the
6395 // load.
6396 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00006397 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00006398 MachinePointerInfo::getGOT(), false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006399
Dan Gohman6520e202008-10-18 02:06:02 +00006400 // If there was a non-zero offset that we didn't fold, create an explicit
6401 // addition for it.
6402 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00006403 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00006404 DAG.getConstant(Offset, getPointerTy()));
6405
Evan Cheng0db9fe62006-04-25 20:13:52 +00006406 return Result;
6407}
6408
Evan Chengda43bcf2008-09-24 00:05:32 +00006409SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006410X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00006411 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00006412 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006413 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00006414}
6415
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006416static SDValue
6417GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00006418 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerb903bed2009-06-26 21:20:29 +00006419 unsigned char OperandFlags) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006420 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006421 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006422 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00006423 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006424 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00006425 GA->getOffset(),
6426 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006427 if (InFlag) {
6428 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00006429 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006430 } else {
6431 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00006432 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006433 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006434
6435 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00006436 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006437
Rafael Espindola15f1b662009-04-24 12:59:40 +00006438 SDValue Flag = Chain.getValue(1);
6439 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006440}
6441
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006442// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00006443static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006444LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006445 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00006446 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00006447 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
6448 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006449 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006450 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006451 InFlag = Chain.getValue(1);
6452
Chris Lattnerb903bed2009-06-26 21:20:29 +00006453 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006454}
6455
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006456// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00006457static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006458LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006459 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00006460 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
6461 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006462}
6463
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006464// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
6465// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00006466static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006467 const EVT PtrVT, TLSModel::Model model,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00006468 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00006469 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00006470
Chris Lattnerf93b90c2010-09-22 04:39:11 +00006471 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
6472 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
6473 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00006474
Michael J. Spencerec38de22010-10-10 22:04:20 +00006475 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00006476 DAG.getIntPtrConstant(0),
6477 MachinePointerInfo(Ptr), false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00006478
Chris Lattnerb903bed2009-06-26 21:20:29 +00006479 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006480 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
6481 // initialexec.
6482 unsigned WrapperKind = X86ISD::Wrapper;
6483 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00006484 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00006485 } else if (is64Bit) {
6486 assert(model == TLSModel::InitialExec);
6487 OperandFlags = X86II::MO_GOTTPOFF;
6488 WrapperKind = X86ISD::WrapperRIP;
6489 } else {
6490 assert(model == TLSModel::InitialExec);
6491 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00006492 }
Eric Christopherfd179292009-08-27 18:07:15 +00006493
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006494 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
6495 // exec)
Michael J. Spencerec38de22010-10-10 22:04:20 +00006496 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00006497 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00006498 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00006499 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00006500
Rafael Espindola9a580232009-02-27 13:37:18 +00006501 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00006502 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00006503 MachinePointerInfo::getGOT(), false, false, 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00006504
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006505 // The address of the thread local variable is the add of the thread
6506 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00006507 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006508}
6509
Dan Gohman475871a2008-07-27 21:46:04 +00006510SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006511X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00006512
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006513 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00006514 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00006515
Eric Christopher30ef0e52010-06-03 04:07:48 +00006516 if (Subtarget->isTargetELF()) {
6517 // TODO: implement the "local dynamic" model
6518 // TODO: implement the "initial exec"model for pic executables
Michael J. Spencerec38de22010-10-10 22:04:20 +00006519
Eric Christopher30ef0e52010-06-03 04:07:48 +00006520 // If GV is an alias then use the aliasee for determining
6521 // thread-localness.
6522 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
6523 GV = GA->resolveAliasedGlobal(false);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006524
6525 TLSModel::Model model
Eric Christopher30ef0e52010-06-03 04:07:48 +00006526 = getTLSModel(GV, getTargetMachine().getRelocationModel());
Michael J. Spencerec38de22010-10-10 22:04:20 +00006527
Eric Christopher30ef0e52010-06-03 04:07:48 +00006528 switch (model) {
6529 case TLSModel::GeneralDynamic:
6530 case TLSModel::LocalDynamic: // not implemented
6531 if (Subtarget->is64Bit())
6532 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
6533 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Michael J. Spencerec38de22010-10-10 22:04:20 +00006534
Eric Christopher30ef0e52010-06-03 04:07:48 +00006535 case TLSModel::InitialExec:
6536 case TLSModel::LocalExec:
6537 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
6538 Subtarget->is64Bit());
6539 }
6540 } else if (Subtarget->isTargetDarwin()) {
6541 // Darwin only has one model of TLS. Lower to that.
6542 unsigned char OpFlag = 0;
6543 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
6544 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006545
Eric Christopher30ef0e52010-06-03 04:07:48 +00006546 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6547 // global base reg.
6548 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
6549 !Subtarget->is64Bit();
6550 if (PIC32)
6551 OpFlag = X86II::MO_TLVP_PIC_BASE;
6552 else
6553 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006554 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00006555 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00006556 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00006557 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00006558 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006559
Eric Christopher30ef0e52010-06-03 04:07:48 +00006560 // With PIC32, the address is actually $g + Offset.
6561 if (PIC32)
6562 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6563 DAG.getNode(X86ISD::GlobalBaseReg,
6564 DebugLoc(), getPointerTy()),
6565 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006566
Eric Christopher30ef0e52010-06-03 04:07:48 +00006567 // Lowering the machine isd will make sure everything is in the right
6568 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00006569 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006570 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00006571 SDValue Args[] = { Chain, Offset };
6572 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006573
Eric Christopher30ef0e52010-06-03 04:07:48 +00006574 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
6575 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
6576 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00006577
Eric Christopher30ef0e52010-06-03 04:07:48 +00006578 // And our return value (tls address) is in the standard call return value
6579 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00006580 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
6581 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy());
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006582 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00006583
Eric Christopher30ef0e52010-06-03 04:07:48 +00006584 assert(false &&
6585 "TLS not implemented for this target.");
Eric Christopherfd179292009-08-27 18:07:15 +00006586
Torok Edwinc23197a2009-07-14 16:55:14 +00006587 llvm_unreachable("Unreachable");
Chris Lattner5867de12009-04-01 22:14:45 +00006588 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006589}
6590
Evan Cheng0db9fe62006-04-25 20:13:52 +00006591
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006592/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00006593/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00006594SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman4c1fa612008-03-03 22:22:09 +00006595 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00006596 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006597 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006598 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006599 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00006600 SDValue ShOpLo = Op.getOperand(0);
6601 SDValue ShOpHi = Op.getOperand(1);
6602 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00006603 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00006604 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00006605 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00006606
Dan Gohman475871a2008-07-27 21:46:04 +00006607 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006608 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00006609 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
6610 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006611 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006612 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
6613 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006614 }
Evan Chenge3413162006-01-09 18:33:28 +00006615
Owen Anderson825b72b2009-08-11 20:47:22 +00006616 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
6617 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00006618 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00006619 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00006620
Dan Gohman475871a2008-07-27 21:46:04 +00006621 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00006622 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00006623 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
6624 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00006625
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006626 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00006627 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
6628 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006629 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006630 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
6631 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006632 }
6633
Dan Gohman475871a2008-07-27 21:46:04 +00006634 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00006635 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006636}
Evan Chenga3195e82006-01-12 22:54:21 +00006637
Dan Gohmand858e902010-04-17 15:26:15 +00006638SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
6639 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006640 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00006641
Dale Johannesen0488fb62010-09-30 23:57:10 +00006642 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00006643 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00006644
Owen Anderson825b72b2009-08-11 20:47:22 +00006645 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00006646 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00006647
Eli Friedman36df4992009-05-27 00:47:34 +00006648 // These are really Legal; return the operand so the caller accepts it as
6649 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00006650 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00006651 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00006652 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00006653 Subtarget->is64Bit()) {
6654 return Op;
6655 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006656
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006657 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006658 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006659 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00006660 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006661 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00006662 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00006663 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006664 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006665 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00006666 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
6667}
Evan Cheng0db9fe62006-04-25 20:13:52 +00006668
Owen Andersone50ed302009-08-10 22:56:29 +00006669SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00006670 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00006671 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006672 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00006673 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00006674 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00006675 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006676 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006677 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00006678 else
Owen Anderson825b72b2009-08-11 20:47:22 +00006679 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006680
Chris Lattner492a43e2010-09-22 01:28:21 +00006681 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006682
Chris Lattner492a43e2010-09-22 01:28:21 +00006683 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
6684 MachineMemOperand *MMO =
6685 DAG.getMachineFunction()
6686 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6687 MachineMemOperand::MOLoad, ByteSize, ByteSize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006688
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006689 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00006690 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
6691 X86ISD::FILD, DL,
6692 Tys, Ops, array_lengthof(Ops),
6693 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006694
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006695 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006696 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00006697 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006698
6699 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
6700 // shouldn't be necessary except that RFP cannot be live across
6701 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006702 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00006703 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
6704 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006705 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00006706 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006707 SDValue Ops[] = {
6708 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
6709 };
Chris Lattner492a43e2010-09-22 01:28:21 +00006710 MachineMemOperand *MMO =
6711 DAG.getMachineFunction()
6712 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00006713 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006714
Chris Lattner492a43e2010-09-22 01:28:21 +00006715 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
6716 Ops, array_lengthof(Ops),
6717 Op.getValueType(), MMO);
6718 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006719 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006720 false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006721 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006722
Evan Cheng0db9fe62006-04-25 20:13:52 +00006723 return Result;
6724}
6725
Bill Wendling8b8a6362009-01-17 03:56:04 +00006726// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00006727SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
6728 SelectionDAG &DAG) const {
Bill Wendling8b8a6362009-01-17 03:56:04 +00006729 // This algorithm is not obvious. Here it is in C code, more or less:
6730 /*
6731 double uint64_to_double( uint32_t hi, uint32_t lo ) {
6732 static const __m128i exp = { 0x4330000045300000ULL, 0 };
6733 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00006734
Bill Wendling8b8a6362009-01-17 03:56:04 +00006735 // Copy ints to xmm registers.
6736 __m128i xh = _mm_cvtsi32_si128( hi );
6737 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00006738
Bill Wendling8b8a6362009-01-17 03:56:04 +00006739 // Combine into low half of a single xmm register.
6740 __m128i x = _mm_unpacklo_epi32( xh, xl );
6741 __m128d d;
6742 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00006743
Bill Wendling8b8a6362009-01-17 03:56:04 +00006744 // Merge in appropriate exponents to give the integer bits the right
6745 // magnitude.
6746 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00006747
Bill Wendling8b8a6362009-01-17 03:56:04 +00006748 // Subtract away the biases to deal with the IEEE-754 double precision
6749 // implicit 1.
6750 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00006751
Bill Wendling8b8a6362009-01-17 03:56:04 +00006752 // All conversions up to here are exact. The correctly rounded result is
6753 // calculated using the current rounding mode using the following
6754 // horizontal add.
6755 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
6756 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
6757 // store doesn't really need to be here (except
6758 // maybe to zero the other double)
6759 return sd;
6760 }
6761 */
Dale Johannesen040225f2008-10-21 23:07:49 +00006762
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006763 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00006764 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00006765
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006766 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00006767 std::vector<Constant*> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00006768 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
6769 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
6770 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
6771 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00006772 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006773 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006774
Bill Wendling8b8a6362009-01-17 03:56:04 +00006775 std::vector<Constant*> CV1;
Owen Andersona90b3dc2009-07-15 21:51:10 +00006776 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006777 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
Owen Andersona90b3dc2009-07-15 21:51:10 +00006778 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006779 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00006780 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006781 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006782
Owen Anderson825b72b2009-08-11 20:47:22 +00006783 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6784 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00006785 Op.getOperand(0),
6786 DAG.getIntPtrConstant(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006787 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6788 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00006789 Op.getOperand(0),
6790 DAG.getIntPtrConstant(0)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006791 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
6792 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00006793 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006794 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00006795 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006796 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck2);
Owen Anderson825b72b2009-08-11 20:47:22 +00006797 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00006798 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006799 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00006800 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006801
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006802 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00006803 int ShufMask[2] = { 1, -1 };
Owen Anderson825b72b2009-08-11 20:47:22 +00006804 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
6805 DAG.getUNDEF(MVT::v2f64), ShufMask);
6806 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
6807 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006808 DAG.getIntPtrConstant(0));
6809}
6810
Bill Wendling8b8a6362009-01-17 03:56:04 +00006811// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00006812SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
6813 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006814 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00006815 // FP constant to bias correct the final result.
6816 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00006817 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006818
6819 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00006820 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6821 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Bill Wendling8b8a6362009-01-17 03:56:04 +00006822 Op.getOperand(0),
6823 DAG.getIntPtrConstant(0)));
6824
Owen Anderson825b72b2009-08-11 20:47:22 +00006825 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006826 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00006827 DAG.getIntPtrConstant(0));
6828
6829 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00006830 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006831 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00006832 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006833 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006834 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00006835 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006836 MVT::v2f64, Bias)));
6837 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006838 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00006839 DAG.getIntPtrConstant(0));
6840
6841 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00006842 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006843
6844 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00006845 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00006846
Owen Anderson825b72b2009-08-11 20:47:22 +00006847 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006848 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00006849 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00006850 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006851 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00006852 }
6853
6854 // Handle final rounding.
6855 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00006856}
6857
Dan Gohmand858e902010-04-17 15:26:15 +00006858SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
6859 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00006860 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006861 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00006862
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006863 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00006864 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
6865 // the optimization here.
6866 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00006867 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00006868
Owen Andersone50ed302009-08-10 22:56:29 +00006869 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006870 EVT DstVT = Op.getValueType();
6871 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00006872 return LowerUINT_TO_FP_i64(Op, DAG);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006873 else if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00006874 return LowerUINT_TO_FP_i32(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00006875
6876 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00006877 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006878 if (SrcVT == MVT::i32) {
6879 SDValue WordOff = DAG.getConstant(4, getPointerTy());
6880 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
6881 getPointerTy(), StackSlot, WordOff);
6882 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006883 StackSlot, MachinePointerInfo(),
6884 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006885 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006886 OffsetSlot, MachinePointerInfo(),
6887 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006888 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
6889 return Fild;
6890 }
6891
6892 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
6893 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006894 StackSlot, MachinePointerInfo(),
6895 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006896 // For i64 source, we need to add the appropriate power of 2 if the input
6897 // was negative. This is the same as the optimization in
6898 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
6899 // we must be careful to do the computation in x87 extended precision, not
6900 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00006901 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
6902 MachineMemOperand *MMO =
6903 DAG.getMachineFunction()
6904 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6905 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006906
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006907 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
6908 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00006909 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
6910 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006911
6912 APInt FF(32, 0x5F800000ULL);
6913
6914 // Check whether the sign bit is set.
6915 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
6916 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
6917 ISD::SETLT);
6918
6919 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
6920 SDValue FudgePtr = DAG.getConstantPool(
6921 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
6922 getPointerTy());
6923
6924 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
6925 SDValue Zero = DAG.getIntPtrConstant(0);
6926 SDValue Four = DAG.getIntPtrConstant(4);
6927 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
6928 Zero, Four);
6929 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
6930
6931 // Load the value out, extending it from f32 to f80.
6932 // FIXME: Avoid the extend by constructing the right constant pool?
Stuart Hastingsa9011292011-02-16 16:23:55 +00006933 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00006934 FudgePtr, MachinePointerInfo::getConstantPool(),
6935 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006936 // Extend everything to 80 bits to force it to be done on x87.
6937 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
6938 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00006939}
6940
Dan Gohman475871a2008-07-27 21:46:04 +00006941std::pair<SDValue,SDValue> X86TargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00006942FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) const {
Chris Lattner07290932010-09-22 01:05:16 +00006943 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00006944
Owen Andersone50ed302009-08-10 22:56:29 +00006945 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00006946
6947 if (!IsSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006948 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
6949 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00006950 }
6951
Owen Anderson825b72b2009-08-11 20:47:22 +00006952 assert(DstTy.getSimpleVT() <= MVT::i64 &&
6953 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00006954 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00006955
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006956 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00006957 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00006958 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00006959 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00006960 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00006961 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00006962 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00006963 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006964
Evan Cheng87c89352007-10-15 20:11:21 +00006965 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
6966 // stack slot.
6967 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00006968 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00006969 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006970 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00006971
Michael J. Spencerec38de22010-10-10 22:04:20 +00006972
6973
Evan Cheng0db9fe62006-04-25 20:13:52 +00006974 unsigned Opc;
Owen Anderson825b72b2009-08-11 20:47:22 +00006975 switch (DstTy.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00006976 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson825b72b2009-08-11 20:47:22 +00006977 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
6978 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
6979 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006980 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006981
Dan Gohman475871a2008-07-27 21:46:04 +00006982 SDValue Chain = DAG.getEntryNode();
6983 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00006984 EVT TheVT = Op.getOperand(0).getValueType();
6985 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006986 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00006987 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006988 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006989 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00006990 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00006991 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00006992 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00006993 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00006994
Chris Lattner492a43e2010-09-22 01:28:21 +00006995 MachineMemOperand *MMO =
6996 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6997 MachineMemOperand::MOLoad, MemSize, MemSize);
6998 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
6999 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007000 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00007001 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007002 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
7003 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007004
Chris Lattner07290932010-09-22 01:05:16 +00007005 MachineMemOperand *MMO =
7006 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7007 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007008
Evan Cheng0db9fe62006-04-25 20:13:52 +00007009 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00007010 SDValue Ops[] = { Chain, Value, StackSlot };
Chris Lattner07290932010-09-22 01:05:16 +00007011 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
7012 Ops, 3, DstTy, MMO);
Evan Chengd9558e02006-01-06 00:43:03 +00007013
Chris Lattner27a6c732007-11-24 07:07:01 +00007014 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007015}
7016
Dan Gohmand858e902010-04-17 15:26:15 +00007017SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
7018 SelectionDAG &DAG) const {
Dale Johannesen0488fb62010-09-30 23:57:10 +00007019 if (Op.getValueType().isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007020 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007021
Eli Friedman948e95a2009-05-23 09:59:16 +00007022 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00007023 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00007024 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
7025 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00007026
Chris Lattner27a6c732007-11-24 07:07:01 +00007027 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007028 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00007029 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00007030}
7031
Dan Gohmand858e902010-04-17 15:26:15 +00007032SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
7033 SelectionDAG &DAG) const {
Eli Friedman948e95a2009-05-23 09:59:16 +00007034 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
7035 SDValue FIST = Vals.first, StackSlot = Vals.second;
7036 assert(FIST.getNode() && "Unexpected failure");
7037
7038 // Load the result.
7039 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00007040 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007041}
7042
Dan Gohmand858e902010-04-17 15:26:15 +00007043SDValue X86TargetLowering::LowerFABS(SDValue Op,
7044 SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007045 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007046 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007047 EVT VT = Op.getValueType();
7048 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007049 if (VT.isVector())
7050 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007051 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007052 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007053 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00007054 CV.push_back(C);
7055 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007056 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007057 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00007058 CV.push_back(C);
7059 CV.push_back(C);
7060 CV.push_back(C);
7061 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007062 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007063 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007064 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007065 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007066 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007067 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007068 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007069}
7070
Dan Gohmand858e902010-04-17 15:26:15 +00007071SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007072 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007073 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007074 EVT VT = Op.getValueType();
7075 EVT EltVT = VT;
Duncan Sandsda9ad382009-09-06 19:29:07 +00007076 if (VT.isVector())
Duncan Sands83ec4b62008-06-06 12:08:01 +00007077 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007078 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007079 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007080 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00007081 CV.push_back(C);
7082 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007083 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007084 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00007085 CV.push_back(C);
7086 CV.push_back(C);
7087 CV.push_back(C);
7088 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007089 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007090 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007091 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007092 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007093 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007094 false, false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00007095 if (VT.isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007096 return DAG.getNode(ISD::BITCAST, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007097 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007098 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007099 Op.getOperand(0)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007100 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00007101 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007102 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00007103 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00007104}
7105
Dan Gohmand858e902010-04-17 15:26:15 +00007106SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007107 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00007108 SDValue Op0 = Op.getOperand(0);
7109 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007110 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007111 EVT VT = Op.getValueType();
7112 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00007113
7114 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007115 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007116 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00007117 SrcVT = VT;
7118 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007119 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007120 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007121 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007122 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007123 }
7124
7125 // At this point the operands and the result should have the same
7126 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00007127
Evan Cheng68c47cb2007-01-05 07:55:56 +00007128 // First get the sign bit of second operand.
7129 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007130 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007131 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
7132 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007133 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007134 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
7135 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7136 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7137 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007138 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007139 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007140 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007141 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007142 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007143 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007144 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00007145
7146 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007147 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007148 // Op0 is MVT::f32, Op1 is MVT::f64.
7149 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
7150 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
7151 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007152 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00007153 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00007154 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007155 }
7156
Evan Cheng73d6cf12007-01-05 21:37:56 +00007157 // Clear first operand sign bit.
7158 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00007159 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007160 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
7161 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00007162 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007163 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
7164 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7165 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7166 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00007167 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007168 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007169 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007170 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007171 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007172 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007173 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00007174
7175 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00007176 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00007177}
7178
Dan Gohman076aee32009-03-04 19:44:21 +00007179/// Emit nodes that will be selected as "test Op0,Op0", or something
7180/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00007181SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00007182 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00007183 DebugLoc dl = Op.getDebugLoc();
7184
Dan Gohman31125812009-03-07 01:58:32 +00007185 // CF and OF aren't always set the way we want. Determine which
7186 // of these we need.
7187 bool NeedCF = false;
7188 bool NeedOF = false;
7189 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007190 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00007191 case X86::COND_A: case X86::COND_AE:
7192 case X86::COND_B: case X86::COND_BE:
7193 NeedCF = true;
7194 break;
7195 case X86::COND_G: case X86::COND_GE:
7196 case X86::COND_L: case X86::COND_LE:
7197 case X86::COND_O: case X86::COND_NO:
7198 NeedOF = true;
7199 break;
Dan Gohman31125812009-03-07 01:58:32 +00007200 }
7201
Dan Gohman076aee32009-03-04 19:44:21 +00007202 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00007203 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
7204 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007205 if (Op.getResNo() != 0 || NeedOF || NeedCF)
7206 // Emit a CMP with 0, which is the TEST pattern.
7207 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
7208 DAG.getConstant(0, Op.getValueType()));
7209
7210 unsigned Opcode = 0;
7211 unsigned NumOperands = 0;
7212 switch (Op.getNode()->getOpcode()) {
7213 case ISD::ADD:
7214 // Due to an isel shortcoming, be conservative if this add is likely to be
7215 // selected as part of a load-modify-store instruction. When the root node
7216 // in a match is a store, isel doesn't know how to remap non-chain non-flag
7217 // uses of other nodes in the match, such as the ADD in this case. This
7218 // leads to the ADD being left around and reselected, with the result being
7219 // two adds in the output. Alas, even if none our users are stores, that
7220 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
7221 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
7222 // climbing the DAG back to the root, and it doesn't seem to be worth the
7223 // effort.
7224 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Dan Gohman076aee32009-03-04 19:44:21 +00007225 UE = Op.getNode()->use_end(); UI != UE; ++UI)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007226 if (UI->getOpcode() != ISD::CopyToReg && UI->getOpcode() != ISD::SETCC)
7227 goto default_case;
7228
7229 if (ConstantSDNode *C =
7230 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
7231 // An add of one will be selected as an INC.
7232 if (C->getAPIntValue() == 1) {
7233 Opcode = X86ISD::INC;
7234 NumOperands = 1;
7235 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00007236 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007237
7238 // An add of negative one (subtract of one) will be selected as a DEC.
7239 if (C->getAPIntValue().isAllOnesValue()) {
7240 Opcode = X86ISD::DEC;
7241 NumOperands = 1;
7242 break;
7243 }
Dan Gohman076aee32009-03-04 19:44:21 +00007244 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007245
7246 // Otherwise use a regular EFLAGS-setting add.
7247 Opcode = X86ISD::ADD;
7248 NumOperands = 2;
7249 break;
7250 case ISD::AND: {
7251 // If the primary and result isn't used, don't bother using X86ISD::AND,
7252 // because a TEST instruction will be better.
7253 bool NonFlagUse = false;
7254 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
7255 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
7256 SDNode *User = *UI;
7257 unsigned UOpNo = UI.getOperandNo();
7258 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
7259 // Look pass truncate.
7260 UOpNo = User->use_begin().getOperandNo();
7261 User = *User->use_begin();
7262 }
7263
7264 if (User->getOpcode() != ISD::BRCOND &&
7265 User->getOpcode() != ISD::SETCC &&
7266 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
7267 NonFlagUse = true;
7268 break;
7269 }
Dan Gohman076aee32009-03-04 19:44:21 +00007270 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007271
7272 if (!NonFlagUse)
7273 break;
7274 }
7275 // FALL THROUGH
7276 case ISD::SUB:
7277 case ISD::OR:
7278 case ISD::XOR:
7279 // Due to the ISEL shortcoming noted above, be conservative if this op is
7280 // likely to be selected as part of a load-modify-store instruction.
7281 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
7282 UE = Op.getNode()->use_end(); UI != UE; ++UI)
7283 if (UI->getOpcode() == ISD::STORE)
7284 goto default_case;
7285
7286 // Otherwise use a regular EFLAGS-setting instruction.
7287 switch (Op.getNode()->getOpcode()) {
7288 default: llvm_unreachable("unexpected operator!");
7289 case ISD::SUB: Opcode = X86ISD::SUB; break;
7290 case ISD::OR: Opcode = X86ISD::OR; break;
7291 case ISD::XOR: Opcode = X86ISD::XOR; break;
7292 case ISD::AND: Opcode = X86ISD::AND; break;
7293 }
7294
7295 NumOperands = 2;
7296 break;
7297 case X86ISD::ADD:
7298 case X86ISD::SUB:
7299 case X86ISD::INC:
7300 case X86ISD::DEC:
7301 case X86ISD::OR:
7302 case X86ISD::XOR:
7303 case X86ISD::AND:
7304 return SDValue(Op.getNode(), 1);
7305 default:
7306 default_case:
7307 break;
Dan Gohman076aee32009-03-04 19:44:21 +00007308 }
7309
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007310 if (Opcode == 0)
7311 // Emit a CMP with 0, which is the TEST pattern.
7312 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
7313 DAG.getConstant(0, Op.getValueType()));
7314
7315 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
7316 SmallVector<SDValue, 4> Ops;
7317 for (unsigned i = 0; i != NumOperands; ++i)
7318 Ops.push_back(Op.getOperand(i));
7319
7320 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
7321 DAG.ReplaceAllUsesWith(Op, New);
7322 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00007323}
7324
7325/// Emit nodes that will be selected as "cmp Op0,Op1", or something
7326/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00007327SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00007328 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00007329 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
7330 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00007331 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00007332
7333 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00007334 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00007335}
7336
Evan Chengd40d03e2010-01-06 19:38:29 +00007337/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
7338/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00007339SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
7340 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00007341 SDValue Op0 = And.getOperand(0);
7342 SDValue Op1 = And.getOperand(1);
7343 if (Op0.getOpcode() == ISD::TRUNCATE)
7344 Op0 = Op0.getOperand(0);
7345 if (Op1.getOpcode() == ISD::TRUNCATE)
7346 Op1 = Op1.getOperand(0);
7347
Evan Chengd40d03e2010-01-06 19:38:29 +00007348 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00007349 if (Op1.getOpcode() == ISD::SHL)
7350 std::swap(Op0, Op1);
7351 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00007352 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
7353 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00007354 // If we looked past a truncate, check that it's only truncating away
7355 // known zeros.
7356 unsigned BitWidth = Op0.getValueSizeInBits();
7357 unsigned AndBitWidth = And.getValueSizeInBits();
7358 if (BitWidth > AndBitWidth) {
7359 APInt Mask = APInt::getAllOnesValue(BitWidth), Zeros, Ones;
7360 DAG.ComputeMaskedBits(Op0, Mask, Zeros, Ones);
7361 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
7362 return SDValue();
7363 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007364 LHS = Op1;
7365 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00007366 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007367 } else if (Op1.getOpcode() == ISD::Constant) {
7368 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
7369 SDValue AndLHS = Op0;
Evan Chengd40d03e2010-01-06 19:38:29 +00007370 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
7371 LHS = AndLHS.getOperand(0);
7372 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00007373 }
Evan Chengd40d03e2010-01-06 19:38:29 +00007374 }
Evan Cheng0488db92007-09-25 01:57:46 +00007375
Evan Chengd40d03e2010-01-06 19:38:29 +00007376 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00007377 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00007378 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00007379 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00007380 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00007381 // Also promote i16 to i32 for performance / code size reason.
7382 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00007383 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00007384 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00007385
Evan Chengd40d03e2010-01-06 19:38:29 +00007386 // If the operand types disagree, extend the shift amount to match. Since
7387 // BT ignores high bits (like shifts) we can use anyextend.
7388 if (LHS.getValueType() != RHS.getValueType())
7389 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00007390
Evan Chengd40d03e2010-01-06 19:38:29 +00007391 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
7392 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
7393 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7394 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00007395 }
7396
Evan Cheng54de3ea2010-01-05 06:52:31 +00007397 return SDValue();
7398}
7399
Dan Gohmand858e902010-04-17 15:26:15 +00007400SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng54de3ea2010-01-05 06:52:31 +00007401 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
7402 SDValue Op0 = Op.getOperand(0);
7403 SDValue Op1 = Op.getOperand(1);
7404 DebugLoc dl = Op.getDebugLoc();
7405 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
7406
7407 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00007408 // Lower (X & (1 << N)) == 0 to BT(X, N).
7409 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
7410 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Chris Lattner481eebc2010-12-19 21:23:48 +00007411 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00007412 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00007413 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00007414 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
7415 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
7416 if (NewSetCC.getNode())
7417 return NewSetCC;
7418 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00007419
Chris Lattner481eebc2010-12-19 21:23:48 +00007420 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
7421 // these.
7422 if (Op1.getOpcode() == ISD::Constant &&
Evan Cheng2c755ba2010-02-27 07:36:59 +00007423 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
7424 cast<ConstantSDNode>(Op1)->isNullValue()) &&
7425 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007426
Chris Lattner481eebc2010-12-19 21:23:48 +00007427 // If the input is a setcc, then reuse the input setcc or use a new one with
7428 // the inverted condition.
7429 if (Op0.getOpcode() == X86ISD::SETCC) {
7430 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
7431 bool Invert = (CC == ISD::SETNE) ^
7432 cast<ConstantSDNode>(Op1)->isNullValue();
7433 if (!Invert) return Op0;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007434
Evan Cheng2c755ba2010-02-27 07:36:59 +00007435 CCode = X86::GetOppositeBranchCondition(CCode);
Chris Lattner481eebc2010-12-19 21:23:48 +00007436 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7437 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
7438 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007439 }
7440
Evan Chenge5b51ac2010-04-17 06:13:15 +00007441 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00007442 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00007443 if (X86CC == X86::COND_INVALID)
7444 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00007445
Chris Lattnerc19d1c32010-12-19 22:08:31 +00007446 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00007447 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnerc19d1c32010-12-19 22:08:31 +00007448 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
Evan Cheng0488db92007-09-25 01:57:46 +00007449}
7450
Dan Gohmand858e902010-04-17 15:26:15 +00007451SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00007452 SDValue Cond;
7453 SDValue Op0 = Op.getOperand(0);
7454 SDValue Op1 = Op.getOperand(1);
7455 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00007456 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00007457 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
7458 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007459 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00007460
7461 if (isFP) {
7462 unsigned SSECC = 8;
Owen Andersone50ed302009-08-10 22:56:29 +00007463 EVT VT0 = Op0.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00007464 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
7465 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00007466 bool Swap = false;
7467
7468 switch (SetCCOpcode) {
7469 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00007470 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00007471 case ISD::SETEQ: SSECC = 0; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00007472 case ISD::SETOGT:
Nate Begeman30a0de92008-07-17 16:51:19 +00007473 case ISD::SETGT: Swap = true; // Fallthrough
7474 case ISD::SETLT:
7475 case ISD::SETOLT: SSECC = 1; break;
7476 case ISD::SETOGE:
7477 case ISD::SETGE: Swap = true; // Fallthrough
7478 case ISD::SETLE:
7479 case ISD::SETOLE: SSECC = 2; break;
7480 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00007481 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00007482 case ISD::SETNE: SSECC = 4; break;
7483 case ISD::SETULE: Swap = true;
7484 case ISD::SETUGE: SSECC = 5; break;
7485 case ISD::SETULT: Swap = true;
7486 case ISD::SETUGT: SSECC = 6; break;
7487 case ISD::SETO: SSECC = 7; break;
7488 }
7489 if (Swap)
7490 std::swap(Op0, Op1);
7491
Nate Begemanfb8ead02008-07-25 19:05:58 +00007492 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00007493 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00007494 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00007495 SDValue UNORD, EQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00007496 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
7497 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00007498 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00007499 }
7500 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00007501 SDValue ORD, NEQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00007502 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
7503 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00007504 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00007505 }
Torok Edwinc23197a2009-07-14 16:55:14 +00007506 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00007507 }
7508 // Handle all other FP comparisons here.
Owen Anderson825b72b2009-08-11 20:47:22 +00007509 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00007510 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007511
Nate Begeman30a0de92008-07-17 16:51:19 +00007512 // We are handling one of the integer comparisons here. Since SSE only has
7513 // GT and EQ comparisons for integer, swapping operands and multiple
7514 // operations may be required for some comparisons.
7515 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
7516 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007517
Owen Anderson825b72b2009-08-11 20:47:22 +00007518 switch (VT.getSimpleVT().SimpleTy) {
Nate Begeman30a0de92008-07-17 16:51:19 +00007519 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007520 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007521 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007522 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
7523 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00007524 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007525
Nate Begeman30a0de92008-07-17 16:51:19 +00007526 switch (SetCCOpcode) {
7527 default: break;
7528 case ISD::SETNE: Invert = true;
7529 case ISD::SETEQ: Opc = EQOpc; break;
7530 case ISD::SETLT: Swap = true;
7531 case ISD::SETGT: Opc = GTOpc; break;
7532 case ISD::SETGE: Swap = true;
7533 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
7534 case ISD::SETULT: Swap = true;
7535 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
7536 case ISD::SETUGE: Swap = true;
7537 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
7538 }
7539 if (Swap)
7540 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007541
Nate Begeman30a0de92008-07-17 16:51:19 +00007542 // Since SSE has no unsigned integer comparisons, we need to flip the sign
7543 // bits of the inputs before performing those operations.
7544 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00007545 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00007546 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
7547 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00007548 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00007549 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
7550 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00007551 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
7552 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00007553 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007554
Dale Johannesenace16102009-02-03 19:33:06 +00007555 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00007556
7557 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00007558 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00007559 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00007560
Nate Begeman30a0de92008-07-17 16:51:19 +00007561 return Result;
7562}
Evan Cheng0488db92007-09-25 01:57:46 +00007563
Evan Cheng370e5342008-12-03 08:38:43 +00007564// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00007565static bool isX86LogicalCmp(SDValue Op) {
7566 unsigned Opc = Op.getNode()->getOpcode();
7567 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
7568 return true;
7569 if (Op.getResNo() == 1 &&
7570 (Opc == X86ISD::ADD ||
7571 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00007572 Opc == X86ISD::ADC ||
7573 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00007574 Opc == X86ISD::SMUL ||
7575 Opc == X86ISD::UMUL ||
7576 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00007577 Opc == X86ISD::DEC ||
7578 Opc == X86ISD::OR ||
7579 Opc == X86ISD::XOR ||
7580 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00007581 return true;
7582
Chris Lattner9637d5b2010-12-05 07:49:54 +00007583 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
7584 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007585
Dan Gohman076aee32009-03-04 19:44:21 +00007586 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00007587}
7588
Chris Lattnera2b56002010-12-05 01:23:24 +00007589static bool isZero(SDValue V) {
7590 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
7591 return C && C->isNullValue();
7592}
7593
Chris Lattner96908b12010-12-05 02:00:51 +00007594static bool isAllOnes(SDValue V) {
7595 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
7596 return C && C->isAllOnesValue();
7597}
7598
Dan Gohmand858e902010-04-17 15:26:15 +00007599SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00007600 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007601 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00007602 SDValue Op1 = Op.getOperand(1);
7603 SDValue Op2 = Op.getOperand(2);
7604 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00007605 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00007606
Dan Gohman1a492952009-10-20 16:22:37 +00007607 if (Cond.getOpcode() == ISD::SETCC) {
7608 SDValue NewCond = LowerSETCC(Cond, DAG);
7609 if (NewCond.getNode())
7610 Cond = NewCond;
7611 }
Evan Cheng734503b2006-09-11 02:19:56 +00007612
Chris Lattnera2b56002010-12-05 01:23:24 +00007613 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00007614 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00007615 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00007616 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007617 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00007618 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
7619 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007620 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007621
Chris Lattnera2b56002010-12-05 01:23:24 +00007622 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007623
7624 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00007625 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
7626 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00007627
7628 SDValue CmpOp0 = Cmp.getOperand(0);
7629 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
7630 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007631
Chris Lattner96908b12010-12-05 02:00:51 +00007632 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00007633 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
7634 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007635
Chris Lattner96908b12010-12-05 02:00:51 +00007636 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
7637 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007638
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007639 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00007640 if (N2C == 0 || !N2C->isNullValue())
7641 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
7642 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007643 }
7644 }
7645
Chris Lattnera2b56002010-12-05 01:23:24 +00007646 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00007647 if (Cond.getOpcode() == ISD::AND &&
7648 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
7649 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00007650 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00007651 Cond = Cond.getOperand(0);
7652 }
7653
Evan Cheng3f41d662007-10-08 22:16:29 +00007654 // If condition flag is set by a X86ISD::CMP, then use it as the condition
7655 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00007656 if (Cond.getOpcode() == X86ISD::SETCC ||
7657 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00007658 CC = Cond.getOperand(0);
7659
Dan Gohman475871a2008-07-27 21:46:04 +00007660 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00007661 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00007662 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00007663
Evan Cheng3f41d662007-10-08 22:16:29 +00007664 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007665 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00007666 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00007667 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00007668
Chris Lattnerd1980a52009-03-12 06:52:53 +00007669 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
7670 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00007671 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00007672 addTest = false;
7673 }
7674 }
7675
7676 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007677 // Look pass the truncate.
7678 if (Cond.getOpcode() == ISD::TRUNCATE)
7679 Cond = Cond.getOperand(0);
7680
7681 // We know the result of AND is compared against zero. Try to match
7682 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00007683 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00007684 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00007685 if (NewSetCC.getNode()) {
7686 CC = NewSetCC.getOperand(0);
7687 Cond = NewSetCC.getOperand(1);
7688 addTest = false;
7689 }
7690 }
7691 }
7692
7693 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007694 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00007695 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00007696 }
7697
Benjamin Kramere915ff32010-12-22 23:09:28 +00007698 // a < b ? -1 : 0 -> RES = ~setcc_carry
7699 // a < b ? 0 : -1 -> RES = setcc_carry
7700 // a >= b ? -1 : 0 -> RES = setcc_carry
7701 // a >= b ? 0 : -1 -> RES = ~setcc_carry
7702 if (Cond.getOpcode() == X86ISD::CMP) {
7703 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
7704
7705 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
7706 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
7707 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
7708 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
7709 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
7710 return DAG.getNOT(DL, Res, Res.getValueType());
7711 return Res;
7712 }
7713 }
7714
Evan Cheng0488db92007-09-25 01:57:46 +00007715 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
7716 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007717 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007718 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00007719 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00007720}
7721
Evan Cheng370e5342008-12-03 08:38:43 +00007722// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
7723// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
7724// from the AND / OR.
7725static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
7726 Opc = Op.getOpcode();
7727 if (Opc != ISD::OR && Opc != ISD::AND)
7728 return false;
7729 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
7730 Op.getOperand(0).hasOneUse() &&
7731 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
7732 Op.getOperand(1).hasOneUse());
7733}
7734
Evan Cheng961d6d42009-02-02 08:19:07 +00007735// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
7736// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00007737static bool isXor1OfSetCC(SDValue Op) {
7738 if (Op.getOpcode() != ISD::XOR)
7739 return false;
7740 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
7741 if (N1C && N1C->getAPIntValue() == 1) {
7742 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
7743 Op.getOperand(0).hasOneUse();
7744 }
7745 return false;
7746}
7747
Dan Gohmand858e902010-04-17 15:26:15 +00007748SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00007749 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007750 SDValue Chain = Op.getOperand(0);
7751 SDValue Cond = Op.getOperand(1);
7752 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007753 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00007754 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00007755
Dan Gohman1a492952009-10-20 16:22:37 +00007756 if (Cond.getOpcode() == ISD::SETCC) {
7757 SDValue NewCond = LowerSETCC(Cond, DAG);
7758 if (NewCond.getNode())
7759 Cond = NewCond;
7760 }
Chris Lattnere55484e2008-12-25 05:34:37 +00007761#if 0
7762 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00007763 else if (Cond.getOpcode() == X86ISD::ADD ||
7764 Cond.getOpcode() == X86ISD::SUB ||
7765 Cond.getOpcode() == X86ISD::SMUL ||
7766 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00007767 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00007768#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00007769
Evan Chengad9c0a32009-12-15 00:53:42 +00007770 // Look pass (and (setcc_carry (cmp ...)), 1).
7771 if (Cond.getOpcode() == ISD::AND &&
7772 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
7773 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00007774 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00007775 Cond = Cond.getOperand(0);
7776 }
7777
Evan Cheng3f41d662007-10-08 22:16:29 +00007778 // If condition flag is set by a X86ISD::CMP, then use it as the condition
7779 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00007780 if (Cond.getOpcode() == X86ISD::SETCC ||
7781 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00007782 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007783
Dan Gohman475871a2008-07-27 21:46:04 +00007784 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00007785 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00007786 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00007787 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00007788 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00007789 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00007790 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00007791 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00007792 default: break;
7793 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00007794 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00007795 // These can only come from an arithmetic instruction with overflow,
7796 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00007797 Cond = Cond.getNode()->getOperand(1);
7798 addTest = false;
7799 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00007800 }
Evan Cheng0488db92007-09-25 01:57:46 +00007801 }
Evan Cheng370e5342008-12-03 08:38:43 +00007802 } else {
7803 unsigned CondOpc;
7804 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
7805 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00007806 if (CondOpc == ISD::OR) {
7807 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
7808 // two branches instead of an explicit OR instruction with a
7809 // separate test.
7810 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00007811 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00007812 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007813 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00007814 Chain, Dest, CC, Cmp);
7815 CC = Cond.getOperand(1).getOperand(0);
7816 Cond = Cmp;
7817 addTest = false;
7818 }
7819 } else { // ISD::AND
7820 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
7821 // two branches instead of an explicit AND instruction with a
7822 // separate test. However, we only do this if this block doesn't
7823 // have a fall-through edge, because this requires an explicit
7824 // jmp when the condition is false.
7825 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00007826 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00007827 Op.getNode()->hasOneUse()) {
7828 X86::CondCode CCode =
7829 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
7830 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007831 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00007832 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00007833 // Look for an unconditional branch following this conditional branch.
7834 // We need this because we need to reverse the successors in order
7835 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00007836 if (User->getOpcode() == ISD::BR) {
7837 SDValue FalseBB = User->getOperand(1);
7838 SDNode *NewBR =
7839 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00007840 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00007841 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00007842 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00007843
Dale Johannesene4d209d2009-02-03 20:21:25 +00007844 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00007845 Chain, Dest, CC, Cmp);
7846 X86::CondCode CCode =
7847 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
7848 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007849 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00007850 Cond = Cmp;
7851 addTest = false;
7852 }
7853 }
Dan Gohman279c22e2008-10-21 03:29:32 +00007854 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00007855 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
7856 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
7857 // It should be transformed during dag combiner except when the condition
7858 // is set by a arithmetics with overflow node.
7859 X86::CondCode CCode =
7860 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
7861 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007862 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00007863 Cond = Cond.getOperand(0).getOperand(1);
7864 addTest = false;
Dan Gohman279c22e2008-10-21 03:29:32 +00007865 }
Evan Cheng0488db92007-09-25 01:57:46 +00007866 }
7867
7868 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007869 // Look pass the truncate.
7870 if (Cond.getOpcode() == ISD::TRUNCATE)
7871 Cond = Cond.getOperand(0);
7872
7873 // We know the result of AND is compared against zero. Try to match
7874 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00007875 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007876 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
7877 if (NewSetCC.getNode()) {
7878 CC = NewSetCC.getOperand(0);
7879 Cond = NewSetCC.getOperand(1);
7880 addTest = false;
7881 }
7882 }
7883 }
7884
7885 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007886 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00007887 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00007888 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00007889 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00007890 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00007891}
7892
Anton Korobeynikove060b532007-04-17 19:34:00 +00007893
7894// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
7895// Calls to _alloca is needed to probe the stack when allocating more than 4k
7896// bytes in one go. Touching the stack at 4K increments is necessary to ensure
7897// that the guard pages used by the OS virtual memory manager are allocated in
7898// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00007899SDValue
7900X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00007901 SelectionDAG &DAG) const {
Duncan Sands1e1ca0b2010-10-21 16:02:12 +00007902 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows()) &&
Michael J. Spencere9c253e2010-10-21 01:41:01 +00007903 "This should be used only on Windows targets");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007904 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007905
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007906 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00007907 SDValue Chain = Op.getOperand(0);
7908 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007909 // FIXME: Ensure alignment here
7910
Dan Gohman475871a2008-07-27 21:46:04 +00007911 SDValue Flag;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007912
Owen Anderson825b72b2009-08-11 20:47:22 +00007913 EVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007914
Dale Johannesendd64c412009-02-04 00:33:20 +00007915 Chain = DAG.getCopyToReg(Chain, dl, X86::EAX, Size, Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00007916 Flag = Chain.getValue(1);
7917
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007918 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00007919
Michael J. Spencere9c253e2010-10-21 01:41:01 +00007920 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00007921 Flag = Chain.getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007922
Dale Johannesendd64c412009-02-04 00:33:20 +00007923 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007924
Dan Gohman475871a2008-07-27 21:46:04 +00007925 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007926 return DAG.getMergeValues(Ops1, 2, dl);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007927}
7928
Dan Gohmand858e902010-04-17 15:26:15 +00007929SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00007930 MachineFunction &MF = DAG.getMachineFunction();
7931 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
7932
Dan Gohman69de1932008-02-06 22:27:42 +00007933 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00007934 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00007935
Anton Korobeynikove7beda12010-10-03 22:52:07 +00007936 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00007937 // vastart just stores the address of the VarArgsFrameIndex slot into the
7938 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00007939 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
7940 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00007941 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
7942 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007943 }
7944
7945 // __va_list_tag:
7946 // gp_offset (0 - 6 * 8)
7947 // fp_offset (48 - 48 + 8 * 16)
7948 // overflow_arg_area (point to parameters coming in memory).
7949 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00007950 SmallVector<SDValue, 8> MemOps;
7951 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00007952 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00007953 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00007954 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
7955 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007956 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007957 MemOps.push_back(Store);
7958
7959 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00007960 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007961 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +00007962 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00007963 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
7964 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007965 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007966 MemOps.push_back(Store);
7967
7968 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +00007969 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007970 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00007971 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
7972 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00007973 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
7974 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +00007975 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007976 MemOps.push_back(Store);
7977
7978 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +00007979 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007980 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00007981 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
7982 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00007983 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
7984 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007985 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +00007986 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00007987 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00007988}
7989
Dan Gohmand858e902010-04-17 15:26:15 +00007990SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +00007991 assert(Subtarget->is64Bit() &&
7992 "LowerVAARG only handles 64-bit va_arg!");
7993 assert((Subtarget->isTargetLinux() ||
7994 Subtarget->isTargetDarwin()) &&
7995 "Unhandled target in LowerVAARG");
7996 assert(Op.getNode()->getNumOperands() == 4);
7997 SDValue Chain = Op.getOperand(0);
7998 SDValue SrcPtr = Op.getOperand(1);
7999 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
8000 unsigned Align = Op.getConstantOperandVal(3);
8001 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +00008002
Dan Gohman320afb82010-10-12 18:00:49 +00008003 EVT ArgVT = Op.getNode()->getValueType(0);
8004 const Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
8005 uint32_t ArgSize = getTargetData()->getTypeAllocSize(ArgTy);
8006 uint8_t ArgMode;
8007
8008 // Decide which area this value should be read from.
8009 // TODO: Implement the AMD64 ABI in its entirety. This simple
8010 // selection mechanism works only for the basic types.
8011 if (ArgVT == MVT::f80) {
8012 llvm_unreachable("va_arg for f80 not yet implemented");
8013 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
8014 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
8015 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
8016 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
8017 } else {
8018 llvm_unreachable("Unhandled argument type in LowerVAARG");
8019 }
8020
8021 if (ArgMode == 2) {
8022 // Sanity Check: Make sure using fp_offset makes sense.
Michael J. Spencer87b86652010-10-19 07:32:42 +00008023 assert(!UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +00008024 !(DAG.getMachineFunction()
8025 .getFunction()->hasFnAttr(Attribute::NoImplicitFloat)) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00008026 Subtarget->hasXMM());
Dan Gohman320afb82010-10-12 18:00:49 +00008027 }
8028
8029 // Insert VAARG_64 node into the DAG
8030 // VAARG_64 returns two values: Variable Argument Address, Chain
8031 SmallVector<SDValue, 11> InstOps;
8032 InstOps.push_back(Chain);
8033 InstOps.push_back(SrcPtr);
8034 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
8035 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
8036 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
8037 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
8038 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
8039 VTs, &InstOps[0], InstOps.size(),
8040 MVT::i64,
8041 MachinePointerInfo(SV),
8042 /*Align=*/0,
8043 /*Volatile=*/false,
8044 /*ReadMem=*/true,
8045 /*WriteMem=*/true);
8046 Chain = VAARG.getValue(1);
8047
8048 // Load the next argument and return it
8049 return DAG.getLoad(ArgVT, dl,
8050 Chain,
8051 VAARG,
8052 MachinePointerInfo(),
8053 false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +00008054}
8055
Dan Gohmand858e902010-04-17 15:26:15 +00008056SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) const {
Evan Chengae642192007-03-02 23:16:35 +00008057 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00008058 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00008059 SDValue Chain = Op.getOperand(0);
8060 SDValue DstPtr = Op.getOperand(1);
8061 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00008062 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
8063 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +00008064 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00008065
Chris Lattnere72f2022010-09-21 05:40:29 +00008066 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00008067 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +00008068 false,
Chris Lattnere72f2022010-09-21 05:40:29 +00008069 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +00008070}
8071
Dan Gohman475871a2008-07-27 21:46:04 +00008072SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00008073X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008074 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00008075 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00008076 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00008077 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00008078 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00008079 case Intrinsic::x86_sse_comieq_ss:
8080 case Intrinsic::x86_sse_comilt_ss:
8081 case Intrinsic::x86_sse_comile_ss:
8082 case Intrinsic::x86_sse_comigt_ss:
8083 case Intrinsic::x86_sse_comige_ss:
8084 case Intrinsic::x86_sse_comineq_ss:
8085 case Intrinsic::x86_sse_ucomieq_ss:
8086 case Intrinsic::x86_sse_ucomilt_ss:
8087 case Intrinsic::x86_sse_ucomile_ss:
8088 case Intrinsic::x86_sse_ucomigt_ss:
8089 case Intrinsic::x86_sse_ucomige_ss:
8090 case Intrinsic::x86_sse_ucomineq_ss:
8091 case Intrinsic::x86_sse2_comieq_sd:
8092 case Intrinsic::x86_sse2_comilt_sd:
8093 case Intrinsic::x86_sse2_comile_sd:
8094 case Intrinsic::x86_sse2_comigt_sd:
8095 case Intrinsic::x86_sse2_comige_sd:
8096 case Intrinsic::x86_sse2_comineq_sd:
8097 case Intrinsic::x86_sse2_ucomieq_sd:
8098 case Intrinsic::x86_sse2_ucomilt_sd:
8099 case Intrinsic::x86_sse2_ucomile_sd:
8100 case Intrinsic::x86_sse2_ucomigt_sd:
8101 case Intrinsic::x86_sse2_ucomige_sd:
8102 case Intrinsic::x86_sse2_ucomineq_sd: {
8103 unsigned Opc = 0;
8104 ISD::CondCode CC = ISD::SETCC_INVALID;
8105 switch (IntNo) {
8106 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008107 case Intrinsic::x86_sse_comieq_ss:
8108 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008109 Opc = X86ISD::COMI;
8110 CC = ISD::SETEQ;
8111 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00008112 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008113 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008114 Opc = X86ISD::COMI;
8115 CC = ISD::SETLT;
8116 break;
8117 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008118 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008119 Opc = X86ISD::COMI;
8120 CC = ISD::SETLE;
8121 break;
8122 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008123 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008124 Opc = X86ISD::COMI;
8125 CC = ISD::SETGT;
8126 break;
8127 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008128 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008129 Opc = X86ISD::COMI;
8130 CC = ISD::SETGE;
8131 break;
8132 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008133 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008134 Opc = X86ISD::COMI;
8135 CC = ISD::SETNE;
8136 break;
8137 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008138 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008139 Opc = X86ISD::UCOMI;
8140 CC = ISD::SETEQ;
8141 break;
8142 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008143 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008144 Opc = X86ISD::UCOMI;
8145 CC = ISD::SETLT;
8146 break;
8147 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008148 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008149 Opc = X86ISD::UCOMI;
8150 CC = ISD::SETLE;
8151 break;
8152 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008153 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008154 Opc = X86ISD::UCOMI;
8155 CC = ISD::SETGT;
8156 break;
8157 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008158 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008159 Opc = X86ISD::UCOMI;
8160 CC = ISD::SETGE;
8161 break;
8162 case Intrinsic::x86_sse_ucomineq_ss:
8163 case Intrinsic::x86_sse2_ucomineq_sd:
8164 Opc = X86ISD::UCOMI;
8165 CC = ISD::SETNE;
8166 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00008167 }
Evan Cheng734503b2006-09-11 02:19:56 +00008168
Dan Gohman475871a2008-07-27 21:46:04 +00008169 SDValue LHS = Op.getOperand(1);
8170 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00008171 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00008172 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00008173 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
8174 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8175 DAG.getConstant(X86CC, MVT::i8), Cond);
8176 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00008177 }
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008178 // ptest and testp intrinsics. The intrinsic these come from are designed to
8179 // return an integer value, not just an instruction so lower it to the ptest
8180 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00008181 case Intrinsic::x86_sse41_ptestz:
8182 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008183 case Intrinsic::x86_sse41_ptestnzc:
8184 case Intrinsic::x86_avx_ptestz_256:
8185 case Intrinsic::x86_avx_ptestc_256:
8186 case Intrinsic::x86_avx_ptestnzc_256:
8187 case Intrinsic::x86_avx_vtestz_ps:
8188 case Intrinsic::x86_avx_vtestc_ps:
8189 case Intrinsic::x86_avx_vtestnzc_ps:
8190 case Intrinsic::x86_avx_vtestz_pd:
8191 case Intrinsic::x86_avx_vtestc_pd:
8192 case Intrinsic::x86_avx_vtestnzc_pd:
8193 case Intrinsic::x86_avx_vtestz_ps_256:
8194 case Intrinsic::x86_avx_vtestc_ps_256:
8195 case Intrinsic::x86_avx_vtestnzc_ps_256:
8196 case Intrinsic::x86_avx_vtestz_pd_256:
8197 case Intrinsic::x86_avx_vtestc_pd_256:
8198 case Intrinsic::x86_avx_vtestnzc_pd_256: {
8199 bool IsTestPacked = false;
Eric Christopher71c67532009-07-29 00:28:05 +00008200 unsigned X86CC = 0;
8201 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00008202 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008203 case Intrinsic::x86_avx_vtestz_ps:
8204 case Intrinsic::x86_avx_vtestz_pd:
8205 case Intrinsic::x86_avx_vtestz_ps_256:
8206 case Intrinsic::x86_avx_vtestz_pd_256:
8207 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00008208 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008209 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +00008210 // ZF = 1
8211 X86CC = X86::COND_E;
8212 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008213 case Intrinsic::x86_avx_vtestc_ps:
8214 case Intrinsic::x86_avx_vtestc_pd:
8215 case Intrinsic::x86_avx_vtestc_ps_256:
8216 case Intrinsic::x86_avx_vtestc_pd_256:
8217 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00008218 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008219 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00008220 // CF = 1
8221 X86CC = X86::COND_B;
8222 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008223 case Intrinsic::x86_avx_vtestnzc_ps:
8224 case Intrinsic::x86_avx_vtestnzc_pd:
8225 case Intrinsic::x86_avx_vtestnzc_ps_256:
8226 case Intrinsic::x86_avx_vtestnzc_pd_256:
8227 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +00008228 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008229 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00008230 // ZF and CF = 0
8231 X86CC = X86::COND_A;
8232 break;
8233 }
Eric Christopherfd179292009-08-27 18:07:15 +00008234
Eric Christopher71c67532009-07-29 00:28:05 +00008235 SDValue LHS = Op.getOperand(1);
8236 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008237 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
8238 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00008239 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
8240 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
8241 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00008242 }
Evan Cheng5759f972008-05-04 09:15:50 +00008243
8244 // Fix vector shift instructions where the last operand is a non-immediate
8245 // i32 value.
8246 case Intrinsic::x86_sse2_pslli_w:
8247 case Intrinsic::x86_sse2_pslli_d:
8248 case Intrinsic::x86_sse2_pslli_q:
8249 case Intrinsic::x86_sse2_psrli_w:
8250 case Intrinsic::x86_sse2_psrli_d:
8251 case Intrinsic::x86_sse2_psrli_q:
8252 case Intrinsic::x86_sse2_psrai_w:
8253 case Intrinsic::x86_sse2_psrai_d:
8254 case Intrinsic::x86_mmx_pslli_w:
8255 case Intrinsic::x86_mmx_pslli_d:
8256 case Intrinsic::x86_mmx_pslli_q:
8257 case Intrinsic::x86_mmx_psrli_w:
8258 case Intrinsic::x86_mmx_psrli_d:
8259 case Intrinsic::x86_mmx_psrli_q:
8260 case Intrinsic::x86_mmx_psrai_w:
8261 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00008262 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00008263 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00008264 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00008265
8266 unsigned NewIntNo = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00008267 EVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00008268 switch (IntNo) {
8269 case Intrinsic::x86_sse2_pslli_w:
8270 NewIntNo = Intrinsic::x86_sse2_psll_w;
8271 break;
8272 case Intrinsic::x86_sse2_pslli_d:
8273 NewIntNo = Intrinsic::x86_sse2_psll_d;
8274 break;
8275 case Intrinsic::x86_sse2_pslli_q:
8276 NewIntNo = Intrinsic::x86_sse2_psll_q;
8277 break;
8278 case Intrinsic::x86_sse2_psrli_w:
8279 NewIntNo = Intrinsic::x86_sse2_psrl_w;
8280 break;
8281 case Intrinsic::x86_sse2_psrli_d:
8282 NewIntNo = Intrinsic::x86_sse2_psrl_d;
8283 break;
8284 case Intrinsic::x86_sse2_psrli_q:
8285 NewIntNo = Intrinsic::x86_sse2_psrl_q;
8286 break;
8287 case Intrinsic::x86_sse2_psrai_w:
8288 NewIntNo = Intrinsic::x86_sse2_psra_w;
8289 break;
8290 case Intrinsic::x86_sse2_psrai_d:
8291 NewIntNo = Intrinsic::x86_sse2_psra_d;
8292 break;
8293 default: {
Owen Anderson825b72b2009-08-11 20:47:22 +00008294 ShAmtVT = MVT::v2i32;
Evan Cheng5759f972008-05-04 09:15:50 +00008295 switch (IntNo) {
8296 case Intrinsic::x86_mmx_pslli_w:
8297 NewIntNo = Intrinsic::x86_mmx_psll_w;
8298 break;
8299 case Intrinsic::x86_mmx_pslli_d:
8300 NewIntNo = Intrinsic::x86_mmx_psll_d;
8301 break;
8302 case Intrinsic::x86_mmx_pslli_q:
8303 NewIntNo = Intrinsic::x86_mmx_psll_q;
8304 break;
8305 case Intrinsic::x86_mmx_psrli_w:
8306 NewIntNo = Intrinsic::x86_mmx_psrl_w;
8307 break;
8308 case Intrinsic::x86_mmx_psrli_d:
8309 NewIntNo = Intrinsic::x86_mmx_psrl_d;
8310 break;
8311 case Intrinsic::x86_mmx_psrli_q:
8312 NewIntNo = Intrinsic::x86_mmx_psrl_q;
8313 break;
8314 case Intrinsic::x86_mmx_psrai_w:
8315 NewIntNo = Intrinsic::x86_mmx_psra_w;
8316 break;
8317 case Intrinsic::x86_mmx_psrai_d:
8318 NewIntNo = Intrinsic::x86_mmx_psra_d;
8319 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00008320 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00008321 }
8322 break;
8323 }
8324 }
Mon P Wangefa42202009-09-03 19:56:25 +00008325
8326 // The vector shift intrinsics with scalars uses 32b shift amounts but
8327 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
8328 // to be zero.
8329 SDValue ShOps[4];
8330 ShOps[0] = ShAmt;
8331 ShOps[1] = DAG.getConstant(0, MVT::i32);
8332 if (ShAmtVT == MVT::v4i32) {
8333 ShOps[2] = DAG.getUNDEF(MVT::i32);
8334 ShOps[3] = DAG.getUNDEF(MVT::i32);
8335 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
8336 } else {
8337 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
Dale Johannesen0488fb62010-09-30 23:57:10 +00008338// FIXME this must be lowered to get rid of the invalid type.
Mon P Wangefa42202009-09-03 19:56:25 +00008339 }
8340
Owen Andersone50ed302009-08-10 22:56:29 +00008341 EVT VT = Op.getValueType();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008342 ShAmt = DAG.getNode(ISD::BITCAST, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008343 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008344 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00008345 Op.getOperand(1), ShAmt);
8346 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00008347 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008348}
Evan Cheng72261582005-12-20 06:22:03 +00008349
Dan Gohmand858e902010-04-17 15:26:15 +00008350SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
8351 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +00008352 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
8353 MFI->setReturnAddressIsTaken(true);
8354
Bill Wendling64e87322009-01-16 19:25:27 +00008355 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008356 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00008357
8358 if (Depth > 0) {
8359 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
8360 SDValue Offset =
8361 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00008362 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008363 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00008364 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008365 FrameAddr, Offset),
Chris Lattner51abfe42010-09-21 06:02:19 +00008366 MachinePointerInfo(), false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +00008367 }
8368
8369 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00008370 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00008371 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Chris Lattner51abfe42010-09-21 06:02:19 +00008372 RetAddrFI, MachinePointerInfo(), false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00008373}
8374
Dan Gohmand858e902010-04-17 15:26:15 +00008375SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +00008376 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
8377 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00008378
Owen Andersone50ed302009-08-10 22:56:29 +00008379 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008380 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00008381 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
8382 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00008383 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00008384 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +00008385 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
8386 MachinePointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +00008387 false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00008388 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00008389}
8390
Dan Gohman475871a2008-07-27 21:46:04 +00008391SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008392 SelectionDAG &DAG) const {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00008393 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008394}
8395
Dan Gohmand858e902010-04-17 15:26:15 +00008396SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008397 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00008398 SDValue Chain = Op.getOperand(0);
8399 SDValue Offset = Op.getOperand(1);
8400 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008401 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008402
Dan Gohmand8816272010-08-11 18:14:00 +00008403 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
8404 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
8405 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008406 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008407
Dan Gohmand8816272010-08-11 18:14:00 +00008408 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
8409 DAG.getIntPtrConstant(TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008410 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +00008411 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
8412 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00008413 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008414 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008415
Dale Johannesene4d209d2009-02-03 20:21:25 +00008416 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008417 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008418 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008419}
8420
Dan Gohman475871a2008-07-27 21:46:04 +00008421SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008422 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008423 SDValue Root = Op.getOperand(0);
8424 SDValue Trmp = Op.getOperand(1); // trampoline
8425 SDValue FPtr = Op.getOperand(2); // nested function
8426 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008427 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008428
Dan Gohman69de1932008-02-06 22:27:42 +00008429 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008430
8431 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00008432 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00008433
8434 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +00008435 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
8436 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +00008437
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00008438 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
8439 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00008440
8441 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
8442
8443 // Load the pointer to the nested function into R11.
8444 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00008445 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00008446 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008447 Addr, MachinePointerInfo(TrmpAddr),
8448 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008449
Owen Anderson825b72b2009-08-11 20:47:22 +00008450 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8451 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008452 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
8453 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +00008454 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00008455
8456 // Load the 'nest' parameter value into R10.
8457 // R10 is specified in X86CallingConv.td
8458 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +00008459 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8460 DAG.getConstant(10, MVT::i64));
8461 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008462 Addr, MachinePointerInfo(TrmpAddr, 10),
8463 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008464
Owen Anderson825b72b2009-08-11 20:47:22 +00008465 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8466 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008467 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
8468 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +00008469 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00008470
8471 // Jump to the nested function.
8472 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +00008473 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8474 DAG.getConstant(20, MVT::i64));
8475 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008476 Addr, MachinePointerInfo(TrmpAddr, 20),
8477 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008478
8479 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +00008480 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8481 DAG.getConstant(22, MVT::i64));
8482 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00008483 MachinePointerInfo(TrmpAddr, 22),
8484 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008485
Dan Gohman475871a2008-07-27 21:46:04 +00008486 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00008487 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008488 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008489 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00008490 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00008491 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00008492 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00008493 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008494
8495 switch (CC) {
8496 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00008497 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00008498 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00008499 case CallingConv::X86_StdCall: {
8500 // Pass 'nest' parameter in ECX.
8501 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00008502 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008503
8504 // Check that ECX wasn't needed by an 'inreg' parameter.
8505 const FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00008506 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008507
Chris Lattner58d74912008-03-12 17:45:29 +00008508 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00008509 unsigned InRegCount = 0;
8510 unsigned Idx = 1;
8511
8512 for (FunctionType::param_iterator I = FTy->param_begin(),
8513 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00008514 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00008515 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00008516 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008517
8518 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +00008519 report_fatal_error("Nest register in use - reduce number of inreg"
8520 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00008521 }
8522 }
8523 break;
8524 }
8525 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +00008526 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00008527 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00008528 // Pass 'nest' parameter in EAX.
8529 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00008530 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008531 break;
8532 }
8533
Dan Gohman475871a2008-07-27 21:46:04 +00008534 SDValue OutChains[4];
8535 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008536
Owen Anderson825b72b2009-08-11 20:47:22 +00008537 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8538 DAG.getConstant(10, MVT::i32));
8539 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008540
Chris Lattnera62fe662010-02-05 19:20:30 +00008541 // This is storing the opcode for MOV32ri.
8542 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00008543 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00008544 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008545 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008546 Trmp, MachinePointerInfo(TrmpAddr),
8547 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008548
Owen Anderson825b72b2009-08-11 20:47:22 +00008549 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8550 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008551 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
8552 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +00008553 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008554
Chris Lattnera62fe662010-02-05 19:20:30 +00008555 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +00008556 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8557 DAG.getConstant(5, MVT::i32));
8558 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00008559 MachinePointerInfo(TrmpAddr, 5),
8560 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008561
Owen Anderson825b72b2009-08-11 20:47:22 +00008562 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8563 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008564 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
8565 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +00008566 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008567
Dan Gohman475871a2008-07-27 21:46:04 +00008568 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00008569 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008570 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008571 }
8572}
8573
Dan Gohmand858e902010-04-17 15:26:15 +00008574SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
8575 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008576 /*
8577 The rounding mode is in bits 11:10 of FPSR, and has the following
8578 settings:
8579 00 Round to nearest
8580 01 Round to -inf
8581 10 Round to +inf
8582 11 Round to 0
8583
8584 FLT_ROUNDS, on the other hand, expects the following:
8585 -1 Undefined
8586 0 Round to 0
8587 1 Round to nearest
8588 2 Round to +inf
8589 3 Round to -inf
8590
8591 To perform the conversion, we do:
8592 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
8593 */
8594
8595 MachineFunction &MF = DAG.getMachineFunction();
8596 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00008597 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008598 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00008599 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +00008600 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008601
8602 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00008603 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +00008604 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008605
Michael J. Spencerec38de22010-10-10 22:04:20 +00008606
Chris Lattner2156b792010-09-22 01:11:26 +00008607 MachineMemOperand *MMO =
8608 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8609 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008610
Chris Lattner2156b792010-09-22 01:11:26 +00008611 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
8612 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
8613 DAG.getVTList(MVT::Other),
8614 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008615
8616 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +00008617 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Chris Lattner51abfe42010-09-21 06:02:19 +00008618 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008619
8620 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00008621 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +00008622 DAG.getNode(ISD::SRL, DL, MVT::i16,
8623 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00008624 CWD, DAG.getConstant(0x800, MVT::i16)),
8625 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00008626 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +00008627 DAG.getNode(ISD::SRL, DL, MVT::i16,
8628 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00008629 CWD, DAG.getConstant(0x400, MVT::i16)),
8630 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008631
Dan Gohman475871a2008-07-27 21:46:04 +00008632 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +00008633 DAG.getNode(ISD::AND, DL, MVT::i16,
8634 DAG.getNode(ISD::ADD, DL, MVT::i16,
8635 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +00008636 DAG.getConstant(1, MVT::i16)),
8637 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008638
8639
Duncan Sands83ec4b62008-06-06 12:08:01 +00008640 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +00008641 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008642}
8643
Dan Gohmand858e902010-04-17 15:26:15 +00008644SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008645 EVT VT = Op.getValueType();
8646 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008647 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008648 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00008649
8650 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008651 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00008652 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +00008653 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00008654 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008655 }
Evan Cheng18efe262007-12-14 02:13:44 +00008656
Evan Cheng152804e2007-12-14 08:30:15 +00008657 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008658 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008659 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00008660
8661 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00008662 SDValue Ops[] = {
8663 Op,
8664 DAG.getConstant(NumBits+NumBits-1, OpVT),
8665 DAG.getConstant(X86::COND_E, MVT::i8),
8666 Op.getValue(1)
8667 };
8668 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00008669
8670 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00008671 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00008672
Owen Anderson825b72b2009-08-11 20:47:22 +00008673 if (VT == MVT::i8)
8674 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008675 return Op;
8676}
8677
Dan Gohmand858e902010-04-17 15:26:15 +00008678SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008679 EVT VT = Op.getValueType();
8680 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008681 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008682 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00008683
8684 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008685 if (VT == MVT::i8) {
8686 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00008687 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008688 }
Evan Cheng152804e2007-12-14 08:30:15 +00008689
8690 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008691 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008692 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00008693
8694 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00008695 SDValue Ops[] = {
8696 Op,
8697 DAG.getConstant(NumBits, OpVT),
8698 DAG.getConstant(X86::COND_E, MVT::i8),
8699 Op.getValue(1)
8700 };
8701 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00008702
Owen Anderson825b72b2009-08-11 20:47:22 +00008703 if (VT == MVT::i8)
8704 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008705 return Op;
8706}
8707
Dan Gohmand858e902010-04-17 15:26:15 +00008708SDValue X86TargetLowering::LowerMUL_V2I64(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008709 EVT VT = Op.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00008710 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008711 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00008712
Mon P Wangaf9b9522008-12-18 21:42:19 +00008713 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
8714 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
8715 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
8716 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
8717 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
8718 //
8719 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
8720 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
8721 // return AloBlo + AloBhi + AhiBlo;
8722
8723 SDValue A = Op.getOperand(0);
8724 SDValue B = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008725
Dale Johannesene4d209d2009-02-03 20:21:25 +00008726 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008727 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8728 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008729 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008730 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8731 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008732 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008733 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008734 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008735 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008736 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008737 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008738 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008739 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008740 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008741 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008742 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8743 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008744 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008745 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8746 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008747 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
8748 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +00008749 return Res;
8750}
8751
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008752SDValue X86TargetLowering::LowerSHL(SDValue Op, SelectionDAG &DAG) const {
8753 EVT VT = Op.getValueType();
8754 DebugLoc dl = Op.getDebugLoc();
8755 SDValue R = Op.getOperand(0);
8756
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008757 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008758
Nate Begeman51409212010-07-28 00:21:48 +00008759 assert(Subtarget->hasSSE41() && "Cannot lower SHL without SSE4.1 or later");
8760
8761 if (VT == MVT::v4i32) {
8762 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8763 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
8764 Op.getOperand(1), DAG.getConstant(23, MVT::i32));
8765
8766 ConstantInt *CI = ConstantInt::get(*Context, APInt(32, 0x3f800000U));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008767
Nate Begeman51409212010-07-28 00:21:48 +00008768 std::vector<Constant*> CV(4, CI);
8769 Constant *C = ConstantVector::get(CV);
8770 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8771 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008772 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00008773 false, false, 16);
8774
8775 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008776 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +00008777 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
8778 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
8779 }
8780 if (VT == MVT::v16i8) {
8781 // a = a << 5;
8782 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8783 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
8784 Op.getOperand(1), DAG.getConstant(5, MVT::i32));
8785
8786 ConstantInt *CM1 = ConstantInt::get(*Context, APInt(8, 15));
8787 ConstantInt *CM2 = ConstantInt::get(*Context, APInt(8, 63));
8788
8789 std::vector<Constant*> CVM1(16, CM1);
8790 std::vector<Constant*> CVM2(16, CM2);
8791 Constant *C = ConstantVector::get(CVM1);
8792 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8793 SDValue M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008794 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00008795 false, false, 16);
8796
8797 // r = pblendv(r, psllw(r & (char16)15, 4), a);
8798 M = DAG.getNode(ISD::AND, dl, VT, R, M);
8799 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8800 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
8801 DAG.getConstant(4, MVT::i32));
Nate Begeman672fb622010-12-20 22:04:24 +00008802 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT, R, M, Op);
Nate Begeman51409212010-07-28 00:21:48 +00008803 // a += a
8804 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008805
Nate Begeman51409212010-07-28 00:21:48 +00008806 C = ConstantVector::get(CVM2);
8807 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8808 M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008809 MachinePointerInfo::getConstantPool(),
Chris Lattner51abfe42010-09-21 06:02:19 +00008810 false, false, 16);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008811
Nate Begeman51409212010-07-28 00:21:48 +00008812 // r = pblendv(r, psllw(r & (char16)63, 2), a);
8813 M = DAG.getNode(ISD::AND, dl, VT, R, M);
8814 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8815 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
8816 DAG.getConstant(2, MVT::i32));
Nate Begeman672fb622010-12-20 22:04:24 +00008817 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT, R, M, Op);
Nate Begeman51409212010-07-28 00:21:48 +00008818 // a += a
8819 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008820
Nate Begeman51409212010-07-28 00:21:48 +00008821 // return pblendv(r, r+r, a);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008822 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT,
Nate Begeman51409212010-07-28 00:21:48 +00008823 R, DAG.getNode(ISD::ADD, dl, VT, R, R), Op);
8824 return R;
8825 }
8826 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008827}
Mon P Wangaf9b9522008-12-18 21:42:19 +00008828
Dan Gohmand858e902010-04-17 15:26:15 +00008829SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
Bill Wendling74c37652008-12-09 22:08:41 +00008830 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
8831 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +00008832 // looks for this combo and may remove the "setcc" instruction if the "setcc"
8833 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +00008834 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +00008835 SDValue LHS = N->getOperand(0);
8836 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +00008837 unsigned BaseOp = 0;
8838 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008839 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +00008840 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008841 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +00008842 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +00008843 // A subtract of one will be selected as a INC. Note that INC doesn't
8844 // set CF, so we can't do this for UADDO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +00008845 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
8846 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +00008847 BaseOp = X86ISD::INC;
8848 Cond = X86::COND_O;
8849 break;
8850 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008851 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +00008852 Cond = X86::COND_O;
8853 break;
8854 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008855 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +00008856 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00008857 break;
8858 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +00008859 // A subtract of one will be selected as a DEC. Note that DEC doesn't
8860 // set CF, so we can't do this for USUBO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +00008861 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
8862 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +00008863 BaseOp = X86ISD::DEC;
8864 Cond = X86::COND_O;
8865 break;
8866 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008867 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +00008868 Cond = X86::COND_O;
8869 break;
8870 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008871 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +00008872 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00008873 break;
8874 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00008875 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +00008876 Cond = X86::COND_O;
8877 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008878 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
8879 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
8880 MVT::i32);
8881 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008882
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008883 SDValue SetCC =
8884 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
8885 DAG.getConstant(X86::COND_O, MVT::i32),
8886 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008887
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008888 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
8889 return Sum;
8890 }
Bill Wendling74c37652008-12-09 22:08:41 +00008891 }
Bill Wendling3fafd932008-11-26 22:37:40 +00008892
Bill Wendling61edeb52008-12-02 01:06:39 +00008893 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008894 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008895 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +00008896
Bill Wendling61edeb52008-12-02 01:06:39 +00008897 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008898 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
8899 DAG.getConstant(Cond, MVT::i32),
8900 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +00008901
Bill Wendling61edeb52008-12-02 01:06:39 +00008902 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
8903 return Sum;
Bill Wendling41ea7e72008-11-24 19:21:46 +00008904}
8905
Eric Christopher9a9d2752010-07-22 02:48:34 +00008906SDValue X86TargetLowering::LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG) const{
8907 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00008908
Eric Christopherb6729dc2010-08-04 23:03:04 +00008909 if (!Subtarget->hasSSE2()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +00008910 SDValue Chain = Op.getOperand(0);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008911 SDValue Zero = DAG.getConstant(0,
Eric Christopherb6729dc2010-08-04 23:03:04 +00008912 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +00008913 SDValue Ops[] = {
8914 DAG.getRegister(X86::ESP, MVT::i32), // Base
8915 DAG.getTargetConstant(1, MVT::i8), // Scale
8916 DAG.getRegister(0, MVT::i32), // Index
8917 DAG.getTargetConstant(0, MVT::i32), // Disp
8918 DAG.getRegister(0, MVT::i32), // Segment.
8919 Zero,
8920 Chain
8921 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00008922 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +00008923 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
8924 array_lengthof(Ops));
8925 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +00008926 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00008927
Eric Christopher9a9d2752010-07-22 02:48:34 +00008928 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +00008929 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +00008930 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008931
Chris Lattner132929a2010-08-14 17:26:09 +00008932 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
8933 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
8934 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
8935 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +00008936
Chris Lattner132929a2010-08-14 17:26:09 +00008937 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
8938 if (!Op1 && !Op2 && !Op3 && Op4)
8939 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008940
Chris Lattner132929a2010-08-14 17:26:09 +00008941 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
8942 if (Op1 && !Op2 && !Op3 && !Op4)
8943 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008944
8945 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +00008946 // (MFENCE)>;
8947 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +00008948}
8949
Dan Gohmand858e902010-04-17 15:26:15 +00008950SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008951 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008952 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +00008953 unsigned Reg = 0;
8954 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00008955 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008956 default:
8957 assert(false && "Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +00008958 case MVT::i8: Reg = X86::AL; size = 1; break;
8959 case MVT::i16: Reg = X86::AX; size = 2; break;
8960 case MVT::i32: Reg = X86::EAX; size = 4; break;
8961 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +00008962 assert(Subtarget->is64Bit() && "Node not type legal!");
8963 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00008964 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00008965 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008966 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +00008967 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +00008968 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +00008969 Op.getOperand(1),
8970 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +00008971 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +00008972 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008973 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008974 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
8975 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
8976 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +00008977 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008978 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +00008979 return cpOut;
8980}
8981
Duncan Sands1607f052008-12-01 11:39:25 +00008982SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008983 SelectionDAG &DAG) const {
Duncan Sands1607f052008-12-01 11:39:25 +00008984 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008985 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +00008986 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008987 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +00008988 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00008989 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
8990 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +00008991 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +00008992 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
8993 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +00008994 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +00008995 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +00008996 rdx.getValue(1)
8997 };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008998 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008999}
9000
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009001SDValue X86TargetLowering::LowerBITCAST(SDValue Op,
Dale Johannesen7d07b482010-05-21 00:52:33 +00009002 SelectionDAG &DAG) const {
9003 EVT SrcVT = Op.getOperand(0).getValueType();
9004 EVT DstVT = Op.getValueType();
Chris Lattner2a786eb2010-12-19 20:19:20 +00009005 assert(Subtarget->is64Bit() && !Subtarget->hasSSE2() &&
9006 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +00009007 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +00009008 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009009 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +00009010 // i64 <=> MMX conversions are Legal.
9011 if (SrcVT==MVT::i64 && DstVT.isVector())
9012 return Op;
9013 if (DstVT==MVT::i64 && SrcVT.isVector())
9014 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +00009015 // MMX <=> MMX conversions are Legal.
9016 if (SrcVT.isVector() && DstVT.isVector())
9017 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +00009018 // All other conversions need to be expanded.
9019 return SDValue();
9020}
Chris Lattner5b856542010-12-20 00:59:46 +00009021
Dan Gohmand858e902010-04-17 15:26:15 +00009022SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen71d1bf52008-09-29 22:25:26 +00009023 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +00009024 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00009025 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009026 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +00009027 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009028 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009029 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +00009030 Node->getOperand(0),
9031 Node->getOperand(1), negOp,
9032 cast<AtomicSDNode>(Node)->getSrcValue(),
9033 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang63307c32008-05-05 19:05:59 +00009034}
9035
Chris Lattner5b856542010-12-20 00:59:46 +00009036static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
9037 EVT VT = Op.getNode()->getValueType(0);
9038
9039 // Let legalize expand this if it isn't a legal type yet.
9040 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
9041 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009042
Chris Lattner5b856542010-12-20 00:59:46 +00009043 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009044
Chris Lattner5b856542010-12-20 00:59:46 +00009045 unsigned Opc;
9046 bool ExtraOp = false;
9047 switch (Op.getOpcode()) {
9048 default: assert(0 && "Invalid code");
9049 case ISD::ADDC: Opc = X86ISD::ADD; break;
9050 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
9051 case ISD::SUBC: Opc = X86ISD::SUB; break;
9052 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
9053 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009054
Chris Lattner5b856542010-12-20 00:59:46 +00009055 if (!ExtraOp)
9056 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
9057 Op.getOperand(1));
9058 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
9059 Op.getOperand(1), Op.getOperand(2));
9060}
9061
Evan Cheng0db9fe62006-04-25 20:13:52 +00009062/// LowerOperation - Provide custom lowering hooks for some operations.
9063///
Dan Gohmand858e902010-04-17 15:26:15 +00009064SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00009065 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00009066 default: llvm_unreachable("Should not custom lower this!");
Eric Christopher9a9d2752010-07-22 02:48:34 +00009067 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op,DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009068 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
9069 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009070 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00009071 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009072 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
9073 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
9074 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
David Greene91585092011-01-26 15:38:49 +00009075 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
David Greenecfe33c42011-01-26 19:13:22 +00009076 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009077 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
9078 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
9079 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00009080 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +00009081 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +00009082 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009083 case ISD::SHL_PARTS:
9084 case ISD::SRA_PARTS:
9085 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
9086 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00009087 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009088 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00009089 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009090 case ISD::FABS: return LowerFABS(Op, DAG);
9091 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00009092 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00009093 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman30a0de92008-07-17 16:51:19 +00009094 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00009095 case ISD::SELECT: return LowerSELECT(Op, DAG);
9096 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009097 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009098 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +00009099 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +00009100 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009101 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00009102 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
9103 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009104 case ISD::FRAME_TO_ARGS_OFFSET:
9105 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009106 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009107 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009108 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00009109 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +00009110 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
9111 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Mon P Wangaf9b9522008-12-18 21:42:19 +00009112 case ISD::MUL: return LowerMUL_V2I64(Op, DAG);
Nate Begemanbdcb5af2010-07-27 22:37:06 +00009113 case ISD::SHL: return LowerSHL(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +00009114 case ISD::SADDO:
9115 case ISD::UADDO:
9116 case ISD::SSUBO:
9117 case ISD::USUBO:
9118 case ISD::SMULO:
9119 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00009120 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009121 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +00009122 case ISD::ADDC:
9123 case ISD::ADDE:
9124 case ISD::SUBC:
9125 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009126 }
Chris Lattner27a6c732007-11-24 07:07:01 +00009127}
9128
Duncan Sands1607f052008-12-01 11:39:25 +00009129void X86TargetLowering::
9130ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00009131 SelectionDAG &DAG, unsigned NewOp) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009132 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009133 DebugLoc dl = Node->getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00009134 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +00009135
9136 SDValue Chain = Node->getOperand(0);
9137 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00009138 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00009139 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00009140 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00009141 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +00009142 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +00009143 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +00009144 SDValue Result =
9145 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
9146 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +00009147 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +00009148 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00009149 Results.push_back(Result.getValue(2));
9150}
9151
Duncan Sands126d9072008-07-04 11:47:58 +00009152/// ReplaceNodeResults - Replace a node with an illegal result type
9153/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00009154void X86TargetLowering::ReplaceNodeResults(SDNode *N,
9155 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00009156 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +00009157 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +00009158 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +00009159 default:
Duncan Sands1607f052008-12-01 11:39:25 +00009160 assert(false && "Do not know how to custom type legalize this operation!");
9161 return;
Chris Lattner5b856542010-12-20 00:59:46 +00009162 case ISD::ADDC:
9163 case ISD::ADDE:
9164 case ISD::SUBC:
9165 case ISD::SUBE:
9166 // We don't want to expand or promote these.
9167 return;
Duncan Sands1607f052008-12-01 11:39:25 +00009168 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +00009169 std::pair<SDValue,SDValue> Vals =
9170 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +00009171 SDValue FIST = Vals.first, StackSlot = Vals.second;
9172 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +00009173 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +00009174 // Return a load from the stack slot.
Chris Lattner51abfe42010-09-21 06:02:19 +00009175 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
9176 MachinePointerInfo(), false, false, 0));
Duncan Sands1607f052008-12-01 11:39:25 +00009177 }
9178 return;
9179 }
9180 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009181 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +00009182 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009183 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00009184 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +00009185 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00009186 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00009187 eax.getValue(2));
9188 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
9189 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +00009190 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00009191 Results.push_back(edx.getValue(1));
9192 return;
9193 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009194 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +00009195 EVT T = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009196 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Duncan Sands1607f052008-12-01 11:39:25 +00009197 SDValue cpInL, cpInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00009198 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
9199 DAG.getConstant(0, MVT::i32));
9200 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
9201 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00009202 cpInL = DAG.getCopyToReg(N->getOperand(0), dl, X86::EAX, cpInL, SDValue());
9203 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl, X86::EDX, cpInH,
Duncan Sands1607f052008-12-01 11:39:25 +00009204 cpInL.getValue(1));
9205 SDValue swapInL, swapInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00009206 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
9207 DAG.getConstant(0, MVT::i32));
9208 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
9209 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00009210 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl, X86::EBX, swapInL,
Duncan Sands1607f052008-12-01 11:39:25 +00009211 cpInH.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00009212 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl, X86::ECX, swapInH,
Duncan Sands1607f052008-12-01 11:39:25 +00009213 swapInL.getValue(1));
9214 SDValue Ops[] = { swapInH.getValue(0),
9215 N->getOperand(1),
9216 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009217 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +00009218 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
9219 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG8_DAG, dl, Tys,
9220 Ops, 3, T, MMO);
Dale Johannesendd64c412009-02-04 00:33:20 +00009221 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl, X86::EAX,
Owen Anderson825b72b2009-08-11 20:47:22 +00009222 MVT::i32, Result.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00009223 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl, X86::EDX,
Owen Anderson825b72b2009-08-11 20:47:22 +00009224 MVT::i32, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +00009225 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Owen Anderson825b72b2009-08-11 20:47:22 +00009226 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00009227 Results.push_back(cpOutH.getValue(1));
9228 return;
9229 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009230 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +00009231 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
9232 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009233 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +00009234 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
9235 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009236 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +00009237 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
9238 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009239 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +00009240 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
9241 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009242 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +00009243 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
9244 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009245 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +00009246 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
9247 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009248 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +00009249 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
9250 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00009251 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00009252}
9253
Evan Cheng72261582005-12-20 06:22:03 +00009254const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
9255 switch (Opcode) {
9256 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +00009257 case X86ISD::BSF: return "X86ISD::BSF";
9258 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +00009259 case X86ISD::SHLD: return "X86ISD::SHLD";
9260 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00009261 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00009262 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00009263 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00009264 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00009265 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00009266 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00009267 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
9268 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
9269 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00009270 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00009271 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +00009272 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +00009273 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +00009274 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +00009275 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00009276 case X86ISD::COMI: return "X86ISD::COMI";
9277 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00009278 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +00009279 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Evan Cheng72261582005-12-20 06:22:03 +00009280 case X86ISD::CMOV: return "X86ISD::CMOV";
9281 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00009282 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00009283 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
9284 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +00009285 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00009286 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +00009287 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +00009288 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +00009289 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +00009290 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
9291 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +00009292 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +00009293 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Nate Begemanb65c1752010-12-17 22:55:37 +00009294 case X86ISD::PANDN: return "X86ISD::PANDN";
9295 case X86ISD::PSIGNB: return "X86ISD::PSIGNB";
9296 case X86ISD::PSIGNW: return "X86ISD::PSIGNW";
9297 case X86ISD::PSIGND: return "X86ISD::PSIGND";
Nate Begeman672fb622010-12-20 22:04:24 +00009298 case X86ISD::PBLENDVB: return "X86ISD::PBLENDVB";
Evan Cheng8ca29322006-11-10 21:43:37 +00009299 case X86ISD::FMAX: return "X86ISD::FMAX";
9300 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +00009301 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
9302 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00009303 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +00009304 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009305 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00009306 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009307 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +00009308 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
9309 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009310 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
9311 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
9312 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
9313 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
9314 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
9315 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +00009316 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
9317 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +00009318 case X86ISD::VSHL: return "X86ISD::VSHL";
9319 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +00009320 case X86ISD::CMPPD: return "X86ISD::CMPPD";
9321 case X86ISD::CMPPS: return "X86ISD::CMPPS";
9322 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
9323 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
9324 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
9325 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
9326 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
9327 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
9328 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
9329 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +00009330 case X86ISD::ADD: return "X86ISD::ADD";
9331 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +00009332 case X86ISD::ADC: return "X86ISD::ADC";
9333 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +00009334 case X86ISD::SMUL: return "X86ISD::SMUL";
9335 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +00009336 case X86ISD::INC: return "X86ISD::INC";
9337 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +00009338 case X86ISD::OR: return "X86ISD::OR";
9339 case X86ISD::XOR: return "X86ISD::XOR";
9340 case X86ISD::AND: return "X86ISD::AND";
Evan Cheng73f24c92009-03-30 21:36:47 +00009341 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +00009342 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009343 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009344 case X86ISD::PALIGN: return "X86ISD::PALIGN";
9345 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
9346 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
9347 case X86ISD::PSHUFHW_LD: return "X86ISD::PSHUFHW_LD";
9348 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
9349 case X86ISD::PSHUFLW_LD: return "X86ISD::PSHUFLW_LD";
9350 case X86ISD::SHUFPS: return "X86ISD::SHUFPS";
9351 case X86ISD::SHUFPD: return "X86ISD::SHUFPD";
9352 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009353 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00009354 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009355 case X86ISD::MOVHLPD: return "X86ISD::MOVHLPD";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00009356 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
9357 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009358 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
9359 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
9360 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
9361 case X86ISD::MOVSHDUP_LD: return "X86ISD::MOVSHDUP_LD";
9362 case X86ISD::MOVSLDUP_LD: return "X86ISD::MOVSLDUP_LD";
9363 case X86ISD::MOVSD: return "X86ISD::MOVSD";
9364 case X86ISD::MOVSS: return "X86ISD::MOVSS";
9365 case X86ISD::UNPCKLPS: return "X86ISD::UNPCKLPS";
9366 case X86ISD::UNPCKLPD: return "X86ISD::UNPCKLPD";
David Greenefbf05d32011-02-22 23:31:46 +00009367 case X86ISD::VUNPCKLPS: return "X86ISD::VUNPCKLPS";
9368 case X86ISD::VUNPCKLPD: return "X86ISD::VUNPCKLPD";
9369 case X86ISD::VUNPCKLPSY: return "X86ISD::VUNPCKLPSY";
9370 case X86ISD::VUNPCKLPDY: return "X86ISD::VUNPCKLPDY";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009371 case X86ISD::UNPCKHPS: return "X86ISD::UNPCKHPS";
9372 case X86ISD::UNPCKHPD: return "X86ISD::UNPCKHPD";
9373 case X86ISD::PUNPCKLBW: return "X86ISD::PUNPCKLBW";
9374 case X86ISD::PUNPCKLWD: return "X86ISD::PUNPCKLWD";
9375 case X86ISD::PUNPCKLDQ: return "X86ISD::PUNPCKLDQ";
9376 case X86ISD::PUNPCKLQDQ: return "X86ISD::PUNPCKLQDQ";
9377 case X86ISD::PUNPCKHBW: return "X86ISD::PUNPCKHBW";
9378 case X86ISD::PUNPCKHWD: return "X86ISD::PUNPCKHWD";
9379 case X86ISD::PUNPCKHDQ: return "X86ISD::PUNPCKHDQ";
9380 case X86ISD::PUNPCKHQDQ: return "X86ISD::PUNPCKHQDQ";
Dan Gohmand6708ea2009-08-15 01:38:56 +00009381 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +00009382 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +00009383 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Evan Cheng72261582005-12-20 06:22:03 +00009384 }
9385}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00009386
Chris Lattnerc9addb72007-03-30 23:15:24 +00009387// isLegalAddressingMode - Return true if the addressing mode represented
9388// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +00009389bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerc9addb72007-03-30 23:15:24 +00009390 const Type *Ty) const {
9391 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009392 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +00009393 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +00009394
Chris Lattnerc9addb72007-03-30 23:15:24 +00009395 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009396 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +00009397 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00009398
Chris Lattnerc9addb72007-03-30 23:15:24 +00009399 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +00009400 unsigned GVFlags =
9401 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009402
Chris Lattnerdfed4132009-07-10 07:38:24 +00009403 // If a reference to this global requires an extra load, we can't fold it.
9404 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +00009405 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009406
Chris Lattnerdfed4132009-07-10 07:38:24 +00009407 // If BaseGV requires a register for the PIC base, we cannot also have a
9408 // BaseReg specified.
9409 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +00009410 return false;
Evan Cheng52787842007-08-01 23:46:47 +00009411
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009412 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +00009413 if ((M != CodeModel::Small || R != Reloc::Static) &&
9414 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009415 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +00009416 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009417
Chris Lattnerc9addb72007-03-30 23:15:24 +00009418 switch (AM.Scale) {
9419 case 0:
9420 case 1:
9421 case 2:
9422 case 4:
9423 case 8:
9424 // These scales always work.
9425 break;
9426 case 3:
9427 case 5:
9428 case 9:
9429 // These scales are formed with basereg+scalereg. Only accept if there is
9430 // no basereg yet.
9431 if (AM.HasBaseReg)
9432 return false;
9433 break;
9434 default: // Other stuff never works.
9435 return false;
9436 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009437
Chris Lattnerc9addb72007-03-30 23:15:24 +00009438 return true;
9439}
9440
9441
Evan Cheng2bd122c2007-10-26 01:56:11 +00009442bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009443 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +00009444 return false;
Evan Chenge127a732007-10-29 07:57:50 +00009445 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
9446 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00009447 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +00009448 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00009449 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +00009450}
9451
Owen Andersone50ed302009-08-10 22:56:29 +00009452bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +00009453 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009454 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009455 unsigned NumBits1 = VT1.getSizeInBits();
9456 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00009457 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009458 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00009459 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009460}
Evan Cheng2bd122c2007-10-26 01:56:11 +00009461
Dan Gohman97121ba2009-04-08 00:15:30 +00009462bool X86TargetLowering::isZExtFree(const Type *Ty1, const Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00009463 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009464 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00009465}
9466
Owen Andersone50ed302009-08-10 22:56:29 +00009467bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00009468 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00009469 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00009470}
9471
Owen Andersone50ed302009-08-10 22:56:29 +00009472bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +00009473 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +00009474 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +00009475}
9476
Evan Cheng60c07e12006-07-05 22:17:51 +00009477/// isShuffleMaskLegal - Targets can use this to indicate that they only
9478/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
9479/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
9480/// are assumed to be legal.
9481bool
Eric Christopherfd179292009-08-27 18:07:15 +00009482X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +00009483 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +00009484 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +00009485 if (VT.getSizeInBits() == 64)
Eric Christophercff6f852010-04-15 01:40:20 +00009486 return isPALIGNRMask(M, VT, Subtarget->hasSSSE3());
Nate Begeman9008ca62009-04-27 18:41:29 +00009487
Nate Begemana09008b2009-10-19 02:17:23 +00009488 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +00009489 return (VT.getVectorNumElements() == 2 ||
9490 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
9491 isMOVLMask(M, VT) ||
9492 isSHUFPMask(M, VT) ||
9493 isPSHUFDMask(M, VT) ||
9494 isPSHUFHWMask(M, VT) ||
9495 isPSHUFLWMask(M, VT) ||
Nate Begemana09008b2009-10-19 02:17:23 +00009496 isPALIGNRMask(M, VT, Subtarget->hasSSSE3()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +00009497 isUNPCKLMask(M, VT) ||
9498 isUNPCKHMask(M, VT) ||
9499 isUNPCKL_v_undef_Mask(M, VT) ||
9500 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00009501}
9502
Dan Gohman7d8143f2008-04-09 20:09:42 +00009503bool
Nate Begeman5a5ca152009-04-29 05:20:52 +00009504X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +00009505 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00009506 unsigned NumElts = VT.getVectorNumElements();
9507 // FIXME: This collection of masks seems suspect.
9508 if (NumElts == 2)
9509 return true;
9510 if (NumElts == 4 && VT.getSizeInBits() == 128) {
9511 return (isMOVLMask(Mask, VT) ||
9512 isCommutedMOVLMask(Mask, VT, true) ||
9513 isSHUFPMask(Mask, VT) ||
9514 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00009515 }
9516 return false;
9517}
9518
9519//===----------------------------------------------------------------------===//
9520// X86 Scheduler Hooks
9521//===----------------------------------------------------------------------===//
9522
Mon P Wang63307c32008-05-05 19:05:59 +00009523// private utility function
9524MachineBasicBlock *
9525X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
9526 MachineBasicBlock *MBB,
9527 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009528 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009529 unsigned LoadOpc,
9530 unsigned CXchgOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009531 unsigned notOpc,
9532 unsigned EAXreg,
9533 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009534 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00009535 // For the atomic bitwise operator, we generate
9536 // thisMBB:
9537 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00009538 // ld t1 = [bitinstr.addr]
9539 // op t2 = t1, [bitinstr.val]
9540 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00009541 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
9542 // bz newMBB
9543 // fallthrough -->nextMBB
9544 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9545 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009546 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00009547 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009548
Mon P Wang63307c32008-05-05 19:05:59 +00009549 /// First build the CFG
9550 MachineFunction *F = MBB->getParent();
9551 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009552 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9553 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9554 F->insert(MBBIter, newMBB);
9555 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009556
Dan Gohman14152b42010-07-06 20:24:04 +00009557 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9558 nextMBB->splice(nextMBB->begin(), thisMBB,
9559 llvm::next(MachineBasicBlock::iterator(bInstr)),
9560 thisMBB->end());
9561 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009562
Mon P Wang63307c32008-05-05 19:05:59 +00009563 // Update thisMBB to fall through to newMBB
9564 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009565
Mon P Wang63307c32008-05-05 19:05:59 +00009566 // newMBB jumps to itself and fall through to nextMBB
9567 newMBB->addSuccessor(nextMBB);
9568 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009569
Mon P Wang63307c32008-05-05 19:05:59 +00009570 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009571 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009572 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +00009573 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00009574 MachineOperand& destOper = bInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009575 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00009576 int numArgs = bInstr->getNumOperands() - 1;
9577 for (int i=0; i < numArgs; ++i)
9578 argOpers[i] = &bInstr->getOperand(i+1);
9579
9580 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009581 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009582 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00009583
Dale Johannesen140be2d2008-08-19 18:47:28 +00009584 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009585 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00009586 for (int i=0; i <= lastAddrIndx; ++i)
9587 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009588
Dale Johannesen140be2d2008-08-19 18:47:28 +00009589 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009590 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00009591 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009592 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009593 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009594 tt = t1;
9595
Dale Johannesen140be2d2008-08-19 18:47:28 +00009596 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +00009597 assert((argOpers[valArgIndx]->isReg() ||
9598 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00009599 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +00009600 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009601 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00009602 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009603 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009604 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +00009605 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009606
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009607 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +00009608 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009609
Dale Johannesene4d209d2009-02-03 20:21:25 +00009610 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +00009611 for (int i=0; i <= lastAddrIndx; ++i)
9612 (*MIB).addOperand(*argOpers[i]);
9613 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +00009614 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009615 (*MIB).setMemRefs(bInstr->memoperands_begin(),
9616 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +00009617
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009618 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +00009619 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +00009620
Mon P Wang63307c32008-05-05 19:05:59 +00009621 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009622 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00009623
Dan Gohman14152b42010-07-06 20:24:04 +00009624 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00009625 return nextMBB;
9626}
9627
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00009628// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +00009629MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009630X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
9631 MachineBasicBlock *MBB,
9632 unsigned regOpcL,
9633 unsigned regOpcH,
9634 unsigned immOpcL,
9635 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009636 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009637 // For the atomic bitwise operator, we generate
9638 // thisMBB (instructions are in pairs, except cmpxchg8b)
9639 // ld t1,t2 = [bitinstr.addr]
9640 // newMBB:
9641 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
9642 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +00009643 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009644 // mov ECX, EBX <- t5, t6
9645 // mov EAX, EDX <- t1, t2
9646 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
9647 // mov t3, t4 <- EAX, EDX
9648 // bz newMBB
9649 // result in out1, out2
9650 // fallthrough -->nextMBB
9651
9652 const TargetRegisterClass *RC = X86::GR32RegisterClass;
9653 const unsigned LoadOpc = X86::MOV32rm;
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009654 const unsigned NotOpc = X86::NOT32r;
9655 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9656 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
9657 MachineFunction::iterator MBBIter = MBB;
9658 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009659
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009660 /// First build the CFG
9661 MachineFunction *F = MBB->getParent();
9662 MachineBasicBlock *thisMBB = MBB;
9663 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9664 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9665 F->insert(MBBIter, newMBB);
9666 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009667
Dan Gohman14152b42010-07-06 20:24:04 +00009668 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9669 nextMBB->splice(nextMBB->begin(), thisMBB,
9670 llvm::next(MachineBasicBlock::iterator(bInstr)),
9671 thisMBB->end());
9672 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009673
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009674 // Update thisMBB to fall through to newMBB
9675 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009676
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009677 // newMBB jumps to itself and fall through to nextMBB
9678 newMBB->addSuccessor(nextMBB);
9679 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009680
Dale Johannesene4d209d2009-02-03 20:21:25 +00009681 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009682 // Insert instructions into newMBB based on incoming instruction
9683 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009684 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009685 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009686 MachineOperand& dest1Oper = bInstr->getOperand(0);
9687 MachineOperand& dest2Oper = bInstr->getOperand(1);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009688 MachineOperand* argOpers[2 + X86::AddrNumOperands];
9689 for (int i=0; i < 2 + X86::AddrNumOperands; ++i) {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009690 argOpers[i] = &bInstr->getOperand(i+2);
9691
Dan Gohman71ea4e52010-05-14 21:01:44 +00009692 // We use some of the operands multiple times, so conservatively just
9693 // clear any kill flags that might be present.
9694 if (argOpers[i]->isReg() && argOpers[i]->isUse())
9695 argOpers[i]->setIsKill(false);
9696 }
9697
Evan Chengad5b52f2010-01-08 19:14:57 +00009698 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009699 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +00009700
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009701 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009702 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009703 for (int i=0; i <= lastAddrIndx; ++i)
9704 (*MIB).addOperand(*argOpers[i]);
9705 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009706 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +00009707 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +00009708 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009709 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +00009710 MachineOperand newOp3 = *(argOpers[3]);
9711 if (newOp3.isImm())
9712 newOp3.setImm(newOp3.getImm()+4);
9713 else
9714 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009715 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +00009716 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009717
9718 // t3/4 are defined later, at the bottom of the loop
9719 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
9720 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009721 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009722 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009723 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009724 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
9725
Evan Cheng306b4ca2010-01-08 23:41:50 +00009726 // The subsequent operations should be using the destination registers of
9727 //the PHI instructions.
Scott Michelfdc40a02009-02-17 22:15:04 +00009728 if (invSrc) {
Evan Cheng306b4ca2010-01-08 23:41:50 +00009729 t1 = F->getRegInfo().createVirtualRegister(RC);
9730 t2 = F->getRegInfo().createVirtualRegister(RC);
9731 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
9732 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009733 } else {
Evan Cheng306b4ca2010-01-08 23:41:50 +00009734 t1 = dest1Oper.getReg();
9735 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009736 }
9737
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009738 int valArgIndx = lastAddrIndx + 1;
9739 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +00009740 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009741 "invalid operand");
9742 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
9743 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009744 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009745 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009746 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009747 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +00009748 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00009749 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009750 (*MIB).addOperand(*argOpers[valArgIndx]);
9751 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00009752 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009753 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00009754 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009755 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009756 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009757 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009758 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +00009759 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00009760 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009761 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009762
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009763 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009764 MIB.addReg(t1);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009765 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009766 MIB.addReg(t2);
9767
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009768 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009769 MIB.addReg(t5);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009770 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009771 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +00009772
Dale Johannesene4d209d2009-02-03 20:21:25 +00009773 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009774 for (int i=0; i <= lastAddrIndx; ++i)
9775 (*MIB).addOperand(*argOpers[i]);
9776
9777 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009778 (*MIB).setMemRefs(bInstr->memoperands_begin(),
9779 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009780
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009781 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009782 MIB.addReg(X86::EAX);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009783 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009784 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +00009785
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009786 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009787 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009788
Dan Gohman14152b42010-07-06 20:24:04 +00009789 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009790 return nextMBB;
9791}
9792
9793// private utility function
9794MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +00009795X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
9796 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009797 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00009798 // For the atomic min/max operator, we generate
9799 // thisMBB:
9800 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00009801 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +00009802 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +00009803 // cmp t1, t2
9804 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +00009805 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00009806 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
9807 // bz newMBB
9808 // fallthrough -->nextMBB
9809 //
9810 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9811 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009812 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00009813 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009814
Mon P Wang63307c32008-05-05 19:05:59 +00009815 /// First build the CFG
9816 MachineFunction *F = MBB->getParent();
9817 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009818 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9819 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9820 F->insert(MBBIter, newMBB);
9821 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009822
Dan Gohman14152b42010-07-06 20:24:04 +00009823 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9824 nextMBB->splice(nextMBB->begin(), thisMBB,
9825 llvm::next(MachineBasicBlock::iterator(mInstr)),
9826 thisMBB->end());
9827 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009828
Mon P Wang63307c32008-05-05 19:05:59 +00009829 // Update thisMBB to fall through to newMBB
9830 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009831
Mon P Wang63307c32008-05-05 19:05:59 +00009832 // newMBB jumps to newMBB and fall through to nextMBB
9833 newMBB->addSuccessor(nextMBB);
9834 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009835
Dale Johannesene4d209d2009-02-03 20:21:25 +00009836 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00009837 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009838 assert(mInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009839 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +00009840 MachineOperand& destOper = mInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009841 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00009842 int numArgs = mInstr->getNumOperands() - 1;
9843 for (int i=0; i < numArgs; ++i)
9844 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009845
Mon P Wang63307c32008-05-05 19:05:59 +00009846 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009847 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009848 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00009849
Mon P Wangab3e7472008-05-05 22:56:23 +00009850 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009851 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00009852 for (int i=0; i <= lastAddrIndx; ++i)
9853 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +00009854
Mon P Wang63307c32008-05-05 19:05:59 +00009855 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +00009856 assert((argOpers[valArgIndx]->isReg() ||
9857 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00009858 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +00009859
9860 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +00009861 if (argOpers[valArgIndx]->isReg())
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009862 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +00009863 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009864 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00009865 (*MIB).addOperand(*argOpers[valArgIndx]);
9866
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009867 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +00009868 MIB.addReg(t1);
9869
Dale Johannesene4d209d2009-02-03 20:21:25 +00009870 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +00009871 MIB.addReg(t1);
9872 MIB.addReg(t2);
9873
9874 // Generate movc
9875 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009876 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +00009877 MIB.addReg(t2);
9878 MIB.addReg(t1);
9879
9880 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +00009881 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +00009882 for (int i=0; i <= lastAddrIndx; ++i)
9883 (*MIB).addOperand(*argOpers[i]);
9884 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +00009885 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009886 (*MIB).setMemRefs(mInstr->memoperands_begin(),
9887 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +00009888
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009889 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +00009890 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +00009891
Mon P Wang63307c32008-05-05 19:05:59 +00009892 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009893 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00009894
Dan Gohman14152b42010-07-06 20:24:04 +00009895 mInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00009896 return nextMBB;
9897}
9898
Eric Christopherf83a5de2009-08-27 18:08:16 +00009899// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009900// or XMM0_V32I8 in AVX all of this code can be replaced with that
9901// in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +00009902MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +00009903X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +00009904 unsigned numArgs, bool memArg) const {
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009905 assert((Subtarget->hasSSE42() || Subtarget->hasAVX()) &&
9906 "Target must have SSE4.2 or AVX features enabled");
9907
Eric Christopherb120ab42009-08-18 22:50:32 +00009908 DebugLoc dl = MI->getDebugLoc();
9909 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Eric Christopherb120ab42009-08-18 22:50:32 +00009910 unsigned Opc;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009911 if (!Subtarget->hasAVX()) {
9912 if (memArg)
9913 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
9914 else
9915 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
9916 } else {
9917 if (memArg)
9918 Opc = numArgs == 3 ? X86::VPCMPISTRM128rm : X86::VPCMPESTRM128rm;
9919 else
9920 Opc = numArgs == 3 ? X86::VPCMPISTRM128rr : X86::VPCMPESTRM128rr;
9921 }
Eric Christopherb120ab42009-08-18 22:50:32 +00009922
Eric Christopher41c902f2010-11-30 08:20:21 +00009923 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Eric Christopherb120ab42009-08-18 22:50:32 +00009924 for (unsigned i = 0; i < numArgs; ++i) {
9925 MachineOperand &Op = MI->getOperand(i+1);
Eric Christopherb120ab42009-08-18 22:50:32 +00009926 if (!(Op.isReg() && Op.isImplicit()))
9927 MIB.addOperand(Op);
9928 }
Eric Christopher41c902f2010-11-30 08:20:21 +00009929 BuildMI(*BB, MI, dl, TII->get(X86::MOVAPSrr), MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +00009930 .addReg(X86::XMM0);
9931
Dan Gohman14152b42010-07-06 20:24:04 +00009932 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +00009933 return BB;
9934}
9935
9936MachineBasicBlock *
Eric Christopher228232b2010-11-30 07:20:12 +00009937X86TargetLowering::EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +00009938 DebugLoc dl = MI->getDebugLoc();
9939 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009940
Eric Christopher228232b2010-11-30 07:20:12 +00009941 // Address into RAX/EAX, other two args into ECX, EDX.
9942 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
9943 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
9944 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
9945 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +00009946 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009947
Eric Christopher228232b2010-11-30 07:20:12 +00009948 unsigned ValOps = X86::AddrNumOperands;
9949 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
9950 .addReg(MI->getOperand(ValOps).getReg());
9951 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
9952 .addReg(MI->getOperand(ValOps+1).getReg());
9953
9954 // The instruction doesn't actually take any operands though.
9955 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009956
Eric Christopher228232b2010-11-30 07:20:12 +00009957 MI->eraseFromParent(); // The pseudo is gone now.
9958 return BB;
9959}
9960
9961MachineBasicBlock *
9962X86TargetLowering::EmitMwait(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +00009963 DebugLoc dl = MI->getDebugLoc();
9964 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009965
Eric Christopher228232b2010-11-30 07:20:12 +00009966 // First arg in ECX, the second in EAX.
9967 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
9968 .addReg(MI->getOperand(0).getReg());
9969 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EAX)
9970 .addReg(MI->getOperand(1).getReg());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009971
Eric Christopher228232b2010-11-30 07:20:12 +00009972 // The instruction doesn't actually take any operands though.
9973 BuildMI(*BB, MI, dl, TII->get(X86::MWAITrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009974
Eric Christopher228232b2010-11-30 07:20:12 +00009975 MI->eraseFromParent(); // The pseudo is gone now.
9976 return BB;
9977}
9978
9979MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +00009980X86TargetLowering::EmitVAARG64WithCustomInserter(
9981 MachineInstr *MI,
9982 MachineBasicBlock *MBB) const {
9983 // Emit va_arg instruction on X86-64.
9984
9985 // Operands to this pseudo-instruction:
9986 // 0 ) Output : destination address (reg)
9987 // 1-5) Input : va_list address (addr, i64mem)
9988 // 6 ) ArgSize : Size (in bytes) of vararg type
9989 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
9990 // 8 ) Align : Alignment of type
9991 // 9 ) EFLAGS (implicit-def)
9992
9993 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
9994 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
9995
9996 unsigned DestReg = MI->getOperand(0).getReg();
9997 MachineOperand &Base = MI->getOperand(1);
9998 MachineOperand &Scale = MI->getOperand(2);
9999 MachineOperand &Index = MI->getOperand(3);
10000 MachineOperand &Disp = MI->getOperand(4);
10001 MachineOperand &Segment = MI->getOperand(5);
10002 unsigned ArgSize = MI->getOperand(6).getImm();
10003 unsigned ArgMode = MI->getOperand(7).getImm();
10004 unsigned Align = MI->getOperand(8).getImm();
10005
10006 // Memory Reference
10007 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
10008 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
10009 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
10010
10011 // Machine Information
10012 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10013 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
10014 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
10015 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
10016 DebugLoc DL = MI->getDebugLoc();
10017
10018 // struct va_list {
10019 // i32 gp_offset
10020 // i32 fp_offset
10021 // i64 overflow_area (address)
10022 // i64 reg_save_area (address)
10023 // }
10024 // sizeof(va_list) = 24
10025 // alignment(va_list) = 8
10026
10027 unsigned TotalNumIntRegs = 6;
10028 unsigned TotalNumXMMRegs = 8;
10029 bool UseGPOffset = (ArgMode == 1);
10030 bool UseFPOffset = (ArgMode == 2);
10031 unsigned MaxOffset = TotalNumIntRegs * 8 +
10032 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
10033
10034 /* Align ArgSize to a multiple of 8 */
10035 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
10036 bool NeedsAlign = (Align > 8);
10037
10038 MachineBasicBlock *thisMBB = MBB;
10039 MachineBasicBlock *overflowMBB;
10040 MachineBasicBlock *offsetMBB;
10041 MachineBasicBlock *endMBB;
10042
10043 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
10044 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
10045 unsigned OffsetReg = 0;
10046
10047 if (!UseGPOffset && !UseFPOffset) {
10048 // If we only pull from the overflow region, we don't create a branch.
10049 // We don't need to alter control flow.
10050 OffsetDestReg = 0; // unused
10051 OverflowDestReg = DestReg;
10052
10053 offsetMBB = NULL;
10054 overflowMBB = thisMBB;
10055 endMBB = thisMBB;
10056 } else {
10057 // First emit code to check if gp_offset (or fp_offset) is below the bound.
10058 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
10059 // If not, pull from overflow_area. (branch to overflowMBB)
10060 //
10061 // thisMBB
10062 // | .
10063 // | .
10064 // offsetMBB overflowMBB
10065 // | .
10066 // | .
10067 // endMBB
10068
10069 // Registers for the PHI in endMBB
10070 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
10071 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
10072
10073 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
10074 MachineFunction *MF = MBB->getParent();
10075 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
10076 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
10077 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
10078
10079 MachineFunction::iterator MBBIter = MBB;
10080 ++MBBIter;
10081
10082 // Insert the new basic blocks
10083 MF->insert(MBBIter, offsetMBB);
10084 MF->insert(MBBIter, overflowMBB);
10085 MF->insert(MBBIter, endMBB);
10086
10087 // Transfer the remainder of MBB and its successor edges to endMBB.
10088 endMBB->splice(endMBB->begin(), thisMBB,
10089 llvm::next(MachineBasicBlock::iterator(MI)),
10090 thisMBB->end());
10091 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
10092
10093 // Make offsetMBB and overflowMBB successors of thisMBB
10094 thisMBB->addSuccessor(offsetMBB);
10095 thisMBB->addSuccessor(overflowMBB);
10096
10097 // endMBB is a successor of both offsetMBB and overflowMBB
10098 offsetMBB->addSuccessor(endMBB);
10099 overflowMBB->addSuccessor(endMBB);
10100
10101 // Load the offset value into a register
10102 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
10103 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
10104 .addOperand(Base)
10105 .addOperand(Scale)
10106 .addOperand(Index)
10107 .addDisp(Disp, UseFPOffset ? 4 : 0)
10108 .addOperand(Segment)
10109 .setMemRefs(MMOBegin, MMOEnd);
10110
10111 // Check if there is enough room left to pull this argument.
10112 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
10113 .addReg(OffsetReg)
10114 .addImm(MaxOffset + 8 - ArgSizeA8);
10115
10116 // Branch to "overflowMBB" if offset >= max
10117 // Fall through to "offsetMBB" otherwise
10118 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
10119 .addMBB(overflowMBB);
10120 }
10121
10122 // In offsetMBB, emit code to use the reg_save_area.
10123 if (offsetMBB) {
10124 assert(OffsetReg != 0);
10125
10126 // Read the reg_save_area address.
10127 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
10128 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
10129 .addOperand(Base)
10130 .addOperand(Scale)
10131 .addOperand(Index)
10132 .addDisp(Disp, 16)
10133 .addOperand(Segment)
10134 .setMemRefs(MMOBegin, MMOEnd);
10135
10136 // Zero-extend the offset
10137 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
10138 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
10139 .addImm(0)
10140 .addReg(OffsetReg)
10141 .addImm(X86::sub_32bit);
10142
10143 // Add the offset to the reg_save_area to get the final address.
10144 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
10145 .addReg(OffsetReg64)
10146 .addReg(RegSaveReg);
10147
10148 // Compute the offset for the next argument
10149 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
10150 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
10151 .addReg(OffsetReg)
10152 .addImm(UseFPOffset ? 16 : 8);
10153
10154 // Store it back into the va_list.
10155 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
10156 .addOperand(Base)
10157 .addOperand(Scale)
10158 .addOperand(Index)
10159 .addDisp(Disp, UseFPOffset ? 4 : 0)
10160 .addOperand(Segment)
10161 .addReg(NextOffsetReg)
10162 .setMemRefs(MMOBegin, MMOEnd);
10163
10164 // Jump to endMBB
10165 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
10166 .addMBB(endMBB);
10167 }
10168
10169 //
10170 // Emit code to use overflow area
10171 //
10172
10173 // Load the overflow_area address into a register.
10174 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
10175 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
10176 .addOperand(Base)
10177 .addOperand(Scale)
10178 .addOperand(Index)
10179 .addDisp(Disp, 8)
10180 .addOperand(Segment)
10181 .setMemRefs(MMOBegin, MMOEnd);
10182
10183 // If we need to align it, do so. Otherwise, just copy the address
10184 // to OverflowDestReg.
10185 if (NeedsAlign) {
10186 // Align the overflow address
10187 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
10188 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
10189
10190 // aligned_addr = (addr + (align-1)) & ~(align-1)
10191 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
10192 .addReg(OverflowAddrReg)
10193 .addImm(Align-1);
10194
10195 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
10196 .addReg(TmpReg)
10197 .addImm(~(uint64_t)(Align-1));
10198 } else {
10199 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
10200 .addReg(OverflowAddrReg);
10201 }
10202
10203 // Compute the next overflow address after this argument.
10204 // (the overflow address should be kept 8-byte aligned)
10205 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
10206 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
10207 .addReg(OverflowDestReg)
10208 .addImm(ArgSizeA8);
10209
10210 // Store the new overflow address.
10211 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
10212 .addOperand(Base)
10213 .addOperand(Scale)
10214 .addOperand(Index)
10215 .addDisp(Disp, 8)
10216 .addOperand(Segment)
10217 .addReg(NextAddrReg)
10218 .setMemRefs(MMOBegin, MMOEnd);
10219
10220 // If we branched, emit the PHI to the front of endMBB.
10221 if (offsetMBB) {
10222 BuildMI(*endMBB, endMBB->begin(), DL,
10223 TII->get(X86::PHI), DestReg)
10224 .addReg(OffsetDestReg).addMBB(offsetMBB)
10225 .addReg(OverflowDestReg).addMBB(overflowMBB);
10226 }
10227
10228 // Erase the pseudo instruction
10229 MI->eraseFromParent();
10230
10231 return endMBB;
10232}
10233
10234MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +000010235X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
10236 MachineInstr *MI,
10237 MachineBasicBlock *MBB) const {
10238 // Emit code to save XMM registers to the stack. The ABI says that the
10239 // number of registers to save is given in %al, so it's theoretically
10240 // possible to do an indirect jump trick to avoid saving all of them,
10241 // however this code takes a simpler approach and just executes all
10242 // of the stores if %al is non-zero. It's less code, and it's probably
10243 // easier on the hardware branch predictor, and stores aren't all that
10244 // expensive anyway.
10245
10246 // Create the new basic blocks. One block contains all the XMM stores,
10247 // and one block is the final destination regardless of whether any
10248 // stores were performed.
10249 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
10250 MachineFunction *F = MBB->getParent();
10251 MachineFunction::iterator MBBIter = MBB;
10252 ++MBBIter;
10253 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
10254 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
10255 F->insert(MBBIter, XMMSaveMBB);
10256 F->insert(MBBIter, EndMBB);
10257
Dan Gohman14152b42010-07-06 20:24:04 +000010258 // Transfer the remainder of MBB and its successor edges to EndMBB.
10259 EndMBB->splice(EndMBB->begin(), MBB,
10260 llvm::next(MachineBasicBlock::iterator(MI)),
10261 MBB->end());
10262 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
10263
Dan Gohmand6708ea2009-08-15 01:38:56 +000010264 // The original block will now fall through to the XMM save block.
10265 MBB->addSuccessor(XMMSaveMBB);
10266 // The XMMSaveMBB will fall through to the end block.
10267 XMMSaveMBB->addSuccessor(EndMBB);
10268
10269 // Now add the instructions.
10270 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10271 DebugLoc DL = MI->getDebugLoc();
10272
10273 unsigned CountReg = MI->getOperand(0).getReg();
10274 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
10275 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
10276
10277 if (!Subtarget->isTargetWin64()) {
10278 // If %al is 0, branch around the XMM save block.
10279 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +000010280 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010281 MBB->addSuccessor(EndMBB);
10282 }
10283
10284 // In the XMM save block, save all the XMM argument registers.
10285 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
10286 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +000010287 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +000010288 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +000010289 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +000010290 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +000010291 /*Size=*/16, /*Align=*/16);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010292 BuildMI(XMMSaveMBB, DL, TII->get(X86::MOVAPSmr))
10293 .addFrameIndex(RegSaveFrameIndex)
10294 .addImm(/*Scale=*/1)
10295 .addReg(/*IndexReg=*/0)
10296 .addImm(/*Disp=*/Offset)
10297 .addReg(/*Segment=*/0)
10298 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +000010299 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010300 }
10301
Dan Gohman14152b42010-07-06 20:24:04 +000010302 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +000010303
10304 return EndMBB;
10305}
Mon P Wang63307c32008-05-05 19:05:59 +000010306
Evan Cheng60c07e12006-07-05 22:17:51 +000010307MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +000010308X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010309 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +000010310 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10311 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +000010312
Chris Lattner52600972009-09-02 05:57:00 +000010313 // To "insert" a SELECT_CC instruction, we actually have to insert the
10314 // diamond control-flow pattern. The incoming instruction knows the
10315 // destination vreg to set, the condition code register to branch on, the
10316 // true/false values to select between, and a branch opcode to use.
10317 const BasicBlock *LLVM_BB = BB->getBasicBlock();
10318 MachineFunction::iterator It = BB;
10319 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +000010320
Chris Lattner52600972009-09-02 05:57:00 +000010321 // thisMBB:
10322 // ...
10323 // TrueVal = ...
10324 // cmpTY ccX, r1, r2
10325 // bCC copy1MBB
10326 // fallthrough --> copy0MBB
10327 MachineBasicBlock *thisMBB = BB;
10328 MachineFunction *F = BB->getParent();
10329 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
10330 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +000010331 F->insert(It, copy0MBB);
10332 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +000010333
Bill Wendling730c07e2010-06-25 20:48:10 +000010334 // If the EFLAGS register isn't dead in the terminator, then claim that it's
10335 // live into the sink and copy blocks.
10336 const MachineFunction *MF = BB->getParent();
10337 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
10338 BitVector ReservedRegs = TRI->getReservedRegs(*MF);
Bill Wendling730c07e2010-06-25 20:48:10 +000010339
Dan Gohman14152b42010-07-06 20:24:04 +000010340 for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) {
10341 const MachineOperand &MO = MI->getOperand(I);
10342 if (!MO.isReg() || !MO.isUse() || MO.isKill()) continue;
Bill Wendling730c07e2010-06-25 20:48:10 +000010343 unsigned Reg = MO.getReg();
10344 if (Reg != X86::EFLAGS) continue;
10345 copy0MBB->addLiveIn(Reg);
10346 sinkMBB->addLiveIn(Reg);
10347 }
10348
Dan Gohman14152b42010-07-06 20:24:04 +000010349 // Transfer the remainder of BB and its successor edges to sinkMBB.
10350 sinkMBB->splice(sinkMBB->begin(), BB,
10351 llvm::next(MachineBasicBlock::iterator(MI)),
10352 BB->end());
10353 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
10354
10355 // Add the true and fallthrough blocks as its successors.
10356 BB->addSuccessor(copy0MBB);
10357 BB->addSuccessor(sinkMBB);
10358
10359 // Create the conditional branch instruction.
10360 unsigned Opc =
10361 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
10362 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
10363
Chris Lattner52600972009-09-02 05:57:00 +000010364 // copy0MBB:
10365 // %FalseValue = ...
10366 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000010367 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000010368
Chris Lattner52600972009-09-02 05:57:00 +000010369 // sinkMBB:
10370 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
10371 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000010372 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
10373 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000010374 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
10375 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
10376
Dan Gohman14152b42010-07-06 20:24:04 +000010377 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000010378 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000010379}
10380
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010381MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010382X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010383 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010384 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10385 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010386
10387 // The lowering is pretty easy: we're just emitting the call to _alloca. The
10388 // non-trivial part is impdef of ESP.
10389 // FIXME: The code should be tweaked as soon as we'll try to do codegen for
10390 // mingw-w64.
10391
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010392 const char *StackProbeSymbol =
10393 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
10394
Dan Gohman14152b42010-07-06 20:24:04 +000010395 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010396 .addExternalSymbol(StackProbeSymbol)
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010397 .addReg(X86::EAX, RegState::Implicit)
10398 .addReg(X86::ESP, RegState::Implicit)
10399 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
Anton Korobeynikov9f7f83b2010-08-25 07:50:11 +000010400 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
10401 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010402
Dan Gohman14152b42010-07-06 20:24:04 +000010403 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010404 return BB;
10405}
Chris Lattner52600972009-09-02 05:57:00 +000010406
10407MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000010408X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
10409 MachineBasicBlock *BB) const {
10410 // This is pretty easy. We're taking the value that we received from
10411 // our load from the relocation, sticking it in either RDI (x86-64)
10412 // or EAX and doing an indirect call. The return value will then
10413 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000010414 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000010415 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000010416 DebugLoc DL = MI->getDebugLoc();
10417 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000010418
10419 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000010420 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000010421
Eric Christopher30ef0e52010-06-03 04:07:48 +000010422 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000010423 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10424 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000010425 .addReg(X86::RIP)
10426 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010427 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000010428 MI->getOperand(3).getTargetFlags())
10429 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000010430 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000010431 addDirectMem(MIB, X86::RDI);
Eric Christopher61025492010-06-15 23:08:42 +000010432 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000010433 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10434 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000010435 .addReg(0)
10436 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010437 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000010438 MI->getOperand(3).getTargetFlags())
10439 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000010440 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000010441 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010442 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000010443 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10444 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000010445 .addReg(TII->getGlobalBaseReg(F))
10446 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010447 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000010448 MI->getOperand(3).getTargetFlags())
10449 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000010450 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000010451 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010452 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000010453
Dan Gohman14152b42010-07-06 20:24:04 +000010454 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000010455 return BB;
10456}
10457
10458MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000010459X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010460 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000010461 switch (MI->getOpcode()) {
10462 default: assert(false && "Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000010463 case X86::TAILJMPd64:
10464 case X86::TAILJMPr64:
10465 case X86::TAILJMPm64:
10466 assert(!"TAILJMP64 would not be touched here.");
10467 case X86::TCRETURNdi64:
10468 case X86::TCRETURNri64:
10469 case X86::TCRETURNmi64:
10470 // Defs of TCRETURNxx64 has Win64's callee-saved registers, as subset.
10471 // On AMD64, additional defs should be added before register allocation.
10472 if (!Subtarget->isTargetWin64()) {
10473 MI->addRegisterDefined(X86::RSI);
10474 MI->addRegisterDefined(X86::RDI);
10475 MI->addRegisterDefined(X86::XMM6);
10476 MI->addRegisterDefined(X86::XMM7);
10477 MI->addRegisterDefined(X86::XMM8);
10478 MI->addRegisterDefined(X86::XMM9);
10479 MI->addRegisterDefined(X86::XMM10);
10480 MI->addRegisterDefined(X86::XMM11);
10481 MI->addRegisterDefined(X86::XMM12);
10482 MI->addRegisterDefined(X86::XMM13);
10483 MI->addRegisterDefined(X86::XMM14);
10484 MI->addRegisterDefined(X86::XMM15);
10485 }
10486 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010487 case X86::WIN_ALLOCA:
10488 return EmitLoweredWinAlloca(MI, BB);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010489 case X86::TLSCall_32:
10490 case X86::TLSCall_64:
10491 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000010492 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000010493 case X86::CMOV_FR32:
10494 case X86::CMOV_FR64:
10495 case X86::CMOV_V4F32:
10496 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000010497 case X86::CMOV_V2I64:
Chris Lattner314a1132010-03-14 18:31:44 +000010498 case X86::CMOV_GR16:
10499 case X86::CMOV_GR32:
10500 case X86::CMOV_RFP32:
10501 case X86::CMOV_RFP64:
10502 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010503 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000010504
Dale Johannesen849f2142007-07-03 00:53:03 +000010505 case X86::FP32_TO_INT16_IN_MEM:
10506 case X86::FP32_TO_INT32_IN_MEM:
10507 case X86::FP32_TO_INT64_IN_MEM:
10508 case X86::FP64_TO_INT16_IN_MEM:
10509 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000010510 case X86::FP64_TO_INT64_IN_MEM:
10511 case X86::FP80_TO_INT16_IN_MEM:
10512 case X86::FP80_TO_INT32_IN_MEM:
10513 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000010514 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10515 DebugLoc DL = MI->getDebugLoc();
10516
Evan Cheng60c07e12006-07-05 22:17:51 +000010517 // Change the floating point control register to use "round towards zero"
10518 // mode when truncating to an integer value.
10519 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000010520 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000010521 addFrameReference(BuildMI(*BB, MI, DL,
10522 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010523
10524 // Load the old value of the high byte of the control word...
10525 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +000010526 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohman14152b42010-07-06 20:24:04 +000010527 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000010528 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010529
10530 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000010531 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000010532 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000010533
10534 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000010535 addFrameReference(BuildMI(*BB, MI, DL,
10536 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010537
10538 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000010539 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000010540 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000010541
10542 // Get the X86 opcode to use.
10543 unsigned Opc;
10544 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010545 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000010546 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
10547 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
10548 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
10549 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
10550 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
10551 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000010552 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
10553 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
10554 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000010555 }
10556
10557 X86AddressMode AM;
10558 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000010559 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000010560 AM.BaseType = X86AddressMode::RegBase;
10561 AM.Base.Reg = Op.getReg();
10562 } else {
10563 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000010564 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000010565 }
10566 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000010567 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000010568 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010569 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000010570 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000010571 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010572 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000010573 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000010574 AM.GV = Op.getGlobal();
10575 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000010576 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010577 }
Dan Gohman14152b42010-07-06 20:24:04 +000010578 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010579 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000010580
10581 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000010582 addFrameReference(BuildMI(*BB, MI, DL,
10583 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010584
Dan Gohman14152b42010-07-06 20:24:04 +000010585 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000010586 return BB;
10587 }
Eric Christopherb120ab42009-08-18 22:50:32 +000010588 // String/text processing lowering.
10589 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010590 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000010591 return EmitPCMP(MI, BB, 3, false /* in-mem */);
10592 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010593 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000010594 return EmitPCMP(MI, BB, 3, true /* in-mem */);
10595 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010596 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000010597 return EmitPCMP(MI, BB, 5, false /* in mem */);
10598 case X86::PCMPESTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010599 case X86::VPCMPESTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000010600 return EmitPCMP(MI, BB, 5, true /* in mem */);
10601
Eric Christopher228232b2010-11-30 07:20:12 +000010602 // Thread synchronization.
10603 case X86::MONITOR:
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010604 return EmitMonitor(MI, BB);
Eric Christopher228232b2010-11-30 07:20:12 +000010605 case X86::MWAIT:
10606 return EmitMwait(MI, BB);
10607
Eric Christopherb120ab42009-08-18 22:50:32 +000010608 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +000010609 case X86::ATOMAND32:
10610 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010611 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010612 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010613 X86::NOT32r, X86::EAX,
10614 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000010615 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +000010616 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
10617 X86::OR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010618 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010619 X86::NOT32r, X86::EAX,
10620 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000010621 case X86::ATOMXOR32:
10622 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010623 X86::XOR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010624 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010625 X86::NOT32r, X86::EAX,
10626 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010627 case X86::ATOMNAND32:
10628 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010629 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010630 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010631 X86::NOT32r, X86::EAX,
10632 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +000010633 case X86::ATOMMIN32:
10634 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
10635 case X86::ATOMMAX32:
10636 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
10637 case X86::ATOMUMIN32:
10638 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
10639 case X86::ATOMUMAX32:
10640 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +000010641
10642 case X86::ATOMAND16:
10643 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
10644 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010645 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010646 X86::NOT16r, X86::AX,
10647 X86::GR16RegisterClass);
10648 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +000010649 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010650 X86::OR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010651 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010652 X86::NOT16r, X86::AX,
10653 X86::GR16RegisterClass);
10654 case X86::ATOMXOR16:
10655 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
10656 X86::XOR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010657 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010658 X86::NOT16r, X86::AX,
10659 X86::GR16RegisterClass);
10660 case X86::ATOMNAND16:
10661 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
10662 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010663 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010664 X86::NOT16r, X86::AX,
10665 X86::GR16RegisterClass, true);
10666 case X86::ATOMMIN16:
10667 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
10668 case X86::ATOMMAX16:
10669 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
10670 case X86::ATOMUMIN16:
10671 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
10672 case X86::ATOMUMAX16:
10673 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
10674
10675 case X86::ATOMAND8:
10676 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
10677 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010678 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010679 X86::NOT8r, X86::AL,
10680 X86::GR8RegisterClass);
10681 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +000010682 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010683 X86::OR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010684 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010685 X86::NOT8r, X86::AL,
10686 X86::GR8RegisterClass);
10687 case X86::ATOMXOR8:
10688 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
10689 X86::XOR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010690 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010691 X86::NOT8r, X86::AL,
10692 X86::GR8RegisterClass);
10693 case X86::ATOMNAND8:
10694 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
10695 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010696 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010697 X86::NOT8r, X86::AL,
10698 X86::GR8RegisterClass, true);
10699 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010700 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +000010701 case X86::ATOMAND64:
10702 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010703 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010704 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010705 X86::NOT64r, X86::RAX,
10706 X86::GR64RegisterClass);
10707 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +000010708 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
10709 X86::OR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010710 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010711 X86::NOT64r, X86::RAX,
10712 X86::GR64RegisterClass);
10713 case X86::ATOMXOR64:
10714 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010715 X86::XOR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010716 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010717 X86::NOT64r, X86::RAX,
10718 X86::GR64RegisterClass);
10719 case X86::ATOMNAND64:
10720 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
10721 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010722 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010723 X86::NOT64r, X86::RAX,
10724 X86::GR64RegisterClass, true);
10725 case X86::ATOMMIN64:
10726 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
10727 case X86::ATOMMAX64:
10728 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
10729 case X86::ATOMUMIN64:
10730 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
10731 case X86::ATOMUMAX64:
10732 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010733
10734 // This group does 64-bit operations on a 32-bit host.
10735 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010736 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010737 X86::AND32rr, X86::AND32rr,
10738 X86::AND32ri, X86::AND32ri,
10739 false);
10740 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010741 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010742 X86::OR32rr, X86::OR32rr,
10743 X86::OR32ri, X86::OR32ri,
10744 false);
10745 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010746 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010747 X86::XOR32rr, X86::XOR32rr,
10748 X86::XOR32ri, X86::XOR32ri,
10749 false);
10750 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010751 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010752 X86::AND32rr, X86::AND32rr,
10753 X86::AND32ri, X86::AND32ri,
10754 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010755 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010756 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010757 X86::ADD32rr, X86::ADC32rr,
10758 X86::ADD32ri, X86::ADC32ri,
10759 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010760 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010761 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010762 X86::SUB32rr, X86::SBB32rr,
10763 X86::SUB32ri, X86::SBB32ri,
10764 false);
Dale Johannesen880ae362008-10-03 22:25:52 +000010765 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010766 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +000010767 X86::MOV32rr, X86::MOV32rr,
10768 X86::MOV32ri, X86::MOV32ri,
10769 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010770 case X86::VASTART_SAVE_XMM_REGS:
10771 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000010772
10773 case X86::VAARG_64:
10774 return EmitVAARG64WithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000010775 }
10776}
10777
10778//===----------------------------------------------------------------------===//
10779// X86 Optimization Hooks
10780//===----------------------------------------------------------------------===//
10781
Dan Gohman475871a2008-07-27 21:46:04 +000010782void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +000010783 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000010784 APInt &KnownZero,
10785 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000010786 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000010787 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010788 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000010789 assert((Opc >= ISD::BUILTIN_OP_END ||
10790 Opc == ISD::INTRINSIC_WO_CHAIN ||
10791 Opc == ISD::INTRINSIC_W_CHAIN ||
10792 Opc == ISD::INTRINSIC_VOID) &&
10793 "Should use MaskedValueIsZero if you don't know whether Op"
10794 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010795
Dan Gohmanf4f92f52008-02-13 23:07:24 +000010796 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010797 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000010798 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000010799 case X86ISD::ADD:
10800 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000010801 case X86ISD::ADC:
10802 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000010803 case X86ISD::SMUL:
10804 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000010805 case X86ISD::INC:
10806 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000010807 case X86ISD::OR:
10808 case X86ISD::XOR:
10809 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000010810 // These nodes' second result is a boolean.
10811 if (Op.getResNo() == 0)
10812 break;
10813 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010814 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000010815 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
10816 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000010817 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010818 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010819}
Chris Lattner259e97c2006-01-31 19:43:35 +000010820
Owen Andersonbc146b02010-09-21 20:42:50 +000010821unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
10822 unsigned Depth) const {
10823 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
10824 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
10825 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000010826
Owen Andersonbc146b02010-09-21 20:42:50 +000010827 // Fallback case.
10828 return 1;
10829}
10830
Evan Cheng206ee9d2006-07-07 08:33:52 +000010831/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000010832/// node is a GlobalAddress + offset.
10833bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000010834 const GlobalValue* &GA,
10835 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000010836 if (N->getOpcode() == X86ISD::Wrapper) {
10837 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000010838 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000010839 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000010840 return true;
10841 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000010842 }
Evan Chengad4196b2008-05-12 19:56:52 +000010843 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000010844}
10845
Evan Cheng206ee9d2006-07-07 08:33:52 +000010846/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
10847/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
10848/// if the load addresses are consecutive, non-overlapping, and in the right
Nate Begemanfdea31a2010-03-24 20:49:50 +000010849/// order.
Dan Gohman475871a2008-07-27 21:46:04 +000010850static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Mon P Wanga0fd0d52010-12-19 23:55:53 +000010851 TargetLowering::DAGCombinerInfo &DCI) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000010852 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000010853 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000010854
Eli Friedman7a5e5552009-06-07 06:52:44 +000010855 if (VT.getSizeInBits() != 128)
10856 return SDValue();
10857
Mon P Wanga0fd0d52010-12-19 23:55:53 +000010858 // Don't create instructions with illegal types after legalize types has run.
10859 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10860 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
10861 return SDValue();
10862
Nate Begemanfdea31a2010-03-24 20:49:50 +000010863 SmallVector<SDValue, 16> Elts;
10864 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000010865 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000010866
Nate Begemanfdea31a2010-03-24 20:49:50 +000010867 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000010868}
Evan Chengd880b972008-05-09 21:53:03 +000010869
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000010870/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
10871/// generation and convert it from being a bunch of shuffles and extracts
10872/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010873static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
10874 const TargetLowering &TLI) {
10875 SDValue InputVector = N->getOperand(0);
10876
10877 // Only operate on vectors of 4 elements, where the alternative shuffling
10878 // gets to be more expensive.
10879 if (InputVector.getValueType() != MVT::v4i32)
10880 return SDValue();
10881
10882 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
10883 // single use which is a sign-extend or zero-extend, and all elements are
10884 // used.
10885 SmallVector<SDNode *, 4> Uses;
10886 unsigned ExtractedElements = 0;
10887 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
10888 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
10889 if (UI.getUse().getResNo() != InputVector.getResNo())
10890 return SDValue();
10891
10892 SDNode *Extract = *UI;
10893 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
10894 return SDValue();
10895
10896 if (Extract->getValueType(0) != MVT::i32)
10897 return SDValue();
10898 if (!Extract->hasOneUse())
10899 return SDValue();
10900 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
10901 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
10902 return SDValue();
10903 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
10904 return SDValue();
10905
10906 // Record which element was extracted.
10907 ExtractedElements |=
10908 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
10909
10910 Uses.push_back(Extract);
10911 }
10912
10913 // If not all the elements were used, this may not be worthwhile.
10914 if (ExtractedElements != 15)
10915 return SDValue();
10916
10917 // Ok, we've now decided to do the transformation.
10918 DebugLoc dl = InputVector.getDebugLoc();
10919
10920 // Store the value to a temporary stack slot.
10921 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000010922 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
10923 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010924
10925 // Replace each use (extract) with a load of the appropriate element.
10926 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
10927 UE = Uses.end(); UI != UE; ++UI) {
10928 SDNode *Extract = *UI;
10929
10930 // Compute the element's address.
10931 SDValue Idx = Extract->getOperand(1);
10932 unsigned EltSize =
10933 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
10934 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
10935 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
10936
Eric Christopher90eb4022010-07-22 00:26:08 +000010937 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(),
Chris Lattner51abfe42010-09-21 06:02:19 +000010938 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010939
10940 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000010941 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000010942 ScalarAddr, MachinePointerInfo(),
10943 false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010944
10945 // Replace the exact with the load.
10946 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
10947 }
10948
10949 // The replacement was made in place; don't return anything.
10950 return SDValue();
10951}
10952
Chris Lattner83e6c992006-10-04 06:57:07 +000010953/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000010954static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +000010955 const X86Subtarget *Subtarget) {
10956 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000010957 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000010958 // Get the LHS/RHS of the select.
10959 SDValue LHS = N->getOperand(1);
10960 SDValue RHS = N->getOperand(2);
Eric Christopherfd179292009-08-27 18:07:15 +000010961
Dan Gohman670e5392009-09-21 18:03:22 +000010962 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000010963 // instructions match the semantics of the common C idiom x<y?x:y but not
10964 // x<=y?x:y, because of how they handle negative zero (which can be
10965 // ignored in unsafe-math mode).
Chris Lattner83e6c992006-10-04 06:57:07 +000010966 if (Subtarget->hasSSE2() &&
Owen Anderson825b72b2009-08-11 20:47:22 +000010967 (LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64) &&
Chris Lattner47b4ce82009-03-11 05:48:52 +000010968 Cond.getOpcode() == ISD::SETCC) {
10969 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010970
Chris Lattner47b4ce82009-03-11 05:48:52 +000010971 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000010972 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000010973 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
10974 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000010975 switch (CC) {
10976 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000010977 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000010978 // Converting this to a min would handle NaNs incorrectly, and swapping
10979 // the operands would cause it to handle comparisons between positive
10980 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000010981 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000010982 if (!UnsafeFPMath &&
10983 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
10984 break;
10985 std::swap(LHS, RHS);
10986 }
Dan Gohman670e5392009-09-21 18:03:22 +000010987 Opcode = X86ISD::FMIN;
10988 break;
10989 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000010990 // Converting this to a min would handle comparisons between positive
10991 // and negative zero incorrectly.
10992 if (!UnsafeFPMath &&
10993 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
10994 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010995 Opcode = X86ISD::FMIN;
10996 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000010997 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000010998 // Converting this to a min would handle both negative zeros and NaNs
10999 // incorrectly, but we can swap the operands to fix both.
11000 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000011001 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011002 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000011003 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011004 Opcode = X86ISD::FMIN;
11005 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011006
Dan Gohman670e5392009-09-21 18:03:22 +000011007 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000011008 // Converting this to a max would handle comparisons between positive
11009 // and negative zero incorrectly.
11010 if (!UnsafeFPMath &&
11011 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(LHS))
11012 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011013 Opcode = X86ISD::FMAX;
11014 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000011015 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000011016 // Converting this to a max would handle NaNs incorrectly, and swapping
11017 // the operands would cause it to handle comparisons between positive
11018 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000011019 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000011020 if (!UnsafeFPMath &&
11021 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
11022 break;
11023 std::swap(LHS, RHS);
11024 }
Dan Gohman670e5392009-09-21 18:03:22 +000011025 Opcode = X86ISD::FMAX;
11026 break;
11027 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000011028 // Converting this to a max would handle both negative zeros and NaNs
11029 // incorrectly, but we can swap the operands to fix both.
11030 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000011031 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011032 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011033 case ISD::SETGE:
11034 Opcode = X86ISD::FMAX;
11035 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000011036 }
Dan Gohman670e5392009-09-21 18:03:22 +000011037 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000011038 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
11039 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000011040 switch (CC) {
11041 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000011042 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000011043 // Converting this to a min would handle comparisons between positive
11044 // and negative zero incorrectly, and swapping the operands would
11045 // cause it to handle NaNs incorrectly.
11046 if (!UnsafeFPMath &&
11047 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000011048 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000011049 break;
11050 std::swap(LHS, RHS);
11051 }
Dan Gohman670e5392009-09-21 18:03:22 +000011052 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000011053 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011054 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000011055 // Converting this to a min would handle NaNs incorrectly.
11056 if (!UnsafeFPMath &&
11057 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
11058 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011059 Opcode = X86ISD::FMIN;
11060 break;
11061 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000011062 // Converting this to a min would handle both negative zeros and NaNs
11063 // incorrectly, but we can swap the operands to fix both.
11064 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000011065 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011066 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011067 case ISD::SETGE:
11068 Opcode = X86ISD::FMIN;
11069 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011070
Dan Gohman670e5392009-09-21 18:03:22 +000011071 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000011072 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000011073 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000011074 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011075 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000011076 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011077 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000011078 // Converting this to a max would handle comparisons between positive
11079 // and negative zero incorrectly, and swapping the operands would
11080 // cause it to handle NaNs incorrectly.
11081 if (!UnsafeFPMath &&
11082 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000011083 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000011084 break;
11085 std::swap(LHS, RHS);
11086 }
Dan Gohman670e5392009-09-21 18:03:22 +000011087 Opcode = X86ISD::FMAX;
11088 break;
11089 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000011090 // Converting this to a max would handle both negative zeros and NaNs
11091 // incorrectly, but we can swap the operands to fix both.
11092 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000011093 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011094 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000011095 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011096 Opcode = X86ISD::FMAX;
11097 break;
11098 }
Chris Lattner83e6c992006-10-04 06:57:07 +000011099 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011100
Chris Lattner47b4ce82009-03-11 05:48:52 +000011101 if (Opcode)
11102 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000011103 }
Eric Christopherfd179292009-08-27 18:07:15 +000011104
Chris Lattnerd1980a52009-03-12 06:52:53 +000011105 // If this is a select between two integer constants, try to do some
11106 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000011107 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
11108 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000011109 // Don't do this for crazy integer types.
11110 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
11111 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000011112 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000011113 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000011114
Chris Lattnercee56e72009-03-13 05:53:31 +000011115 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000011116 // Efficiently invertible.
11117 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
11118 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
11119 isa<ConstantSDNode>(Cond.getOperand(1))))) {
11120 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000011121 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000011122 }
Eric Christopherfd179292009-08-27 18:07:15 +000011123
Chris Lattnerd1980a52009-03-12 06:52:53 +000011124 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000011125 if (FalseC->getAPIntValue() == 0 &&
11126 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000011127 if (NeedsCondInvert) // Invert the condition if needed.
11128 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
11129 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011130
Chris Lattnerd1980a52009-03-12 06:52:53 +000011131 // Zero extend the condition if needed.
11132 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011133
Chris Lattnercee56e72009-03-13 05:53:31 +000011134 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000011135 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000011136 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000011137 }
Eric Christopherfd179292009-08-27 18:07:15 +000011138
Chris Lattner97a29a52009-03-13 05:22:11 +000011139 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000011140 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +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 Lattner97a29a52009-03-13 05:22:11 +000011145 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000011146 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
11147 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000011148 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000011149 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000011150 }
Eric Christopherfd179292009-08-27 18:07:15 +000011151
Chris Lattnercee56e72009-03-13 05:53:31 +000011152 // Optimize cases that will turn into an LEA instruction. This requires
11153 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000011154 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000011155 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000011156 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000011157
Chris Lattnercee56e72009-03-13 05:53:31 +000011158 bool isFastMultiplier = false;
11159 if (Diff < 10) {
11160 switch ((unsigned char)Diff) {
11161 default: break;
11162 case 1: // result = add base, cond
11163 case 2: // result = lea base( , cond*2)
11164 case 3: // result = lea base(cond, cond*2)
11165 case 4: // result = lea base( , cond*4)
11166 case 5: // result = lea base(cond, cond*4)
11167 case 8: // result = lea base( , cond*8)
11168 case 9: // result = lea base(cond, cond*8)
11169 isFastMultiplier = true;
11170 break;
11171 }
11172 }
Eric Christopherfd179292009-08-27 18:07:15 +000011173
Chris Lattnercee56e72009-03-13 05:53:31 +000011174 if (isFastMultiplier) {
11175 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
11176 if (NeedsCondInvert) // Invert the condition if needed.
11177 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
11178 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011179
Chris Lattnercee56e72009-03-13 05:53:31 +000011180 // Zero extend the condition if needed.
11181 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
11182 Cond);
11183 // Scale the condition by the difference.
11184 if (Diff != 1)
11185 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
11186 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011187
Chris Lattnercee56e72009-03-13 05:53:31 +000011188 // Add the base if non-zero.
11189 if (FalseC->getAPIntValue() != 0)
11190 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
11191 SDValue(FalseC, 0));
11192 return Cond;
11193 }
Eric Christopherfd179292009-08-27 18:07:15 +000011194 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000011195 }
11196 }
Eric Christopherfd179292009-08-27 18:07:15 +000011197
Dan Gohman475871a2008-07-27 21:46:04 +000011198 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000011199}
11200
Chris Lattnerd1980a52009-03-12 06:52:53 +000011201/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
11202static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
11203 TargetLowering::DAGCombinerInfo &DCI) {
11204 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000011205
Chris Lattnerd1980a52009-03-12 06:52:53 +000011206 // If the flag operand isn't dead, don't touch this CMOV.
11207 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
11208 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000011209
Chris Lattnerd1980a52009-03-12 06:52:53 +000011210 // If this is a select between two integer constants, try to do some
11211 // optimizations. Note that the operands are ordered the opposite of SELECT
11212 // operands.
11213 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
11214 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
11215 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
11216 // larger than FalseC (the false value).
11217 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
Eric Christopherfd179292009-08-27 18:07:15 +000011218
Chris Lattnerd1980a52009-03-12 06:52:53 +000011219 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
11220 CC = X86::GetOppositeBranchCondition(CC);
11221 std::swap(TrueC, FalseC);
11222 }
Eric Christopherfd179292009-08-27 18:07:15 +000011223
Chris Lattnerd1980a52009-03-12 06:52:53 +000011224 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000011225 // This is efficient for any integer data type (including i8/i16) and
11226 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000011227 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
11228 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +000011229 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11230 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011231
Chris Lattnerd1980a52009-03-12 06:52:53 +000011232 // Zero extend the condition if needed.
11233 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011234
Chris Lattnerd1980a52009-03-12 06:52:53 +000011235 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
11236 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000011237 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000011238 if (N->getNumValues() == 2) // Dead flag value?
11239 return DCI.CombineTo(N, Cond, SDValue());
11240 return Cond;
11241 }
Eric Christopherfd179292009-08-27 18:07:15 +000011242
Chris Lattnercee56e72009-03-13 05:53:31 +000011243 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
11244 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000011245 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
11246 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +000011247 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11248 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011249
Chris Lattner97a29a52009-03-13 05:22:11 +000011250 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000011251 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
11252 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000011253 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
11254 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000011255
Chris Lattner97a29a52009-03-13 05:22:11 +000011256 if (N->getNumValues() == 2) // Dead flag value?
11257 return DCI.CombineTo(N, Cond, SDValue());
11258 return Cond;
11259 }
Eric Christopherfd179292009-08-27 18:07:15 +000011260
Chris Lattnercee56e72009-03-13 05:53:31 +000011261 // Optimize cases that will turn into an LEA instruction. This requires
11262 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000011263 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000011264 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000011265 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000011266
Chris Lattnercee56e72009-03-13 05:53:31 +000011267 bool isFastMultiplier = false;
11268 if (Diff < 10) {
11269 switch ((unsigned char)Diff) {
11270 default: break;
11271 case 1: // result = add base, cond
11272 case 2: // result = lea base( , cond*2)
11273 case 3: // result = lea base(cond, cond*2)
11274 case 4: // result = lea base( , cond*4)
11275 case 5: // result = lea base(cond, cond*4)
11276 case 8: // result = lea base( , cond*8)
11277 case 9: // result = lea base(cond, cond*8)
11278 isFastMultiplier = true;
11279 break;
11280 }
11281 }
Eric Christopherfd179292009-08-27 18:07:15 +000011282
Chris Lattnercee56e72009-03-13 05:53:31 +000011283 if (isFastMultiplier) {
11284 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
11285 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +000011286 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11287 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000011288 // Zero extend the condition if needed.
11289 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
11290 Cond);
11291 // Scale the condition by the difference.
11292 if (Diff != 1)
11293 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
11294 DAG.getConstant(Diff, Cond.getValueType()));
11295
11296 // Add the base if non-zero.
11297 if (FalseC->getAPIntValue() != 0)
11298 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
11299 SDValue(FalseC, 0));
11300 if (N->getNumValues() == 2) // Dead flag value?
11301 return DCI.CombineTo(N, Cond, SDValue());
11302 return Cond;
11303 }
Eric Christopherfd179292009-08-27 18:07:15 +000011304 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000011305 }
11306 }
11307 return SDValue();
11308}
11309
11310
Evan Cheng0b0cd912009-03-28 05:57:29 +000011311/// PerformMulCombine - Optimize a single multiply with constant into two
11312/// in order to implement it with two cheaper instructions, e.g.
11313/// LEA + SHL, LEA + LEA.
11314static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
11315 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000011316 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
11317 return SDValue();
11318
Owen Andersone50ed302009-08-10 22:56:29 +000011319 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000011320 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000011321 return SDValue();
11322
11323 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
11324 if (!C)
11325 return SDValue();
11326 uint64_t MulAmt = C->getZExtValue();
11327 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
11328 return SDValue();
11329
11330 uint64_t MulAmt1 = 0;
11331 uint64_t MulAmt2 = 0;
11332 if ((MulAmt % 9) == 0) {
11333 MulAmt1 = 9;
11334 MulAmt2 = MulAmt / 9;
11335 } else if ((MulAmt % 5) == 0) {
11336 MulAmt1 = 5;
11337 MulAmt2 = MulAmt / 5;
11338 } else if ((MulAmt % 3) == 0) {
11339 MulAmt1 = 3;
11340 MulAmt2 = MulAmt / 3;
11341 }
11342 if (MulAmt2 &&
11343 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
11344 DebugLoc DL = N->getDebugLoc();
11345
11346 if (isPowerOf2_64(MulAmt2) &&
11347 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
11348 // If second multiplifer is pow2, issue it first. We want the multiply by
11349 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
11350 // is an add.
11351 std::swap(MulAmt1, MulAmt2);
11352
11353 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000011354 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000011355 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000011356 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000011357 else
Evan Cheng73f24c92009-03-30 21:36:47 +000011358 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000011359 DAG.getConstant(MulAmt1, VT));
11360
Eric Christopherfd179292009-08-27 18:07:15 +000011361 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000011362 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000011363 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000011364 else
Evan Cheng73f24c92009-03-30 21:36:47 +000011365 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000011366 DAG.getConstant(MulAmt2, VT));
11367
11368 // Do not add new nodes to DAG combiner worklist.
11369 DCI.CombineTo(N, NewMul, false);
11370 }
11371 return SDValue();
11372}
11373
Evan Chengad9c0a32009-12-15 00:53:42 +000011374static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
11375 SDValue N0 = N->getOperand(0);
11376 SDValue N1 = N->getOperand(1);
11377 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
11378 EVT VT = N0.getValueType();
11379
11380 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
11381 // since the result of setcc_c is all zero's or all ones.
11382 if (N1C && N0.getOpcode() == ISD::AND &&
11383 N0.getOperand(1).getOpcode() == ISD::Constant) {
11384 SDValue N00 = N0.getOperand(0);
11385 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
11386 ((N00.getOpcode() == ISD::ANY_EXTEND ||
11387 N00.getOpcode() == ISD::ZERO_EXTEND) &&
11388 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
11389 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
11390 APInt ShAmt = N1C->getAPIntValue();
11391 Mask = Mask.shl(ShAmt);
11392 if (Mask != 0)
11393 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
11394 N00, DAG.getConstant(Mask, VT));
11395 }
11396 }
11397
11398 return SDValue();
11399}
Evan Cheng0b0cd912009-03-28 05:57:29 +000011400
Nate Begeman740ab032009-01-26 00:52:55 +000011401/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
11402/// when possible.
11403static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
11404 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000011405 EVT VT = N->getValueType(0);
11406 if (!VT.isVector() && VT.isInteger() &&
11407 N->getOpcode() == ISD::SHL)
11408 return PerformSHLCombine(N, DAG);
11409
Nate Begeman740ab032009-01-26 00:52:55 +000011410 // On X86 with SSE2 support, we can transform this to a vector shift if
11411 // all elements are shifted by the same amount. We can't do this in legalize
11412 // because the a constant vector is typically transformed to a constant pool
11413 // so we have no knowledge of the shift amount.
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011414 if (!Subtarget->hasSSE2())
11415 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000011416
Owen Anderson825b72b2009-08-11 20:47:22 +000011417 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011418 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000011419
Mon P Wang3becd092009-01-28 08:12:05 +000011420 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000011421 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000011422 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000011423 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000011424 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
11425 unsigned NumElts = VT.getVectorNumElements();
11426 unsigned i = 0;
11427 for (; i != NumElts; ++i) {
11428 SDValue Arg = ShAmtOp.getOperand(i);
11429 if (Arg.getOpcode() == ISD::UNDEF) continue;
11430 BaseShAmt = Arg;
11431 break;
11432 }
11433 for (; i != NumElts; ++i) {
11434 SDValue Arg = ShAmtOp.getOperand(i);
11435 if (Arg.getOpcode() == ISD::UNDEF) continue;
11436 if (Arg != BaseShAmt) {
11437 return SDValue();
11438 }
11439 }
11440 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000011441 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000011442 SDValue InVec = ShAmtOp.getOperand(0);
11443 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
11444 unsigned NumElts = InVec.getValueType().getVectorNumElements();
11445 unsigned i = 0;
11446 for (; i != NumElts; ++i) {
11447 SDValue Arg = InVec.getOperand(i);
11448 if (Arg.getOpcode() == ISD::UNDEF) continue;
11449 BaseShAmt = Arg;
11450 break;
11451 }
11452 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
11453 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000011454 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000011455 if (C->getZExtValue() == SplatIdx)
11456 BaseShAmt = InVec.getOperand(1);
11457 }
11458 }
11459 if (BaseShAmt.getNode() == 0)
11460 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
11461 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +000011462 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011463 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000011464
Mon P Wangefa42202009-09-03 19:56:25 +000011465 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000011466 if (EltVT.bitsGT(MVT::i32))
11467 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
11468 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000011469 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000011470
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011471 // The shift amount is identical so we can do a vector shift.
11472 SDValue ValOp = N->getOperand(0);
11473 switch (N->getOpcode()) {
11474 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000011475 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011476 break;
11477 case ISD::SHL:
Owen Anderson825b72b2009-08-11 20:47:22 +000011478 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011479 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011480 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011481 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011482 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011483 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011484 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011485 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011486 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011487 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011488 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011489 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011490 break;
11491 case ISD::SRA:
Owen Anderson825b72b2009-08-11 20:47:22 +000011492 if (VT == MVT::v4i32)
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_psrai_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011495 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011496 if (VT == MVT::v8i16)
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_psrai_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011499 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011500 break;
11501 case ISD::SRL:
Owen Anderson825b72b2009-08-11 20:47:22 +000011502 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011503 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011504 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011505 ValOp, BaseShAmt);
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_psrli_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_psrli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011513 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011514 break;
Nate Begeman740ab032009-01-26 00:52:55 +000011515 }
11516 return SDValue();
11517}
11518
Nate Begemanb65c1752010-12-17 22:55:37 +000011519
11520static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
11521 TargetLowering::DAGCombinerInfo &DCI,
11522 const X86Subtarget *Subtarget) {
11523 if (DCI.isBeforeLegalizeOps())
11524 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011525
Nate Begemanb65c1752010-12-17 22:55:37 +000011526 // Want to form PANDN nodes, in the hopes of then easily combining them with
11527 // OR and AND nodes to form PBLEND/PSIGN.
11528 EVT VT = N->getValueType(0);
11529 if (VT != MVT::v2i64)
11530 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011531
Nate Begemanb65c1752010-12-17 22:55:37 +000011532 SDValue N0 = N->getOperand(0);
11533 SDValue N1 = N->getOperand(1);
11534 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011535
Nate Begemanb65c1752010-12-17 22:55:37 +000011536 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011537 if (N0.getOpcode() == ISD::XOR &&
Nate Begemanb65c1752010-12-17 22:55:37 +000011538 ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
11539 return DAG.getNode(X86ISD::PANDN, DL, VT, N0.getOperand(0), N1);
11540
11541 // Check RHS for vnot
11542 if (N1.getOpcode() == ISD::XOR &&
11543 ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
11544 return DAG.getNode(X86ISD::PANDN, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011545
Nate Begemanb65c1752010-12-17 22:55:37 +000011546 return SDValue();
11547}
11548
Evan Cheng760d1942010-01-04 21:22:48 +000011549static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000011550 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000011551 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +000011552 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000011553 return SDValue();
11554
Evan Cheng760d1942010-01-04 21:22:48 +000011555 EVT VT = N->getValueType(0);
Nate Begemanb65c1752010-12-17 22:55:37 +000011556 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64 && VT != MVT::v2i64)
Evan Cheng760d1942010-01-04 21:22:48 +000011557 return SDValue();
11558
Evan Cheng760d1942010-01-04 21:22:48 +000011559 SDValue N0 = N->getOperand(0);
11560 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011561
Nate Begemanb65c1752010-12-17 22:55:37 +000011562 // look for psign/blend
11563 if (Subtarget->hasSSSE3()) {
11564 if (VT == MVT::v2i64) {
11565 // Canonicalize pandn to RHS
11566 if (N0.getOpcode() == X86ISD::PANDN)
11567 std::swap(N0, N1);
11568 // or (and (m, x), (pandn m, y))
11569 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::PANDN) {
11570 SDValue Mask = N1.getOperand(0);
11571 SDValue X = N1.getOperand(1);
11572 SDValue Y;
11573 if (N0.getOperand(0) == Mask)
11574 Y = N0.getOperand(1);
11575 if (N0.getOperand(1) == Mask)
11576 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011577
Nate Begemanb65c1752010-12-17 22:55:37 +000011578 // Check to see if the mask appeared in both the AND and PANDN and
11579 if (!Y.getNode())
11580 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011581
Nate Begemanb65c1752010-12-17 22:55:37 +000011582 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
11583 if (Mask.getOpcode() != ISD::BITCAST ||
11584 X.getOpcode() != ISD::BITCAST ||
11585 Y.getOpcode() != ISD::BITCAST)
11586 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011587
Nate Begemanb65c1752010-12-17 22:55:37 +000011588 // Look through mask bitcast.
11589 Mask = Mask.getOperand(0);
11590 EVT MaskVT = Mask.getValueType();
11591
11592 // Validate that the Mask operand is a vector sra node. The sra node
11593 // will be an intrinsic.
11594 if (Mask.getOpcode() != ISD::INTRINSIC_WO_CHAIN)
11595 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011596
Nate Begemanb65c1752010-12-17 22:55:37 +000011597 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
11598 // there is no psrai.b
11599 switch (cast<ConstantSDNode>(Mask.getOperand(0))->getZExtValue()) {
11600 case Intrinsic::x86_sse2_psrai_w:
11601 case Intrinsic::x86_sse2_psrai_d:
11602 break;
11603 default: return SDValue();
11604 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011605
Nate Begemanb65c1752010-12-17 22:55:37 +000011606 // Check that the SRA is all signbits.
11607 SDValue SraC = Mask.getOperand(2);
11608 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
11609 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
11610 if ((SraAmt + 1) != EltBits)
11611 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011612
Nate Begemanb65c1752010-12-17 22:55:37 +000011613 DebugLoc DL = N->getDebugLoc();
11614
11615 // Now we know we at least have a plendvb with the mask val. See if
11616 // we can form a psignb/w/d.
11617 // psign = x.type == y.type == mask.type && y = sub(0, x);
11618 X = X.getOperand(0);
11619 Y = Y.getOperand(0);
11620 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
11621 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
11622 X.getValueType() == MaskVT && X.getValueType() == Y.getValueType()){
11623 unsigned Opc = 0;
11624 switch (EltBits) {
11625 case 8: Opc = X86ISD::PSIGNB; break;
11626 case 16: Opc = X86ISD::PSIGNW; break;
11627 case 32: Opc = X86ISD::PSIGND; break;
11628 default: break;
11629 }
11630 if (Opc) {
11631 SDValue Sign = DAG.getNode(Opc, DL, MaskVT, X, Mask.getOperand(1));
11632 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Sign);
11633 }
11634 }
11635 // PBLENDVB only available on SSE 4.1
11636 if (!Subtarget->hasSSE41())
11637 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011638
Nate Begemanb65c1752010-12-17 22:55:37 +000011639 X = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, X);
11640 Y = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Y);
11641 Mask = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Mask);
Nate Begeman672fb622010-12-20 22:04:24 +000011642 Mask = DAG.getNode(X86ISD::PBLENDVB, DL, MVT::v16i8, X, Y, Mask);
Nate Begemanb65c1752010-12-17 22:55:37 +000011643 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Mask);
11644 }
11645 }
11646 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011647
Nate Begemanb65c1752010-12-17 22:55:37 +000011648 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000011649 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
11650 std::swap(N0, N1);
11651 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
11652 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000011653 if (!N0.hasOneUse() || !N1.hasOneUse())
11654 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000011655
11656 SDValue ShAmt0 = N0.getOperand(1);
11657 if (ShAmt0.getValueType() != MVT::i8)
11658 return SDValue();
11659 SDValue ShAmt1 = N1.getOperand(1);
11660 if (ShAmt1.getValueType() != MVT::i8)
11661 return SDValue();
11662 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
11663 ShAmt0 = ShAmt0.getOperand(0);
11664 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
11665 ShAmt1 = ShAmt1.getOperand(0);
11666
11667 DebugLoc DL = N->getDebugLoc();
11668 unsigned Opc = X86ISD::SHLD;
11669 SDValue Op0 = N0.getOperand(0);
11670 SDValue Op1 = N1.getOperand(0);
11671 if (ShAmt0.getOpcode() == ISD::SUB) {
11672 Opc = X86ISD::SHRD;
11673 std::swap(Op0, Op1);
11674 std::swap(ShAmt0, ShAmt1);
11675 }
11676
Evan Cheng8b1190a2010-04-28 01:18:01 +000011677 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000011678 if (ShAmt1.getOpcode() == ISD::SUB) {
11679 SDValue Sum = ShAmt1.getOperand(0);
11680 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000011681 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
11682 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
11683 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
11684 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000011685 return DAG.getNode(Opc, DL, VT,
11686 Op0, Op1,
11687 DAG.getNode(ISD::TRUNCATE, DL,
11688 MVT::i8, ShAmt0));
11689 }
11690 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
11691 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
11692 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000011693 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000011694 return DAG.getNode(Opc, DL, VT,
11695 N0.getOperand(0), N1.getOperand(0),
11696 DAG.getNode(ISD::TRUNCATE, DL,
11697 MVT::i8, ShAmt0));
11698 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011699
Evan Cheng760d1942010-01-04 21:22:48 +000011700 return SDValue();
11701}
11702
Chris Lattner149a4e52008-02-22 02:09:43 +000011703/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000011704static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000011705 const X86Subtarget *Subtarget) {
Chris Lattner149a4e52008-02-22 02:09:43 +000011706 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
11707 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000011708 // A preferable solution to the general problem is to figure out the right
11709 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000011710
11711 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng7e2ff772008-05-08 00:57:18 +000011712 StoreSDNode *St = cast<StoreSDNode>(N);
Owen Andersone50ed302009-08-10 22:56:29 +000011713 EVT VT = St->getValue().getValueType();
Evan Cheng536e6672009-03-12 05:59:15 +000011714 if (VT.getSizeInBits() != 64)
11715 return SDValue();
11716
Devang Patel578efa92009-06-05 21:57:13 +000011717 const Function *F = DAG.getMachineFunction().getFunction();
11718 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Eric Christopherfd179292009-08-27 18:07:15 +000011719 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
Devang Patel578efa92009-06-05 21:57:13 +000011720 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +000011721 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000011722 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000011723 isa<LoadSDNode>(St->getValue()) &&
11724 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
11725 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000011726 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011727 LoadSDNode *Ld = 0;
11728 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000011729 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000011730 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011731 // Must be a store of a load. We currently handle two cases: the load
11732 // is a direct child, and it's under an intervening TokenFactor. It is
11733 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000011734 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000011735 Ld = cast<LoadSDNode>(St->getChain());
11736 else if (St->getValue().hasOneUse() &&
11737 ChainVal->getOpcode() == ISD::TokenFactor) {
11738 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000011739 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000011740 TokenFactorIndex = i;
11741 Ld = cast<LoadSDNode>(St->getValue());
11742 } else
11743 Ops.push_back(ChainVal->getOperand(i));
11744 }
11745 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000011746
Evan Cheng536e6672009-03-12 05:59:15 +000011747 if (!Ld || !ISD::isNormalLoad(Ld))
11748 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011749
Evan Cheng536e6672009-03-12 05:59:15 +000011750 // If this is not the MMX case, i.e. we are just turning i64 load/store
11751 // into f64 load/store, avoid the transformation if there are multiple
11752 // uses of the loaded value.
11753 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
11754 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011755
Evan Cheng536e6672009-03-12 05:59:15 +000011756 DebugLoc LdDL = Ld->getDebugLoc();
11757 DebugLoc StDL = N->getDebugLoc();
11758 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
11759 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
11760 // pair instead.
11761 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000011762 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000011763 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
11764 Ld->getPointerInfo(), Ld->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000011765 Ld->isNonTemporal(), Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000011766 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000011767 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000011768 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000011769 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000011770 Ops.size());
11771 }
Evan Cheng536e6672009-03-12 05:59:15 +000011772 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000011773 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000011774 St->isVolatile(), St->isNonTemporal(),
11775 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000011776 }
Evan Cheng536e6672009-03-12 05:59:15 +000011777
11778 // Otherwise, lower to two pairs of 32-bit loads / stores.
11779 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000011780 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
11781 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000011782
Owen Anderson825b72b2009-08-11 20:47:22 +000011783 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000011784 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000011785 Ld->isVolatile(), Ld->isNonTemporal(),
11786 Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000011787 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000011788 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000011789 Ld->isVolatile(), Ld->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000011790 MinAlign(Ld->getAlignment(), 4));
11791
11792 SDValue NewChain = LoLd.getValue(1);
11793 if (TokenFactorIndex != -1) {
11794 Ops.push_back(LoLd);
11795 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000011796 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000011797 Ops.size());
11798 }
11799
11800 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000011801 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
11802 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000011803
11804 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000011805 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000011806 St->isVolatile(), St->isNonTemporal(),
11807 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000011808 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000011809 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000011810 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000011811 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000011812 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000011813 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000011814 }
Dan Gohman475871a2008-07-27 21:46:04 +000011815 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000011816}
11817
Chris Lattner6cf73262008-01-25 06:14:17 +000011818/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
11819/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000011820static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000011821 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
11822 // F[X]OR(0.0, x) -> x
11823 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000011824 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
11825 if (C->getValueAPF().isPosZero())
11826 return N->getOperand(1);
11827 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
11828 if (C->getValueAPF().isPosZero())
11829 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000011830 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000011831}
11832
11833/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000011834static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000011835 // FAND(0.0, x) -> 0.0
11836 // FAND(x, 0.0) -> 0.0
11837 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
11838 if (C->getValueAPF().isPosZero())
11839 return N->getOperand(0);
11840 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
11841 if (C->getValueAPF().isPosZero())
11842 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000011843 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000011844}
11845
Dan Gohmane5af2d32009-01-29 01:59:02 +000011846static SDValue PerformBTCombine(SDNode *N,
11847 SelectionDAG &DAG,
11848 TargetLowering::DAGCombinerInfo &DCI) {
11849 // BT ignores high bits in the bit index operand.
11850 SDValue Op1 = N->getOperand(1);
11851 if (Op1.hasOneUse()) {
11852 unsigned BitWidth = Op1.getValueSizeInBits();
11853 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
11854 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000011855 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
11856 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000011857 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000011858 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
11859 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
11860 DCI.CommitTargetLoweringOpt(TLO);
11861 }
11862 return SDValue();
11863}
Chris Lattner83e6c992006-10-04 06:57:07 +000011864
Eli Friedman7a5e5552009-06-07 06:52:44 +000011865static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
11866 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011867 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000011868 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000011869 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000011870 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000011871 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000011872 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011873 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000011874 }
11875 return SDValue();
11876}
11877
Evan Cheng2e489c42009-12-16 00:53:11 +000011878static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
11879 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
11880 // (and (i32 x86isd::setcc_carry), 1)
11881 // This eliminates the zext. This transformation is necessary because
11882 // ISD::SETCC is always legalized to i8.
11883 DebugLoc dl = N->getDebugLoc();
11884 SDValue N0 = N->getOperand(0);
11885 EVT VT = N->getValueType(0);
11886 if (N0.getOpcode() == ISD::AND &&
11887 N0.hasOneUse() &&
11888 N0.getOperand(0).hasOneUse()) {
11889 SDValue N00 = N0.getOperand(0);
11890 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
11891 return SDValue();
11892 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
11893 if (!C || C->getZExtValue() != 1)
11894 return SDValue();
11895 return DAG.getNode(ISD::AND, dl, VT,
11896 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
11897 N00.getOperand(0), N00.getOperand(1)),
11898 DAG.getConstant(1, VT));
11899 }
11900
11901 return SDValue();
11902}
11903
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011904// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
11905static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG) {
11906 unsigned X86CC = N->getConstantOperandVal(0);
11907 SDValue EFLAG = N->getOperand(1);
11908 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011909
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011910 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
11911 // a zext and produces an all-ones bit which is more useful than 0/1 in some
11912 // cases.
11913 if (X86CC == X86::COND_B)
11914 return DAG.getNode(ISD::AND, DL, MVT::i8,
11915 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
11916 DAG.getConstant(X86CC, MVT::i8), EFLAG),
11917 DAG.getConstant(1, MVT::i8));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011918
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011919 return SDValue();
11920}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011921
Chris Lattner23a01992010-12-20 01:37:09 +000011922// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
11923static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
11924 X86TargetLowering::DAGCombinerInfo &DCI) {
11925 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
11926 // the result is either zero or one (depending on the input carry bit).
11927 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
11928 if (X86::isZeroNode(N->getOperand(0)) &&
11929 X86::isZeroNode(N->getOperand(1)) &&
11930 // We don't have a good way to replace an EFLAGS use, so only do this when
11931 // dead right now.
11932 SDValue(N, 1).use_empty()) {
11933 DebugLoc DL = N->getDebugLoc();
11934 EVT VT = N->getValueType(0);
11935 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
11936 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
11937 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
11938 DAG.getConstant(X86::COND_B,MVT::i8),
11939 N->getOperand(2)),
11940 DAG.getConstant(1, VT));
11941 return DCI.CombineTo(N, Res1, CarryOut);
11942 }
11943
11944 return SDValue();
11945}
11946
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000011947// fold (add Y, (sete X, 0)) -> adc 0, Y
11948// (add Y, (setne X, 0)) -> sbb -1, Y
11949// (sub (sete X, 0), Y) -> sbb 0, Y
11950// (sub (setne X, 0), Y) -> adc -1, Y
11951static SDValue OptimizeConditonalInDecrement(SDNode *N, SelectionDAG &DAG) {
11952 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011953
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000011954 // Look through ZExts.
11955 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
11956 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
11957 return SDValue();
11958
11959 SDValue SetCC = Ext.getOperand(0);
11960 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
11961 return SDValue();
11962
11963 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
11964 if (CC != X86::COND_E && CC != X86::COND_NE)
11965 return SDValue();
11966
11967 SDValue Cmp = SetCC.getOperand(1);
11968 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000011969 !X86::isZeroNode(Cmp.getOperand(1)) ||
11970 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000011971 return SDValue();
11972
11973 SDValue CmpOp0 = Cmp.getOperand(0);
11974 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
11975 DAG.getConstant(1, CmpOp0.getValueType()));
11976
11977 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
11978 if (CC == X86::COND_NE)
11979 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
11980 DL, OtherVal.getValueType(), OtherVal,
11981 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
11982 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
11983 DL, OtherVal.getValueType(), OtherVal,
11984 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
11985}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011986
Dan Gohman475871a2008-07-27 21:46:04 +000011987SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000011988 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000011989 SelectionDAG &DAG = DCI.DAG;
11990 switch (N->getOpcode()) {
11991 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000011992 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011993 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, *this);
Chris Lattneraf723b92008-01-25 05:46:26 +000011994 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +000011995 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000011996 case ISD::ADD:
11997 case ISD::SUB: return OptimizeConditonalInDecrement(N, DAG);
Chris Lattner23a01992010-12-20 01:37:09 +000011998 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000011999 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000012000 case ISD::SHL:
12001 case ISD::SRA:
12002 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000012003 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000012004 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000012005 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +000012006 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000012007 case X86ISD::FOR: return PerformFORCombine(N, DAG);
12008 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000012009 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000012010 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Evan Cheng2e489c42009-12-16 00:53:11 +000012011 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG);
Chris Lattnerc19d1c32010-12-19 22:08:31 +000012012 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000012013 case X86ISD::SHUFPS: // Handle all target specific shuffles
12014 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000012015 case X86ISD::PALIGN:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000012016 case X86ISD::PUNPCKHBW:
12017 case X86ISD::PUNPCKHWD:
12018 case X86ISD::PUNPCKHDQ:
12019 case X86ISD::PUNPCKHQDQ:
12020 case X86ISD::UNPCKHPS:
12021 case X86ISD::UNPCKHPD:
12022 case X86ISD::PUNPCKLBW:
12023 case X86ISD::PUNPCKLWD:
12024 case X86ISD::PUNPCKLDQ:
12025 case X86ISD::PUNPCKLQDQ:
12026 case X86ISD::UNPCKLPS:
12027 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +000012028 case X86ISD::VUNPCKLPS:
12029 case X86ISD::VUNPCKLPD:
12030 case X86ISD::VUNPCKLPSY:
12031 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000012032 case X86ISD::MOVHLPS:
12033 case X86ISD::MOVLHPS:
12034 case X86ISD::PSHUFD:
12035 case X86ISD::PSHUFHW:
12036 case X86ISD::PSHUFLW:
12037 case X86ISD::MOVSS:
12038 case X86ISD::MOVSD:
Mon P Wanga0fd0d52010-12-19 23:55:53 +000012039 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI);
Evan Cheng206ee9d2006-07-07 08:33:52 +000012040 }
12041
Dan Gohman475871a2008-07-27 21:46:04 +000012042 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000012043}
12044
Evan Chenge5b51ac2010-04-17 06:13:15 +000012045/// isTypeDesirableForOp - Return true if the target has native support for
12046/// the specified value type and it is 'desirable' to use the type for the
12047/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
12048/// instruction encodings are longer and some i16 instructions are slow.
12049bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
12050 if (!isTypeLegal(VT))
12051 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000012052 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000012053 return true;
12054
12055 switch (Opc) {
12056 default:
12057 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000012058 case ISD::LOAD:
12059 case ISD::SIGN_EXTEND:
12060 case ISD::ZERO_EXTEND:
12061 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000012062 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000012063 case ISD::SRL:
12064 case ISD::SUB:
12065 case ISD::ADD:
12066 case ISD::MUL:
12067 case ISD::AND:
12068 case ISD::OR:
12069 case ISD::XOR:
12070 return false;
12071 }
12072}
12073
12074/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000012075/// beneficial for dag combiner to promote the specified node. If true, it
12076/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000012077bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000012078 EVT VT = Op.getValueType();
12079 if (VT != MVT::i16)
12080 return false;
12081
Evan Cheng4c26e932010-04-19 19:29:22 +000012082 bool Promote = false;
12083 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000012084 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000012085 default: break;
12086 case ISD::LOAD: {
12087 LoadSDNode *LD = cast<LoadSDNode>(Op);
12088 // If the non-extending load has a single use and it's not live out, then it
12089 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000012090 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
12091 Op.hasOneUse()*/) {
12092 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
12093 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
12094 // The only case where we'd want to promote LOAD (rather then it being
12095 // promoted as an operand is when it's only use is liveout.
12096 if (UI->getOpcode() != ISD::CopyToReg)
12097 return false;
12098 }
12099 }
Evan Cheng4c26e932010-04-19 19:29:22 +000012100 Promote = true;
12101 break;
12102 }
12103 case ISD::SIGN_EXTEND:
12104 case ISD::ZERO_EXTEND:
12105 case ISD::ANY_EXTEND:
12106 Promote = true;
12107 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000012108 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000012109 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000012110 SDValue N0 = Op.getOperand(0);
12111 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000012112 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000012113 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000012114 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000012115 break;
12116 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000012117 case ISD::ADD:
12118 case ISD::MUL:
12119 case ISD::AND:
12120 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000012121 case ISD::XOR:
12122 Commute = true;
12123 // fallthrough
12124 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000012125 SDValue N0 = Op.getOperand(0);
12126 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000012127 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000012128 return false;
12129 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000012130 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000012131 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000012132 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000012133 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000012134 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000012135 }
12136 }
12137
12138 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000012139 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000012140}
12141
Evan Cheng60c07e12006-07-05 22:17:51 +000012142//===----------------------------------------------------------------------===//
12143// X86 Inline Assembly Support
12144//===----------------------------------------------------------------------===//
12145
Chris Lattnerb8105652009-07-20 17:51:36 +000012146bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
12147 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000012148
12149 std::string AsmStr = IA->getAsmString();
12150
12151 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000012152 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000012153 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000012154
12155 switch (AsmPieces.size()) {
12156 default: return false;
12157 case 1:
12158 AsmStr = AsmPieces[0];
12159 AsmPieces.clear();
12160 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
12161
Evan Cheng55d42002011-01-08 01:24:27 +000012162 // FIXME: this should verify that we are targetting a 486 or better. If not,
12163 // we will turn this bswap into something that will be lowered to logical ops
12164 // instead of emitting the bswap asm. For now, we don't support 486 or lower
12165 // so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000012166 // bswap $0
12167 if (AsmPieces.size() == 2 &&
12168 (AsmPieces[0] == "bswap" ||
12169 AsmPieces[0] == "bswapq" ||
12170 AsmPieces[0] == "bswapl") &&
12171 (AsmPieces[1] == "$0" ||
12172 AsmPieces[1] == "${0:q}")) {
12173 // No need to check constraints, nothing other than the equivalent of
12174 // "=r,0" would be valid here.
Evan Cheng55d42002011-01-08 01:24:27 +000012175 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
12176 if (!Ty || Ty->getBitWidth() % 16 != 0)
12177 return false;
12178 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000012179 }
12180 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000012181 if (CI->getType()->isIntegerTy(16) &&
Chris Lattnerb8105652009-07-20 17:51:36 +000012182 AsmPieces.size() == 3 &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000012183 (AsmPieces[0] == "rorw" || AsmPieces[0] == "rolw") &&
Chris Lattnerb8105652009-07-20 17:51:36 +000012184 AsmPieces[1] == "$$8," &&
12185 AsmPieces[2] == "${0:w}" &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000012186 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
12187 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000012188 const std::string &ConstraintsStr = IA->getConstraintString();
12189 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000012190 std::sort(AsmPieces.begin(), AsmPieces.end());
12191 if (AsmPieces.size() == 4 &&
12192 AsmPieces[0] == "~{cc}" &&
12193 AsmPieces[1] == "~{dirflag}" &&
12194 AsmPieces[2] == "~{flags}" &&
12195 AsmPieces[3] == "~{fpsr}") {
Evan Cheng55d42002011-01-08 01:24:27 +000012196 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
12197 if (!Ty || Ty->getBitWidth() % 16 != 0)
12198 return false;
12199 return IntrinsicLowering::LowerToByteSwap(CI);
Dan Gohman0ef701e2010-03-04 19:58:08 +000012200 }
Chris Lattnerb8105652009-07-20 17:51:36 +000012201 }
12202 break;
12203 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000012204 if (CI->getType()->isIntegerTy(32) &&
12205 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
12206 SmallVector<StringRef, 4> Words;
12207 SplitString(AsmPieces[0], Words, " \t,");
12208 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
12209 Words[2] == "${0:w}") {
12210 Words.clear();
12211 SplitString(AsmPieces[1], Words, " \t,");
12212 if (Words.size() == 3 && Words[0] == "rorl" && Words[1] == "$$16" &&
12213 Words[2] == "$0") {
12214 Words.clear();
12215 SplitString(AsmPieces[2], Words, " \t,");
12216 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
12217 Words[2] == "${0:w}") {
12218 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000012219 const std::string &ConstraintsStr = IA->getConstraintString();
12220 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Peter Collingbourne948cf022010-11-13 19:54:30 +000012221 std::sort(AsmPieces.begin(), AsmPieces.end());
12222 if (AsmPieces.size() == 4 &&
12223 AsmPieces[0] == "~{cc}" &&
12224 AsmPieces[1] == "~{dirflag}" &&
12225 AsmPieces[2] == "~{flags}" &&
12226 AsmPieces[3] == "~{fpsr}") {
Evan Cheng55d42002011-01-08 01:24:27 +000012227 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
12228 if (!Ty || Ty->getBitWidth() % 16 != 0)
12229 return false;
12230 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000012231 }
12232 }
12233 }
12234 }
12235 }
Evan Cheng55d42002011-01-08 01:24:27 +000012236
12237 if (CI->getType()->isIntegerTy(64)) {
12238 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
12239 if (Constraints.size() >= 2 &&
12240 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
12241 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
12242 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
12243 SmallVector<StringRef, 4> Words;
12244 SplitString(AsmPieces[0], Words, " \t");
12245 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
Chris Lattnerb8105652009-07-20 17:51:36 +000012246 Words.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000012247 SplitString(AsmPieces[1], Words, " \t");
12248 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
12249 Words.clear();
12250 SplitString(AsmPieces[2], Words, " \t,");
12251 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
12252 Words[2] == "%edx") {
12253 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
12254 if (!Ty || Ty->getBitWidth() % 16 != 0)
12255 return false;
12256 return IntrinsicLowering::LowerToByteSwap(CI);
12257 }
Chris Lattnerb8105652009-07-20 17:51:36 +000012258 }
12259 }
12260 }
12261 }
12262 break;
12263 }
12264 return false;
12265}
12266
12267
12268
Chris Lattnerf4dff842006-07-11 02:54:03 +000012269/// getConstraintType - Given a constraint letter, return the type of
12270/// constraint it is for this target.
12271X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000012272X86TargetLowering::getConstraintType(const std::string &Constraint) const {
12273 if (Constraint.size() == 1) {
12274 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000012275 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000012276 case 'q':
12277 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000012278 case 'f':
12279 case 't':
12280 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000012281 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000012282 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000012283 case 'Y':
12284 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000012285 case 'a':
12286 case 'b':
12287 case 'c':
12288 case 'd':
12289 case 'S':
12290 case 'D':
12291 case 'A':
12292 return C_Register;
12293 case 'I':
12294 case 'J':
12295 case 'K':
12296 case 'L':
12297 case 'M':
12298 case 'N':
12299 case 'G':
12300 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000012301 case 'e':
12302 case 'Z':
12303 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000012304 default:
12305 break;
12306 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000012307 }
Chris Lattner4234f572007-03-25 02:14:49 +000012308 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000012309}
12310
John Thompson44ab89e2010-10-29 17:29:13 +000012311/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000012312/// This object must already have been set up with the operand type
12313/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000012314TargetLowering::ConstraintWeight
12315 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000012316 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000012317 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012318 Value *CallOperandVal = info.CallOperandVal;
12319 // If we don't have a value, we can't do a match,
12320 // but allow it at the lowest weight.
12321 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000012322 return CW_Default;
12323 const Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000012324 // Look at the constraint type.
12325 switch (*constraint) {
12326 default:
John Thompson44ab89e2010-10-29 17:29:13 +000012327 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
12328 case 'R':
12329 case 'q':
12330 case 'Q':
12331 case 'a':
12332 case 'b':
12333 case 'c':
12334 case 'd':
12335 case 'S':
12336 case 'D':
12337 case 'A':
12338 if (CallOperandVal->getType()->isIntegerTy())
12339 weight = CW_SpecificReg;
12340 break;
12341 case 'f':
12342 case 't':
12343 case 'u':
12344 if (type->isFloatingPointTy())
12345 weight = CW_SpecificReg;
12346 break;
12347 case 'y':
Chris Lattner2a786eb2010-12-19 20:19:20 +000012348 if (type->isX86_MMXTy() && Subtarget->hasMMX())
John Thompson44ab89e2010-10-29 17:29:13 +000012349 weight = CW_SpecificReg;
12350 break;
12351 case 'x':
12352 case 'Y':
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012353 if ((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasXMM())
John Thompson44ab89e2010-10-29 17:29:13 +000012354 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012355 break;
12356 case 'I':
12357 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
12358 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000012359 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012360 }
12361 break;
John Thompson44ab89e2010-10-29 17:29:13 +000012362 case 'J':
12363 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12364 if (C->getZExtValue() <= 63)
12365 weight = CW_Constant;
12366 }
12367 break;
12368 case 'K':
12369 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12370 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
12371 weight = CW_Constant;
12372 }
12373 break;
12374 case 'L':
12375 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12376 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
12377 weight = CW_Constant;
12378 }
12379 break;
12380 case 'M':
12381 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12382 if (C->getZExtValue() <= 3)
12383 weight = CW_Constant;
12384 }
12385 break;
12386 case 'N':
12387 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12388 if (C->getZExtValue() <= 0xff)
12389 weight = CW_Constant;
12390 }
12391 break;
12392 case 'G':
12393 case 'C':
12394 if (dyn_cast<ConstantFP>(CallOperandVal)) {
12395 weight = CW_Constant;
12396 }
12397 break;
12398 case 'e':
12399 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12400 if ((C->getSExtValue() >= -0x80000000LL) &&
12401 (C->getSExtValue() <= 0x7fffffffLL))
12402 weight = CW_Constant;
12403 }
12404 break;
12405 case 'Z':
12406 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12407 if (C->getZExtValue() <= 0xffffffff)
12408 weight = CW_Constant;
12409 }
12410 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012411 }
12412 return weight;
12413}
12414
Dale Johannesenba2a0b92008-01-29 02:21:21 +000012415/// LowerXConstraint - try to replace an X constraint, which matches anything,
12416/// with another that has more specific requirements based on the type of the
12417/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000012418const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000012419LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000012420 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
12421 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000012422 if (ConstraintVT.isFloatingPoint()) {
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012423 if (Subtarget->hasXMMInt())
Chris Lattner5e764232008-04-26 23:02:14 +000012424 return "Y";
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012425 if (Subtarget->hasXMM())
Chris Lattner5e764232008-04-26 23:02:14 +000012426 return "x";
12427 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012428
Chris Lattner5e764232008-04-26 23:02:14 +000012429 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000012430}
12431
Chris Lattner48884cd2007-08-25 00:47:38 +000012432/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
12433/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000012434void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattner48884cd2007-08-25 00:47:38 +000012435 char Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000012436 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000012437 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000012438 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000012439
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012440 switch (Constraint) {
12441 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000012442 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000012443 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000012444 if (C->getZExtValue() <= 31) {
12445 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000012446 break;
12447 }
Devang Patel84f7fd22007-03-17 00:13:28 +000012448 }
Chris Lattner48884cd2007-08-25 00:47:38 +000012449 return;
Evan Cheng364091e2008-09-22 23:57:37 +000012450 case 'J':
12451 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000012452 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000012453 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
12454 break;
12455 }
12456 }
12457 return;
12458 case 'K':
12459 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000012460 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000012461 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
12462 break;
12463 }
12464 }
12465 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000012466 case 'N':
12467 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000012468 if (C->getZExtValue() <= 255) {
12469 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000012470 break;
12471 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000012472 }
Chris Lattner48884cd2007-08-25 00:47:38 +000012473 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000012474 case 'e': {
12475 // 32-bit signed value
12476 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000012477 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
12478 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000012479 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000012480 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000012481 break;
12482 }
12483 // FIXME gcc accepts some relocatable values here too, but only in certain
12484 // memory models; it's complicated.
12485 }
12486 return;
12487 }
12488 case 'Z': {
12489 // 32-bit unsigned 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->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000012493 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
12494 break;
12495 }
12496 }
12497 // FIXME gcc accepts some relocatable values here too, but only in certain
12498 // memory models; it's complicated.
12499 return;
12500 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012501 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012502 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000012503 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000012504 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000012505 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000012506 break;
12507 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012508
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000012509 // In any sort of PIC mode addresses need to be computed at runtime by
12510 // adding in a register or some sort of table lookup. These can't
12511 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000012512 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000012513 return;
12514
Chris Lattnerdc43a882007-05-03 16:52:29 +000012515 // If we are in non-pic codegen mode, we allow the address of a global (with
12516 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000012517 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000012518 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000012519
Chris Lattner49921962009-05-08 18:23:14 +000012520 // Match either (GA), (GA+C), (GA+C1+C2), etc.
12521 while (1) {
12522 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
12523 Offset += GA->getOffset();
12524 break;
12525 } else if (Op.getOpcode() == ISD::ADD) {
12526 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
12527 Offset += C->getZExtValue();
12528 Op = Op.getOperand(0);
12529 continue;
12530 }
12531 } else if (Op.getOpcode() == ISD::SUB) {
12532 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
12533 Offset += -C->getZExtValue();
12534 Op = Op.getOperand(0);
12535 continue;
12536 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012537 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012538
Chris Lattner49921962009-05-08 18:23:14 +000012539 // Otherwise, this isn't something we can handle, reject it.
12540 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000012541 }
Eric Christopherfd179292009-08-27 18:07:15 +000012542
Dan Gohman46510a72010-04-15 01:51:59 +000012543 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012544 // If we require an extra load to get this address, as in PIC mode, we
12545 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000012546 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
12547 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012548 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000012549
Devang Patel0d881da2010-07-06 22:08:15 +000012550 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
12551 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000012552 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012553 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012554 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012555
Gabor Greifba36cb52008-08-28 21:40:38 +000012556 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000012557 Ops.push_back(Result);
12558 return;
12559 }
Dale Johannesen1784d162010-06-25 21:55:36 +000012560 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012561}
12562
Chris Lattner259e97c2006-01-31 19:43:35 +000012563std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +000012564getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000012565 EVT VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +000012566 if (Constraint.size() == 1) {
12567 // FIXME: not handling fp-stack yet!
Chris Lattner259e97c2006-01-31 19:43:35 +000012568 switch (Constraint[0]) { // GCC X86 Constraint Letters
Chris Lattnerf4dff842006-07-11 02:54:03 +000012569 default: break; // Unknown constraint letter
Evan Cheng47e9fab2009-07-17 22:13:25 +000012570 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
12571 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000012572 if (VT == MVT::i32)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012573 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
12574 X86::ESI, X86::EDI, X86::R8D, X86::R9D,
12575 X86::R10D,X86::R11D,X86::R12D,
12576 X86::R13D,X86::R14D,X86::R15D,
12577 X86::EBP, X86::ESP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012578 else if (VT == MVT::i16)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012579 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
12580 X86::SI, X86::DI, X86::R8W,X86::R9W,
12581 X86::R10W,X86::R11W,X86::R12W,
12582 X86::R13W,X86::R14W,X86::R15W,
12583 X86::BP, X86::SP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012584 else if (VT == MVT::i8)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012585 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL,
12586 X86::SIL, X86::DIL, X86::R8B,X86::R9B,
12587 X86::R10B,X86::R11B,X86::R12B,
12588 X86::R13B,X86::R14B,X86::R15B,
12589 X86::BPL, X86::SPL, 0);
12590
Owen Anderson825b72b2009-08-11 20:47:22 +000012591 else if (VT == MVT::i64)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012592 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX,
12593 X86::RSI, X86::RDI, X86::R8, X86::R9,
12594 X86::R10, X86::R11, X86::R12,
12595 X86::R13, X86::R14, X86::R15,
12596 X86::RBP, X86::RSP, 0);
12597
12598 break;
12599 }
Eric Christopherfd179292009-08-27 18:07:15 +000012600 // 32-bit fallthrough
Chris Lattner259e97c2006-01-31 19:43:35 +000012601 case 'Q': // Q_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000012602 if (VT == MVT::i32)
Chris Lattner80a7ecc2006-05-06 00:29:37 +000012603 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012604 else if (VT == MVT::i16)
Chris Lattner80a7ecc2006-05-06 00:29:37 +000012605 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012606 else if (VT == MVT::i8)
Evan Cheng12914382007-08-13 23:27:11 +000012607 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012608 else if (VT == MVT::i64)
Chris Lattner03e6c702007-11-04 06:51:12 +000012609 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
12610 break;
Chris Lattner259e97c2006-01-31 19:43:35 +000012611 }
12612 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012613
Chris Lattner1efa40f2006-02-22 00:56:39 +000012614 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +000012615}
Chris Lattnerf76d1802006-07-31 23:26:50 +000012616
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012617std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000012618X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000012619 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000012620 // First, see if this is a constraint that directly corresponds to an LLVM
12621 // register class.
12622 if (Constraint.size() == 1) {
12623 // GCC Constraint Letters
12624 switch (Constraint[0]) {
12625 default: break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000012626 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000012627 case 'l': // INDEX_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000012628 if (VT == MVT::i8)
Chris Lattner0f65cad2007-04-09 05:49:22 +000012629 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012630 if (VT == MVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +000012631 return std::make_pair(0U, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012632 if (VT == MVT::i32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +000012633 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +000012634 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000012635 case 'R': // LEGACY_REGS
12636 if (VT == MVT::i8)
12637 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
12638 if (VT == MVT::i16)
12639 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
12640 if (VT == MVT::i32 || !Subtarget->is64Bit())
12641 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
12642 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000012643 case 'f': // FP Stack registers.
12644 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
12645 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000012646 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000012647 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012648 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000012649 return std::make_pair(0U, X86::RFP64RegisterClass);
12650 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000012651 case 'y': // MMX_REGS if MMX allowed.
12652 if (!Subtarget->hasMMX()) break;
12653 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000012654 case 'Y': // SSE_REGS if SSE2 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012655 if (!Subtarget->hasXMMInt()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000012656 // FALL THROUGH.
12657 case 'x': // SSE_REGS if SSE1 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012658 if (!Subtarget->hasXMM()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000012659
Owen Anderson825b72b2009-08-11 20:47:22 +000012660 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000012661 default: break;
12662 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000012663 case MVT::f32:
12664 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +000012665 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012666 case MVT::f64:
12667 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +000012668 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000012669 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000012670 case MVT::v16i8:
12671 case MVT::v8i16:
12672 case MVT::v4i32:
12673 case MVT::v2i64:
12674 case MVT::v4f32:
12675 case MVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +000012676 return std::make_pair(0U, X86::VR128RegisterClass);
12677 }
Chris Lattnerad043e82007-04-09 05:11:28 +000012678 break;
12679 }
12680 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012681
Chris Lattnerf76d1802006-07-31 23:26:50 +000012682 // Use the default implementation in TargetLowering to convert the register
12683 // constraint into a member of a register class.
12684 std::pair<unsigned, const TargetRegisterClass*> Res;
12685 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000012686
12687 // Not found as a standard register?
12688 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000012689 // Map st(0) -> st(7) -> ST0
12690 if (Constraint.size() == 7 && Constraint[0] == '{' &&
12691 tolower(Constraint[1]) == 's' &&
12692 tolower(Constraint[2]) == 't' &&
12693 Constraint[3] == '(' &&
12694 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
12695 Constraint[5] == ')' &&
12696 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000012697
Chris Lattner56d77c72009-09-13 22:41:48 +000012698 Res.first = X86::ST0+Constraint[4]-'0';
12699 Res.second = X86::RFP80RegisterClass;
12700 return Res;
12701 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000012702
Chris Lattner56d77c72009-09-13 22:41:48 +000012703 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000012704 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000012705 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +000012706 Res.second = X86::RFP80RegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000012707 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000012708 }
Chris Lattner56d77c72009-09-13 22:41:48 +000012709
12710 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000012711 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000012712 Res.first = X86::EFLAGS;
12713 Res.second = X86::CCRRegisterClass;
12714 return Res;
12715 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000012716
Dale Johannesen330169f2008-11-13 21:52:36 +000012717 // 'A' means EAX + EDX.
12718 if (Constraint == "A") {
12719 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +000012720 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000012721 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000012722 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000012723 return Res;
12724 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012725
Chris Lattnerf76d1802006-07-31 23:26:50 +000012726 // Otherwise, check to see if this is a register class of the wrong value
12727 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
12728 // turn into {ax},{dx}.
12729 if (Res.second->hasType(VT))
12730 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012731
Chris Lattnerf76d1802006-07-31 23:26:50 +000012732 // All of the single-register GCC register classes map their values onto
12733 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
12734 // really want an 8-bit or 32-bit register, map to the appropriate register
12735 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +000012736 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000012737 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000012738 unsigned DestReg = 0;
12739 switch (Res.first) {
12740 default: break;
12741 case X86::AX: DestReg = X86::AL; break;
12742 case X86::DX: DestReg = X86::DL; break;
12743 case X86::CX: DestReg = X86::CL; break;
12744 case X86::BX: DestReg = X86::BL; break;
12745 }
12746 if (DestReg) {
12747 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000012748 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000012749 }
Owen Anderson825b72b2009-08-11 20:47:22 +000012750 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000012751 unsigned DestReg = 0;
12752 switch (Res.first) {
12753 default: break;
12754 case X86::AX: DestReg = X86::EAX; break;
12755 case X86::DX: DestReg = X86::EDX; break;
12756 case X86::CX: DestReg = X86::ECX; break;
12757 case X86::BX: DestReg = X86::EBX; break;
12758 case X86::SI: DestReg = X86::ESI; break;
12759 case X86::DI: DestReg = X86::EDI; break;
12760 case X86::BP: DestReg = X86::EBP; break;
12761 case X86::SP: DestReg = X86::ESP; break;
12762 }
12763 if (DestReg) {
12764 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000012765 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000012766 }
Owen Anderson825b72b2009-08-11 20:47:22 +000012767 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000012768 unsigned DestReg = 0;
12769 switch (Res.first) {
12770 default: break;
12771 case X86::AX: DestReg = X86::RAX; break;
12772 case X86::DX: DestReg = X86::RDX; break;
12773 case X86::CX: DestReg = X86::RCX; break;
12774 case X86::BX: DestReg = X86::RBX; break;
12775 case X86::SI: DestReg = X86::RSI; break;
12776 case X86::DI: DestReg = X86::RDI; break;
12777 case X86::BP: DestReg = X86::RBP; break;
12778 case X86::SP: DestReg = X86::RSP; break;
12779 }
12780 if (DestReg) {
12781 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000012782 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000012783 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000012784 }
Chris Lattner6ba50a92008-08-26 06:19:02 +000012785 } else if (Res.second == X86::FR32RegisterClass ||
12786 Res.second == X86::FR64RegisterClass ||
12787 Res.second == X86::VR128RegisterClass) {
12788 // Handle references to XMM physical registers that got mapped into the
12789 // wrong class. This can happen with constraints like {xmm0} where the
12790 // target independent register mapper will just pick the first match it can
12791 // find, ignoring the required type.
Owen Anderson825b72b2009-08-11 20:47:22 +000012792 if (VT == MVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +000012793 Res.second = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +000012794 else if (VT == MVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +000012795 Res.second = X86::FR64RegisterClass;
12796 else if (X86::VR128RegisterClass->hasType(VT))
12797 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000012798 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012799
Chris Lattnerf76d1802006-07-31 23:26:50 +000012800 return Res;
12801}