blob: c1aa7200befcb43eec709ce8466cf99749da3651 [file] [log] [blame]
Arnold Schwaighofer92226dd2007-10-12 21:53:12 +00001//===-- X86ISelLowering.cpp - X86 DAG Lowering Implementation -------------===//
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that X86 uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
Evan Chengb1712452010-01-27 06:25:16 +000015#define DEBUG_TYPE "x86-isel"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000016#include "X86.h"
Evan Cheng0cc39452006-01-16 21:21:29 +000017#include "X86InstrBuilder.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000018#include "X86ISelLowering.h"
19#include "X86TargetMachine.h"
Chris Lattner8c6ed052009-09-16 01:46:41 +000020#include "X86TargetObjectFile.h"
David Greene583b68f2011-02-17 19:18:59 +000021#include "Utils/X86ShuffleDecode.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000022#include "llvm/CallingConv.h"
Evan Cheng223547a2006-01-31 22:28:30 +000023#include "llvm/Constants.h"
Evan Cheng347d5f72006-04-28 21:29:37 +000024#include "llvm/DerivedTypes.h"
Chris Lattnerb903bed2009-06-26 21:20:29 +000025#include "llvm/GlobalAlias.h"
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000026#include "llvm/GlobalVariable.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000027#include "llvm/Function.h"
Chris Lattnerb8105652009-07-20 17:51:36 +000028#include "llvm/Instructions.h"
Evan Cheng6be2c582006-04-05 23:38:46 +000029#include "llvm/Intrinsics.h"
Owen Andersona90b3dc2009-07-15 21:51:10 +000030#include "llvm/LLVMContext.h"
Evan Cheng55d42002011-01-08 01:24:27 +000031#include "llvm/CodeGen/IntrinsicLowering.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000032#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng4a460802006-01-11 00:33:36 +000033#include "llvm/CodeGen/MachineFunction.h"
34#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner5e1df8d2010-01-25 23:38:14 +000035#include "llvm/CodeGen/MachineJumpTableInfo.h"
Evan Chenga844bde2008-02-02 04:07:54 +000036#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000037#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman69de1932008-02-06 22:27:42 +000038#include "llvm/CodeGen/PseudoSourceValue.h"
Chris Lattner589c6f62010-01-26 06:28:43 +000039#include "llvm/MC/MCAsmInfo.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000040#include "llvm/MC/MCContext.h"
Daniel Dunbar4e815f82010-03-15 23:51:06 +000041#include "llvm/MC/MCExpr.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000042#include "llvm/MC/MCSymbol.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000043#include "llvm/ADT/BitVector.h"
Evan Cheng14b32e12007-12-11 01:46:18 +000044#include "llvm/ADT/SmallSet.h"
Evan Chengb1712452010-01-27 06:25:16 +000045#include "llvm/ADT/Statistic.h"
Chris Lattner1a60aa72006-10-31 19:42:44 +000046#include "llvm/ADT/StringExtras.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000047#include "llvm/ADT/VectorExtras.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000048#include "llvm/Support/Debug.h"
Bill Wendlingec041eb2010-03-12 19:20:40 +000049#include "llvm/Support/Dwarf.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000050#include "llvm/Support/ErrorHandling.h"
51#include "llvm/Support/MathExtras.h"
Torok Edwindac237e2009-07-08 20:53:28 +000052#include "llvm/Support/raw_ostream.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000053using namespace llvm;
Bill Wendlingec041eb2010-03-12 19:20:40 +000054using namespace dwarf;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000055
Evan Chengb1712452010-01-27 06:25:16 +000056STATISTIC(NumTailCalls, "Number of tail calls");
57
Evan Cheng10e86422008-04-25 19:11:04 +000058// Forward declarations.
Owen Andersone50ed302009-08-10 22:56:29 +000059static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +000060 SDValue V2);
Evan Cheng10e86422008-04-25 19:11:04 +000061
David Greenea5f26012011-02-07 19:36:54 +000062static SDValue Insert128BitVector(SDValue Result,
63 SDValue Vec,
64 SDValue Idx,
65 SelectionDAG &DAG,
66 DebugLoc dl);
David Greenef125a292011-02-08 19:04:41 +000067
David Greenea5f26012011-02-07 19:36:54 +000068static SDValue Extract128BitVector(SDValue Vec,
69 SDValue Idx,
70 SelectionDAG &DAG,
71 DebugLoc dl);
72
David Greenef125a292011-02-08 19:04:41 +000073static SDValue ConcatVectors(SDValue Lower, SDValue Upper, SelectionDAG &DAG);
74
75
David Greenea5f26012011-02-07 19:36:54 +000076/// Generate a DAG to grab 128-bits from a vector > 128 bits. This
77/// sets things up to match to an AVX VEXTRACTF128 instruction or a
David Greene74a579d2011-02-10 16:57:36 +000078/// simple subregister reference. Idx is an index in the 128 bits we
79/// want. It need not be aligned to a 128-bit bounday. That makes
80/// lowering EXTRACT_VECTOR_ELT operations easier.
David Greenea5f26012011-02-07 19:36:54 +000081static SDValue Extract128BitVector(SDValue Vec,
82 SDValue Idx,
83 SelectionDAG &DAG,
84 DebugLoc dl) {
85 EVT VT = Vec.getValueType();
86 assert(VT.getSizeInBits() == 256 && "Unexpected vector size!");
87
88 EVT ElVT = VT.getVectorElementType();
89
90 int Factor = VT.getSizeInBits() / 128;
91
92 EVT ResultVT = EVT::getVectorVT(*DAG.getContext(),
93 ElVT,
94 VT.getVectorNumElements() / Factor);
95
96 // Extract from UNDEF is UNDEF.
97 if (Vec.getOpcode() == ISD::UNDEF)
98 return DAG.getNode(ISD::UNDEF, dl, ResultVT);
99
100 if (isa<ConstantSDNode>(Idx)) {
101 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
102
103 // Extract the relevant 128 bits. Generate an EXTRACT_SUBVECTOR
104 // we can match to VEXTRACTF128.
105 unsigned ElemsPerChunk = 128 / ElVT.getSizeInBits();
106
107 // This is the index of the first element of the 128-bit chunk
108 // we want.
109 unsigned NormalizedIdxVal = (((IdxVal * ElVT.getSizeInBits()) / 128)
110 * ElemsPerChunk);
111
112 SDValue VecIdx = DAG.getConstant(NormalizedIdxVal, MVT::i32);
113
114 SDValue Result = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, ResultVT, Vec,
115 VecIdx);
116
117 return Result;
118 }
119
120 return SDValue();
121}
122
123/// Generate a DAG to put 128-bits into a vector > 128 bits. This
124/// sets things up to match to an AVX VINSERTF128 instruction or a
David Greene6b381262011-02-09 15:32:06 +0000125/// simple superregister reference. Idx is an index in the 128 bits
126/// we want. It need not be aligned to a 128-bit bounday. That makes
127/// lowering INSERT_VECTOR_ELT operations easier.
David Greenea5f26012011-02-07 19:36:54 +0000128static SDValue Insert128BitVector(SDValue Result,
129 SDValue Vec,
130 SDValue Idx,
131 SelectionDAG &DAG,
132 DebugLoc dl) {
133 if (isa<ConstantSDNode>(Idx)) {
134 EVT VT = Vec.getValueType();
135 assert(VT.getSizeInBits() == 128 && "Unexpected vector size!");
136
137 EVT ElVT = VT.getVectorElementType();
138
139 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
140
141 EVT ResultVT = Result.getValueType();
142
143 // Insert the relevant 128 bits.
144 unsigned ElemsPerChunk = 128 / ElVT.getSizeInBits();
145
146 // This is the index of the first element of the 128-bit chunk
147 // we want.
148 unsigned NormalizedIdxVal = (((IdxVal * ElVT.getSizeInBits()) / 128)
149 * ElemsPerChunk);
150
151 SDValue VecIdx = DAG.getConstant(NormalizedIdxVal, MVT::i32);
152
153 Result = DAG.getNode(ISD::INSERT_SUBVECTOR, dl, ResultVT, Result, Vec,
154 VecIdx);
155 return Result;
156 }
157
158 return SDValue();
159}
160
David Greenef125a292011-02-08 19:04:41 +0000161/// Given two vectors, concat them.
162static SDValue ConcatVectors(SDValue Lower, SDValue Upper, SelectionDAG &DAG) {
163 DebugLoc dl = Lower.getDebugLoc();
164
165 assert(Lower.getValueType() == Upper.getValueType() && "Mismatched vectors!");
166
167 EVT VT = EVT::getVectorVT(*DAG.getContext(),
168 Lower.getValueType().getVectorElementType(),
169 Lower.getValueType().getVectorNumElements() * 2);
170
171 // TODO: Generalize to arbitrary vector length (this assumes 256-bit vectors).
172 assert(VT.getSizeInBits() == 256 && "Unsupported vector concat!");
173
174 // Insert the upper subvector.
175 SDValue Vec = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT), Upper,
176 DAG.getConstant(
177 // This is half the length of the result
178 // vector. Start inserting the upper 128
179 // bits here.
180 Lower.getValueType().getVectorNumElements(),
181 MVT::i32),
182 DAG, dl);
183
184 // Insert the lower subvector.
185 Vec = Insert128BitVector(Vec, Lower, DAG.getConstant(0, MVT::i32), DAG, dl);
186 return Vec;
187}
188
Chris Lattnerf0144122009-07-28 03:13:23 +0000189static TargetLoweringObjectFile *createTLOF(X86TargetMachine &TM) {
Evan Cheng2bffee22011-02-01 01:14:13 +0000190 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
191 bool is64Bit = Subtarget->is64Bit();
NAKAMURA Takumi27635382011-02-05 15:10:54 +0000192
Evan Cheng2bffee22011-02-01 01:14:13 +0000193 if (Subtarget->isTargetEnvMacho()) {
Chris Lattnere019ec12010-12-19 20:07:10 +0000194 if (is64Bit)
195 return new X8664_MachoTargetObjectFile();
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000196 return new TargetLoweringObjectFileMachO();
Michael J. Spencerec38de22010-10-10 22:04:20 +0000197 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000198
Evan Cheng2bffee22011-02-01 01:14:13 +0000199 if (Subtarget->isTargetELF()) {
Chris Lattnere019ec12010-12-19 20:07:10 +0000200 if (is64Bit)
201 return new X8664_ELFTargetObjectFile(TM);
202 return new X8632_ELFTargetObjectFile(TM);
203 }
Evan Cheng2bffee22011-02-01 01:14:13 +0000204 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
Chris Lattnere019ec12010-12-19 20:07:10 +0000205 return new TargetLoweringObjectFileCOFF();
Eric Christopher62f35a22010-07-05 19:26:33 +0000206 llvm_unreachable("unknown subtarget type");
Chris Lattnerf0144122009-07-28 03:13:23 +0000207}
208
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +0000209X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
Chris Lattnerf0144122009-07-28 03:13:23 +0000210 : TargetLowering(TM, createTLOF(TM)) {
Evan Cheng559806f2006-01-27 08:10:46 +0000211 Subtarget = &TM.getSubtarget<X86Subtarget>();
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000212 X86ScalarSSEf64 = Subtarget->hasXMMInt();
213 X86ScalarSSEf32 = Subtarget->hasXMM();
Evan Cheng25ab6902006-09-08 06:48:29 +0000214 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000215
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000216 RegInfo = TM.getRegisterInfo();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000217 TD = getTargetData();
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000218
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000219 // Set up the TargetLowering object.
Chris Lattnera34b3cf2010-12-19 20:03:11 +0000220 static MVT IntVTs[] = { MVT::i8, MVT::i16, MVT::i32, MVT::i64 };
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000221
222 // X86 is weird, it always uses i8 for shift amounts and setcc results.
Duncan Sands03228082008-11-23 15:47:28 +0000223 setBooleanContents(ZeroOrOneBooleanContent);
Eric Christopherde5e1012011-03-11 01:05:58 +0000224
225 // For 64-bit since we have so many registers use the ILP scheduler, for
226 // 32-bit code use the register pressure specific scheduling.
227 if (Subtarget->is64Bit())
228 setSchedulingPreference(Sched::ILP);
229 else
230 setSchedulingPreference(Sched::RegPressure);
Evan Cheng25ab6902006-09-08 06:48:29 +0000231 setStackPointerRegisterToSaveRestore(X86StackPtr);
Evan Cheng714554d2006-03-16 21:47:42 +0000232
Michael J. Spencer92bf38c2010-10-10 23:11:06 +0000233 if (Subtarget->isTargetWindows() && !Subtarget->isTargetCygMing()) {
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000234 // Setup Windows compiler runtime calls.
235 setLibcallName(RTLIB::SDIV_I64, "_alldiv");
Michael J. Spencer335b8062010-10-11 05:29:15 +0000236 setLibcallName(RTLIB::UDIV_I64, "_aulldiv");
237 setLibcallName(RTLIB::FPTOUINT_F64_I64, "_ftol2");
Michael J. Spencer94f7eeb2010-10-19 07:32:52 +0000238 setLibcallName(RTLIB::FPTOUINT_F32_I64, "_ftol2");
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000239 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::X86_StdCall);
Michael J. Spencer335b8062010-10-11 05:29:15 +0000240 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::X86_StdCall);
Michael J. Spencer6dad10e2010-10-27 18:52:38 +0000241 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I64, CallingConv::C);
242 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I64, CallingConv::C);
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000243 }
244
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000245 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +0000246 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000247 setUseUnderscoreSetJmp(false);
248 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000249 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000250 // MS runtime is weird: it exports _setjmp, but longjmp!
251 setUseUnderscoreSetJmp(true);
252 setUseUnderscoreLongJmp(false);
253 } else {
254 setUseUnderscoreSetJmp(true);
255 setUseUnderscoreLongJmp(true);
256 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000257
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000258 // Set up the register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000259 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
Dan Gohman71edb242010-04-30 18:30:26 +0000260 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000261 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
Evan Cheng25ab6902006-09-08 06:48:29 +0000262 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000263 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000264
Owen Anderson825b72b2009-08-11 20:47:22 +0000265 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Evan Chengc5484282006-10-04 00:56:09 +0000266
Scott Michelfdc40a02009-02-17 22:15:04 +0000267 // We don't accept any truncstore of integer registers.
Owen Anderson825b72b2009-08-11 20:47:22 +0000268 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000269 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000270 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000271 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000272 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
273 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
Evan Cheng7f042682008-10-15 02:05:31 +0000274
275 // SETOEQ and SETUNE require checking two conditions.
Owen Anderson825b72b2009-08-11 20:47:22 +0000276 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
277 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
278 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
279 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
280 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
281 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
Chris Lattnerddf89562008-01-17 19:59:44 +0000282
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000283 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
284 // operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000285 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
286 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
287 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +0000288
Evan Cheng25ab6902006-09-08 06:48:29 +0000289 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000290 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
291 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
Eli Friedman948e95a2009-05-23 09:59:16 +0000292 } else if (!UseSoftFloat) {
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000293 // We have an algorithm for SSE2->double, and we turn this into a
294 // 64-bit FILD followed by conditional FADD for other targets.
295 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Eli Friedman948e95a2009-05-23 09:59:16 +0000296 // We have an algorithm for SSE2, and we turn this into a 64-bit
297 // FILD for other targets.
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000298 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000299 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000300
301 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
302 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000303 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
304 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000305
Devang Patel6a784892009-06-05 18:48:29 +0000306 if (!UseSoftFloat) {
Bill Wendling105be5a2009-03-13 08:41:47 +0000307 // SSE has no i16 to fp conversion, only i32
308 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000309 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000310 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000311 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000312 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000313 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
314 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000315 }
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000316 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000317 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
318 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000319 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000320
Dale Johannesen73328d12007-09-19 23:55:34 +0000321 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
322 // are Legal, f80 is custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000323 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
324 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000325
Evan Cheng02568ff2006-01-30 22:13:22 +0000326 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
327 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000328 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
329 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
Evan Cheng02568ff2006-01-30 22:13:22 +0000330
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000331 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000332 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000333 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000334 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000335 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000336 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
337 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000338 }
339
340 // Handle FP_TO_UINT by promoting the destination to a larger signed
341 // conversion.
Owen Anderson825b72b2009-08-11 20:47:22 +0000342 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
343 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
344 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000345
Evan Cheng25ab6902006-09-08 06:48:29 +0000346 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000347 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
348 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Eli Friedman948e95a2009-05-23 09:59:16 +0000349 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000350 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000351 // Expand FP_TO_UINT into a select.
352 // FIXME: We would like to use a Custom expander here eventually to do
353 // the optimal thing for SSE vs. the default expansion in the legalizer.
Owen Anderson825b72b2009-08-11 20:47:22 +0000354 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000355 else
Eli Friedman948e95a2009-05-23 09:59:16 +0000356 // With SSE3 we can use fisttpll to convert to a signed i64; without
357 // SSE, we're stuck with a fistpll.
Owen Anderson825b72b2009-08-11 20:47:22 +0000358 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000359 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000360
Chris Lattner399610a2006-12-05 18:22:22 +0000361 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Michael J. Spencerec38de22010-10-10 22:04:20 +0000362 if (!X86ScalarSSEf64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000363 setOperationAction(ISD::BITCAST , MVT::f32 , Expand);
364 setOperationAction(ISD::BITCAST , MVT::i32 , Expand);
Dale Johannesene39859a2010-05-21 18:40:15 +0000365 if (Subtarget->is64Bit()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000366 setOperationAction(ISD::BITCAST , MVT::f64 , Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000367 // Without SSE, i64->f64 goes through memory.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000368 setOperationAction(ISD::BITCAST , MVT::i64 , Expand);
Dale Johannesen7d07b482010-05-21 00:52:33 +0000369 }
Chris Lattnerf3597a12006-12-05 18:45:06 +0000370 }
Chris Lattner21f66852005-12-23 05:15:23 +0000371
Dan Gohmanb00ee212008-02-18 19:34:53 +0000372 // Scalar integer divide and remainder are lowered to use operations that
373 // produce two results, to match the available instructions. This exposes
374 // the two-result form to trivial CSE, which is able to combine x/y and x%y
375 // into a single instruction.
376 //
377 // Scalar integer multiply-high is also lowered to use two-result
378 // operations, to match the available instructions. However, plain multiply
379 // (low) operations are left as Legal, as there are single-result
380 // instructions for this in x86. Using the two-result multiply instructions
381 // when both high and low results are needed must be arranged by dagcombine.
Chris Lattnere019ec12010-12-19 20:07:10 +0000382 for (unsigned i = 0, e = 4; i != e; ++i) {
383 MVT VT = IntVTs[i];
384 setOperationAction(ISD::MULHS, VT, Expand);
385 setOperationAction(ISD::MULHU, VT, Expand);
386 setOperationAction(ISD::SDIV, VT, Expand);
387 setOperationAction(ISD::UDIV, VT, Expand);
388 setOperationAction(ISD::SREM, VT, Expand);
389 setOperationAction(ISD::UREM, VT, Expand);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000390
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000391 // Add/Sub overflow ops with MVT::Glues are lowered to EFLAGS dependences.
Chris Lattnerd8ff7ec2010-12-20 01:03:27 +0000392 setOperationAction(ISD::ADDC, VT, Custom);
393 setOperationAction(ISD::ADDE, VT, Custom);
394 setOperationAction(ISD::SUBC, VT, Custom);
395 setOperationAction(ISD::SUBE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000396 }
Dan Gohmana37c9f72007-09-25 18:23:27 +0000397
Owen Anderson825b72b2009-08-11 20:47:22 +0000398 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
399 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
400 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
401 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000402 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000403 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
404 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
405 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
406 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
407 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
408 setOperationAction(ISD::FREM , MVT::f32 , Expand);
409 setOperationAction(ISD::FREM , MVT::f64 , Expand);
410 setOperationAction(ISD::FREM , MVT::f80 , Expand);
411 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000412
Owen Anderson825b72b2009-08-11 20:47:22 +0000413 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
414 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000415 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
416 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000417 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
418 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000419 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000420 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
421 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000422 }
423
Benjamin Kramer1292c222010-12-04 20:32:23 +0000424 if (Subtarget->hasPOPCNT()) {
425 setOperationAction(ISD::CTPOP , MVT::i8 , Promote);
426 } else {
427 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
428 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
429 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
430 if (Subtarget->is64Bit())
431 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
432 }
433
Owen Anderson825b72b2009-08-11 20:47:22 +0000434 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
435 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000436
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000437 // These should be promoted to a larger select which is supported.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000438 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000439 // X86 wants to expand cmov itself.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000440 setOperationAction(ISD::SELECT , MVT::i8 , Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000441 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000442 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
443 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
444 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
445 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
446 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000447 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000448 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
449 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
450 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
451 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000452 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000453 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
454 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000455 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000456 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000457
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000458 // Darwin ABI issue.
Owen Anderson825b72b2009-08-11 20:47:22 +0000459 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
460 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
461 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
462 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000463 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000464 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
465 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000466 setOperationAction(ISD::BlockAddress , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000467 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000468 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
469 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
470 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
471 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000472 setOperationAction(ISD::BlockAddress , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000473 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000474 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Owen Anderson825b72b2009-08-11 20:47:22 +0000475 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
476 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
477 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000478 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000479 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
480 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
481 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000482 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000483
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000484 if (Subtarget->hasXMM())
Owen Anderson825b72b2009-08-11 20:47:22 +0000485 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000486
Eric Christopher9a9d2752010-07-22 02:48:34 +0000487 // We may not have a libcall for MEMBARRIER so we should lower this.
488 setOperationAction(ISD::MEMBARRIER , MVT::Other, Custom);
Michael J. Spencerec38de22010-10-10 22:04:20 +0000489
Jim Grosbachf1ab49e2010-06-23 16:25:07 +0000490 // On X86 and X86-64, atomic operations are lowered to locked instructions.
491 // Locked instructions, in turn, have implicit fence semantics (all memory
492 // operations are flushed before issuing the locked instruction, and they
493 // are not buffered), so we can fold away the common pattern of
494 // fence-atomic-fence.
495 setShouldFoldAtomicFences(true);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000496
Mon P Wang63307c32008-05-05 19:05:59 +0000497 // Expand certain atomics
Chris Lattnere019ec12010-12-19 20:07:10 +0000498 for (unsigned i = 0, e = 4; i != e; ++i) {
499 MVT VT = IntVTs[i];
500 setOperationAction(ISD::ATOMIC_CMP_SWAP, VT, Custom);
501 setOperationAction(ISD::ATOMIC_LOAD_SUB, VT, Custom);
502 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000503
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000504 if (!Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000505 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
506 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
507 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
508 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
509 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
510 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
511 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000512 }
513
Evan Cheng3c992d22006-03-07 02:02:57 +0000514 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000515 if (!Subtarget->isTargetDarwin() &&
516 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000517 !Subtarget->isTargetCygMing()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000518 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Dan Gohman44066042008-07-01 00:05:16 +0000519 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000520
Owen Anderson825b72b2009-08-11 20:47:22 +0000521 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
522 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
523 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
524 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000525 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000526 setExceptionPointerRegister(X86::RAX);
527 setExceptionSelectorRegister(X86::RDX);
528 } else {
529 setExceptionPointerRegister(X86::EAX);
530 setExceptionSelectorRegister(X86::EDX);
531 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000532 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
533 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000534
Owen Anderson825b72b2009-08-11 20:47:22 +0000535 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000536
Owen Anderson825b72b2009-08-11 20:47:22 +0000537 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000538
Nate Begemanacc398c2006-01-25 18:21:52 +0000539 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000540 setOperationAction(ISD::VASTART , MVT::Other, Custom);
541 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000542 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000543 setOperationAction(ISD::VAARG , MVT::Other, Custom);
544 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000545 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000546 setOperationAction(ISD::VAARG , MVT::Other, Expand);
547 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000548 }
Evan Chengae642192007-03-02 23:16:35 +0000549
Owen Anderson825b72b2009-08-11 20:47:22 +0000550 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
551 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000552 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000553 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Michael J. Spencere9c253e2010-10-21 01:41:01 +0000554 if (Subtarget->isTargetCygMing() || Subtarget->isTargetWindows())
Owen Anderson825b72b2009-08-11 20:47:22 +0000555 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +0000556 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000557 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000558
Evan Chengc7ce29b2009-02-13 22:36:38 +0000559 if (!UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000560 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000561 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000562 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
563 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000564
Evan Cheng223547a2006-01-31 22:28:30 +0000565 // Use ANDPD to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000566 setOperationAction(ISD::FABS , MVT::f64, Custom);
567 setOperationAction(ISD::FABS , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000568
569 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000570 setOperationAction(ISD::FNEG , MVT::f64, Custom);
571 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000572
Evan Cheng68c47cb2007-01-05 07:55:56 +0000573 // Use ANDPD and ORPD to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000574 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
575 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000576
Evan Chengd25e9e82006-02-02 00:28:23 +0000577 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000578 setOperationAction(ISD::FSIN , MVT::f64, Expand);
579 setOperationAction(ISD::FCOS , MVT::f64, Expand);
580 setOperationAction(ISD::FSIN , MVT::f32, Expand);
581 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000582
Chris Lattnera54aa942006-01-29 06:26:08 +0000583 // Expand FP immediates into loads from the stack, except for the special
584 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000585 addLegalFPImmediate(APFloat(+0.0)); // xorpd
586 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Evan Chengc7ce29b2009-02-13 22:36:38 +0000587 } else if (!UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000588 // Use SSE for f32, x87 for f64.
589 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000590 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
591 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000592
593 // Use ANDPS to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000594 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000595
596 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000597 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000598
Owen Anderson825b72b2009-08-11 20:47:22 +0000599 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000600
601 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000602 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
603 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000604
605 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000606 setOperationAction(ISD::FSIN , MVT::f32, Expand);
607 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000608
Nate Begemane1795842008-02-14 08:57:00 +0000609 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000610 addLegalFPImmediate(APFloat(+0.0f)); // xorps
611 addLegalFPImmediate(APFloat(+0.0)); // FLD0
612 addLegalFPImmediate(APFloat(+1.0)); // FLD1
613 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
614 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
615
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000616 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000617 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
618 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000619 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000620 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000621 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000622 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000623 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
624 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000625
Owen Anderson825b72b2009-08-11 20:47:22 +0000626 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
627 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
628 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
629 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000630
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000631 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000632 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
633 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000634 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000635 addLegalFPImmediate(APFloat(+0.0)); // FLD0
636 addLegalFPImmediate(APFloat(+1.0)); // FLD1
637 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
638 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000639 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
640 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
641 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
642 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000643 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000644
Dale Johannesen59a58732007-08-05 18:49:15 +0000645 // Long double always uses X87.
Evan Cheng92722532009-03-26 23:06:32 +0000646 if (!UseSoftFloat) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000647 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
648 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
649 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000650 {
Benjamin Kramer98383962010-12-04 14:22:24 +0000651 APFloat TmpFlt = APFloat::getZero(APFloat::x87DoubleExtended);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000652 addLegalFPImmediate(TmpFlt); // FLD0
653 TmpFlt.changeSign();
654 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
Benjamin Kramer98383962010-12-04 14:22:24 +0000655
656 bool ignored;
Evan Chengc7ce29b2009-02-13 22:36:38 +0000657 APFloat TmpFlt2(+1.0);
658 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
659 &ignored);
660 addLegalFPImmediate(TmpFlt2); // FLD1
661 TmpFlt2.changeSign();
662 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
663 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000664
Evan Chengc7ce29b2009-02-13 22:36:38 +0000665 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000666 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
667 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000668 }
Dale Johannesen2f429012007-09-26 21:10:55 +0000669 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000670
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000671 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000672 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
673 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
674 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000675
Owen Anderson825b72b2009-08-11 20:47:22 +0000676 setOperationAction(ISD::FLOG, MVT::f80, Expand);
677 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
678 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
679 setOperationAction(ISD::FEXP, MVT::f80, Expand);
680 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000681
Mon P Wangf007a8b2008-11-06 05:31:54 +0000682 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000683 // (for widening) or expand (for scalarization). Then we will selectively
684 // turn on ones that can be effectively codegen'd.
Owen Anderson825b72b2009-08-11 20:47:22 +0000685 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
686 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
687 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
688 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
689 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
690 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
691 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
692 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
693 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
694 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
695 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
696 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
697 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
698 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
699 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
700 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
701 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000702 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
David Greenecfe33c42011-01-26 19:13:22 +0000703 setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
704 setOperationAction(ISD::INSERT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000705 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
706 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
707 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
708 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
709 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
710 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
711 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
712 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
713 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
714 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
715 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
716 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
717 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
718 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
719 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
720 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
721 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
722 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
723 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
724 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
725 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
726 setOperationAction(ISD::VSETCC, (MVT::SimpleValueType)VT, Expand);
727 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
728 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
729 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
730 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
731 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
732 setOperationAction(ISD::FP_TO_UINT, (MVT::SimpleValueType)VT, Expand);
733 setOperationAction(ISD::FP_TO_SINT, (MVT::SimpleValueType)VT, Expand);
734 setOperationAction(ISD::UINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
735 setOperationAction(ISD::SINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
Dan Gohman87862e72009-12-11 21:31:27 +0000736 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000737 setOperationAction(ISD::TRUNCATE, (MVT::SimpleValueType)VT, Expand);
738 setOperationAction(ISD::SIGN_EXTEND, (MVT::SimpleValueType)VT, Expand);
739 setOperationAction(ISD::ZERO_EXTEND, (MVT::SimpleValueType)VT, Expand);
740 setOperationAction(ISD::ANY_EXTEND, (MVT::SimpleValueType)VT, Expand);
741 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
742 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
743 setTruncStoreAction((MVT::SimpleValueType)VT,
744 (MVT::SimpleValueType)InnerVT, Expand);
745 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
746 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
747 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000748 }
749
Evan Chengc7ce29b2009-02-13 22:36:38 +0000750 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
751 // with -msoft-float, disable use of MMX as well.
Chris Lattner2a786eb2010-12-19 20:19:20 +0000752 if (!UseSoftFloat && Subtarget->hasMMX()) {
Dale Johannesene93d99c2010-10-20 21:32:10 +0000753 addRegisterClass(MVT::x86mmx, X86::VR64RegisterClass);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000754 // No operations on x86mmx supported, everything uses intrinsics.
Evan Cheng470a6ad2006-02-22 02:26:30 +0000755 }
756
Dale Johannesen0488fb62010-09-30 23:57:10 +0000757 // MMX-sized vectors (other than x86mmx) are expected to be expanded
758 // into smaller operations.
759 setOperationAction(ISD::MULHS, MVT::v8i8, Expand);
760 setOperationAction(ISD::MULHS, MVT::v4i16, Expand);
761 setOperationAction(ISD::MULHS, MVT::v2i32, Expand);
762 setOperationAction(ISD::MULHS, MVT::v1i64, Expand);
763 setOperationAction(ISD::AND, MVT::v8i8, Expand);
764 setOperationAction(ISD::AND, MVT::v4i16, Expand);
765 setOperationAction(ISD::AND, MVT::v2i32, Expand);
766 setOperationAction(ISD::AND, MVT::v1i64, Expand);
767 setOperationAction(ISD::OR, MVT::v8i8, Expand);
768 setOperationAction(ISD::OR, MVT::v4i16, Expand);
769 setOperationAction(ISD::OR, MVT::v2i32, Expand);
770 setOperationAction(ISD::OR, MVT::v1i64, Expand);
771 setOperationAction(ISD::XOR, MVT::v8i8, Expand);
772 setOperationAction(ISD::XOR, MVT::v4i16, Expand);
773 setOperationAction(ISD::XOR, MVT::v2i32, Expand);
774 setOperationAction(ISD::XOR, MVT::v1i64, Expand);
775 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Expand);
776 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Expand);
777 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Expand);
778 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Expand);
779 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v1i64, Expand);
780 setOperationAction(ISD::SELECT, MVT::v8i8, Expand);
781 setOperationAction(ISD::SELECT, MVT::v4i16, Expand);
782 setOperationAction(ISD::SELECT, MVT::v2i32, Expand);
783 setOperationAction(ISD::SELECT, MVT::v1i64, Expand);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000784 setOperationAction(ISD::BITCAST, MVT::v8i8, Expand);
785 setOperationAction(ISD::BITCAST, MVT::v4i16, Expand);
786 setOperationAction(ISD::BITCAST, MVT::v2i32, Expand);
787 setOperationAction(ISD::BITCAST, MVT::v1i64, Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000788
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000789 if (!UseSoftFloat && Subtarget->hasXMM()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000790 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000791
Owen Anderson825b72b2009-08-11 20:47:22 +0000792 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
793 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
794 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
795 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
796 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
797 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
798 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
799 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
800 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
801 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
802 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
803 setOperationAction(ISD::VSETCC, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000804 }
805
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000806 if (!UseSoftFloat && Subtarget->hasXMMInt()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000807 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000808
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000809 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
810 // registers cannot be used even for integer operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000811 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
812 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
813 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
814 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000815
Owen Anderson825b72b2009-08-11 20:47:22 +0000816 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
817 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
818 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
819 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
820 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
821 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
822 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
823 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
824 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
825 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
826 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
827 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
828 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
829 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
830 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
831 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000832
Owen Anderson825b72b2009-08-11 20:47:22 +0000833 setOperationAction(ISD::VSETCC, MVT::v2f64, Custom);
834 setOperationAction(ISD::VSETCC, MVT::v16i8, Custom);
835 setOperationAction(ISD::VSETCC, MVT::v8i16, Custom);
836 setOperationAction(ISD::VSETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000837
Owen Anderson825b72b2009-08-11 20:47:22 +0000838 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
839 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
840 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
841 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
842 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000843
Mon P Wangeb38ebf2010-01-24 00:05:03 +0000844 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2f64, Custom);
845 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2i64, Custom);
846 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i8, Custom);
847 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i16, Custom);
848 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Custom);
849
Evan Cheng2c3ae372006-04-12 21:21:57 +0000850 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Owen Anderson825b72b2009-08-11 20:47:22 +0000851 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
852 EVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000853 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000854 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000855 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000856 // Do not attempt to custom lower non-128-bit vectors
857 if (!VT.is128BitVector())
858 continue;
Owen Anderson825b72b2009-08-11 20:47:22 +0000859 setOperationAction(ISD::BUILD_VECTOR,
860 VT.getSimpleVT().SimpleTy, Custom);
861 setOperationAction(ISD::VECTOR_SHUFFLE,
862 VT.getSimpleVT().SimpleTy, Custom);
863 setOperationAction(ISD::EXTRACT_VECTOR_ELT,
864 VT.getSimpleVT().SimpleTy, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000865 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000866
Owen Anderson825b72b2009-08-11 20:47:22 +0000867 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
868 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
869 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
870 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
871 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
872 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000873
Nate Begemancdd1eec2008-02-12 22:51:28 +0000874 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000875 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
876 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000877 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000878
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000879 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000880 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; i++) {
881 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
Owen Andersone50ed302009-08-10 22:56:29 +0000882 EVT VT = SVT;
David Greene9b9838d2009-06-29 16:47:10 +0000883
884 // Do not attempt to promote non-128-bit vectors
Chris Lattner32b4b5a2010-07-05 05:53:14 +0000885 if (!VT.is128BitVector())
David Greene9b9838d2009-06-29 16:47:10 +0000886 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +0000887
Owen Andersond6662ad2009-08-10 20:46:15 +0000888 setOperationAction(ISD::AND, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000889 AddPromotedToType (ISD::AND, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000890 setOperationAction(ISD::OR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000891 AddPromotedToType (ISD::OR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000892 setOperationAction(ISD::XOR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000893 AddPromotedToType (ISD::XOR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000894 setOperationAction(ISD::LOAD, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000895 AddPromotedToType (ISD::LOAD, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000896 setOperationAction(ISD::SELECT, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000897 AddPromotedToType (ISD::SELECT, SVT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000898 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000899
Owen Anderson825b72b2009-08-11 20:47:22 +0000900 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000901
Evan Cheng2c3ae372006-04-12 21:21:57 +0000902 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000903 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
904 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
905 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
906 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000907
Owen Anderson825b72b2009-08-11 20:47:22 +0000908 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
909 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000910 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000911
Nate Begeman14d12ca2008-02-11 04:19:36 +0000912 if (Subtarget->hasSSE41()) {
Dale Johannesen54feef22010-05-27 20:12:41 +0000913 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
914 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
915 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
916 setOperationAction(ISD::FRINT, MVT::f32, Legal);
917 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
918 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
919 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
920 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
921 setOperationAction(ISD::FRINT, MVT::f64, Legal);
922 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
923
Nate Begeman14d12ca2008-02-11 04:19:36 +0000924 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +0000925 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000926
Nate Begemanbdcb5af2010-07-27 22:37:06 +0000927 // Can turn SHL into an integer multiply.
928 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
Nate Begeman51409212010-07-28 00:21:48 +0000929 setOperationAction(ISD::SHL, MVT::v16i8, Custom);
Nate Begemanbdcb5af2010-07-27 22:37:06 +0000930
Nate Begeman14d12ca2008-02-11 04:19:36 +0000931 // i8 and i16 vectors are custom , because the source register and source
932 // source memory operand types are not the same width. f32 vectors are
933 // custom since the immediate controlling the insert encodes additional
934 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +0000935 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
936 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
937 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
938 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000939
Owen Anderson825b72b2009-08-11 20:47:22 +0000940 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
941 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
942 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
943 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000944
945 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000946 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
947 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000948 }
949 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000950
Chris Lattnera34b3cf2010-12-19 20:03:11 +0000951 if (Subtarget->hasSSE42())
Owen Anderson825b72b2009-08-11 20:47:22 +0000952 setOperationAction(ISD::VSETCC, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000953
David Greene9b9838d2009-06-29 16:47:10 +0000954 if (!UseSoftFloat && Subtarget->hasAVX()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000955 addRegisterClass(MVT::v8f32, X86::VR256RegisterClass);
956 addRegisterClass(MVT::v4f64, X86::VR256RegisterClass);
957 addRegisterClass(MVT::v8i32, X86::VR256RegisterClass);
958 addRegisterClass(MVT::v4i64, X86::VR256RegisterClass);
Bruno Cardoso Lopes405f11b2010-08-10 01:43:16 +0000959 addRegisterClass(MVT::v32i8, X86::VR256RegisterClass);
David Greened94c1012009-06-29 22:50:51 +0000960
Owen Anderson825b72b2009-08-11 20:47:22 +0000961 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
962 setOperationAction(ISD::LOAD, MVT::v8i32, Legal);
963 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
964 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
David Greene54d8eba2011-01-27 22:38:56 +0000965
Owen Anderson825b72b2009-08-11 20:47:22 +0000966 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
967 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
968 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
969 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
970 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
971 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000972
Owen Anderson825b72b2009-08-11 20:47:22 +0000973 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
974 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
975 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
976 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
977 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
978 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000979
David Greene54d8eba2011-01-27 22:38:56 +0000980 // Custom lower build_vector, vector_shuffle, scalar_to_vector,
981 // insert_vector_elt extract_subvector and extract_vector_elt for
982 // 256-bit types.
983 for (unsigned i = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
984 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE;
985 ++i) {
986 MVT::SimpleValueType VT = (MVT::SimpleValueType)i;
987 // Do not attempt to custom lower non-256-bit vectors
988 if (!isPowerOf2_32(MVT(VT).getVectorNumElements())
989 || (MVT(VT).getSizeInBits() < 256))
David Greene9b9838d2009-06-29 16:47:10 +0000990 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000991 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
992 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
David Greene54d8eba2011-01-27 22:38:56 +0000993 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000994 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
David Greene54d8eba2011-01-27 22:38:56 +0000995 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000996 }
David Greene54d8eba2011-01-27 22:38:56 +0000997 // Custom-lower insert_subvector and extract_subvector based on
998 // the result type.
999 for (unsigned i = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
1000 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE;
1001 ++i) {
1002 MVT::SimpleValueType VT = (MVT::SimpleValueType)i;
1003 // Do not attempt to custom lower non-256-bit vectors
1004 if (!isPowerOf2_32(MVT(VT).getVectorNumElements()))
David Greene9b9838d2009-06-29 16:47:10 +00001005 continue;
David Greene54d8eba2011-01-27 22:38:56 +00001006
1007 if (MVT(VT).getSizeInBits() == 128) {
1008 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001009 }
David Greene54d8eba2011-01-27 22:38:56 +00001010 else if (MVT(VT).getSizeInBits() == 256) {
1011 setOperationAction(ISD::INSERT_SUBVECTOR, VT, Custom);
1012 }
David Greene9b9838d2009-06-29 16:47:10 +00001013 }
1014
David Greene54d8eba2011-01-27 22:38:56 +00001015 // Promote v32i8, v16i16, v8i32 select, and, or, xor to v4i64.
1016 // Don't promote loads because we need them for VPERM vector index versions.
1017
1018 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
1019 VT != (unsigned)MVT::LAST_VECTOR_VALUETYPE;
1020 VT++) {
1021 if (!isPowerOf2_32(MVT((MVT::SimpleValueType)VT).getVectorNumElements())
1022 || (MVT((MVT::SimpleValueType)VT).getSizeInBits() < 256))
1023 continue;
1024 setOperationAction(ISD::AND, (MVT::SimpleValueType)VT, Promote);
1025 AddPromotedToType (ISD::AND, (MVT::SimpleValueType)VT, MVT::v4i64);
1026 setOperationAction(ISD::OR, (MVT::SimpleValueType)VT, Promote);
1027 AddPromotedToType (ISD::OR, (MVT::SimpleValueType)VT, MVT::v4i64);
1028 setOperationAction(ISD::XOR, (MVT::SimpleValueType)VT, Promote);
1029 AddPromotedToType (ISD::XOR, (MVT::SimpleValueType)VT, MVT::v4i64);
1030 //setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Promote);
1031 //AddPromotedToType (ISD::LOAD, (MVT::SimpleValueType)VT, MVT::v4i64);
1032 setOperationAction(ISD::SELECT, (MVT::SimpleValueType)VT, Promote);
1033 AddPromotedToType (ISD::SELECT, (MVT::SimpleValueType)VT, MVT::v4i64);
1034 }
David Greene9b9838d2009-06-29 16:47:10 +00001035 }
1036
Evan Cheng6be2c582006-04-05 23:38:46 +00001037 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +00001038 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +00001039
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001040
Eli Friedman962f5492010-06-02 19:35:46 +00001041 // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
1042 // handle type legalization for these operations here.
Dan Gohman71c62a22010-06-02 19:13:40 +00001043 //
Eli Friedman962f5492010-06-02 19:35:46 +00001044 // FIXME: We really should do custom legalization for addition and
1045 // subtraction on x86-32 once PR3203 is fixed. We really can't do much better
1046 // than generic legalization for 64-bit multiplication-with-overflow, though.
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001047 for (unsigned i = 0, e = 3+Subtarget->is64Bit(); i != e; ++i) {
1048 // Add/Sub/Mul with overflow operations are custom lowered.
1049 MVT VT = IntVTs[i];
1050 setOperationAction(ISD::SADDO, VT, Custom);
1051 setOperationAction(ISD::UADDO, VT, Custom);
1052 setOperationAction(ISD::SSUBO, VT, Custom);
1053 setOperationAction(ISD::USUBO, VT, Custom);
1054 setOperationAction(ISD::SMULO, VT, Custom);
1055 setOperationAction(ISD::UMULO, VT, Custom);
Eli Friedmana993f0a2010-06-02 00:27:18 +00001056 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001057
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001058 // There are no 8-bit 3-address imul/mul instructions
1059 setOperationAction(ISD::SMULO, MVT::i8, Expand);
1060 setOperationAction(ISD::UMULO, MVT::i8, Expand);
Bill Wendling41ea7e72008-11-24 19:21:46 +00001061
Evan Chengd54f2d52009-03-31 19:38:51 +00001062 if (!Subtarget->is64Bit()) {
1063 // These libcalls are not available in 32-bit.
1064 setLibcallName(RTLIB::SHL_I128, 0);
1065 setLibcallName(RTLIB::SRL_I128, 0);
1066 setLibcallName(RTLIB::SRA_I128, 0);
1067 }
1068
Evan Cheng206ee9d2006-07-07 08:33:52 +00001069 // We have target-specific dag combine patterns for the following nodes:
1070 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00001071 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Evan Chengd880b972008-05-09 21:53:03 +00001072 setTargetDAGCombine(ISD::BUILD_VECTOR);
Chris Lattner83e6c992006-10-04 06:57:07 +00001073 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +00001074 setTargetDAGCombine(ISD::SHL);
1075 setTargetDAGCombine(ISD::SRA);
1076 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +00001077 setTargetDAGCombine(ISD::OR);
Nate Begemanb65c1752010-12-17 22:55:37 +00001078 setTargetDAGCombine(ISD::AND);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001079 setTargetDAGCombine(ISD::ADD);
1080 setTargetDAGCombine(ISD::SUB);
Chris Lattner149a4e52008-02-22 02:09:43 +00001081 setTargetDAGCombine(ISD::STORE);
Evan Cheng2e489c42009-12-16 00:53:11 +00001082 setTargetDAGCombine(ISD::ZERO_EXTEND);
Evan Cheng0b0cd912009-03-28 05:57:29 +00001083 if (Subtarget->is64Bit())
1084 setTargetDAGCombine(ISD::MUL);
Evan Cheng206ee9d2006-07-07 08:33:52 +00001085
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001086 computeRegisterProperties();
1087
Evan Cheng05219282011-01-06 06:52:41 +00001088 // On Darwin, -Os means optimize for size without hurting performance,
1089 // do not reduce the limit.
Dan Gohman87060f52008-06-30 21:00:56 +00001090 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001091 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 16 : 8;
Evan Cheng255f20f2010-04-01 06:04:33 +00001092 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001093 maxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
1094 maxStoresPerMemmove = 8; // For @llvm.memmove -> sequence of stores
1095 maxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Evan Chengfb8075d2008-02-28 00:43:03 +00001096 setPrefLoopAlignment(16);
Evan Cheng6ebf7bc2009-05-13 21:42:09 +00001097 benefitFromCodePlacementOpt = true;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001098}
1099
Scott Michel5b8f82e2008-03-10 15:42:14 +00001100
Owen Anderson825b72b2009-08-11 20:47:22 +00001101MVT::SimpleValueType X86TargetLowering::getSetCCResultType(EVT VT) const {
1102 return MVT::i8;
Scott Michel5b8f82e2008-03-10 15:42:14 +00001103}
1104
1105
Evan Cheng29286502008-01-23 23:17:41 +00001106/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1107/// the desired ByVal argument alignment.
1108static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
1109 if (MaxAlign == 16)
1110 return;
1111 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
1112 if (VTy->getBitWidth() == 128)
1113 MaxAlign = 16;
Evan Cheng29286502008-01-23 23:17:41 +00001114 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
1115 unsigned EltAlign = 0;
1116 getMaxByValAlign(ATy->getElementType(), EltAlign);
1117 if (EltAlign > MaxAlign)
1118 MaxAlign = EltAlign;
1119 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
1120 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1121 unsigned EltAlign = 0;
1122 getMaxByValAlign(STy->getElementType(i), EltAlign);
1123 if (EltAlign > MaxAlign)
1124 MaxAlign = EltAlign;
1125 if (MaxAlign == 16)
1126 break;
1127 }
1128 }
1129 return;
1130}
1131
1132/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1133/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001134/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1135/// are at 4-byte boundaries.
Evan Cheng29286502008-01-23 23:17:41 +00001136unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001137 if (Subtarget->is64Bit()) {
1138 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001139 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001140 if (TyAlign > 8)
1141 return TyAlign;
1142 return 8;
1143 }
1144
Evan Cheng29286502008-01-23 23:17:41 +00001145 unsigned Align = 4;
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001146 if (Subtarget->hasXMM())
Dale Johannesen0c191872008-02-08 19:48:20 +00001147 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001148 return Align;
1149}
Chris Lattner2b02a442007-02-25 08:29:00 +00001150
Evan Chengf0df0312008-05-15 08:39:06 +00001151/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001152/// and store operations as a result of memset, memcpy, and memmove
1153/// lowering. If DstAlign is zero that means it's safe to destination
1154/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1155/// means there isn't a need to check it against alignment requirement,
1156/// probably because the source does not need to be loaded. If
1157/// 'NonScalarIntSafe' is true, that means it's safe to return a
1158/// non-scalar-integer type, e.g. empty string source, constant, or loaded
1159/// from memory. 'MemcpyStrSrc' indicates whether the memcpy source is
1160/// constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001161/// It returns EVT::Other if the type should be determined using generic
1162/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001163EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001164X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1165 unsigned DstAlign, unsigned SrcAlign,
Evan Chengf28f8bc2010-04-02 19:36:14 +00001166 bool NonScalarIntSafe,
Evan Chengc3b0c342010-04-08 07:37:57 +00001167 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001168 MachineFunction &MF) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001169 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1170 // linux. This is because the stack realignment code can't handle certain
1171 // cases like PR2962. This should be removed when PR2962 is fixed.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001172 const Function *F = MF.getFunction();
Evan Chenga5e13622011-01-07 19:35:30 +00001173 if (NonScalarIntSafe &&
1174 !F->hasFnAttr(Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001175 if (Size >= 16 &&
Evan Chenga5e13622011-01-07 19:35:30 +00001176 (Subtarget->isUnalignedMemAccessFast() ||
1177 ((DstAlign == 0 || DstAlign >= 16) &&
1178 (SrcAlign == 0 || SrcAlign >= 16))) &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001179 Subtarget->getStackAlignment() >= 16) {
1180 if (Subtarget->hasSSE2())
1181 return MVT::v4i32;
Evan Chengf28f8bc2010-04-02 19:36:14 +00001182 if (Subtarget->hasSSE1())
Evan Cheng255f20f2010-04-01 06:04:33 +00001183 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001184 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001185 !Subtarget->is64Bit() &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001186 Subtarget->getStackAlignment() >= 8 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001187 Subtarget->hasXMMInt()) {
Evan Chengc3b0c342010-04-08 07:37:57 +00001188 // Do not use f64 to lower memcpy if source is string constant. It's
1189 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001190 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001191 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001192 }
Evan Chengf0df0312008-05-15 08:39:06 +00001193 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001194 return MVT::i64;
1195 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001196}
1197
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001198/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1199/// current function. The returned value is a member of the
1200/// MachineJumpTableInfo::JTEntryKind enum.
1201unsigned X86TargetLowering::getJumpTableEncoding() const {
1202 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1203 // symbol.
1204 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1205 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001206 return MachineJumpTableInfo::EK_Custom32;
Michael J. Spencerec38de22010-10-10 22:04:20 +00001207
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001208 // Otherwise, use the normal jump table encoding heuristics.
1209 return TargetLowering::getJumpTableEncoding();
1210}
1211
Chris Lattnerc64daab2010-01-26 05:02:42 +00001212const MCExpr *
1213X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1214 const MachineBasicBlock *MBB,
1215 unsigned uid,MCContext &Ctx) const{
1216 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1217 Subtarget->isPICStyleGOT());
1218 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1219 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001220 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1221 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001222}
1223
Evan Chengcc415862007-11-09 01:32:10 +00001224/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1225/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001226SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001227 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001228 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001229 // This doesn't have DebugLoc associated with it, but is not really the
1230 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001231 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001232 return Table;
1233}
1234
Chris Lattner589c6f62010-01-26 06:28:43 +00001235/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1236/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1237/// MCExpr.
1238const MCExpr *X86TargetLowering::
1239getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1240 MCContext &Ctx) const {
1241 // X86-64 uses RIP relative addressing based on the jump table label.
1242 if (Subtarget->isPICStyleRIPRel())
1243 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1244
1245 // Otherwise, the reference is relative to the PIC base.
Chris Lattner142b5312010-11-14 22:48:15 +00001246 return MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
Chris Lattner589c6f62010-01-26 06:28:43 +00001247}
1248
Bill Wendlingb4202b82009-07-01 18:50:55 +00001249/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +00001250unsigned X86TargetLowering::getFunctionAlignment(const Function *F) const {
Dan Gohman25103a22009-08-18 00:20:06 +00001251 return F->hasFnAttr(Attribute::OptimizeForSize) ? 0 : 4;
Bill Wendling20c568f2009-06-30 22:38:32 +00001252}
1253
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001254// FIXME: Why this routine is here? Move to RegInfo!
Evan Chengdee81012010-07-26 21:50:05 +00001255std::pair<const TargetRegisterClass*, uint8_t>
1256X86TargetLowering::findRepresentativeClass(EVT VT) const{
1257 const TargetRegisterClass *RRC = 0;
1258 uint8_t Cost = 1;
1259 switch (VT.getSimpleVT().SimpleTy) {
1260 default:
1261 return TargetLowering::findRepresentativeClass(VT);
1262 case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
1263 RRC = (Subtarget->is64Bit()
1264 ? X86::GR64RegisterClass : X86::GR32RegisterClass);
1265 break;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001266 case MVT::x86mmx:
Evan Chengdee81012010-07-26 21:50:05 +00001267 RRC = X86::VR64RegisterClass;
1268 break;
1269 case MVT::f32: case MVT::f64:
1270 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1271 case MVT::v4f32: case MVT::v2f64:
1272 case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1273 case MVT::v4f64:
1274 RRC = X86::VR128RegisterClass;
1275 break;
1276 }
1277 return std::make_pair(RRC, Cost);
1278}
1279
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001280bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1281 unsigned &Offset) const {
1282 if (!Subtarget->isTargetLinux())
1283 return false;
1284
1285 if (Subtarget->is64Bit()) {
1286 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1287 Offset = 0x28;
1288 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1289 AddressSpace = 256;
1290 else
1291 AddressSpace = 257;
1292 } else {
1293 // %gs:0x14 on i386
1294 Offset = 0x14;
1295 AddressSpace = 256;
1296 }
1297 return true;
1298}
1299
1300
Chris Lattner2b02a442007-02-25 08:29:00 +00001301//===----------------------------------------------------------------------===//
1302// Return Value Calling Convention Implementation
1303//===----------------------------------------------------------------------===//
1304
Chris Lattner59ed56b2007-02-28 04:55:35 +00001305#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001306
Michael J. Spencerec38de22010-10-10 22:04:20 +00001307bool
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001308X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001309 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001310 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001311 SmallVector<CCValAssign, 16> RVLocs;
1312 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001313 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001314 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001315}
1316
Dan Gohman98ca4f22009-08-05 01:29:28 +00001317SDValue
1318X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001319 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001320 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001321 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001322 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001323 MachineFunction &MF = DAG.getMachineFunction();
1324 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001325
Chris Lattner9774c912007-02-27 05:28:59 +00001326 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001327 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1328 RVLocs, *DAG.getContext());
1329 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001330
Evan Chengdcea1632010-02-04 02:40:39 +00001331 // Add the regs to the liveout set for the function.
1332 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1333 for (unsigned i = 0; i != RVLocs.size(); ++i)
1334 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1335 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001336
Dan Gohman475871a2008-07-27 21:46:04 +00001337 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001338
Dan Gohman475871a2008-07-27 21:46:04 +00001339 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001340 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1341 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001342 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1343 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001344
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001345 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001346 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1347 CCValAssign &VA = RVLocs[i];
1348 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001349 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001350 EVT ValVT = ValToCopy.getValueType();
1351
Dale Johannesenc4510512010-09-24 19:05:48 +00001352 // If this is x86-64, and we disabled SSE, we can't return FP values,
1353 // or SSE or MMX vectors.
1354 if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
1355 VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001356 (Subtarget->is64Bit() && !Subtarget->hasXMM())) {
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001357 report_fatal_error("SSE register return with SSE disabled");
1358 }
1359 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1360 // llvm-gcc has never done it right and no one has noticed, so this
1361 // should be OK for now.
1362 if (ValVT == MVT::f64 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001363 (Subtarget->is64Bit() && !Subtarget->hasXMMInt()))
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001364 report_fatal_error("SSE2 register return with SSE2 disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001365
Chris Lattner447ff682008-03-11 03:23:40 +00001366 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1367 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001368 if (VA.getLocReg() == X86::ST0 ||
1369 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001370 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1371 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001372 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001373 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001374 RetOps.push_back(ValToCopy);
1375 // Don't emit a copytoreg.
1376 continue;
1377 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001378
Evan Cheng242b38b2009-02-23 09:03:22 +00001379 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1380 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001381 if (Subtarget->is64Bit()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001382 if (ValVT == MVT::x86mmx) {
Chris Lattner97a2a562010-08-26 05:24:29 +00001383 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001384 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ValToCopy);
Eric Christopher90eb4022010-07-22 00:26:08 +00001385 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1386 ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001387 // If we don't have SSE2 available, convert to v4f32 so the generated
1388 // register is legal.
1389 if (!Subtarget->hasSSE2())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001390 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32,ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001391 }
Evan Cheng242b38b2009-02-23 09:03:22 +00001392 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001393 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00001394
Dale Johannesendd64c412009-02-04 00:33:20 +00001395 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001396 Flag = Chain.getValue(1);
1397 }
Dan Gohman61a92132008-04-21 23:59:07 +00001398
1399 // The x86-64 ABI for returning structs by value requires that we copy
1400 // the sret argument into %rax for the return. We saved the argument into
1401 // a virtual register in the entry block, so now we copy the value out
1402 // and into %rax.
1403 if (Subtarget->is64Bit() &&
1404 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1405 MachineFunction &MF = DAG.getMachineFunction();
1406 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1407 unsigned Reg = FuncInfo->getSRetReturnReg();
Michael J. Spencerec38de22010-10-10 22:04:20 +00001408 assert(Reg &&
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001409 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001410 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001411
Dale Johannesendd64c412009-02-04 00:33:20 +00001412 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001413 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001414
1415 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001416 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001417 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001418
Chris Lattner447ff682008-03-11 03:23:40 +00001419 RetOps[0] = Chain; // Update chain.
1420
1421 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001422 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001423 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001424
1425 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001426 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001427}
1428
Evan Cheng3d2125c2010-11-30 23:55:39 +00001429bool X86TargetLowering::isUsedByReturnOnly(SDNode *N) const {
1430 if (N->getNumValues() != 1)
1431 return false;
1432 if (!N->hasNUsesOfValue(1, 0))
1433 return false;
1434
1435 SDNode *Copy = *N->use_begin();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001436 if (Copy->getOpcode() != ISD::CopyToReg &&
1437 Copy->getOpcode() != ISD::FP_EXTEND)
Evan Cheng3d2125c2010-11-30 23:55:39 +00001438 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001439
1440 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001441 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001442 UI != UE; ++UI) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001443 if (UI->getOpcode() != X86ISD::RET_FLAG)
1444 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001445 HasRet = true;
1446 }
Evan Cheng3d2125c2010-11-30 23:55:39 +00001447
Evan Cheng1bf891a2010-12-01 22:59:46 +00001448 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001449}
1450
Cameron Zwarichebe81732011-03-16 22:20:18 +00001451MVT
1452X86TargetLowering::getTypeForExtendedInteger(EVT VT,
1453 ISD::NodeType ExtendKind) const {
1454 // TODO: Is this also valid on 32-bit?
1455 if (Subtarget->is64Bit() && VT == MVT::i1 && ExtendKind == ISD::ZERO_EXTEND)
1456 return MVT::i8;
1457 return MVT::i32;
1458}
1459
Dan Gohman98ca4f22009-08-05 01:29:28 +00001460/// LowerCallResult - Lower the result values of a call into the
1461/// appropriate copies out of appropriate physical registers.
1462///
1463SDValue
1464X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001465 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001466 const SmallVectorImpl<ISD::InputArg> &Ins,
1467 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001468 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001469
Chris Lattnere32bbf62007-02-28 07:09:55 +00001470 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001471 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001472 bool Is64Bit = Subtarget->is64Bit();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001473 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +00001474 RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001475 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001476
Chris Lattner3085e152007-02-25 08:59:22 +00001477 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001478 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001479 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001480 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001481
Torok Edwin3f142c32009-02-01 18:15:56 +00001482 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001483 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001484 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasXMM())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001485 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001486 }
1487
Evan Cheng79fb3b42009-02-20 20:43:02 +00001488 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001489
1490 // If this is a call to a function that returns an fp value on the floating
1491 // point stack, we must guarantee the the value is popped from the stack, so
1492 // a CopyFromReg is not good enough - the copy instruction may be eliminated
1493 // if the return value is not used. We use the FpGET_ST0 instructions
1494 // instead.
1495 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1496 // If we prefer to use the value in xmm registers, copy it out as f80 and
1497 // use a truncate to move it from fp stack reg to xmm reg.
1498 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
1499 bool isST0 = VA.getLocReg() == X86::ST0;
1500 unsigned Opc = 0;
1501 if (CopyVT == MVT::f32) Opc = isST0 ? X86::FpGET_ST0_32:X86::FpGET_ST1_32;
1502 if (CopyVT == MVT::f64) Opc = isST0 ? X86::FpGET_ST0_64:X86::FpGET_ST1_64;
1503 if (CopyVT == MVT::f80) Opc = isST0 ? X86::FpGET_ST0_80:X86::FpGET_ST1_80;
1504 SDValue Ops[] = { Chain, InFlag };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001505 Chain = SDValue(DAG.getMachineNode(Opc, dl, CopyVT, MVT::Other, MVT::Glue,
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001506 Ops, 2), 1);
1507 Val = Chain.getValue(0);
1508
1509 // Round the f80 to the right size, which also moves it to the appropriate
1510 // xmm register.
1511 if (CopyVT != VA.getValVT())
1512 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1513 // This truncation won't change the value.
1514 DAG.getIntPtrConstant(1));
1515 } else if (Is64Bit && CopyVT.isVector() && CopyVT.getSizeInBits() == 64) {
Evan Cheng242b38b2009-02-23 09:03:22 +00001516 // For x86-64, MMX values are returned in XMM0 / XMM1 except for v1i64.
1517 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
1518 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001519 MVT::v2i64, InFlag).getValue(1);
Evan Cheng242b38b2009-02-23 09:03:22 +00001520 Val = Chain.getValue(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00001521 Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1522 Val, DAG.getConstant(0, MVT::i64));
Evan Cheng242b38b2009-02-23 09:03:22 +00001523 } else {
1524 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001525 MVT::i64, InFlag).getValue(1);
Evan Cheng242b38b2009-02-23 09:03:22 +00001526 Val = Chain.getValue(0);
1527 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001528 Val = DAG.getNode(ISD::BITCAST, dl, CopyVT, Val);
Evan Cheng79fb3b42009-02-20 20:43:02 +00001529 } else {
1530 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1531 CopyVT, InFlag).getValue(1);
1532 Val = Chain.getValue(0);
1533 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001534 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001535 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001536 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001537
Dan Gohman98ca4f22009-08-05 01:29:28 +00001538 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001539}
1540
1541
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001542//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001543// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001544//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001545// StdCall calling convention seems to be standard for many Windows' API
1546// routines and around. It differs from C calling convention just a little:
1547// callee should clean up the stack, not caller. Symbols should be also
1548// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001549// For info on fast calling convention see Fast Calling Convention (tail call)
1550// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001551
Dan Gohman98ca4f22009-08-05 01:29:28 +00001552/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001553/// semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001554static bool CallIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
1555 if (Outs.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001556 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001557
Dan Gohman98ca4f22009-08-05 01:29:28 +00001558 return Outs[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001559}
1560
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001561/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001562/// return semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001563static bool
1564ArgsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
1565 if (Ins.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001566 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001567
Dan Gohman98ca4f22009-08-05 01:29:28 +00001568 return Ins[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001569}
1570
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001571/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1572/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001573/// the specific parameter attribute. The copy will be passed as a byval
1574/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001575static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001576CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001577 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1578 DebugLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00001579 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Michael J. Spencerec38de22010-10-10 22:04:20 +00001580
Dale Johannesendd64c412009-02-04 00:33:20 +00001581 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Stuart Hastings03d58262011-03-10 00:25:53 +00001582 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001583 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001584}
1585
Chris Lattner29689432010-03-11 00:22:57 +00001586/// IsTailCallConvention - Return true if the calling convention is one that
1587/// supports tail call optimization.
1588static bool IsTailCallConvention(CallingConv::ID CC) {
1589 return (CC == CallingConv::Fast || CC == CallingConv::GHC);
1590}
1591
Evan Cheng0c439eb2010-01-27 00:07:07 +00001592/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1593/// a tailcall target by changing its ABI.
1594static bool FuncIsMadeTailCallSafe(CallingConv::ID CC) {
Chris Lattner29689432010-03-11 00:22:57 +00001595 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001596}
1597
Dan Gohman98ca4f22009-08-05 01:29:28 +00001598SDValue
1599X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001600 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001601 const SmallVectorImpl<ISD::InputArg> &Ins,
1602 DebugLoc dl, SelectionDAG &DAG,
1603 const CCValAssign &VA,
1604 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001605 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001606 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001607 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Evan Cheng0c439eb2010-01-27 00:07:07 +00001608 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001609 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001610 EVT ValVT;
1611
1612 // If value is passed by pointer we have address passed instead of the value
1613 // itself.
1614 if (VA.getLocInfo() == CCValAssign::Indirect)
1615 ValVT = VA.getLocVT();
1616 else
1617 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001618
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001619 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001620 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001621 // In case of tail call optimization mark all arguments mutable. Since they
1622 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001623 if (Flags.isByVal()) {
1624 int FI = MFI->CreateFixedObject(Flags.getByValSize(),
Evan Chenged2ae132010-07-03 00:40:23 +00001625 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001626 return DAG.getFrameIndex(FI, getPointerTy());
1627 } else {
1628 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001629 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001630 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1631 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001632 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00001633 false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001634 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001635}
1636
Dan Gohman475871a2008-07-27 21:46:04 +00001637SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001638X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001639 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001640 bool isVarArg,
1641 const SmallVectorImpl<ISD::InputArg> &Ins,
1642 DebugLoc dl,
1643 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001644 SmallVectorImpl<SDValue> &InVals)
1645 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001646 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001647 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001648
Gordon Henriksen86737662008-01-05 16:56:59 +00001649 const Function* Fn = MF.getFunction();
1650 if (Fn->hasExternalLinkage() &&
1651 Subtarget->isTargetCygMing() &&
1652 Fn->getName() == "main")
1653 FuncInfo->setForceFramePointer(true);
1654
Evan Cheng1bc78042006-04-26 01:20:17 +00001655 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001656 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001657 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001658
Chris Lattner29689432010-03-11 00:22:57 +00001659 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1660 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001661
Chris Lattner638402b2007-02-28 07:00:42 +00001662 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001663 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001664 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1665 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001666
1667 // Allocate shadow area for Win64
1668 if (IsWin64) {
1669 CCInfo.AllocateStack(32, 8);
1670 }
1671
Duncan Sands45907662010-10-31 13:21:44 +00001672 CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001673
Chris Lattnerf39f7712007-02-28 05:46:49 +00001674 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001675 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001676 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1677 CCValAssign &VA = ArgLocs[i];
1678 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1679 // places.
1680 assert(VA.getValNo() != LastVal &&
1681 "Don't support value assigned to multiple locs yet");
1682 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001683
Chris Lattnerf39f7712007-02-28 05:46:49 +00001684 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001685 EVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001686 TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001687 if (RegVT == MVT::i32)
Chris Lattnerf39f7712007-02-28 05:46:49 +00001688 RC = X86::GR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001689 else if (Is64Bit && RegVT == MVT::i64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001690 RC = X86::GR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001691 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001692 RC = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001693 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001694 RC = X86::FR64RegisterClass;
Bruno Cardoso Lopesac098352010-08-05 23:35:51 +00001695 else if (RegVT.isVector() && RegVT.getSizeInBits() == 256)
1696 RC = X86::VR256RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001697 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001698 RC = X86::VR128RegisterClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001699 else if (RegVT == MVT::x86mmx)
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001700 RC = X86::VR64RegisterClass;
1701 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001702 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001703
Devang Patel68e6bee2011-02-21 23:21:26 +00001704 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001705 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001706
Chris Lattnerf39f7712007-02-28 05:46:49 +00001707 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1708 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1709 // right size.
1710 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001711 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001712 DAG.getValueType(VA.getValVT()));
1713 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001714 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001715 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001716 else if (VA.getLocInfo() == CCValAssign::BCvt)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001717 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001718
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001719 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001720 // Handle MMX values passed in XMM regs.
1721 if (RegVT.isVector()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001722 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(),
1723 ArgValue);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001724 } else
1725 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001726 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001727 } else {
1728 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001729 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001730 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001731
1732 // If value is passed via pointer - do a load.
1733 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00001734 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
1735 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001736
Dan Gohman98ca4f22009-08-05 01:29:28 +00001737 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001738 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001739
Dan Gohman61a92132008-04-21 23:59:07 +00001740 // The x86-64 ABI for returning structs by value requires that we copy
1741 // the sret argument into %rax for the return. Save the argument into
1742 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001743 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001744 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1745 unsigned Reg = FuncInfo->getSRetReturnReg();
1746 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001747 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001748 FuncInfo->setSRetReturnReg(Reg);
1749 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001750 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001751 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001752 }
1753
Chris Lattnerf39f7712007-02-28 05:46:49 +00001754 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001755 // Align stack specially for tail calls.
1756 if (FuncIsMadeTailCallSafe(CallConv))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001757 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001758
Evan Cheng1bc78042006-04-26 01:20:17 +00001759 // If the function takes variable number of arguments, make a frame index for
1760 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001761 if (isVarArg) {
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001762 if (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
1763 CallConv != CallingConv::X86_ThisCall)) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00001764 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00001765 }
1766 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001767 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1768
1769 // FIXME: We should really autogenerate these arrays
1770 static const unsigned GPR64ArgRegsWin64[] = {
1771 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001772 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001773 static const unsigned GPR64ArgRegs64Bit[] = {
1774 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1775 };
1776 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001777 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1778 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1779 };
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001780 const unsigned *GPR64ArgRegs;
1781 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001782
1783 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001784 // The XMM registers which might contain var arg parameters are shadowed
1785 // in their paired GPR. So we only need to save the GPR to their home
1786 // slots.
1787 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001788 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001789 } else {
1790 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1791 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001792
1793 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit, TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001794 }
1795 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1796 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001797
Devang Patel578efa92009-06-05 21:57:13 +00001798 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001799 assert(!(NumXMMRegs && !Subtarget->hasXMM()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001800 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001801 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001802 "SSE register cannot be used when SSE is disabled!");
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001803 if (UseSoftFloat || NoImplicitFloatOps || !Subtarget->hasXMM())
Torok Edwin3f142c32009-02-01 18:15:56 +00001804 // Kernel mode asks for SSE to be disabled, so don't push them
1805 // on the stack.
1806 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001807
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001808 if (IsWin64) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001809 const TargetFrameLowering &TFI = *getTargetMachine().getFrameLowering();
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001810 // Get to the caller-allocated home save location. Add 8 to account
1811 // for the return address.
1812 int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001813 FuncInfo->setRegSaveFrameIndex(
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001814 MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001815 // Fixup to set vararg frame on shadow area (4 x i64).
1816 if (NumIntRegs < 4)
1817 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001818 } else {
1819 // For X86-64, if there are vararg parameters that are passed via
1820 // registers, then we must store them to their spots on the stack so they
1821 // may be loaded by deferencing the result of va_next.
1822 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
1823 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
1824 FuncInfo->setRegSaveFrameIndex(
1825 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00001826 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001827 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001828
Gordon Henriksen86737662008-01-05 16:56:59 +00001829 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001830 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00001831 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
1832 getPointerTy());
1833 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001834 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00001835 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
1836 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00001837 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00001838 X86::GR64RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00001839 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001840 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001841 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001842 MachinePointerInfo::getFixedStack(
1843 FuncInfo->getRegSaveFrameIndex(), Offset),
1844 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001845 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001846 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001847 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001848
Dan Gohmanface41a2009-08-16 21:24:25 +00001849 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
1850 // Now store the XMM (fp + vector) parameter registers.
1851 SmallVector<SDValue, 11> SaveXMMOps;
1852 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001853
Devang Patel68e6bee2011-02-21 23:21:26 +00001854 unsigned AL = MF.addLiveIn(X86::AL, X86::GR8RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00001855 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
1856 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001857
Dan Gohman1e93df62010-04-17 14:41:14 +00001858 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1859 FuncInfo->getRegSaveFrameIndex()));
1860 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1861 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00001862
Dan Gohmanface41a2009-08-16 21:24:25 +00001863 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001864 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00001865 X86::VR128RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00001866 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
1867 SaveXMMOps.push_back(Val);
1868 }
1869 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
1870 MVT::Other,
1871 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00001872 }
Dan Gohmanface41a2009-08-16 21:24:25 +00001873
1874 if (!MemOps.empty())
1875 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1876 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001877 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001878 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001879
Gordon Henriksen86737662008-01-05 16:56:59 +00001880 // Some CCs need callee pop.
Dan Gohman4d3d6e12010-05-27 18:43:40 +00001881 if (Subtarget->IsCalleePop(isVarArg, CallConv)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001882 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001883 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00001884 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001885 // If this is an sret function, the return should pop the hidden pointer.
Chris Lattner29689432010-03-11 00:22:57 +00001886 if (!Is64Bit && !IsTailCallConvention(CallConv) && ArgsAreStructReturn(Ins))
Dan Gohman1e93df62010-04-17 14:41:14 +00001887 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001888 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001889
Gordon Henriksen86737662008-01-05 16:56:59 +00001890 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001891 // RegSaveFrameIndex is X86-64 only.
1892 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001893 if (CallConv == CallingConv::X86_FastCall ||
1894 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00001895 // fastcc functions can't have varargs.
1896 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00001897 }
Evan Cheng25caf632006-05-23 21:06:34 +00001898
Dan Gohman98ca4f22009-08-05 01:29:28 +00001899 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001900}
1901
Dan Gohman475871a2008-07-27 21:46:04 +00001902SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001903X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
1904 SDValue StackPtr, SDValue Arg,
1905 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00001906 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001907 ISD::ArgFlagsTy Flags) const {
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001908 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00001909 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00001910 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001911 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00001912 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001913
1914 return DAG.getStore(Chain, dl, Arg, PtrOff,
1915 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00001916 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00001917}
1918
Bill Wendling64e87322009-01-16 19:25:27 +00001919/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001920/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00001921SDValue
1922X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00001923 SDValue &OutRetAddr, SDValue Chain,
1924 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00001925 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001926 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00001927 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001928 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00001929
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001930 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00001931 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
1932 false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001933 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001934}
1935
1936/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1937/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00001938static SDValue
1939EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00001940 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00001941 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001942 // Store the return address to the appropriate stack slot.
1943 if (!FPDiff) return Chain;
1944 // Calculate the new stack slot for the return address.
1945 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00001946 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00001947 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00001948 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00001949 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001950 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00001951 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00001952 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001953 return Chain;
1954}
1955
Dan Gohman98ca4f22009-08-05 01:29:28 +00001956SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001957X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001958 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001959 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001960 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001961 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001962 const SmallVectorImpl<ISD::InputArg> &Ins,
1963 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001964 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001965 MachineFunction &MF = DAG.getMachineFunction();
1966 bool Is64Bit = Subtarget->is64Bit();
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00001967 bool IsWin64 = Subtarget->isTargetWin64();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001968 bool IsStructRet = CallIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00001969 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001970
Evan Cheng5f941932010-02-05 02:21:12 +00001971 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00001972 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00001973 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1974 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001975 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00001976
1977 // Sibcalls are automatically detected tailcalls which do not require
1978 // ABI changes.
Dan Gohman1797ed52010-02-08 20:27:50 +00001979 if (!GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00001980 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00001981
1982 if (isTailCall)
1983 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00001984 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00001985
Chris Lattner29689432010-03-11 00:22:57 +00001986 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1987 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001988
Chris Lattner638402b2007-02-28 07:00:42 +00001989 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00001990 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001991 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1992 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001993
1994 // Allocate shadow area for Win64
1995 if (IsWin64) {
1996 CCInfo.AllocateStack(32, 8);
1997 }
1998
Duncan Sands45907662010-10-31 13:21:44 +00001999 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00002000
Chris Lattner423c5f42007-02-28 05:31:48 +00002001 // Get a count of how many bytes are to be pushed on the stack.
2002 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00002003 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00002004 // This is a sibcall. The memory operands are available in caller's
2005 // own caller's stack.
2006 NumBytes = 0;
Chris Lattner29689432010-03-11 00:22:57 +00002007 else if (GuaranteedTailCallOpt && IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00002008 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002009
Gordon Henriksen86737662008-01-05 16:56:59 +00002010 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00002011 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002012 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00002013 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00002014 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
2015 FPDiff = NumBytesCallerPushed - NumBytes;
2016
2017 // Set the delta of movement of the returnaddr stackslot.
2018 // But only set if delta is greater than previous delta.
2019 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
2020 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
2021 }
2022
Evan Chengf22f9b32010-02-06 03:28:46 +00002023 if (!IsSibcall)
2024 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002025
Dan Gohman475871a2008-07-27 21:46:04 +00002026 SDValue RetAddrFrIdx;
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002027 // Load return adress for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00002028 if (isTailCall && FPDiff)
2029 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
2030 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002031
Dan Gohman475871a2008-07-27 21:46:04 +00002032 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
2033 SmallVector<SDValue, 8> MemOpChains;
2034 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00002035
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002036 // Walk the register/memloc assignments, inserting copies/loads. In the case
2037 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00002038 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2039 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00002040 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00002041 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002042 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00002043 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00002044
Chris Lattner423c5f42007-02-28 05:31:48 +00002045 // Promote the value if needed.
2046 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002047 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00002048 case CCValAssign::Full: break;
2049 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002050 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002051 break;
2052 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002053 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002054 break;
2055 case CCValAssign::AExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002056 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
2057 // Special case: passing MMX values in XMM registers.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002058 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00002059 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
2060 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002061 } else
2062 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
2063 break;
2064 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002065 Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002066 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002067 case CCValAssign::Indirect: {
2068 // Store the argument.
2069 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00002070 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002071 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00002072 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002073 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002074 Arg = SpillSlot;
2075 break;
2076 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00002077 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002078
Chris Lattner423c5f42007-02-28 05:31:48 +00002079 if (VA.isRegLoc()) {
2080 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002081 if (isVarArg && IsWin64) {
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002082 // Win64 ABI requires argument XMM reg to be copied to the corresponding
2083 // shadow reg if callee is a varargs function.
2084 unsigned ShadowReg = 0;
2085 switch (VA.getLocReg()) {
2086 case X86::XMM0: ShadowReg = X86::RCX; break;
2087 case X86::XMM1: ShadowReg = X86::RDX; break;
2088 case X86::XMM2: ShadowReg = X86::R8; break;
2089 case X86::XMM3: ShadowReg = X86::R9; break;
2090 }
2091 if (ShadowReg)
2092 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
2093 }
Evan Chengf22f9b32010-02-06 03:28:46 +00002094 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00002095 assert(VA.isMemLoc());
2096 if (StackPtr.getNode() == 0)
2097 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
2098 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
2099 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002100 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002101 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002102
Evan Cheng32fe1032006-05-25 00:59:30 +00002103 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002104 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002105 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002106
Evan Cheng347d5f72006-04-28 21:29:37 +00002107 // Build a sequence of copy-to-reg nodes chained together with token chain
2108 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002109 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002110 // Tail call byval lowering might overwrite argument registers so in case of
2111 // tail call optimization the copies to registers are lowered later.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002112 if (!isTailCall)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002113 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002114 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002115 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002116 InFlag = Chain.getValue(1);
2117 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002118
Chris Lattner88e1fd52009-07-09 04:24:46 +00002119 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002120 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2121 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002122 if (!isTailCall) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002123 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
2124 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00002125 DebugLoc(), getPointerTy()),
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002126 InFlag);
2127 InFlag = Chain.getValue(1);
2128 } else {
2129 // If we are tail calling and generating PIC/GOT style code load the
2130 // address of the callee into ECX. The value in ecx is used as target of
2131 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2132 // for tail calls on PIC/GOT architectures. Normally we would just put the
2133 // address of GOT into ebx and then call target@PLT. But for tail calls
2134 // ebx would be restored (since ebx is callee saved) before jumping to the
2135 // target@PLT.
2136
2137 // Note: The actual moving to ECX is done further down.
2138 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2139 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2140 !G->getGlobal()->hasProtectedVisibility())
2141 Callee = LowerGlobalAddress(Callee, DAG);
2142 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002143 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002144 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002145 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002146
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002147 if (Is64Bit && isVarArg && !IsWin64) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002148 // From AMD64 ABI document:
2149 // For calls that may call functions that use varargs or stdargs
2150 // (prototype-less calls or calls to functions containing ellipsis (...) in
2151 // the declaration) %al is used as hidden argument to specify the number
2152 // of SSE registers used. The contents of %al do not need to match exactly
2153 // the number of registers, but must be an ubound on the number of SSE
2154 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002155
Gordon Henriksen86737662008-01-05 16:56:59 +00002156 // Count the number of XMM registers allocated.
2157 static const unsigned XMMArgRegs[] = {
2158 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2159 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2160 };
2161 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00002162 assert((Subtarget->hasXMM() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002163 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002164
Dale Johannesendd64c412009-02-04 00:33:20 +00002165 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Owen Anderson825b72b2009-08-11 20:47:22 +00002166 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002167 InFlag = Chain.getValue(1);
2168 }
2169
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002170
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002171 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002172 if (isTailCall) {
2173 // Force all the incoming stack arguments to be loaded from the stack
2174 // before any new outgoing arguments are stored to the stack, because the
2175 // outgoing stack slots may alias the incoming argument stack slots, and
2176 // the alias isn't otherwise explicit. This is slightly more conservative
2177 // than necessary, because it means that each store effectively depends
2178 // on every argument instead of just those arguments it would clobber.
2179 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2180
Dan Gohman475871a2008-07-27 21:46:04 +00002181 SmallVector<SDValue, 8> MemOpChains2;
2182 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002183 int FI = 0;
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002184 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00002185 InFlag = SDValue();
Dan Gohman1797ed52010-02-08 20:27:50 +00002186 if (GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002187 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2188 CCValAssign &VA = ArgLocs[i];
2189 if (VA.isRegLoc())
2190 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002191 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002192 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002193 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002194 // Create frame index.
2195 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002196 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002197 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002198 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002199
Duncan Sands276dcbd2008-03-21 09:14:45 +00002200 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002201 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002202 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002203 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002204 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00002205 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002206 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002207
Dan Gohman98ca4f22009-08-05 01:29:28 +00002208 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2209 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002210 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002211 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002212 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002213 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002214 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002215 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002216 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002217 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002218 }
2219 }
2220
2221 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002222 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002223 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002224
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002225 // Copy arguments to their registers.
2226 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002227 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002228 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002229 InFlag = Chain.getValue(1);
2230 }
Dan Gohman475871a2008-07-27 21:46:04 +00002231 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002232
Gordon Henriksen86737662008-01-05 16:56:59 +00002233 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002234 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002235 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002236 }
2237
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002238 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2239 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2240 // In the 64-bit large code model, we have to make all calls
2241 // through a register, since the call instruction's 32-bit
2242 // pc-relative offset may not be large enough to hold the whole
2243 // address.
2244 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002245 // If the callee is a GlobalAddress node (quite common, every direct call
2246 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2247 // it.
2248
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002249 // We should use extra load for direct calls to dllimported functions in
2250 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002251 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002252 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002253 unsigned char OpFlags = 0;
Eric Christopherfd179292009-08-27 18:07:15 +00002254
Chris Lattner48a7d022009-07-09 05:02:21 +00002255 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2256 // external symbols most go through the PLT in PIC mode. If the symbol
2257 // has hidden or protected visibility, or if it is static or local, then
2258 // we don't need to use the PLT - we can directly call it.
2259 if (Subtarget->isTargetELF() &&
2260 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002261 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002262 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002263 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002264 (GV->isDeclaration() || GV->isWeakForLinker()) &&
2265 Subtarget->getDarwinVers() < 9) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002266 // PC-relative references to external symbols should go through $stub,
2267 // unless we're building with the leopard linker or later, which
2268 // automatically synthesizes these stubs.
2269 OpFlags = X86II::MO_DARWIN_STUB;
2270 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002271
Devang Patel0d881da2010-07-06 22:08:15 +00002272 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002273 G->getOffset(), OpFlags);
2274 }
Bill Wendling056292f2008-09-16 21:48:12 +00002275 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002276 unsigned char OpFlags = 0;
2277
Evan Cheng1bf891a2010-12-01 22:59:46 +00002278 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2279 // external symbols should go through the PLT.
2280 if (Subtarget->isTargetELF() &&
2281 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2282 OpFlags = X86II::MO_PLT;
2283 } else if (Subtarget->isPICStyleStubAny() &&
2284 Subtarget->getDarwinVers() < 9) {
2285 // PC-relative references to external symbols should go through $stub,
2286 // unless we're building with the leopard linker or later, which
2287 // automatically synthesizes these stubs.
2288 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002289 }
Eric Christopherfd179292009-08-27 18:07:15 +00002290
Chris Lattner48a7d022009-07-09 05:02:21 +00002291 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2292 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002293 }
2294
Chris Lattnerd96d0722007-02-25 06:40:16 +00002295 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002296 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002297 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002298
Evan Chengf22f9b32010-02-06 03:28:46 +00002299 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002300 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2301 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002302 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002303 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002304
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002305 Ops.push_back(Chain);
2306 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002307
Dan Gohman98ca4f22009-08-05 01:29:28 +00002308 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002309 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002310
Gordon Henriksen86737662008-01-05 16:56:59 +00002311 // Add argument registers to the end of the list so that they are known live
2312 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002313 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2314 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2315 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002316
Evan Cheng586ccac2008-03-18 23:36:35 +00002317 // Add an implicit use GOT pointer in EBX.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002318 if (!isTailCall && Subtarget->isPICStyleGOT())
Evan Cheng586ccac2008-03-18 23:36:35 +00002319 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
2320
Anton Korobeynikov3a1e54a2010-08-17 21:06:07 +00002321 // Add an implicit use of AL for non-Windows x86 64-bit vararg functions.
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002322 if (Is64Bit && isVarArg && !IsWin64)
Owen Anderson825b72b2009-08-11 20:47:22 +00002323 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
Evan Cheng586ccac2008-03-18 23:36:35 +00002324
Gabor Greifba36cb52008-08-28 21:40:38 +00002325 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002326 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002327
Dan Gohman98ca4f22009-08-05 01:29:28 +00002328 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002329 // We used to do:
2330 //// If this is the first return lowered for this function, add the regs
2331 //// to the liveout set for the function.
2332 // This isn't right, although it's probably harmless on x86; liveouts
2333 // should be computed from returns not tail calls. Consider a void
2334 // function making a tail call to a function returning int.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002335 return DAG.getNode(X86ISD::TC_RETURN, dl,
2336 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002337 }
2338
Dale Johannesenace16102009-02-03 19:33:06 +00002339 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002340 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002341
Chris Lattner2d297092006-05-23 18:50:38 +00002342 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002343 unsigned NumBytesForCalleeToPush;
Dan Gohman4d3d6e12010-05-27 18:43:40 +00002344 if (Subtarget->IsCalleePop(isVarArg, CallConv))
Gordon Henriksen86737662008-01-05 16:56:59 +00002345 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Chris Lattner29689432010-03-11 00:22:57 +00002346 else if (!Is64Bit && !IsTailCallConvention(CallConv) && IsStructRet)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002347 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002348 // pops the hidden struct pointer, so we have to push it back.
2349 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002350 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002351 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002352 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002353
Gordon Henriksenae636f82008-01-03 16:47:34 +00002354 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002355 if (!IsSibcall) {
2356 Chain = DAG.getCALLSEQ_END(Chain,
2357 DAG.getIntPtrConstant(NumBytes, true),
2358 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2359 true),
2360 InFlag);
2361 InFlag = Chain.getValue(1);
2362 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002363
Chris Lattner3085e152007-02-25 08:59:22 +00002364 // Handle result values, copying them out of physregs into vregs that we
2365 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002366 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2367 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002368}
2369
Evan Cheng25ab6902006-09-08 06:48:29 +00002370
2371//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002372// Fast Calling Convention (tail call) implementation
2373//===----------------------------------------------------------------------===//
2374
2375// Like std call, callee cleans arguments, convention except that ECX is
2376// reserved for storing the tail called function address. Only 2 registers are
2377// free for argument passing (inreg). Tail call optimization is performed
2378// provided:
2379// * tailcallopt is enabled
2380// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002381// On X86_64 architecture with GOT-style position independent code only local
2382// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002383// To keep the stack aligned according to platform abi the function
2384// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2385// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002386// If a tail called function callee has more arguments than the caller the
2387// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002388// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002389// original REtADDR, but before the saved framepointer or the spilled registers
2390// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2391// stack layout:
2392// arg1
2393// arg2
2394// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002395// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002396// move area ]
2397// (possible EBP)
2398// ESI
2399// EDI
2400// local1 ..
2401
2402/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2403/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002404unsigned
2405X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2406 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002407 MachineFunction &MF = DAG.getMachineFunction();
2408 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002409 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002410 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002411 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002412 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002413 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002414 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2415 // Number smaller than 12 so just add the difference.
2416 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2417 } else {
2418 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002419 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002420 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002421 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002422 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002423}
2424
Evan Cheng5f941932010-02-05 02:21:12 +00002425/// MatchingStackOffset - Return true if the given stack call argument is
2426/// already available in the same position (relatively) of the caller's
2427/// incoming argument stack.
2428static
2429bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2430 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2431 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002432 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2433 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002434 if (Arg.getOpcode() == ISD::CopyFromReg) {
2435 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00002436 if (!TargetRegisterInfo::isVirtualRegister(VR))
Evan Cheng5f941932010-02-05 02:21:12 +00002437 return false;
2438 MachineInstr *Def = MRI->getVRegDef(VR);
2439 if (!Def)
2440 return false;
2441 if (!Flags.isByVal()) {
2442 if (!TII->isLoadFromStackSlot(Def, FI))
2443 return false;
2444 } else {
2445 unsigned Opcode = Def->getOpcode();
2446 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2447 Def->getOperand(1).isFI()) {
2448 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002449 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002450 } else
2451 return false;
2452 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002453 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2454 if (Flags.isByVal())
2455 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002456 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002457 // define @foo(%struct.X* %A) {
2458 // tail call @bar(%struct.X* byval %A)
2459 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002460 return false;
2461 SDValue Ptr = Ld->getBasePtr();
2462 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2463 if (!FINode)
2464 return false;
2465 FI = FINode->getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002466 } else
2467 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002468
Evan Cheng4cae1332010-03-05 08:38:04 +00002469 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002470 if (!MFI->isFixedObjectIndex(FI))
2471 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002472 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002473}
2474
Dan Gohman98ca4f22009-08-05 01:29:28 +00002475/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2476/// for tail call optimization. Targets which want to do tail call
2477/// optimization should implement this function.
2478bool
2479X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002480 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002481 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002482 bool isCalleeStructRet,
2483 bool isCallerStructRet,
Evan Chengb1712452010-01-27 06:25:16 +00002484 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002485 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002486 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002487 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002488 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002489 CalleeCC != CallingConv::C)
2490 return false;
2491
Evan Cheng7096ae42010-01-29 06:45:59 +00002492 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002493 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002494 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Cheng13617962010-04-30 01:12:32 +00002495 CallingConv::ID CallerCC = CallerF->getCallingConv();
2496 bool CCMatch = CallerCC == CalleeCC;
2497
Dan Gohman1797ed52010-02-08 20:27:50 +00002498 if (GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002499 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002500 return true;
2501 return false;
2502 }
2503
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002504 // Look for obvious safe cases to perform tail call optimization that do not
2505 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002506
Evan Cheng2c12cb42010-03-26 16:26:03 +00002507 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2508 // emit a special epilogue.
2509 if (RegInfo->needsStackRealignment(MF))
2510 return false;
2511
Eric Christopher90eb4022010-07-22 00:26:08 +00002512 // Do not sibcall optimize vararg calls unless the call site is not passing
2513 // any arguments.
Evan Cheng3c262ee2010-03-26 02:13:13 +00002514 if (isVarArg && !Outs.empty())
Evan Cheng843bd692010-01-31 06:44:49 +00002515 return false;
2516
Evan Chenga375d472010-03-15 18:54:48 +00002517 // Also avoid sibcall optimization if either caller or callee uses struct
2518 // return semantics.
2519 if (isCalleeStructRet || isCallerStructRet)
2520 return false;
2521
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002522 // If the call result is in ST0 / ST1, it needs to be popped off the x87 stack.
2523 // Therefore if it's not used by the call it is not safe to optimize this into
2524 // a sibcall.
2525 bool Unused = false;
2526 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2527 if (!Ins[i].Used) {
2528 Unused = true;
2529 break;
2530 }
2531 }
2532 if (Unused) {
2533 SmallVector<CCValAssign, 16> RVLocs;
2534 CCState CCInfo(CalleeCC, false, getTargetMachine(),
2535 RVLocs, *DAG.getContext());
2536 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002537 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002538 CCValAssign &VA = RVLocs[i];
2539 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2540 return false;
2541 }
2542 }
2543
Evan Cheng13617962010-04-30 01:12:32 +00002544 // If the calling conventions do not match, then we'd better make sure the
2545 // results are returned in the same way as what the caller expects.
2546 if (!CCMatch) {
2547 SmallVector<CCValAssign, 16> RVLocs1;
2548 CCState CCInfo1(CalleeCC, false, getTargetMachine(),
2549 RVLocs1, *DAG.getContext());
2550 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2551
2552 SmallVector<CCValAssign, 16> RVLocs2;
2553 CCState CCInfo2(CallerCC, false, getTargetMachine(),
2554 RVLocs2, *DAG.getContext());
2555 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2556
2557 if (RVLocs1.size() != RVLocs2.size())
2558 return false;
2559 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2560 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2561 return false;
2562 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2563 return false;
2564 if (RVLocs1[i].isRegLoc()) {
2565 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2566 return false;
2567 } else {
2568 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2569 return false;
2570 }
2571 }
2572 }
2573
Evan Chenga6bff982010-01-30 01:22:00 +00002574 // If the callee takes no arguments then go on to check the results of the
2575 // call.
2576 if (!Outs.empty()) {
2577 // Check if stack adjustment is needed. For now, do not do this if any
2578 // argument is passed on the stack.
2579 SmallVector<CCValAssign, 16> ArgLocs;
2580 CCState CCInfo(CalleeCC, isVarArg, getTargetMachine(),
2581 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002582
2583 // Allocate shadow area for Win64
2584 if (Subtarget->isTargetWin64()) {
2585 CCInfo.AllocateStack(32, 8);
2586 }
2587
Duncan Sands45907662010-10-31 13:21:44 +00002588 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Evan Chengb2c92902010-02-02 02:22:50 +00002589 if (CCInfo.getNextStackOffset()) {
2590 MachineFunction &MF = DAG.getMachineFunction();
2591 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2592 return false;
Evan Chengb2c92902010-02-02 02:22:50 +00002593
2594 // Check if the arguments are already laid out in the right way as
2595 // the caller's fixed stack objects.
2596 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002597 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2598 const X86InstrInfo *TII =
2599 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002600 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2601 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002602 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002603 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002604 if (VA.getLocInfo() == CCValAssign::Indirect)
2605 return false;
2606 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002607 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2608 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002609 return false;
2610 }
2611 }
2612 }
Evan Cheng9c044672010-05-29 01:35:22 +00002613
2614 // If the tailcall address may be in a register, then make sure it's
2615 // possible to register allocate for it. In 32-bit, the call address can
2616 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002617 // callee-saved registers are restored. These happen to be the same
2618 // registers used to pass 'inreg' arguments so watch out for those.
2619 if (!Subtarget->is64Bit() &&
2620 !isa<GlobalAddressSDNode>(Callee) &&
Evan Cheng9c044672010-05-29 01:35:22 +00002621 !isa<ExternalSymbolSDNode>(Callee)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002622 unsigned NumInRegs = 0;
2623 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2624 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002625 if (!VA.isRegLoc())
2626 continue;
2627 unsigned Reg = VA.getLocReg();
2628 switch (Reg) {
2629 default: break;
2630 case X86::EAX: case X86::EDX: case X86::ECX:
2631 if (++NumInRegs == 3)
Evan Cheng9c044672010-05-29 01:35:22 +00002632 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002633 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002634 }
2635 }
2636 }
Evan Chenga6bff982010-01-30 01:22:00 +00002637 }
Evan Chengb1712452010-01-27 06:25:16 +00002638
Dale Johannesend155d7e2010-10-25 22:17:05 +00002639 // An stdcall caller is expected to clean up its arguments; the callee
Dale Johannesen0e034562010-11-12 00:43:18 +00002640 // isn't going to do that.
Dale Johannesend155d7e2010-10-25 22:17:05 +00002641 if (!CCMatch && CallerCC==CallingConv::X86_StdCall)
2642 return false;
2643
Evan Cheng86809cc2010-02-03 03:28:02 +00002644 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002645}
2646
Dan Gohman3df24e62008-09-03 23:12:08 +00002647FastISel *
Dan Gohmana4160c32010-07-07 16:29:44 +00002648X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
2649 return X86::createFastISel(funcInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00002650}
2651
2652
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002653//===----------------------------------------------------------------------===//
2654// Other Lowering Hooks
2655//===----------------------------------------------------------------------===//
2656
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00002657static bool MayFoldLoad(SDValue Op) {
2658 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
2659}
2660
2661static bool MayFoldIntoStore(SDValue Op) {
2662 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
2663}
2664
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002665static bool isTargetShuffle(unsigned Opcode) {
2666 switch(Opcode) {
2667 default: return false;
2668 case X86ISD::PSHUFD:
2669 case X86ISD::PSHUFHW:
2670 case X86ISD::PSHUFLW:
2671 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002672 case X86ISD::PALIGN:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002673 case X86ISD::SHUFPS:
2674 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002675 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002676 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002677 case X86ISD::MOVLPS:
2678 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002679 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002680 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002681 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002682 case X86ISD::MOVSS:
2683 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002684 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002685 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +00002686 case X86ISD::VUNPCKLPS:
2687 case X86ISD::VUNPCKLPD:
2688 case X86ISD::VUNPCKLPSY:
2689 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002690 case X86ISD::PUNPCKLWD:
2691 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002692 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002693 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002694 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002695 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002696 case X86ISD::PUNPCKHWD:
2697 case X86ISD::PUNPCKHBW:
2698 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002699 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002700 return true;
2701 }
2702 return false;
2703}
2704
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002705static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002706 SDValue V1, SelectionDAG &DAG) {
2707 switch(Opc) {
2708 default: llvm_unreachable("Unknown x86 shuffle node");
2709 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002710 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002711 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002712 return DAG.getNode(Opc, dl, VT, V1);
2713 }
2714
2715 return SDValue();
2716}
2717
2718static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00002719 SDValue V1, unsigned TargetMask, SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002720 switch(Opc) {
2721 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002722 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002723 case X86ISD::PSHUFHW:
2724 case X86ISD::PSHUFLW:
2725 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
2726 }
2727
2728 return SDValue();
2729}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002730
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002731static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2732 SDValue V1, SDValue V2, unsigned TargetMask, SelectionDAG &DAG) {
2733 switch(Opc) {
2734 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002735 case X86ISD::PALIGN:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002736 case X86ISD::SHUFPD:
2737 case X86ISD::SHUFPS:
2738 return DAG.getNode(Opc, dl, VT, V1, V2,
2739 DAG.getConstant(TargetMask, MVT::i8));
2740 }
2741 return SDValue();
2742}
2743
2744static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2745 SDValue V1, SDValue V2, SelectionDAG &DAG) {
2746 switch(Opc) {
2747 default: llvm_unreachable("Unknown x86 shuffle node");
2748 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00002749 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002750 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002751 case X86ISD::MOVLPS:
2752 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002753 case X86ISD::MOVSS:
2754 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002755 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002756 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +00002757 case X86ISD::VUNPCKLPS:
2758 case X86ISD::VUNPCKLPD:
2759 case X86ISD::VUNPCKLPSY:
2760 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002761 case X86ISD::PUNPCKLWD:
2762 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002763 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002764 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002765 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002766 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002767 case X86ISD::PUNPCKHWD:
2768 case X86ISD::PUNPCKHBW:
2769 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002770 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002771 return DAG.getNode(Opc, dl, VT, V1, V2);
2772 }
2773 return SDValue();
2774}
2775
Dan Gohmand858e902010-04-17 15:26:15 +00002776SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002777 MachineFunction &MF = DAG.getMachineFunction();
2778 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2779 int ReturnAddrIndex = FuncInfo->getRAIndex();
2780
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002781 if (ReturnAddrIndex == 0) {
2782 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002783 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002784 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00002785 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002786 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002787 }
2788
Evan Cheng25ab6902006-09-08 06:48:29 +00002789 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002790}
2791
2792
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002793bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2794 bool hasSymbolicDisplacement) {
2795 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00002796 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002797 return false;
2798
2799 // If we don't have a symbolic displacement - we don't have any extra
2800 // restrictions.
2801 if (!hasSymbolicDisplacement)
2802 return true;
2803
2804 // FIXME: Some tweaks might be needed for medium code model.
2805 if (M != CodeModel::Small && M != CodeModel::Kernel)
2806 return false;
2807
2808 // For small code model we assume that latest object is 16MB before end of 31
2809 // bits boundary. We may also accept pretty large negative constants knowing
2810 // that all objects are in the positive half of address space.
2811 if (M == CodeModel::Small && Offset < 16*1024*1024)
2812 return true;
2813
2814 // For kernel code model we know that all object resist in the negative half
2815 // of 32bits address space. We may not accept negative offsets, since they may
2816 // be just off and we may accept pretty large positive ones.
2817 if (M == CodeModel::Kernel && Offset > 0)
2818 return true;
2819
2820 return false;
2821}
2822
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002823/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2824/// specific condition code, returning the condition code and the LHS/RHS of the
2825/// comparison to make.
2826static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2827 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00002828 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002829 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2830 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2831 // X > -1 -> X == 0, jump !sign.
2832 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002833 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002834 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2835 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002836 return X86::COND_S;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002837 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00002838 // X < 1 -> X <= 0
2839 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002840 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002841 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002842 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002843
Evan Chengd9558e02006-01-06 00:43:03 +00002844 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002845 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002846 case ISD::SETEQ: return X86::COND_E;
2847 case ISD::SETGT: return X86::COND_G;
2848 case ISD::SETGE: return X86::COND_GE;
2849 case ISD::SETLT: return X86::COND_L;
2850 case ISD::SETLE: return X86::COND_LE;
2851 case ISD::SETNE: return X86::COND_NE;
2852 case ISD::SETULT: return X86::COND_B;
2853 case ISD::SETUGT: return X86::COND_A;
2854 case ISD::SETULE: return X86::COND_BE;
2855 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00002856 }
Chris Lattner4c78e022008-12-23 23:42:27 +00002857 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002858
Chris Lattner4c78e022008-12-23 23:42:27 +00002859 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00002860
Chris Lattner4c78e022008-12-23 23:42:27 +00002861 // If LHS is a foldable load, but RHS is not, flip the condition.
Rafael Espindolaf297c932011-02-03 03:58:05 +00002862 if (ISD::isNON_EXTLoad(LHS.getNode()) &&
2863 !ISD::isNON_EXTLoad(RHS.getNode())) {
Chris Lattner4c78e022008-12-23 23:42:27 +00002864 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
2865 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00002866 }
2867
Chris Lattner4c78e022008-12-23 23:42:27 +00002868 switch (SetCCOpcode) {
2869 default: break;
2870 case ISD::SETOLT:
2871 case ISD::SETOLE:
2872 case ISD::SETUGT:
2873 case ISD::SETUGE:
2874 std::swap(LHS, RHS);
2875 break;
2876 }
2877
2878 // On a floating point condition, the flags are set as follows:
2879 // ZF PF CF op
2880 // 0 | 0 | 0 | X > Y
2881 // 0 | 0 | 1 | X < Y
2882 // 1 | 0 | 0 | X == Y
2883 // 1 | 1 | 1 | unordered
2884 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002885 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00002886 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002887 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00002888 case ISD::SETOLT: // flipped
2889 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002890 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00002891 case ISD::SETOLE: // flipped
2892 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002893 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002894 case ISD::SETUGT: // flipped
2895 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002896 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00002897 case ISD::SETUGE: // flipped
2898 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002899 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002900 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002901 case ISD::SETNE: return X86::COND_NE;
2902 case ISD::SETUO: return X86::COND_P;
2903 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00002904 case ISD::SETOEQ:
2905 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00002906 }
Evan Chengd9558e02006-01-06 00:43:03 +00002907}
2908
Evan Cheng4a460802006-01-11 00:33:36 +00002909/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2910/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00002911/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00002912static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00002913 switch (X86CC) {
2914 default:
2915 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00002916 case X86::COND_B:
2917 case X86::COND_BE:
2918 case X86::COND_E:
2919 case X86::COND_P:
2920 case X86::COND_A:
2921 case X86::COND_AE:
2922 case X86::COND_NE:
2923 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00002924 return true;
2925 }
2926}
2927
Evan Chengeb2f9692009-10-27 19:56:55 +00002928/// isFPImmLegal - Returns true if the target can instruction select the
2929/// specified FP immediate natively. If false, the legalizer will
2930/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00002931bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00002932 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
2933 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
2934 return true;
2935 }
2936 return false;
2937}
2938
Nate Begeman9008ca62009-04-27 18:41:29 +00002939/// isUndefOrInRange - Return true if Val is undef or if its value falls within
2940/// the specified range (L, H].
2941static bool isUndefOrInRange(int Val, int Low, int Hi) {
2942 return (Val < 0) || (Val >= Low && Val < Hi);
2943}
2944
2945/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
2946/// specified value.
2947static bool isUndefOrEqual(int Val, int CmpVal) {
2948 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002949 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00002950 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00002951}
2952
Nate Begeman9008ca62009-04-27 18:41:29 +00002953/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
2954/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
2955/// the second operand.
Owen Andersone50ed302009-08-10 22:56:29 +00002956static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00002957 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00002958 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002959 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00002960 return (Mask[0] < 2 && Mask[1] < 2);
2961 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002962}
2963
Nate Begeman9008ca62009-04-27 18:41:29 +00002964bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002965 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002966 N->getMask(M);
2967 return ::isPSHUFDMask(M, N->getValueType(0));
2968}
Evan Cheng0188ecb2006-03-22 18:59:22 +00002969
Nate Begeman9008ca62009-04-27 18:41:29 +00002970/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
2971/// is suitable for input to PSHUFHW.
Owen Andersone50ed302009-08-10 22:56:29 +00002972static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002973 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00002974 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002975
Nate Begeman9008ca62009-04-27 18:41:29 +00002976 // Lower quadword copied in order or undef.
2977 for (int i = 0; i != 4; ++i)
2978 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00002979 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002980
Evan Cheng506d3df2006-03-29 23:07:14 +00002981 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002982 for (int i = 4; i != 8; ++i)
2983 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00002984 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002985
Evan Cheng506d3df2006-03-29 23:07:14 +00002986 return true;
2987}
2988
Nate Begeman9008ca62009-04-27 18:41:29 +00002989bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002990 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002991 N->getMask(M);
2992 return ::isPSHUFHWMask(M, N->getValueType(0));
2993}
Evan Cheng506d3df2006-03-29 23:07:14 +00002994
Nate Begeman9008ca62009-04-27 18:41:29 +00002995/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
2996/// is suitable for input to PSHUFLW.
Owen Andersone50ed302009-08-10 22:56:29 +00002997static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002998 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00002999 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003000
Rafael Espindola15684b22009-04-24 12:40:33 +00003001 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00003002 for (int i = 4; i != 8; ++i)
3003 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00003004 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003005
Rafael Espindola15684b22009-04-24 12:40:33 +00003006 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00003007 for (int i = 0; i != 4; ++i)
3008 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00003009 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003010
Rafael Espindola15684b22009-04-24 12:40:33 +00003011 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003012}
3013
Nate Begeman9008ca62009-04-27 18:41:29 +00003014bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003015 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003016 N->getMask(M);
3017 return ::isPSHUFLWMask(M, N->getValueType(0));
3018}
3019
Nate Begemana09008b2009-10-19 02:17:23 +00003020/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
3021/// is suitable for input to PALIGNR.
3022static bool isPALIGNRMask(const SmallVectorImpl<int> &Mask, EVT VT,
3023 bool hasSSSE3) {
3024 int i, e = VT.getVectorNumElements();
Michael J. Spencerec38de22010-10-10 22:04:20 +00003025
Nate Begemana09008b2009-10-19 02:17:23 +00003026 // Do not handle v2i64 / v2f64 shuffles with palignr.
3027 if (e < 4 || !hasSSSE3)
3028 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003029
Nate Begemana09008b2009-10-19 02:17:23 +00003030 for (i = 0; i != e; ++i)
3031 if (Mask[i] >= 0)
3032 break;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003033
Nate Begemana09008b2009-10-19 02:17:23 +00003034 // All undef, not a palignr.
3035 if (i == e)
3036 return false;
3037
3038 // Determine if it's ok to perform a palignr with only the LHS, since we
3039 // don't have access to the actual shuffle elements to see if RHS is undef.
3040 bool Unary = Mask[i] < (int)e;
3041 bool NeedsUnary = false;
3042
3043 int s = Mask[i] - i;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003044
Nate Begemana09008b2009-10-19 02:17:23 +00003045 // Check the rest of the elements to see if they are consecutive.
3046 for (++i; i != e; ++i) {
3047 int m = Mask[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00003048 if (m < 0)
Nate Begemana09008b2009-10-19 02:17:23 +00003049 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003050
Nate Begemana09008b2009-10-19 02:17:23 +00003051 Unary = Unary && (m < (int)e);
3052 NeedsUnary = NeedsUnary || (m < s);
3053
3054 if (NeedsUnary && !Unary)
3055 return false;
3056 if (Unary && m != ((s+i) & (e-1)))
3057 return false;
3058 if (!Unary && m != (s+i))
3059 return false;
3060 }
3061 return true;
3062}
3063
3064bool X86::isPALIGNRMask(ShuffleVectorSDNode *N) {
3065 SmallVector<int, 8> M;
3066 N->getMask(M);
3067 return ::isPALIGNRMask(M, N->getValueType(0), true);
3068}
3069
Evan Cheng14aed5e2006-03-24 01:18:28 +00003070/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
3071/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Owen Andersone50ed302009-08-10 22:56:29 +00003072static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003073 int NumElems = VT.getVectorNumElements();
3074 if (NumElems != 2 && NumElems != 4)
3075 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003076
Nate Begeman9008ca62009-04-27 18:41:29 +00003077 int Half = NumElems / 2;
3078 for (int i = 0; i < Half; ++i)
3079 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003080 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003081 for (int i = Half; i < NumElems; ++i)
3082 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00003083 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003084
Evan Cheng14aed5e2006-03-24 01:18:28 +00003085 return true;
3086}
3087
Nate Begeman9008ca62009-04-27 18:41:29 +00003088bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
3089 SmallVector<int, 8> M;
3090 N->getMask(M);
3091 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003092}
3093
Evan Cheng213d2cf2007-05-17 18:45:50 +00003094/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00003095/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
3096/// half elements to come from vector 1 (which would equal the dest.) and
3097/// the upper half to come from vector 2.
Owen Andersone50ed302009-08-10 22:56:29 +00003098static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003099 int NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003100
3101 if (NumElems != 2 && NumElems != 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00003102 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003103
Nate Begeman9008ca62009-04-27 18:41:29 +00003104 int Half = NumElems / 2;
3105 for (int i = 0; i < Half; ++i)
3106 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00003107 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003108 for (int i = Half; i < NumElems; ++i)
3109 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003110 return false;
3111 return true;
3112}
3113
Nate Begeman9008ca62009-04-27 18:41:29 +00003114static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
3115 SmallVector<int, 8> M;
3116 N->getMask(M);
3117 return isCommutedSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003118}
3119
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003120/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
3121/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00003122bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
3123 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003124 return false;
3125
Evan Cheng2064a2b2006-03-28 06:50:32 +00003126 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00003127 return isUndefOrEqual(N->getMaskElt(0), 6) &&
3128 isUndefOrEqual(N->getMaskElt(1), 7) &&
3129 isUndefOrEqual(N->getMaskElt(2), 2) &&
3130 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003131}
3132
Nate Begeman0b10b912009-11-07 23:17:15 +00003133/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3134/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3135/// <2, 3, 2, 3>
3136bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
3137 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Michael J. Spencerec38de22010-10-10 22:04:20 +00003138
Nate Begeman0b10b912009-11-07 23:17:15 +00003139 if (NumElems != 4)
3140 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003141
Nate Begeman0b10b912009-11-07 23:17:15 +00003142 return isUndefOrEqual(N->getMaskElt(0), 2) &&
3143 isUndefOrEqual(N->getMaskElt(1), 3) &&
3144 isUndefOrEqual(N->getMaskElt(2), 2) &&
3145 isUndefOrEqual(N->getMaskElt(3), 3);
3146}
3147
Evan Cheng5ced1d82006-04-06 23:23:56 +00003148/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3149/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00003150bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
3151 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003152
Evan Cheng5ced1d82006-04-06 23:23:56 +00003153 if (NumElems != 2 && NumElems != 4)
3154 return false;
3155
Evan Chengc5cdff22006-04-07 21:53:05 +00003156 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003157 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003158 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003159
Evan Chengc5cdff22006-04-07 21:53:05 +00003160 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003161 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003162 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003163
3164 return true;
3165}
3166
Nate Begeman0b10b912009-11-07 23:17:15 +00003167/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3168/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
3169bool X86::isMOVLHPSMask(ShuffleVectorSDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003170 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003171
David Greenea20244d2011-03-02 17:23:43 +00003172 if ((NumElems != 2 && NumElems != 4)
3173 || N->getValueType(0).getSizeInBits() > 128)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003174 return false;
3175
Evan Chengc5cdff22006-04-07 21:53:05 +00003176 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003177 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003178 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003179
Nate Begeman9008ca62009-04-27 18:41:29 +00003180 for (unsigned i = 0; i < NumElems/2; ++i)
3181 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003182 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003183
3184 return true;
3185}
3186
Evan Cheng0038e592006-03-28 00:39:58 +00003187/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3188/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Owen Andersone50ed302009-08-10 22:56:29 +00003189static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003190 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003191 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003192 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng0038e592006-03-28 00:39:58 +00003193 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003194
David Greenea20244d2011-03-02 17:23:43 +00003195 // Handle vector lengths > 128 bits. Define a "section" as a set of
3196 // 128 bits. AVX defines UNPCK* to operate independently on 128-bit
3197 // sections.
3198 unsigned NumSections = VT.getSizeInBits() / 128;
3199 if (NumSections == 0 ) NumSections = 1; // Handle MMX
3200 unsigned NumSectionElts = NumElts / NumSections;
3201
3202 unsigned Start = 0;
3203 unsigned End = NumSectionElts;
3204 for (unsigned s = 0; s < NumSections; ++s) {
3205 for (unsigned i = Start, j = s * NumSectionElts;
3206 i != End;
3207 i += 2, ++j) {
3208 int BitI = Mask[i];
3209 int BitI1 = Mask[i+1];
3210 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003211 return false;
David Greenea20244d2011-03-02 17:23:43 +00003212 if (V2IsSplat) {
3213 if (!isUndefOrEqual(BitI1, NumElts))
3214 return false;
3215 } else {
3216 if (!isUndefOrEqual(BitI1, j + NumElts))
3217 return false;
3218 }
Evan Cheng39623da2006-04-20 08:58:49 +00003219 }
David Greenea20244d2011-03-02 17:23:43 +00003220 // Process the next 128 bits.
3221 Start += NumSectionElts;
3222 End += NumSectionElts;
Evan Cheng0038e592006-03-28 00:39:58 +00003223 }
David Greenea20244d2011-03-02 17:23:43 +00003224
Evan Cheng0038e592006-03-28 00:39:58 +00003225 return true;
3226}
3227
Nate Begeman9008ca62009-04-27 18:41:29 +00003228bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3229 SmallVector<int, 8> M;
3230 N->getMask(M);
3231 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003232}
3233
Evan Cheng4fcb9222006-03-28 02:43:26 +00003234/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3235/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Eric Christopherfd179292009-08-27 18:07:15 +00003236static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003237 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003238 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003239 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng4fcb9222006-03-28 02:43:26 +00003240 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003241
Nate Begeman9008ca62009-04-27 18:41:29 +00003242 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
3243 int BitI = Mask[i];
3244 int BitI1 = Mask[i+1];
Chris Lattner5a88b832007-02-25 07:10:00 +00003245 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengc5cdff22006-04-07 21:53:05 +00003246 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00003247 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00003248 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003249 return false;
3250 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00003251 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003252 return false;
3253 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003254 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003255 return true;
3256}
3257
Nate Begeman9008ca62009-04-27 18:41:29 +00003258bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3259 SmallVector<int, 8> M;
3260 N->getMask(M);
3261 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003262}
3263
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003264/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3265/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3266/// <0, 0, 1, 1>
Owen Andersone50ed302009-08-10 22:56:29 +00003267static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003268 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003269 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003270 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003271
David Greenea20244d2011-03-02 17:23:43 +00003272 // Handle vector lengths > 128 bits. Define a "section" as a set of
3273 // 128 bits. AVX defines UNPCK* to operate independently on 128-bit
3274 // sections.
3275 unsigned NumSections = VT.getSizeInBits() / 128;
3276 if (NumSections == 0 ) NumSections = 1; // Handle MMX
3277 unsigned NumSectionElts = NumElems / NumSections;
3278
3279 for (unsigned s = 0; s < NumSections; ++s) {
3280 for (unsigned i = s * NumSectionElts, j = s * NumSectionElts;
3281 i != NumSectionElts * (s + 1);
3282 i += 2, ++j) {
3283 int BitI = Mask[i];
3284 int BitI1 = Mask[i+1];
3285
3286 if (!isUndefOrEqual(BitI, j))
3287 return false;
3288 if (!isUndefOrEqual(BitI1, j))
3289 return false;
3290 }
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003291 }
David Greenea20244d2011-03-02 17:23:43 +00003292
Rafael Espindola15684b22009-04-24 12:40:33 +00003293 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003294}
3295
Nate Begeman9008ca62009-04-27 18:41:29 +00003296bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
3297 SmallVector<int, 8> M;
3298 N->getMask(M);
3299 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
3300}
3301
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003302/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3303/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3304/// <2, 2, 3, 3>
Owen Andersone50ed302009-08-10 22:56:29 +00003305static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003306 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003307 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
3308 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003309
Nate Begeman9008ca62009-04-27 18:41:29 +00003310 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
3311 int BitI = Mask[i];
3312 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003313 if (!isUndefOrEqual(BitI, j))
3314 return false;
3315 if (!isUndefOrEqual(BitI1, j))
3316 return false;
3317 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003318 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003319}
3320
Nate Begeman9008ca62009-04-27 18:41:29 +00003321bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
3322 SmallVector<int, 8> M;
3323 N->getMask(M);
3324 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
3325}
3326
Evan Cheng017dcc62006-04-21 01:05:10 +00003327/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3328/// specifies a shuffle of elements that is suitable for input to MOVSS,
3329/// MOVSD, and MOVD, i.e. setting the lowest element.
Owen Andersone50ed302009-08-10 22:56:29 +00003330static bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003331 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003332 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003333
3334 int NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003335
Nate Begeman9008ca62009-04-27 18:41:29 +00003336 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003337 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003338
Nate Begeman9008ca62009-04-27 18:41:29 +00003339 for (int i = 1; i < NumElts; ++i)
3340 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003341 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003342
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003343 return true;
3344}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003345
Nate Begeman9008ca62009-04-27 18:41:29 +00003346bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
3347 SmallVector<int, 8> M;
3348 N->getMask(M);
3349 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003350}
3351
Evan Cheng017dcc62006-04-21 01:05:10 +00003352/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
3353/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003354/// element of vector 2 and the other elements to come from vector 1 in order.
Owen Andersone50ed302009-08-10 22:56:29 +00003355static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003356 bool V2IsSplat = false, bool V2IsUndef = false) {
3357 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003358 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003359 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003360
Nate Begeman9008ca62009-04-27 18:41:29 +00003361 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003362 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003363
Nate Begeman9008ca62009-04-27 18:41:29 +00003364 for (int i = 1; i < NumOps; ++i)
3365 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3366 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3367 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003368 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003369
Evan Cheng39623da2006-04-20 08:58:49 +00003370 return true;
3371}
3372
Nate Begeman9008ca62009-04-27 18:41:29 +00003373static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00003374 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003375 SmallVector<int, 8> M;
3376 N->getMask(M);
3377 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00003378}
3379
Evan Chengd9539472006-04-14 21:59:03 +00003380/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3381/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003382bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N) {
3383 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003384 return false;
3385
3386 // Expect 1, 1, 3, 3
Rafael Espindola15684b22009-04-24 12:40:33 +00003387 for (unsigned i = 0; i < 2; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003388 int Elt = N->getMaskElt(i);
3389 if (Elt >= 0 && Elt != 1)
3390 return false;
Rafael Espindola15684b22009-04-24 12:40:33 +00003391 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003392
3393 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003394 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003395 int Elt = N->getMaskElt(i);
3396 if (Elt >= 0 && Elt != 3)
3397 return false;
3398 if (Elt == 3)
3399 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003400 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003401 // Don't use movshdup if it can be done with a shufps.
Nate Begeman9008ca62009-04-27 18:41:29 +00003402 // FIXME: verify that matching u, u, 3, 3 is what we want.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003403 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003404}
3405
3406/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3407/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003408bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N) {
3409 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003410 return false;
3411
3412 // Expect 0, 0, 2, 2
Nate Begeman9008ca62009-04-27 18:41:29 +00003413 for (unsigned i = 0; i < 2; ++i)
3414 if (N->getMaskElt(i) > 0)
3415 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003416
3417 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003418 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003419 int Elt = N->getMaskElt(i);
3420 if (Elt >= 0 && Elt != 2)
3421 return false;
3422 if (Elt == 2)
3423 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003424 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003425 // Don't use movsldup if it can be done with a shufps.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003426 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003427}
3428
Evan Cheng0b457f02008-09-25 20:50:48 +00003429/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3430/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003431bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
3432 int e = N->getValueType(0).getVectorNumElements() / 2;
Eric Christopherfd179292009-08-27 18:07:15 +00003433
Nate Begeman9008ca62009-04-27 18:41:29 +00003434 for (int i = 0; i < e; ++i)
3435 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003436 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003437 for (int i = 0; i < e; ++i)
3438 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003439 return false;
3440 return true;
3441}
3442
David Greenec38a03e2011-02-03 15:50:00 +00003443/// isVEXTRACTF128Index - Return true if the specified
3444/// EXTRACT_SUBVECTOR operand specifies a vector extract that is
3445/// suitable for input to VEXTRACTF128.
3446bool X86::isVEXTRACTF128Index(SDNode *N) {
3447 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3448 return false;
3449
3450 // The index should be aligned on a 128-bit boundary.
3451 uint64_t Index =
3452 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
3453
3454 unsigned VL = N->getValueType(0).getVectorNumElements();
3455 unsigned VBits = N->getValueType(0).getSizeInBits();
3456 unsigned ElSize = VBits / VL;
3457 bool Result = (Index * ElSize) % 128 == 0;
3458
3459 return Result;
3460}
3461
David Greeneccacdc12011-02-04 16:08:29 +00003462/// isVINSERTF128Index - Return true if the specified INSERT_SUBVECTOR
3463/// operand specifies a subvector insert that is suitable for input to
3464/// VINSERTF128.
3465bool X86::isVINSERTF128Index(SDNode *N) {
3466 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
3467 return false;
3468
3469 // The index should be aligned on a 128-bit boundary.
3470 uint64_t Index =
3471 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
3472
3473 unsigned VL = N->getValueType(0).getVectorNumElements();
3474 unsigned VBits = N->getValueType(0).getSizeInBits();
3475 unsigned ElSize = VBits / VL;
3476 bool Result = (Index * ElSize) % 128 == 0;
3477
3478 return Result;
3479}
3480
Evan Cheng63d33002006-03-22 08:01:21 +00003481/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003482/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Evan Cheng63d33002006-03-22 08:01:21 +00003483unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003484 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3485 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
3486
Evan Chengb9df0ca2006-03-22 02:53:00 +00003487 unsigned Shift = (NumOperands == 4) ? 2 : 1;
3488 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00003489 for (int i = 0; i < NumOperands; ++i) {
3490 int Val = SVOp->getMaskElt(NumOperands-i-1);
3491 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00003492 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00003493 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00003494 if (i != NumOperands - 1)
3495 Mask <<= Shift;
3496 }
Evan Cheng63d33002006-03-22 08:01:21 +00003497 return Mask;
3498}
3499
Evan Cheng506d3df2006-03-29 23:07:14 +00003500/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003501/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003502unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003503 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003504 unsigned Mask = 0;
3505 // 8 nodes, but we only care about the last 4.
3506 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003507 int Val = SVOp->getMaskElt(i);
3508 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00003509 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00003510 if (i != 4)
3511 Mask <<= 2;
3512 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003513 return Mask;
3514}
3515
3516/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003517/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003518unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003519 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003520 unsigned Mask = 0;
3521 // 8 nodes, but we only care about the first 4.
3522 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003523 int Val = SVOp->getMaskElt(i);
3524 if (Val >= 0)
3525 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00003526 if (i != 0)
3527 Mask <<= 2;
3528 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003529 return Mask;
3530}
3531
Nate Begemana09008b2009-10-19 02:17:23 +00003532/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
3533/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
3534unsigned X86::getShufflePALIGNRImmediate(SDNode *N) {
3535 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3536 EVT VVT = N->getValueType(0);
3537 unsigned EltSize = VVT.getVectorElementType().getSizeInBits() >> 3;
3538 int Val = 0;
3539
3540 unsigned i, e;
3541 for (i = 0, e = VVT.getVectorNumElements(); i != e; ++i) {
3542 Val = SVOp->getMaskElt(i);
3543 if (Val >= 0)
3544 break;
3545 }
3546 return (Val - i) * EltSize;
3547}
3548
David Greenec38a03e2011-02-03 15:50:00 +00003549/// getExtractVEXTRACTF128Immediate - Return the appropriate immediate
3550/// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF128
3551/// instructions.
3552unsigned X86::getExtractVEXTRACTF128Immediate(SDNode *N) {
3553 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3554 llvm_unreachable("Illegal extract subvector for VEXTRACTF128");
3555
3556 uint64_t Index =
3557 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
3558
3559 EVT VecVT = N->getOperand(0).getValueType();
3560 EVT ElVT = VecVT.getVectorElementType();
3561
3562 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
3563
3564 return Index / NumElemsPerChunk;
3565}
3566
David Greeneccacdc12011-02-04 16:08:29 +00003567/// getInsertVINSERTF128Immediate - Return the appropriate immediate
3568/// to insert at the specified INSERT_SUBVECTOR index with VINSERTF128
3569/// instructions.
3570unsigned X86::getInsertVINSERTF128Immediate(SDNode *N) {
3571 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
3572 llvm_unreachable("Illegal insert subvector for VINSERTF128");
3573
3574 uint64_t Index =
NAKAMURA Takumi27635382011-02-05 15:10:54 +00003575 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
David Greeneccacdc12011-02-04 16:08:29 +00003576
3577 EVT VecVT = N->getValueType(0);
3578 EVT ElVT = VecVT.getVectorElementType();
3579
3580 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
3581
3582 return Index / NumElemsPerChunk;
3583}
3584
Evan Cheng37b73872009-07-30 08:33:02 +00003585/// isZeroNode - Returns true if Elt is a constant zero or a floating point
3586/// constant +0.0.
3587bool X86::isZeroNode(SDValue Elt) {
3588 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00003589 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00003590 (isa<ConstantFPSDNode>(Elt) &&
3591 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
3592}
3593
Nate Begeman9008ca62009-04-27 18:41:29 +00003594/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
3595/// their permute mask.
3596static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
3597 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003598 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003599 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00003600 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00003601
Nate Begeman5a5ca152009-04-29 05:20:52 +00003602 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003603 int idx = SVOp->getMaskElt(i);
3604 if (idx < 0)
3605 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003606 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003607 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003608 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003609 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003610 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003611 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
3612 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003613}
3614
Evan Cheng779ccea2007-12-07 21:30:01 +00003615/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3616/// the two vector operands have swapped position.
Owen Andersone50ed302009-08-10 22:56:29 +00003617static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00003618 unsigned NumElems = VT.getVectorNumElements();
3619 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003620 int idx = Mask[i];
3621 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003622 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003623 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003624 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003625 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003626 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003627 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003628}
3629
Evan Cheng533a0aa2006-04-19 20:35:22 +00003630/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
3631/// match movhlps. The lower half elements should come from upper half of
3632/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003633/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00003634static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
3635 if (Op->getValueType(0).getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00003636 return false;
3637 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003638 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003639 return false;
3640 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003641 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003642 return false;
3643 return true;
3644}
3645
Evan Cheng5ced1d82006-04-06 23:23:56 +00003646/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00003647/// is promoted to a vector. It also returns the LoadSDNode by reference if
3648/// required.
3649static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00003650 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
3651 return false;
3652 N = N->getOperand(0).getNode();
3653 if (!ISD::isNON_EXTLoad(N))
3654 return false;
3655 if (LD)
3656 *LD = cast<LoadSDNode>(N);
3657 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003658}
3659
Evan Cheng533a0aa2006-04-19 20:35:22 +00003660/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
3661/// match movlp{s|d}. The lower half elements should come from lower half of
3662/// V1 (and in order), and the upper half elements should come from the upper
3663/// half of V2 (and in order). And since V1 will become the source of the
3664/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003665static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
3666 ShuffleVectorSDNode *Op) {
Evan Cheng466685d2006-10-09 20:57:25 +00003667 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003668 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00003669 // Is V2 is a vector load, don't do this transformation. We will try to use
3670 // load folding shufps op.
3671 if (ISD::isNON_EXTLoad(V2))
3672 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003673
Nate Begeman5a5ca152009-04-29 05:20:52 +00003674 unsigned NumElems = Op->getValueType(0).getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003675
Evan Cheng533a0aa2006-04-19 20:35:22 +00003676 if (NumElems != 2 && NumElems != 4)
3677 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003678 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003679 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003680 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003681 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003682 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003683 return false;
3684 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003685}
3686
Evan Cheng39623da2006-04-20 08:58:49 +00003687/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
3688/// all the same.
3689static bool isSplatVector(SDNode *N) {
3690 if (N->getOpcode() != ISD::BUILD_VECTOR)
3691 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003692
Dan Gohman475871a2008-07-27 21:46:04 +00003693 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00003694 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
3695 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003696 return false;
3697 return true;
3698}
3699
Evan Cheng213d2cf2007-05-17 18:45:50 +00003700/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00003701/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00003702/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00003703static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00003704 SDValue V1 = N->getOperand(0);
3705 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003706 unsigned NumElems = N->getValueType(0).getVectorNumElements();
3707 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003708 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003709 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003710 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00003711 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
3712 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003713 if (Opc != ISD::BUILD_VECTOR ||
3714 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00003715 return false;
3716 } else if (Idx >= 0) {
3717 unsigned Opc = V1.getOpcode();
3718 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
3719 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003720 if (Opc != ISD::BUILD_VECTOR ||
3721 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00003722 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00003723 }
3724 }
3725 return true;
3726}
3727
3728/// getZeroVector - Returns a vector of specified type with all zero elements.
3729///
Owen Andersone50ed302009-08-10 22:56:29 +00003730static SDValue getZeroVector(EVT VT, bool HasSSE2, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00003731 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003732 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003733
Dale Johannesen0488fb62010-09-30 23:57:10 +00003734 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003735 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00003736 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00003737 if (VT.getSizeInBits() == 128) { // SSE
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003738 if (HasSSE2) { // SSE2
3739 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3740 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
3741 } else { // SSE1
3742 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
3743 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
3744 }
3745 } else if (VT.getSizeInBits() == 256) { // AVX
3746 // 256-bit logic and arithmetic instructions in AVX are
3747 // all floating-point, no support for integer ops. Default
3748 // to emitting fp zeroed vectors then.
Owen Anderson825b72b2009-08-11 20:47:22 +00003749 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003750 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
3751 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
Evan Chengf0df0312008-05-15 08:39:06 +00003752 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003753 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00003754}
3755
Chris Lattner8a594482007-11-25 00:24:49 +00003756/// getOnesVector - Returns a vector of specified type with all bits set.
3757///
Owen Andersone50ed302009-08-10 22:56:29 +00003758static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003759 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003760
Chris Lattner8a594482007-11-25 00:24:49 +00003761 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
3762 // type. This ensures they get CSE'd.
Owen Anderson825b72b2009-08-11 20:47:22 +00003763 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00003764 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00003765 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003766 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00003767}
3768
3769
Evan Cheng39623da2006-04-20 08:58:49 +00003770/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
3771/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00003772static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003773 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003774 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003775
Evan Cheng39623da2006-04-20 08:58:49 +00003776 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003777 SmallVector<int, 8> MaskVec;
3778 SVOp->getMask(MaskVec);
Eric Christopherfd179292009-08-27 18:07:15 +00003779
Nate Begeman5a5ca152009-04-29 05:20:52 +00003780 for (unsigned i = 0; i != NumElems; ++i) {
3781 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003782 MaskVec[i] = NumElems;
3783 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00003784 }
Evan Cheng39623da2006-04-20 08:58:49 +00003785 }
Evan Cheng39623da2006-04-20 08:58:49 +00003786 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00003787 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
3788 SVOp->getOperand(1), &MaskVec[0]);
3789 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00003790}
3791
Evan Cheng017dcc62006-04-21 01:05:10 +00003792/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
3793/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00003794static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003795 SDValue V2) {
3796 unsigned NumElems = VT.getVectorNumElements();
3797 SmallVector<int, 8> Mask;
3798 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00003799 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003800 Mask.push_back(i);
3801 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00003802}
3803
Nate Begeman9008ca62009-04-27 18:41:29 +00003804/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003805static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003806 SDValue V2) {
3807 unsigned NumElems = VT.getVectorNumElements();
3808 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00003809 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003810 Mask.push_back(i);
3811 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00003812 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003813 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00003814}
3815
Nate Begeman9008ca62009-04-27 18:41:29 +00003816/// getUnpackhMask - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003817static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003818 SDValue V2) {
3819 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00003820 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00003821 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00003822 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003823 Mask.push_back(i + Half);
3824 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00003825 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003826 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003827}
3828
Bruno Cardoso Lopesbb0a9482010-08-13 17:50:47 +00003829/// PromoteSplat - Promote a splat of v4i32, v8i16 or v16i8 to v4f32.
3830static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003831 EVT PVT = MVT::v4f32;
Owen Andersone50ed302009-08-10 22:56:29 +00003832 EVT VT = SV->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00003833 DebugLoc dl = SV->getDebugLoc();
3834 SDValue V1 = SV->getOperand(0);
3835 int NumElems = VT.getVectorNumElements();
3836 int EltNo = SV->getSplatIndex();
Rafael Espindola15684b22009-04-24 12:40:33 +00003837
Nate Begeman9008ca62009-04-27 18:41:29 +00003838 // unpack elements to the correct location
3839 while (NumElems > 4) {
3840 if (EltNo < NumElems/2) {
3841 V1 = getUnpackl(DAG, dl, VT, V1, V1);
3842 } else {
3843 V1 = getUnpackh(DAG, dl, VT, V1, V1);
3844 EltNo -= NumElems/2;
3845 }
3846 NumElems >>= 1;
3847 }
Eric Christopherfd179292009-08-27 18:07:15 +00003848
Nate Begeman9008ca62009-04-27 18:41:29 +00003849 // Perform the splat.
3850 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003851 V1 = DAG.getNode(ISD::BITCAST, dl, PVT, V1);
Nate Begeman9008ca62009-04-27 18:41:29 +00003852 V1 = DAG.getVectorShuffle(PVT, dl, V1, DAG.getUNDEF(PVT), &SplatMask[0]);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003853 return DAG.getNode(ISD::BITCAST, dl, VT, V1);
Evan Chengc575ca22006-04-17 20:43:08 +00003854}
3855
Evan Chengba05f722006-04-21 23:03:30 +00003856/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00003857/// vector of zero or undef vector. This produces a shuffle where the low
3858/// element of V2 is swizzled into the zero/undef vector, landing at element
3859/// 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 +00003860static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Chengf0df0312008-05-15 08:39:06 +00003861 bool isZero, bool HasSSE2,
3862 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003863 EVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00003864 SDValue V1 = isZero
Nate Begeman9008ca62009-04-27 18:41:29 +00003865 ? getZeroVector(VT, HasSSE2, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
3866 unsigned NumElems = VT.getVectorNumElements();
3867 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00003868 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003869 // If this is the insertion idx, put the low elt of V2 here.
3870 MaskVec.push_back(i == Idx ? NumElems : i);
3871 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00003872}
3873
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003874/// getShuffleScalarElt - Returns the scalar element that will make up the ith
3875/// element of the result of the vector shuffle.
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003876SDValue getShuffleScalarElt(SDNode *N, int Index, SelectionDAG &DAG,
3877 unsigned Depth) {
3878 if (Depth == 6)
3879 return SDValue(); // Limit search depth.
3880
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003881 SDValue V = SDValue(N, 0);
3882 EVT VT = V.getValueType();
3883 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003884
3885 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
3886 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
3887 Index = SV->getMaskElt(Index);
3888
3889 if (Index < 0)
3890 return DAG.getUNDEF(VT.getVectorElementType());
3891
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003892 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003893 SDValue NewV = (Index < NumElems) ? SV->getOperand(0) : SV->getOperand(1);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003894 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00003895 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003896
3897 // Recurse into target specific vector shuffles to find scalars.
3898 if (isTargetShuffle(Opcode)) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003899 int NumElems = VT.getVectorNumElements();
3900 SmallVector<unsigned, 16> ShuffleMask;
3901 SDValue ImmN;
3902
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003903 switch(Opcode) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003904 case X86ISD::SHUFPS:
3905 case X86ISD::SHUFPD:
3906 ImmN = N->getOperand(N->getNumOperands()-1);
3907 DecodeSHUFPSMask(NumElems,
3908 cast<ConstantSDNode>(ImmN)->getZExtValue(),
3909 ShuffleMask);
3910 break;
3911 case X86ISD::PUNPCKHBW:
3912 case X86ISD::PUNPCKHWD:
3913 case X86ISD::PUNPCKHDQ:
3914 case X86ISD::PUNPCKHQDQ:
3915 DecodePUNPCKHMask(NumElems, ShuffleMask);
3916 break;
3917 case X86ISD::UNPCKHPS:
3918 case X86ISD::UNPCKHPD:
3919 DecodeUNPCKHPMask(NumElems, ShuffleMask);
3920 break;
3921 case X86ISD::PUNPCKLBW:
3922 case X86ISD::PUNPCKLWD:
3923 case X86ISD::PUNPCKLDQ:
3924 case X86ISD::PUNPCKLQDQ:
David Greenec4db4e52011-02-28 19:06:56 +00003925 DecodePUNPCKLMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003926 break;
3927 case X86ISD::UNPCKLPS:
3928 case X86ISD::UNPCKLPD:
David Greenec4db4e52011-02-28 19:06:56 +00003929 case X86ISD::VUNPCKLPS:
3930 case X86ISD::VUNPCKLPD:
3931 case X86ISD::VUNPCKLPSY:
3932 case X86ISD::VUNPCKLPDY:
3933 DecodeUNPCKLPMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003934 break;
3935 case X86ISD::MOVHLPS:
3936 DecodeMOVHLPSMask(NumElems, ShuffleMask);
3937 break;
3938 case X86ISD::MOVLHPS:
3939 DecodeMOVLHPSMask(NumElems, ShuffleMask);
3940 break;
3941 case X86ISD::PSHUFD:
3942 ImmN = N->getOperand(N->getNumOperands()-1);
3943 DecodePSHUFMask(NumElems,
3944 cast<ConstantSDNode>(ImmN)->getZExtValue(),
3945 ShuffleMask);
3946 break;
3947 case X86ISD::PSHUFHW:
3948 ImmN = N->getOperand(N->getNumOperands()-1);
3949 DecodePSHUFHWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
3950 ShuffleMask);
3951 break;
3952 case X86ISD::PSHUFLW:
3953 ImmN = N->getOperand(N->getNumOperands()-1);
3954 DecodePSHUFLWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
3955 ShuffleMask);
3956 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003957 case X86ISD::MOVSS:
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00003958 case X86ISD::MOVSD: {
3959 // The index 0 always comes from the first element of the second source,
3960 // this is why MOVSS and MOVSD are used in the first place. The other
3961 // elements come from the other positions of the first source vector.
3962 unsigned OpNum = (Index == 0) ? 1 : 0;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003963 return getShuffleScalarElt(V.getOperand(OpNum).getNode(), Index, DAG,
3964 Depth+1);
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00003965 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003966 default:
3967 assert("not implemented for target shuffle node");
3968 return SDValue();
3969 }
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003970
3971 Index = ShuffleMask[Index];
3972 if (Index < 0)
3973 return DAG.getUNDEF(VT.getVectorElementType());
3974
3975 SDValue NewV = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
3976 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG,
3977 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003978 }
3979
3980 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003981 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003982 V = V.getOperand(0);
3983 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003984 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003985
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003986 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003987 return SDValue();
3988 }
3989
3990 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
3991 return (Index == 0) ? V.getOperand(0)
3992 : DAG.getUNDEF(VT.getVectorElementType());
3993
3994 if (V.getOpcode() == ISD::BUILD_VECTOR)
3995 return V.getOperand(Index);
3996
3997 return SDValue();
3998}
3999
4000/// getNumOfConsecutiveZeros - Return the number of elements of a vector
4001/// shuffle operation which come from a consecutively from a zero. The
4002/// search can start in two diferent directions, from left or right.
4003static
4004unsigned getNumOfConsecutiveZeros(SDNode *N, int NumElems,
4005 bool ZerosFromLeft, SelectionDAG &DAG) {
4006 int i = 0;
4007
4008 while (i < NumElems) {
4009 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004010 SDValue Elt = getShuffleScalarElt(N, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004011 if (!(Elt.getNode() &&
4012 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
4013 break;
4014 ++i;
4015 }
4016
4017 return i;
4018}
4019
4020/// isShuffleMaskConsecutive - Check if the shuffle mask indicies from MaskI to
4021/// MaskE correspond consecutively to elements from one of the vector operands,
4022/// starting from its index OpIdx. Also tell OpNum which source vector operand.
4023static
4024bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp, int MaskI, int MaskE,
4025 int OpIdx, int NumElems, unsigned &OpNum) {
4026 bool SeenV1 = false;
4027 bool SeenV2 = false;
4028
4029 for (int i = MaskI; i <= MaskE; ++i, ++OpIdx) {
4030 int Idx = SVOp->getMaskElt(i);
4031 // Ignore undef indicies
4032 if (Idx < 0)
4033 continue;
4034
4035 if (Idx < NumElems)
4036 SeenV1 = true;
4037 else
4038 SeenV2 = true;
4039
4040 // Only accept consecutive elements from the same vector
4041 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
4042 return false;
4043 }
4044
4045 OpNum = SeenV1 ? 0 : 1;
4046 return true;
4047}
4048
4049/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
4050/// logical left shift of a vector.
4051static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4052 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4053 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4054 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4055 false /* check zeros from right */, DAG);
4056 unsigned OpSrc;
4057
4058 if (!NumZeros)
4059 return false;
4060
4061 // Considering the elements in the mask that are not consecutive zeros,
4062 // check if they consecutively come from only one of the source vectors.
4063 //
4064 // V1 = {X, A, B, C} 0
4065 // \ \ \ /
4066 // vector_shuffle V1, V2 <1, 2, 3, X>
4067 //
4068 if (!isShuffleMaskConsecutive(SVOp,
4069 0, // Mask Start Index
4070 NumElems-NumZeros-1, // Mask End Index
4071 NumZeros, // Where to start looking in the src vector
4072 NumElems, // Number of elements in vector
4073 OpSrc)) // Which source operand ?
4074 return false;
4075
4076 isLeft = false;
4077 ShAmt = NumZeros;
4078 ShVal = SVOp->getOperand(OpSrc);
4079 return true;
4080}
4081
4082/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
4083/// logical left shift of a vector.
4084static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4085 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4086 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4087 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4088 true /* check zeros from left */, DAG);
4089 unsigned OpSrc;
4090
4091 if (!NumZeros)
4092 return false;
4093
4094 // Considering the elements in the mask that are not consecutive zeros,
4095 // check if they consecutively come from only one of the source vectors.
4096 //
4097 // 0 { A, B, X, X } = V2
4098 // / \ / /
4099 // vector_shuffle V1, V2 <X, X, 4, 5>
4100 //
4101 if (!isShuffleMaskConsecutive(SVOp,
4102 NumZeros, // Mask Start Index
4103 NumElems-1, // Mask End Index
4104 0, // Where to start looking in the src vector
4105 NumElems, // Number of elements in vector
4106 OpSrc)) // Which source operand ?
4107 return false;
4108
4109 isLeft = true;
4110 ShAmt = NumZeros;
4111 ShVal = SVOp->getOperand(OpSrc);
4112 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004113}
4114
4115/// isVectorShift - Returns true if the shuffle can be implemented as a
4116/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004117static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00004118 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004119 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
4120 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
4121 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004122
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004123 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00004124}
4125
Evan Chengc78d3b42006-04-24 18:01:45 +00004126/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
4127///
Dan Gohman475871a2008-07-27 21:46:04 +00004128static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00004129 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00004130 SelectionDAG &DAG,
4131 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004132 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00004133 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004134
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004135 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004136 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004137 bool First = true;
4138 for (unsigned i = 0; i < 16; ++i) {
4139 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
4140 if (ThisIsNonZero && First) {
4141 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004142 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004143 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004144 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004145 First = false;
4146 }
4147
4148 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00004149 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004150 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
4151 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004152 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004153 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00004154 }
4155 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004156 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
4157 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
4158 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00004159 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004160 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00004161 } else
4162 ThisElt = LastElt;
4163
Gabor Greifba36cb52008-08-28 21:40:38 +00004164 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00004165 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00004166 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00004167 }
4168 }
4169
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004170 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00004171}
4172
Bill Wendlinga348c562007-03-22 18:42:45 +00004173/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00004174///
Dan Gohman475871a2008-07-27 21:46:04 +00004175static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00004176 unsigned NumNonZero, unsigned NumZero,
4177 SelectionDAG &DAG,
4178 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004179 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00004180 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004181
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004182 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004183 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004184 bool First = true;
4185 for (unsigned i = 0; i < 8; ++i) {
4186 bool isNonZero = (NonZeros & (1 << i)) != 0;
4187 if (isNonZero) {
4188 if (First) {
4189 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004190 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004191 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004192 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004193 First = false;
4194 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004195 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004196 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00004197 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00004198 }
4199 }
4200
4201 return V;
4202}
4203
Evan Chengf26ffe92008-05-29 08:22:04 +00004204/// getVShift - Return a vector logical shift node.
4205///
Owen Andersone50ed302009-08-10 22:56:29 +00004206static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00004207 unsigned NumBits, SelectionDAG &DAG,
4208 const TargetLowering &TLI, DebugLoc dl) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004209 EVT ShVT = MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00004210 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004211 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
4212 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004213 DAG.getNode(Opc, dl, ShVT, SrcOp,
Owen Anderson95771af2011-02-25 21:41:48 +00004214 DAG.getConstant(NumBits,
4215 TLI.getShiftAmountTy(SrcOp.getValueType()))));
Evan Chengf26ffe92008-05-29 08:22:04 +00004216}
4217
Dan Gohman475871a2008-07-27 21:46:04 +00004218SDValue
Evan Chengc3630942009-12-09 21:00:30 +00004219X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00004220 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00004221
Evan Chengc3630942009-12-09 21:00:30 +00004222 // Check if the scalar load can be widened into a vector load. And if
4223 // the address is "base + cst" see if the cst can be "absorbed" into
4224 // the shuffle mask.
4225 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
4226 SDValue Ptr = LD->getBasePtr();
4227 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
4228 return SDValue();
4229 EVT PVT = LD->getValueType(0);
4230 if (PVT != MVT::i32 && PVT != MVT::f32)
4231 return SDValue();
4232
4233 int FI = -1;
4234 int64_t Offset = 0;
4235 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
4236 FI = FINode->getIndex();
4237 Offset = 0;
Chris Lattner0a9481f2011-02-13 22:25:43 +00004238 } else if (DAG.isBaseWithConstantOffset(Ptr) &&
Evan Chengc3630942009-12-09 21:00:30 +00004239 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4240 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4241 Offset = Ptr.getConstantOperandVal(1);
4242 Ptr = Ptr.getOperand(0);
4243 } else {
4244 return SDValue();
4245 }
4246
4247 SDValue Chain = LD->getChain();
4248 // Make sure the stack object alignment is at least 16.
4249 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
4250 if (DAG.InferPtrAlignment(Ptr) < 16) {
4251 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00004252 // Can't change the alignment. FIXME: It's possible to compute
4253 // the exact stack offset and reference FI + adjust offset instead.
4254 // If someone *really* cares about this. That's the way to implement it.
4255 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004256 } else {
4257 MFI->setObjectAlignment(FI, 16);
4258 }
4259 }
4260
4261 // (Offset % 16) must be multiple of 4. Then address is then
4262 // Ptr + (Offset & ~15).
4263 if (Offset < 0)
4264 return SDValue();
4265 if ((Offset % 16) & 3)
4266 return SDValue();
4267 int64_t StartOffset = Offset & ~15;
4268 if (StartOffset)
4269 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
4270 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
4271
4272 int EltNo = (Offset - StartOffset) >> 2;
4273 int Mask[4] = { EltNo, EltNo, EltNo, EltNo };
4274 EVT VT = (PVT == MVT::i32) ? MVT::v4i32 : MVT::v4f32;
Chris Lattner51abfe42010-09-21 06:02:19 +00004275 SDValue V1 = DAG.getLoad(VT, dl, Chain, Ptr,
4276 LD->getPointerInfo().getWithOffset(StartOffset),
David Greene67c9d422010-02-15 16:53:33 +00004277 false, false, 0);
Evan Chengc3630942009-12-09 21:00:30 +00004278 // Canonicalize it to a v4i32 shuffle.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004279 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, V1);
4280 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Chengc3630942009-12-09 21:00:30 +00004281 DAG.getVectorShuffle(MVT::v4i32, dl, V1,
Chris Lattner51abfe42010-09-21 06:02:19 +00004282 DAG.getUNDEF(MVT::v4i32),&Mask[0]));
Evan Chengc3630942009-12-09 21:00:30 +00004283 }
4284
4285 return SDValue();
4286}
4287
Michael J. Spencerec38de22010-10-10 22:04:20 +00004288/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
4289/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00004290/// load which has the same value as a build_vector whose operands are 'elts'.
4291///
4292/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00004293///
Nate Begeman1449f292010-03-24 22:19:06 +00004294/// FIXME: we'd also like to handle the case where the last elements are zero
4295/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
4296/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004297static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00004298 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004299 EVT EltVT = VT.getVectorElementType();
4300 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00004301
Nate Begemanfdea31a2010-03-24 20:49:50 +00004302 LoadSDNode *LDBase = NULL;
4303 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004304
Nate Begeman1449f292010-03-24 22:19:06 +00004305 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00004306 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00004307 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004308 for (unsigned i = 0; i < NumElems; ++i) {
4309 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00004310
Nate Begemanfdea31a2010-03-24 20:49:50 +00004311 if (!Elt.getNode() ||
4312 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
4313 return SDValue();
4314 if (!LDBase) {
4315 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
4316 return SDValue();
4317 LDBase = cast<LoadSDNode>(Elt.getNode());
4318 LastLoadedElt = i;
4319 continue;
4320 }
4321 if (Elt.getOpcode() == ISD::UNDEF)
4322 continue;
4323
4324 LoadSDNode *LD = cast<LoadSDNode>(Elt);
4325 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
4326 return SDValue();
4327 LastLoadedElt = i;
4328 }
Nate Begeman1449f292010-03-24 22:19:06 +00004329
4330 // If we have found an entire vector of loads and undefs, then return a large
4331 // load of the entire vector width starting at the base pointer. If we found
4332 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004333 if (LastLoadedElt == NumElems - 1) {
4334 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00004335 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004336 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004337 LDBase->isVolatile(), LDBase->isNonTemporal(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00004338 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004339 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004340 LDBase->isVolatile(), LDBase->isNonTemporal(),
4341 LDBase->getAlignment());
4342 } else if (NumElems == 4 && LastLoadedElt == 1) {
4343 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
4344 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Chris Lattner88641552010-09-22 00:34:38 +00004345 SDValue ResNode = DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys,
4346 Ops, 2, MVT::i32,
4347 LDBase->getMemOperand());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004348 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00004349 }
4350 return SDValue();
4351}
4352
Evan Chengc3630942009-12-09 21:00:30 +00004353SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004354X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004355 DebugLoc dl = Op.getDebugLoc();
David Greenea5f26012011-02-07 19:36:54 +00004356
David Greenef125a292011-02-08 19:04:41 +00004357 EVT VT = Op.getValueType();
4358 EVT ExtVT = VT.getVectorElementType();
4359
4360 unsigned NumElems = Op.getNumOperands();
4361
4362 // For AVX-length vectors, build the individual 128-bit pieces and
4363 // use shuffles to put them in place.
Owen Anderson95771af2011-02-25 21:41:48 +00004364 if (VT.getSizeInBits() > 256 &&
4365 Subtarget->hasAVX() &&
David Greenef125a292011-02-08 19:04:41 +00004366 !ISD::isBuildVectorAllZeros(Op.getNode())) {
4367 SmallVector<SDValue, 8> V;
4368 V.resize(NumElems);
4369 for (unsigned i = 0; i < NumElems; ++i) {
4370 V[i] = Op.getOperand(i);
4371 }
Owen Anderson95771af2011-02-25 21:41:48 +00004372
David Greenef125a292011-02-08 19:04:41 +00004373 EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElems/2);
4374
4375 // Build the lower subvector.
4376 SDValue Lower = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[0], NumElems/2);
4377 // Build the upper subvector.
4378 SDValue Upper = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[NumElems / 2],
4379 NumElems/2);
4380
4381 return ConcatVectors(Lower, Upper, DAG);
4382 }
4383
Chris Lattner6e80e442010-08-28 17:15:43 +00004384 // All zero's are handled with pxor in SSE2 and above, xorps in SSE1.
4385 // All one's are handled with pcmpeqd. In AVX, zero's are handled with
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004386 // vpxor in 128-bit and xor{pd,ps} in 256-bit, but no 256 version of pcmpeqd
4387 // is present, so AllOnes is ignored.
4388 if (ISD::isBuildVectorAllZeros(Op.getNode()) ||
4389 (Op.getValueType().getSizeInBits() != 256 &&
4390 ISD::isBuildVectorAllOnes(Op.getNode()))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004391 // Canonicalize this to <4 x i32> (SSE) to
Chris Lattner8a594482007-11-25 00:24:49 +00004392 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
4393 // eliminated on x86-32 hosts.
Dale Johannesen0488fb62010-09-30 23:57:10 +00004394 if (Op.getValueType() == MVT::v4i32)
Chris Lattner8a594482007-11-25 00:24:49 +00004395 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004396
Gabor Greifba36cb52008-08-28 21:40:38 +00004397 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00004398 return getOnesVector(Op.getValueType(), DAG, dl);
4399 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00004400 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004401
Owen Andersone50ed302009-08-10 22:56:29 +00004402 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004403
Evan Cheng0db9fe62006-04-25 20:13:52 +00004404 unsigned NumZero = 0;
4405 unsigned NumNonZero = 0;
4406 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004407 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00004408 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004409 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00004410 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00004411 if (Elt.getOpcode() == ISD::UNDEF)
4412 continue;
4413 Values.insert(Elt);
4414 if (Elt.getOpcode() != ISD::Constant &&
4415 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004416 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00004417 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00004418 NumZero++;
4419 else {
4420 NonZeros |= (1 << i);
4421 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004422 }
4423 }
4424
Chris Lattner97a2a562010-08-26 05:24:29 +00004425 // All undef vector. Return an UNDEF. All zero vectors were handled above.
4426 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00004427 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004428
Chris Lattner67f453a2008-03-09 05:42:06 +00004429 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00004430 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004431 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00004432 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00004433
Chris Lattner62098042008-03-09 01:05:04 +00004434 // If this is an insertion of an i64 value on x86-32, and if the top bits of
4435 // the value are obviously zero, truncate the value to i32 and do the
4436 // insertion that way. Only do this if the value is non-constant or if the
4437 // value is a constant being inserted into element 0. It is cheaper to do
4438 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00004439 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00004440 (!IsAllConstants || Idx == 0)) {
4441 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004442 // Handle SSE only.
4443 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
4444 EVT VecVT = MVT::v4i32;
4445 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00004446
Chris Lattner62098042008-03-09 01:05:04 +00004447 // Truncate the value (which may itself be a constant) to i32, and
4448 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00004449 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00004450 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00004451 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
4452 Subtarget->hasSSE2(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00004453
Chris Lattner62098042008-03-09 01:05:04 +00004454 // Now we have our 32-bit value zero extended in the low element of
4455 // a vector. If Idx != 0, swizzle it into place.
4456 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004457 SmallVector<int, 4> Mask;
4458 Mask.push_back(Idx);
4459 for (unsigned i = 1; i != VecElts; ++i)
4460 Mask.push_back(i);
4461 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopherfd179292009-08-27 18:07:15 +00004462 DAG.getUNDEF(Item.getValueType()),
Nate Begeman9008ca62009-04-27 18:41:29 +00004463 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004464 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004465 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00004466 }
4467 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004468
Chris Lattner19f79692008-03-08 22:59:52 +00004469 // If we have a constant or non-constant insertion into the low element of
4470 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
4471 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00004472 // depending on what the source datatype is.
4473 if (Idx == 0) {
4474 if (NumZero == 0) {
4475 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Owen Anderson825b72b2009-08-11 20:47:22 +00004476 } else if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
4477 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Eli Friedman10415532009-06-06 06:05:10 +00004478 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
4479 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
4480 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget->hasSSE2(),
4481 DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00004482 } else if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
4483 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Dale Johannesen0488fb62010-09-30 23:57:10 +00004484 assert(VT.getSizeInBits() == 128 && "Expected an SSE value type!");
4485 EVT MiddleVT = MVT::v4i32;
Eli Friedman10415532009-06-06 06:05:10 +00004486 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
4487 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
4488 Subtarget->hasSSE2(), DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004489 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00004490 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004491 }
Evan Chengf26ffe92008-05-29 08:22:04 +00004492
4493 // Is it a vector logical left shift?
4494 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00004495 X86::isZeroNode(Op.getOperand(0)) &&
4496 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004497 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00004498 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00004499 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004500 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00004501 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004502 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004503
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004504 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00004505 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004506
Chris Lattner19f79692008-03-08 22:59:52 +00004507 // Otherwise, if this is a vector with i32 or f32 elements, and the element
4508 // is a non-constant being inserted into an element other than the low one,
4509 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
4510 // movd/movss) to move this into the low element, then shuffle it into
4511 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004512 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00004513 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00004514
Evan Cheng0db9fe62006-04-25 20:13:52 +00004515 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00004516 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
4517 Subtarget->hasSSE2(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00004518 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004519 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00004520 MaskVec.push_back(i == Idx ? 0 : 1);
4521 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004522 }
4523 }
4524
Chris Lattner67f453a2008-03-09 05:42:06 +00004525 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00004526 if (Values.size() == 1) {
4527 if (EVTBits == 32) {
4528 // Instead of a shuffle like this:
4529 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
4530 // Check if it's possible to issue this instead.
4531 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
4532 unsigned Idx = CountTrailingZeros_32(NonZeros);
4533 SDValue Item = Op.getOperand(Idx);
4534 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
4535 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
4536 }
Dan Gohman475871a2008-07-27 21:46:04 +00004537 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004538 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004539
Dan Gohmana3941172007-07-24 22:55:08 +00004540 // A vector full of immediates; various special cases are already
4541 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004542 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00004543 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00004544
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00004545 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00004546 if (EVTBits == 64) {
4547 if (NumNonZero == 1) {
4548 // One half is zero or undef.
4549 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00004550 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00004551 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00004552 return getShuffleVectorZeroOrUndef(V2, Idx, true,
4553 Subtarget->hasSSE2(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00004554 }
Dan Gohman475871a2008-07-27 21:46:04 +00004555 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00004556 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004557
4558 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00004559 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00004560 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00004561 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004562 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004563 }
4564
Bill Wendling826f36f2007-03-28 00:57:11 +00004565 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00004566 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Chris Lattner97a2a562010-08-26 05:24:29 +00004567 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004568 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004569 }
4570
4571 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00004572 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00004573 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004574 if (NumElems == 4 && NumZero > 0) {
4575 for (unsigned i = 0; i < 4; ++i) {
4576 bool isZero = !(NonZeros & (1 << i));
4577 if (isZero)
Dale Johannesenace16102009-02-03 19:33:06 +00004578 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004579 else
Dale Johannesenace16102009-02-03 19:33:06 +00004580 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004581 }
4582
4583 for (unsigned i = 0; i < 2; ++i) {
4584 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
4585 default: break;
4586 case 0:
4587 V[i] = V[i*2]; // Must be a zero vector.
4588 break;
4589 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00004590 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004591 break;
4592 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00004593 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004594 break;
4595 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00004596 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004597 break;
4598 }
4599 }
4600
Nate Begeman9008ca62009-04-27 18:41:29 +00004601 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004602 bool Reverse = (NonZeros & 0x3) == 2;
4603 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004604 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004605 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
4606 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004607 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
4608 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004609 }
4610
Nate Begemanfdea31a2010-03-24 20:49:50 +00004611 if (Values.size() > 1 && VT.getSizeInBits() == 128) {
4612 // Check for a build vector of consecutive loads.
4613 for (unsigned i = 0; i < NumElems; ++i)
4614 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00004615
Nate Begemanfdea31a2010-03-24 20:49:50 +00004616 // Check for elements which are consecutive loads.
4617 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
4618 if (LD.getNode())
4619 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004620
4621 // For SSE 4.1, use insertps to put the high elements into the low element.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004622 if (getSubtarget()->hasSSE41()) {
Chris Lattner24faf612010-08-28 17:59:08 +00004623 SDValue Result;
4624 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
4625 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
4626 else
4627 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00004628
Chris Lattner24faf612010-08-28 17:59:08 +00004629 for (unsigned i = 1; i < NumElems; ++i) {
4630 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
4631 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00004632 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00004633 }
4634 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00004635 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00004636
Chris Lattner6e80e442010-08-28 17:15:43 +00004637 // Otherwise, expand into a number of unpckl*, start by extending each of
4638 // our (non-undef) elements to the full vector width with the element in the
4639 // bottom slot of the vector (which generates no code for SSE).
4640 for (unsigned i = 0; i < NumElems; ++i) {
4641 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
4642 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
4643 else
4644 V[i] = DAG.getUNDEF(VT);
4645 }
4646
4647 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004648 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
4649 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
4650 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00004651 unsigned EltStride = NumElems >> 1;
4652 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00004653 for (unsigned i = 0; i < EltStride; ++i) {
4654 // If V[i+EltStride] is undef and this is the first round of mixing,
4655 // then it is safe to just drop this shuffle: V[i] is already in the
4656 // right place, the one element (since it's the first round) being
4657 // inserted as undef can be dropped. This isn't safe for successive
4658 // rounds because they will permute elements within both vectors.
4659 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
4660 EltStride == NumElems/2)
4661 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004662
Chris Lattner6e80e442010-08-28 17:15:43 +00004663 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00004664 }
Chris Lattner6e80e442010-08-28 17:15:43 +00004665 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004666 }
4667 return V[0];
4668 }
Dan Gohman475871a2008-07-27 21:46:04 +00004669 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004670}
4671
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004672SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004673X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const {
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004674 // We support concatenate two MMX registers and place them in a MMX
4675 // register. This is better than doing a stack convert.
4676 DebugLoc dl = Op.getDebugLoc();
4677 EVT ResVT = Op.getValueType();
4678 assert(Op.getNumOperands() == 2);
4679 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
4680 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
4681 int Mask[2];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004682 SDValue InVec = DAG.getNode(ISD::BITCAST,dl, MVT::v1i64, Op.getOperand(0));
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004683 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4684 InVec = Op.getOperand(1);
4685 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4686 unsigned NumElts = ResVT.getVectorNumElements();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004687 VecOp = DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004688 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
4689 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
4690 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004691 InVec = DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, InVec);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004692 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4693 Mask[0] = 0; Mask[1] = 2;
4694 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
4695 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004696 return DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004697}
4698
Nate Begemanb9a47b82009-02-23 08:49:38 +00004699// v8i16 shuffles - Prefer shuffles in the following order:
4700// 1. [all] pshuflw, pshufhw, optional move
4701// 2. [ssse3] 1 x pshufb
4702// 3. [ssse3] 2 x pshufb + 1 x por
4703// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004704SDValue
4705X86TargetLowering::LowerVECTOR_SHUFFLEv8i16(SDValue Op,
4706 SelectionDAG &DAG) const {
4707 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00004708 SDValue V1 = SVOp->getOperand(0);
4709 SDValue V2 = SVOp->getOperand(1);
4710 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004711 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00004712
Nate Begemanb9a47b82009-02-23 08:49:38 +00004713 // Determine if more than 1 of the words in each of the low and high quadwords
4714 // of the result come from the same quadword of one of the two inputs. Undef
4715 // mask values count as coming from any quadword, for better codegen.
4716 SmallVector<unsigned, 4> LoQuad(4);
4717 SmallVector<unsigned, 4> HiQuad(4);
4718 BitVector InputQuads(4);
4719 for (unsigned i = 0; i < 8; ++i) {
4720 SmallVectorImpl<unsigned> &Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00004721 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004722 MaskVals.push_back(EltIdx);
4723 if (EltIdx < 0) {
4724 ++Quad[0];
4725 ++Quad[1];
4726 ++Quad[2];
4727 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00004728 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004729 }
4730 ++Quad[EltIdx / 4];
4731 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00004732 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004733
Nate Begemanb9a47b82009-02-23 08:49:38 +00004734 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004735 unsigned MaxQuad = 1;
4736 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004737 if (LoQuad[i] > MaxQuad) {
4738 BestLoQuad = i;
4739 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004740 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004741 }
4742
Nate Begemanb9a47b82009-02-23 08:49:38 +00004743 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004744 MaxQuad = 1;
4745 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004746 if (HiQuad[i] > MaxQuad) {
4747 BestHiQuad = i;
4748 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004749 }
4750 }
4751
Nate Begemanb9a47b82009-02-23 08:49:38 +00004752 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00004753 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00004754 // single pshufb instruction is necessary. If There are more than 2 input
4755 // quads, disable the next transformation since it does not help SSSE3.
4756 bool V1Used = InputQuads[0] || InputQuads[1];
4757 bool V2Used = InputQuads[2] || InputQuads[3];
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004758 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004759 if (InputQuads.count() == 2 && V1Used && V2Used) {
4760 BestLoQuad = InputQuads.find_first();
4761 BestHiQuad = InputQuads.find_next(BestLoQuad);
4762 }
4763 if (InputQuads.count() > 2) {
4764 BestLoQuad = -1;
4765 BestHiQuad = -1;
4766 }
4767 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004768
Nate Begemanb9a47b82009-02-23 08:49:38 +00004769 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
4770 // the shuffle mask. If a quad is scored as -1, that means that it contains
4771 // words from all 4 input quadwords.
4772 SDValue NewV;
4773 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004774 SmallVector<int, 8> MaskV;
4775 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
4776 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Eric Christopherfd179292009-08-27 18:07:15 +00004777 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004778 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
4779 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
4780 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00004781
Nate Begemanb9a47b82009-02-23 08:49:38 +00004782 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
4783 // source words for the shuffle, to aid later transformations.
4784 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00004785 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00004786 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004787 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00004788 if (idx != (int)i)
4789 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004790 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00004791 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004792 AllWordsInNewV = false;
4793 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00004794 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004795
Nate Begemanb9a47b82009-02-23 08:49:38 +00004796 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
4797 if (AllWordsInNewV) {
4798 for (int i = 0; i != 8; ++i) {
4799 int idx = MaskVals[i];
4800 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00004801 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004802 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004803 if ((idx != i) && idx < 4)
4804 pshufhw = false;
4805 if ((idx != i) && idx > 3)
4806 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00004807 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00004808 V1 = NewV;
4809 V2Used = false;
4810 BestLoQuad = 0;
4811 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004812 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004813
Nate Begemanb9a47b82009-02-23 08:49:38 +00004814 // If we've eliminated the use of V2, and the new mask is a pshuflw or
4815 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00004816 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00004817 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
4818 unsigned TargetMask = 0;
4819 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00004820 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00004821 TargetMask = pshufhw ? X86::getShufflePSHUFHWImmediate(NewV.getNode()):
4822 X86::getShufflePSHUFLWImmediate(NewV.getNode());
4823 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004824 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00004825 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004826 }
Eric Christopherfd179292009-08-27 18:07:15 +00004827
Nate Begemanb9a47b82009-02-23 08:49:38 +00004828 // If we have SSSE3, and all words of the result are from 1 input vector,
4829 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
4830 // is present, fall back to case 4.
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004831 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004832 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004833
Nate Begemanb9a47b82009-02-23 08:49:38 +00004834 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00004835 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00004836 // mask, and elements that come from V1 in the V2 mask, so that the two
4837 // results can be OR'd together.
4838 bool TwoInputs = V1Used && V2Used;
4839 for (unsigned i = 0; i != 8; ++i) {
4840 int EltIdx = MaskVals[i] * 2;
4841 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004842 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4843 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004844 continue;
4845 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004846 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
4847 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004848 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004849 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004850 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004851 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004852 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004853 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004854 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004855
Nate Begemanb9a47b82009-02-23 08:49:38 +00004856 // Calculate the shuffle mask for the second input, shuffle it, and
4857 // OR it with the first shuffled input.
4858 pshufbMask.clear();
4859 for (unsigned i = 0; i != 8; ++i) {
4860 int EltIdx = MaskVals[i] * 2;
4861 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004862 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4863 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004864 continue;
4865 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004866 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
4867 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004868 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004869 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00004870 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00004871 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004872 MVT::v16i8, &pshufbMask[0], 16));
4873 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004874 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004875 }
4876
4877 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
4878 // and update MaskVals with new element order.
4879 BitVector InOrder(8);
4880 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004881 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004882 for (int i = 0; i != 4; ++i) {
4883 int idx = MaskVals[i];
4884 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004885 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004886 InOrder.set(i);
4887 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004888 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004889 InOrder.set(i);
4890 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004891 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004892 }
4893 }
4894 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004895 MaskV.push_back(i);
Owen Anderson825b72b2009-08-11 20:47:22 +00004896 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004897 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004898
4899 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
4900 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
4901 NewV.getOperand(0),
4902 X86::getShufflePSHUFLWImmediate(NewV.getNode()),
4903 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004904 }
Eric Christopherfd179292009-08-27 18:07:15 +00004905
Nate Begemanb9a47b82009-02-23 08:49:38 +00004906 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
4907 // and update MaskVals with the new element order.
4908 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004909 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004910 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004911 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004912 for (unsigned i = 4; i != 8; ++i) {
4913 int idx = MaskVals[i];
4914 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004915 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004916 InOrder.set(i);
4917 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004918 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004919 InOrder.set(i);
4920 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004921 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004922 }
4923 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004924 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004925 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004926
4927 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
4928 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
4929 NewV.getOperand(0),
4930 X86::getShufflePSHUFHWImmediate(NewV.getNode()),
4931 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004932 }
Eric Christopherfd179292009-08-27 18:07:15 +00004933
Nate Begemanb9a47b82009-02-23 08:49:38 +00004934 // In case BestHi & BestLo were both -1, which means each quadword has a word
4935 // from each of the four input quadwords, calculate the InOrder bitvector now
4936 // before falling through to the insert/extract cleanup.
4937 if (BestLoQuad == -1 && BestHiQuad == -1) {
4938 NewV = V1;
4939 for (int i = 0; i != 8; ++i)
4940 if (MaskVals[i] < 0 || MaskVals[i] == i)
4941 InOrder.set(i);
4942 }
Eric Christopherfd179292009-08-27 18:07:15 +00004943
Nate Begemanb9a47b82009-02-23 08:49:38 +00004944 // The other elements are put in the right place using pextrw and pinsrw.
4945 for (unsigned i = 0; i != 8; ++i) {
4946 if (InOrder[i])
4947 continue;
4948 int EltIdx = MaskVals[i];
4949 if (EltIdx < 0)
4950 continue;
4951 SDValue ExtOp = (EltIdx < 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00004952 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004953 DAG.getIntPtrConstant(EltIdx))
Owen Anderson825b72b2009-08-11 20:47:22 +00004954 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004955 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00004956 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004957 DAG.getIntPtrConstant(i));
4958 }
4959 return NewV;
4960}
4961
4962// v16i8 shuffles - Prefer shuffles in the following order:
4963// 1. [ssse3] 1 x pshufb
4964// 2. [ssse3] 2 x pshufb + 1 x por
4965// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
4966static
Nate Begeman9008ca62009-04-27 18:41:29 +00004967SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00004968 SelectionDAG &DAG,
4969 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004970 SDValue V1 = SVOp->getOperand(0);
4971 SDValue V2 = SVOp->getOperand(1);
4972 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004973 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00004974 SVOp->getMask(MaskVals);
Eric Christopherfd179292009-08-27 18:07:15 +00004975
Nate Begemanb9a47b82009-02-23 08:49:38 +00004976 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00004977 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00004978 // present, fall back to case 3.
4979 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
4980 bool V1Only = true;
4981 bool V2Only = true;
4982 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004983 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00004984 if (EltIdx < 0)
4985 continue;
4986 if (EltIdx < 16)
4987 V2Only = false;
4988 else
4989 V1Only = false;
4990 }
Eric Christopherfd179292009-08-27 18:07:15 +00004991
Nate Begemanb9a47b82009-02-23 08:49:38 +00004992 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
4993 if (TLI.getSubtarget()->hasSSSE3()) {
4994 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004995
Nate Begemanb9a47b82009-02-23 08:49:38 +00004996 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00004997 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00004998 //
4999 // Otherwise, we have elements from both input vectors, and must zero out
5000 // elements that come from V2 in the first mask, and V1 in the second mask
5001 // so that we can OR them together.
5002 bool TwoInputs = !(V1Only || V2Only);
5003 for (unsigned i = 0; i != 16; ++i) {
5004 int EltIdx = MaskVals[i];
5005 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005006 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005007 continue;
5008 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005009 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005010 }
5011 // If all the elements are from V2, assign it to V1 and return after
5012 // building the first pshufb.
5013 if (V2Only)
5014 V1 = V2;
Owen Anderson825b72b2009-08-11 20:47:22 +00005015 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005016 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005017 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005018 if (!TwoInputs)
5019 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00005020
Nate Begemanb9a47b82009-02-23 08:49:38 +00005021 // Calculate the shuffle mask for the second input, shuffle it, and
5022 // OR it with the first shuffled input.
5023 pshufbMask.clear();
5024 for (unsigned i = 0; i != 16; ++i) {
5025 int EltIdx = MaskVals[i];
5026 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005027 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005028 continue;
5029 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005030 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005031 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005032 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005033 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005034 MVT::v16i8, &pshufbMask[0], 16));
5035 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005036 }
Eric Christopherfd179292009-08-27 18:07:15 +00005037
Nate Begemanb9a47b82009-02-23 08:49:38 +00005038 // No SSSE3 - Calculate in place words and then fix all out of place words
5039 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
5040 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005041 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
5042 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005043 SDValue NewV = V2Only ? V2 : V1;
5044 for (int i = 0; i != 8; ++i) {
5045 int Elt0 = MaskVals[i*2];
5046 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00005047
Nate Begemanb9a47b82009-02-23 08:49:38 +00005048 // This word of the result is all undef, skip it.
5049 if (Elt0 < 0 && Elt1 < 0)
5050 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005051
Nate Begemanb9a47b82009-02-23 08:49:38 +00005052 // This word of the result is already in the correct place, skip it.
5053 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
5054 continue;
5055 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
5056 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005057
Nate Begemanb9a47b82009-02-23 08:49:38 +00005058 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
5059 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
5060 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00005061
5062 // If Elt0 and Elt1 are defined, are consecutive, and can be load
5063 // using a single extract together, load it and store it.
5064 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005065 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005066 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00005067 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005068 DAG.getIntPtrConstant(i));
5069 continue;
5070 }
5071
Nate Begemanb9a47b82009-02-23 08:49:38 +00005072 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00005073 // source byte is not also odd, shift the extracted word left 8 bits
5074 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005075 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005076 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005077 DAG.getIntPtrConstant(Elt1 / 2));
5078 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005079 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Owen Anderson95771af2011-02-25 21:41:48 +00005080 DAG.getConstant(8,
5081 TLI.getShiftAmountTy(InsElt.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005082 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005083 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
5084 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005085 }
5086 // If Elt0 is defined, extract it from the appropriate source. If the
5087 // source byte is not also even, shift the extracted word right 8 bits. If
5088 // Elt1 was also defined, OR the extracted values together before
5089 // inserting them in the result.
5090 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005091 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005092 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
5093 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005094 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Owen Anderson95771af2011-02-25 21:41:48 +00005095 DAG.getConstant(8,
5096 TLI.getShiftAmountTy(InsElt0.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005097 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005098 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
5099 DAG.getConstant(0x00FF, MVT::i16));
5100 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00005101 : InsElt0;
5102 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005103 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005104 DAG.getIntPtrConstant(i));
5105 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005106 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005107}
5108
Evan Cheng7a831ce2007-12-15 03:00:47 +00005109/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005110/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00005111/// done when every pair / quad of shuffle mask elements point to elements in
5112/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005113/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00005114static
Nate Begeman9008ca62009-04-27 18:41:29 +00005115SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005116 SelectionDAG &DAG, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00005117 EVT VT = SVOp->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00005118 SDValue V1 = SVOp->getOperand(0);
5119 SDValue V2 = SVOp->getOperand(1);
5120 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00005121 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005122 EVT NewVT;
Owen Anderson825b72b2009-08-11 20:47:22 +00005123 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005124 default: assert(false && "Unexpected!");
Owen Anderson825b72b2009-08-11 20:47:22 +00005125 case MVT::v4f32: NewVT = MVT::v2f64; break;
5126 case MVT::v4i32: NewVT = MVT::v2i64; break;
5127 case MVT::v8i16: NewVT = MVT::v4i32; break;
5128 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00005129 }
5130
Nate Begeman9008ca62009-04-27 18:41:29 +00005131 int Scale = NumElems / NewWidth;
5132 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00005133 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005134 int StartIdx = -1;
5135 for (int j = 0; j < Scale; ++j) {
5136 int EltIdx = SVOp->getMaskElt(i+j);
5137 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005138 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00005139 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00005140 StartIdx = EltIdx - (EltIdx % Scale);
5141 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00005142 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00005143 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005144 if (StartIdx == -1)
5145 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00005146 else
Nate Begeman9008ca62009-04-27 18:41:29 +00005147 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005148 }
5149
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005150 V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, V1);
5151 V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00005152 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005153}
5154
Evan Chengd880b972008-05-09 21:53:03 +00005155/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005156///
Owen Andersone50ed302009-08-10 22:56:29 +00005157static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00005158 SDValue SrcOp, SelectionDAG &DAG,
5159 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005160 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005161 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00005162 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00005163 LD = dyn_cast<LoadSDNode>(SrcOp);
5164 if (!LD) {
5165 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
5166 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00005167 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00005168 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00005169 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005170 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00005171 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005172 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00005173 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005174 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005175 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
5176 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
5177 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00005178 SrcOp.getOperand(0)
5179 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005180 }
5181 }
5182 }
5183
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005184 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005185 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005186 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00005187 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005188}
5189
Evan Chengace3c172008-07-22 21:13:36 +00005190/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
5191/// shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00005192static SDValue
Nate Begeman9008ca62009-04-27 18:41:29 +00005193LowerVECTOR_SHUFFLE_4wide(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
5194 SDValue V1 = SVOp->getOperand(0);
5195 SDValue V2 = SVOp->getOperand(1);
5196 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005197 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00005198
Evan Chengace3c172008-07-22 21:13:36 +00005199 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00005200 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00005201 SmallVector<int, 8> Mask1(4U, -1);
5202 SmallVector<int, 8> PermMask;
5203 SVOp->getMask(PermMask);
5204
Evan Chengace3c172008-07-22 21:13:36 +00005205 unsigned NumHi = 0;
5206 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00005207 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005208 int Idx = PermMask[i];
5209 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00005210 Locs[i] = std::make_pair(-1, -1);
5211 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005212 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
5213 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00005214 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00005215 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005216 NumLo++;
5217 } else {
5218 Locs[i] = std::make_pair(1, NumHi);
5219 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00005220 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005221 NumHi++;
5222 }
5223 }
5224 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005225
Evan Chengace3c172008-07-22 21:13:36 +00005226 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005227 // If no more than two elements come from either vector. This can be
5228 // implemented with two shuffles. First shuffle gather the elements.
5229 // The second shuffle, which takes the first shuffle as both of its
5230 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00005231 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005232
Nate Begeman9008ca62009-04-27 18:41:29 +00005233 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopherfd179292009-08-27 18:07:15 +00005234
Evan Chengace3c172008-07-22 21:13:36 +00005235 for (unsigned i = 0; i != 4; ++i) {
5236 if (Locs[i].first == -1)
5237 continue;
5238 else {
5239 unsigned Idx = (i < 2) ? 0 : 4;
5240 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00005241 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005242 }
5243 }
5244
Nate Begeman9008ca62009-04-27 18:41:29 +00005245 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005246 } else if (NumLo == 3 || NumHi == 3) {
5247 // Otherwise, we must have three elements from one vector, call it X, and
5248 // one element from the other, call it Y. First, use a shufps to build an
5249 // intermediate vector with the one element from Y and the element from X
5250 // that will be in the same half in the final destination (the indexes don't
5251 // matter). Then, use a shufps to build the final vector, taking the half
5252 // containing the element from Y from the intermediate, and the other half
5253 // from X.
5254 if (NumHi == 3) {
5255 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00005256 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005257 std::swap(V1, V2);
5258 }
5259
5260 // Find the element from V2.
5261 unsigned HiIndex;
5262 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005263 int Val = PermMask[HiIndex];
5264 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005265 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005266 if (Val >= 4)
5267 break;
5268 }
5269
Nate Begeman9008ca62009-04-27 18:41:29 +00005270 Mask1[0] = PermMask[HiIndex];
5271 Mask1[1] = -1;
5272 Mask1[2] = PermMask[HiIndex^1];
5273 Mask1[3] = -1;
5274 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005275
5276 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005277 Mask1[0] = PermMask[0];
5278 Mask1[1] = PermMask[1];
5279 Mask1[2] = HiIndex & 1 ? 6 : 4;
5280 Mask1[3] = HiIndex & 1 ? 4 : 6;
5281 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005282 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005283 Mask1[0] = HiIndex & 1 ? 2 : 0;
5284 Mask1[1] = HiIndex & 1 ? 0 : 2;
5285 Mask1[2] = PermMask[2];
5286 Mask1[3] = PermMask[3];
5287 if (Mask1[2] >= 0)
5288 Mask1[2] += 4;
5289 if (Mask1[3] >= 0)
5290 Mask1[3] += 4;
5291 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005292 }
Evan Chengace3c172008-07-22 21:13:36 +00005293 }
5294
5295 // Break it into (shuffle shuffle_hi, shuffle_lo).
5296 Locs.clear();
David Greenec4db4e52011-02-28 19:06:56 +00005297 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00005298 SmallVector<int,8> LoMask(4U, -1);
5299 SmallVector<int,8> HiMask(4U, -1);
5300
5301 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00005302 unsigned MaskIdx = 0;
5303 unsigned LoIdx = 0;
5304 unsigned HiIdx = 2;
5305 for (unsigned i = 0; i != 4; ++i) {
5306 if (i == 2) {
5307 MaskPtr = &HiMask;
5308 MaskIdx = 1;
5309 LoIdx = 0;
5310 HiIdx = 2;
5311 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005312 int Idx = PermMask[i];
5313 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00005314 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00005315 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00005316 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00005317 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005318 LoIdx++;
5319 } else {
5320 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00005321 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005322 HiIdx++;
5323 }
5324 }
5325
Nate Begeman9008ca62009-04-27 18:41:29 +00005326 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
5327 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
5328 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00005329 for (unsigned i = 0; i != 4; ++i) {
5330 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005331 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00005332 } else {
5333 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00005334 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00005335 }
5336 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005337 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00005338}
5339
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005340static bool MayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005341 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005342 V = V.getOperand(0);
5343 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
5344 V = V.getOperand(0);
5345 if (MayFoldLoad(V))
5346 return true;
5347 return false;
5348}
5349
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005350// FIXME: the version above should always be used. Since there's
5351// a bug where several vector shuffles can't be folded because the
5352// DAG is not updated during lowering and a node claims to have two
5353// uses while it only has one, use this version, and let isel match
5354// another instruction if the load really happens to have more than
5355// one use. Remove this version after this bug get fixed.
Evan Cheng835580f2010-10-07 20:50:20 +00005356// rdar://8434668, PR8156
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005357static bool RelaxedMayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005358 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005359 V = V.getOperand(0);
5360 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
5361 V = V.getOperand(0);
5362 if (ISD::isNormalLoad(V.getNode()))
5363 return true;
5364 return false;
5365}
5366
5367/// CanFoldShuffleIntoVExtract - Check if the current shuffle is used by
5368/// a vector extract, and if both can be later optimized into a single load.
5369/// This is done in visitEXTRACT_VECTOR_ELT and the conditions are checked
5370/// here because otherwise a target specific shuffle node is going to be
5371/// emitted for this shuffle, and the optimization not done.
5372/// FIXME: This is probably not the best approach, but fix the problem
5373/// until the right path is decided.
5374static
5375bool CanXFormVExtractWithShuffleIntoLoad(SDValue V, SelectionDAG &DAG,
5376 const TargetLowering &TLI) {
5377 EVT VT = V.getValueType();
5378 ShuffleVectorSDNode *SVOp = dyn_cast<ShuffleVectorSDNode>(V);
5379
5380 // Be sure that the vector shuffle is present in a pattern like this:
5381 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), c) -> (f32 load $addr)
5382 if (!V.hasOneUse())
5383 return false;
5384
5385 SDNode *N = *V.getNode()->use_begin();
5386 if (N->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
5387 return false;
5388
5389 SDValue EltNo = N->getOperand(1);
5390 if (!isa<ConstantSDNode>(EltNo))
5391 return false;
5392
5393 // If the bit convert changed the number of elements, it is unsafe
5394 // to examine the mask.
5395 bool HasShuffleIntoBitcast = false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005396 if (V.getOpcode() == ISD::BITCAST) {
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005397 EVT SrcVT = V.getOperand(0).getValueType();
5398 if (SrcVT.getVectorNumElements() != VT.getVectorNumElements())
5399 return false;
5400 V = V.getOperand(0);
5401 HasShuffleIntoBitcast = true;
5402 }
5403
5404 // Select the input vector, guarding against out of range extract vector.
5405 unsigned NumElems = VT.getVectorNumElements();
5406 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
5407 int Idx = (Elt > NumElems) ? -1 : SVOp->getMaskElt(Elt);
5408 V = (Idx < (int)NumElems) ? V.getOperand(0) : V.getOperand(1);
5409
5410 // Skip one more bit_convert if necessary
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005411 if (V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005412 V = V.getOperand(0);
5413
5414 if (ISD::isNormalLoad(V.getNode())) {
5415 // Is the original load suitable?
5416 LoadSDNode *LN0 = cast<LoadSDNode>(V);
5417
5418 // FIXME: avoid the multi-use bug that is preventing lots of
5419 // of foldings to be detected, this is still wrong of course, but
5420 // give the temporary desired behavior, and if it happens that
5421 // the load has real more uses, during isel it will not fold, and
5422 // will generate poor code.
5423 if (!LN0 || LN0->isVolatile()) // || !LN0->hasOneUse()
5424 return false;
5425
5426 if (!HasShuffleIntoBitcast)
5427 return true;
5428
5429 // If there's a bitcast before the shuffle, check if the load type and
5430 // alignment is valid.
5431 unsigned Align = LN0->getAlignment();
5432 unsigned NewAlign =
5433 TLI.getTargetData()->getABITypeAlignment(
5434 VT.getTypeForEVT(*DAG.getContext()));
5435
5436 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
5437 return false;
5438 }
5439
5440 return true;
5441}
5442
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005443static
Evan Cheng835580f2010-10-07 20:50:20 +00005444SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
5445 EVT VT = Op.getValueType();
5446
5447 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005448 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
5449 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00005450 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
5451 V1, DAG));
5452}
5453
5454static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005455SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
5456 bool HasSSE2) {
5457 SDValue V1 = Op.getOperand(0);
5458 SDValue V2 = Op.getOperand(1);
5459 EVT VT = Op.getValueType();
5460
5461 assert(VT != MVT::v2i64 && "unsupported shuffle type");
5462
5463 if (HasSSE2 && VT == MVT::v2f64)
5464 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
5465
5466 // v4f32 or v4i32
5467 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V2, DAG);
5468}
5469
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00005470static
5471SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
5472 SDValue V1 = Op.getOperand(0);
5473 SDValue V2 = Op.getOperand(1);
5474 EVT VT = Op.getValueType();
5475
5476 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
5477 "unsupported shuffle type");
5478
5479 if (V2.getOpcode() == ISD::UNDEF)
5480 V2 = V1;
5481
5482 // v4i32 or v4f32
5483 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
5484}
5485
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005486static
5487SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasSSE2) {
5488 SDValue V1 = Op.getOperand(0);
5489 SDValue V2 = Op.getOperand(1);
5490 EVT VT = Op.getValueType();
5491 unsigned NumElems = VT.getVectorNumElements();
5492
5493 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
5494 // operand of these instructions is only memory, so check if there's a
5495 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
5496 // same masks.
5497 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005498
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00005499 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005500 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005501 CanFoldLoad = true;
5502
5503 // When V1 is a load, it can be folded later into a store in isel, example:
5504 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
5505 // turns into:
5506 // (MOVLPSmr addr:$src1, VR128:$src2)
5507 // So, recognize this potential and also use MOVLPS or MOVLPD
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005508 if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005509 CanFoldLoad = true;
5510
Eric Christopher893a8822011-02-20 05:04:42 +00005511 // Both of them can't be memory operations though.
5512 if (MayFoldVectorLoad(V1) && MayFoldVectorLoad(V2))
5513 CanFoldLoad = false;
Owen Anderson95771af2011-02-25 21:41:48 +00005514
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005515 if (CanFoldLoad) {
5516 if (HasSSE2 && NumElems == 2)
5517 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
5518
5519 if (NumElems == 4)
5520 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
5521 }
5522
5523 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5524 // movl and movlp will both match v2i64, but v2i64 is never matched by
5525 // movl earlier because we make it strict to avoid messing with the movlp load
5526 // folding logic (see the code above getMOVLP call). Match it here then,
5527 // this is horrible, but will stay like this until we move all shuffle
5528 // matching to x86 specific nodes. Note that for the 1st condition all
5529 // types are matched with movsd.
5530 if ((HasSSE2 && NumElems == 2) || !X86::isMOVLMask(SVOp))
5531 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
5532 else if (HasSSE2)
5533 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
5534
5535
5536 assert(VT != MVT::v4i32 && "unsupported shuffle type");
5537
5538 // Invert the operand order and use SHUFPS to match it.
5539 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V2, V1,
5540 X86::getShuffleSHUFImmediate(SVOp), DAG);
5541}
5542
David Greenec4db4e52011-02-28 19:06:56 +00005543static inline unsigned getUNPCKLOpcode(EVT VT, const X86Subtarget *Subtarget) {
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005544 switch(VT.getSimpleVT().SimpleTy) {
5545 case MVT::v4i32: return X86ISD::PUNPCKLDQ;
5546 case MVT::v2i64: return X86ISD::PUNPCKLQDQ;
David Greenec4db4e52011-02-28 19:06:56 +00005547 case MVT::v4f32:
5548 return Subtarget->hasAVX() ? X86ISD::VUNPCKLPS : X86ISD::UNPCKLPS;
5549 case MVT::v2f64:
5550 return Subtarget->hasAVX() ? X86ISD::VUNPCKLPD : X86ISD::UNPCKLPD;
5551 case MVT::v8f32: return X86ISD::VUNPCKLPSY;
5552 case MVT::v4f64: return X86ISD::VUNPCKLPDY;
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005553 case MVT::v16i8: return X86ISD::PUNPCKLBW;
5554 case MVT::v8i16: return X86ISD::PUNPCKLWD;
5555 default:
Eric Christopherdd6e40a2011-02-19 03:19:09 +00005556 llvm_unreachable("Unknown type for unpckl");
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005557 }
5558 return 0;
5559}
5560
5561static inline unsigned getUNPCKHOpcode(EVT VT) {
5562 switch(VT.getSimpleVT().SimpleTy) {
5563 case MVT::v4i32: return X86ISD::PUNPCKHDQ;
5564 case MVT::v2i64: return X86ISD::PUNPCKHQDQ;
5565 case MVT::v4f32: return X86ISD::UNPCKHPS;
5566 case MVT::v2f64: return X86ISD::UNPCKHPD;
5567 case MVT::v16i8: return X86ISD::PUNPCKHBW;
5568 case MVT::v8i16: return X86ISD::PUNPCKHWD;
5569 default:
Eric Christopherdd6e40a2011-02-19 03:19:09 +00005570 llvm_unreachable("Unknown type for unpckh");
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005571 }
5572 return 0;
5573}
5574
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005575static
5576SDValue NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG,
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005577 const TargetLowering &TLI,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005578 const X86Subtarget *Subtarget) {
5579 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5580 EVT VT = Op.getValueType();
5581 DebugLoc dl = Op.getDebugLoc();
5582 SDValue V1 = Op.getOperand(0);
5583 SDValue V2 = Op.getOperand(1);
5584
5585 if (isZeroShuffle(SVOp))
5586 return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
5587
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005588 // Handle splat operations
5589 if (SVOp->isSplat()) {
5590 // Special case, this is the only place now where it's
5591 // allowed to return a vector_shuffle operation without
5592 // using a target specific node, because *hopefully* it
5593 // will be optimized away by the dag combiner.
5594 if (VT.getVectorNumElements() <= 4 &&
5595 CanXFormVExtractWithShuffleIntoLoad(Op, DAG, TLI))
5596 return Op;
5597
5598 // Handle splats by matching through known masks
5599 if (VT.getVectorNumElements() <= 4)
5600 return SDValue();
5601
Evan Cheng835580f2010-10-07 20:50:20 +00005602 // Canonicalize all of the remaining to v4f32.
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005603 return PromoteSplat(SVOp, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005604 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005605
5606 // If the shuffle can be profitably rewritten as a narrower shuffle, then
5607 // do it!
5608 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
5609 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5610 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005611 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005612 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
5613 // FIXME: Figure out a cleaner way to do this.
5614 // Try to make use of movq to zero out the top part.
5615 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
5616 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5617 if (NewOp.getNode()) {
5618 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
5619 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
5620 DAG, Subtarget, dl);
5621 }
5622 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
5623 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5624 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
5625 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
5626 DAG, Subtarget, dl);
5627 }
5628 }
5629 return SDValue();
5630}
5631
Dan Gohman475871a2008-07-27 21:46:04 +00005632SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005633X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00005634 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00005635 SDValue V1 = Op.getOperand(0);
5636 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00005637 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005638 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00005639 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005640 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005641 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
5642 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00005643 bool V1IsSplat = false;
5644 bool V2IsSplat = false;
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005645 bool HasSSE2 = Subtarget->hasSSE2() || Subtarget->hasAVX();
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00005646 bool HasSSE3 = Subtarget->hasSSE3() || Subtarget->hasAVX();
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00005647 bool HasSSSE3 = Subtarget->hasSSSE3() || Subtarget->hasAVX();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005648 MachineFunction &MF = DAG.getMachineFunction();
5649 bool OptForSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005650
Dale Johannesen0488fb62010-09-30 23:57:10 +00005651 // Shuffle operations on MMX not supported.
5652 if (isMMX)
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00005653 return Op;
5654
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005655 // Vector shuffle lowering takes 3 steps:
5656 //
5657 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
5658 // narrowing and commutation of operands should be handled.
5659 // 2) Matching of shuffles with known shuffle masks to x86 target specific
5660 // shuffle nodes.
5661 // 3) Rewriting of unmatched masks into new generic shuffle operations,
5662 // so the shuffle can be broken into other shuffles and the legalizer can
5663 // try the lowering again.
5664 //
5665 // The general ideia is that no vector_shuffle operation should be left to
5666 // be matched during isel, all of them must be converted to a target specific
5667 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00005668
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005669 // Normalize the input vectors. Here splats, zeroed vectors, profitable
5670 // narrowing and commutation of operands should be handled. The actual code
5671 // doesn't include all of those, work in progress...
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005672 SDValue NewOp = NormalizeVectorShuffle(Op, DAG, *this, Subtarget);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005673 if (NewOp.getNode())
5674 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00005675
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005676 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
5677 // unpckh_undef). Only use pshufd if speed is more important than size.
5678 if (OptForSize && X86::isUNPCKL_v_undef_Mask(SVOp))
5679 if (VT != MVT::v2i64 && VT != MVT::v2f64)
David Greenec4db4e52011-02-28 19:06:56 +00005680 return getTargetShuffleNode(getUNPCKLOpcode(VT, getSubtarget()), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005681 if (OptForSize && X86::isUNPCKH_v_undef_Mask(SVOp))
5682 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5683 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00005684
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005685 if (X86::isMOVDDUPMask(SVOp) && HasSSE3 && V2IsUndef &&
Dale Johannesen0488fb62010-09-30 23:57:10 +00005686 RelaxedMayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00005687 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005688
Dale Johannesen0488fb62010-09-30 23:57:10 +00005689 if (X86::isMOVHLPS_v_undef_Mask(SVOp))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005690 return getMOVHighToLow(Op, dl, DAG);
5691
5692 // Use to match splats
5693 if (HasSSE2 && X86::isUNPCKHMask(SVOp) && V2IsUndef &&
5694 (VT == MVT::v2f64 || VT == MVT::v2i64))
5695 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
5696
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005697 if (X86::isPSHUFDMask(SVOp)) {
5698 // The actual implementation will match the mask in the if above and then
5699 // during isel it can match several different instructions, not only pshufd
5700 // as its name says, sad but true, emulate the behavior for now...
5701 if (X86::isMOVDDUPMask(SVOp) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
5702 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
5703
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005704 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
5705
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005706 if (HasSSE2 && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005707 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
5708
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005709 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005710 return getTargetShuffleNode(X86ISD::SHUFPD, dl, VT, V1, V1,
5711 TargetMask, DAG);
5712
5713 if (VT == MVT::v4f32)
5714 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V1, V1,
5715 TargetMask, DAG);
5716 }
Eric Christopherfd179292009-08-27 18:07:15 +00005717
Evan Chengf26ffe92008-05-29 08:22:04 +00005718 // Check if this can be converted into a logical shift.
5719 bool isLeft = false;
5720 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00005721 SDValue ShVal;
Nate Begeman9008ca62009-04-27 18:41:29 +00005722 bool isShift = getSubtarget()->hasSSE2() &&
Evan Chengc3630942009-12-09 21:00:30 +00005723 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00005724 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00005725 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00005726 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00005727 EVT EltVT = VT.getVectorElementType();
5728 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00005729 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005730 }
Eric Christopherfd179292009-08-27 18:07:15 +00005731
Nate Begeman9008ca62009-04-27 18:41:29 +00005732 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005733 if (V1IsUndef)
5734 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00005735 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00005736 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Dale Johannesen0488fb62010-09-30 23:57:10 +00005737 if (!X86::isMOVLPMask(SVOp)) {
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005738 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00005739 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
5740
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005741 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00005742 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
5743 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00005744 }
Eric Christopherfd179292009-08-27 18:07:15 +00005745
Nate Begeman9008ca62009-04-27 18:41:29 +00005746 // FIXME: fold these into legal mask.
Dale Johannesen0488fb62010-09-30 23:57:10 +00005747 if (X86::isMOVLHPSMask(SVOp) && !X86::isUNPCKLMask(SVOp))
5748 return getMOVLowToHigh(Op, dl, DAG, HasSSE2);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005749
Dale Johannesen0488fb62010-09-30 23:57:10 +00005750 if (X86::isMOVHLPSMask(SVOp))
5751 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00005752
Dale Johannesen0488fb62010-09-30 23:57:10 +00005753 if (X86::isMOVSHDUPMask(SVOp) && HasSSE3 && V2IsUndef && NumElems == 4)
5754 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00005755
Dale Johannesen0488fb62010-09-30 23:57:10 +00005756 if (X86::isMOVSLDUPMask(SVOp) && HasSSE3 && V2IsUndef && NumElems == 4)
5757 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00005758
Dale Johannesen0488fb62010-09-30 23:57:10 +00005759 if (X86::isMOVLPMask(SVOp))
5760 return getMOVLP(Op, dl, DAG, HasSSE2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005761
Nate Begeman9008ca62009-04-27 18:41:29 +00005762 if (ShouldXformToMOVHLPS(SVOp) ||
5763 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
5764 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005765
Evan Chengf26ffe92008-05-29 08:22:04 +00005766 if (isShift) {
5767 // No better options. Use a vshl / vsrl.
Dan Gohman8a55ce42009-09-23 21:02:20 +00005768 EVT EltVT = VT.getVectorElementType();
5769 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00005770 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005771 }
Eric Christopherfd179292009-08-27 18:07:15 +00005772
Evan Cheng9eca5e82006-10-25 21:49:50 +00005773 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00005774 // FIXME: This should also accept a bitcast of a splat? Be careful, not
5775 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00005776 V1IsSplat = isSplatVector(V1.getNode());
5777 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00005778
Chris Lattner8a594482007-11-25 00:24:49 +00005779 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00005780 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005781 Op = CommuteVectorShuffle(SVOp, DAG);
5782 SVOp = cast<ShuffleVectorSDNode>(Op);
5783 V1 = SVOp->getOperand(0);
5784 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00005785 std::swap(V1IsSplat, V2IsSplat);
5786 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00005787 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00005788 }
5789
Nate Begeman9008ca62009-04-27 18:41:29 +00005790 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
5791 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00005792 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00005793 return V1;
5794 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
5795 // the instruction selector will not match, so get a canonical MOVL with
5796 // swapped operands to undo the commute.
5797 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00005798 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005799
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005800 if (X86::isUNPCKLMask(SVOp))
David Greenec4db4e52011-02-28 19:06:56 +00005801 return getTargetShuffleNode(getUNPCKLOpcode(VT, getSubtarget()),
5802 dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005803
5804 if (X86::isUNPCKHMask(SVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005805 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00005806
Evan Cheng9bbbb982006-10-25 20:48:19 +00005807 if (V2IsSplat) {
5808 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005809 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00005810 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00005811 SDValue NewMask = NormalizeMask(SVOp, DAG);
5812 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
5813 if (NSVOp != SVOp) {
5814 if (X86::isUNPCKLMask(NSVOp, true)) {
5815 return NewMask;
5816 } else if (X86::isUNPCKHMask(NSVOp, true)) {
5817 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005818 }
5819 }
5820 }
5821
Evan Cheng9eca5e82006-10-25 21:49:50 +00005822 if (Commuted) {
5823 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00005824 // FIXME: this seems wrong.
5825 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
5826 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005827
5828 if (X86::isUNPCKLMask(NewSVOp))
David Greenec4db4e52011-02-28 19:06:56 +00005829 return getTargetShuffleNode(getUNPCKLOpcode(VT, getSubtarget()),
5830 dl, VT, V2, V1, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005831
5832 if (X86::isUNPCKHMask(NewSVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005833 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V2, V1, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00005834 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005835
Nate Begeman9008ca62009-04-27 18:41:29 +00005836 // Normalize the node to match x86 shuffle ops if needed
Dale Johannesen0488fb62010-09-30 23:57:10 +00005837 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
Nate Begeman9008ca62009-04-27 18:41:29 +00005838 return CommuteVectorShuffle(SVOp, DAG);
5839
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00005840 // The checks below are all present in isShuffleMaskLegal, but they are
5841 // inlined here right now to enable us to directly emit target specific
5842 // nodes, and remove one by one until they don't return Op anymore.
5843 SmallVector<int, 16> M;
5844 SVOp->getMask(M);
5845
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00005846 if (isPALIGNRMask(M, VT, HasSSSE3))
5847 return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
5848 X86::getShufflePALIGNRImmediate(SVOp),
5849 DAG);
5850
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00005851 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
5852 SVOp->getSplatIndex() == 0 && V2IsUndef) {
David Greenec4db4e52011-02-28 19:06:56 +00005853 if (VT == MVT::v2f64) {
5854 X86ISD::NodeType Opcode =
5855 getSubtarget()->hasAVX() ? X86ISD::VUNPCKLPD : X86ISD::UNPCKLPD;
5856 return getTargetShuffleNode(Opcode, dl, VT, V1, V1, DAG);
5857 }
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00005858 if (VT == MVT::v2i64)
5859 return getTargetShuffleNode(X86ISD::PUNPCKLQDQ, dl, VT, V1, V1, DAG);
5860 }
5861
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00005862 if (isPSHUFHWMask(M, VT))
5863 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
5864 X86::getShufflePSHUFHWImmediate(SVOp),
5865 DAG);
5866
5867 if (isPSHUFLWMask(M, VT))
5868 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
5869 X86::getShufflePSHUFLWImmediate(SVOp),
5870 DAG);
5871
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00005872 if (isSHUFPMask(M, VT)) {
5873 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
5874 if (VT == MVT::v4f32 || VT == MVT::v4i32)
5875 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V1, V2,
5876 TargetMask, DAG);
5877 if (VT == MVT::v2f64 || VT == MVT::v2i64)
5878 return getTargetShuffleNode(X86ISD::SHUFPD, dl, VT, V1, V2,
5879 TargetMask, DAG);
5880 }
5881
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005882 if (X86::isUNPCKL_v_undef_Mask(SVOp))
5883 if (VT != MVT::v2i64 && VT != MVT::v2f64)
David Greenec4db4e52011-02-28 19:06:56 +00005884 return getTargetShuffleNode(getUNPCKLOpcode(VT, getSubtarget()),
5885 dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005886 if (X86::isUNPCKH_v_undef_Mask(SVOp))
5887 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5888 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
5889
Evan Cheng14b32e12007-12-11 01:46:18 +00005890 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
Owen Anderson825b72b2009-08-11 20:47:22 +00005891 if (VT == MVT::v8i16) {
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005892 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00005893 if (NewOp.getNode())
Evan Cheng14b32e12007-12-11 01:46:18 +00005894 return NewOp;
5895 }
5896
Owen Anderson825b72b2009-08-11 20:47:22 +00005897 if (VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005898 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005899 if (NewOp.getNode())
5900 return NewOp;
5901 }
Eric Christopherfd179292009-08-27 18:07:15 +00005902
Dale Johannesen0488fb62010-09-30 23:57:10 +00005903 // Handle all 4 wide cases with a number of shuffles.
5904 if (NumElems == 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00005905 return LowerVECTOR_SHUFFLE_4wide(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005906
Dan Gohman475871a2008-07-27 21:46:04 +00005907 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005908}
5909
Dan Gohman475871a2008-07-27 21:46:04 +00005910SDValue
5911X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00005912 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005913 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005914 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005915 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005916 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005917 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00005918 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005919 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00005920 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005921 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00005922 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
5923 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
5924 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005925 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
5926 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005927 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005928 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00005929 Op.getOperand(0)),
5930 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00005931 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005932 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00005933 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005934 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00005935 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Anderson825b72b2009-08-11 20:47:22 +00005936 } else if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00005937 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
5938 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00005939 // result has a single use which is a store or a bitcast to i32. And in
5940 // the case of a store, it's not worth it if the index is a constant 0,
5941 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00005942 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00005943 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00005944 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00005945 if ((User->getOpcode() != ISD::STORE ||
5946 (isa<ConstantSDNode>(Op.getOperand(1)) &&
5947 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005948 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00005949 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00005950 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00005951 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005952 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00005953 Op.getOperand(0)),
5954 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005955 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Owen Anderson825b72b2009-08-11 20:47:22 +00005956 } else if (VT == MVT::i32) {
Mon P Wangf0fcdd82009-01-15 21:10:20 +00005957 // ExtractPS works with constant index.
5958 if (isa<ConstantSDNode>(Op.getOperand(1)))
5959 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00005960 }
Dan Gohman475871a2008-07-27 21:46:04 +00005961 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005962}
5963
5964
Dan Gohman475871a2008-07-27 21:46:04 +00005965SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005966X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
5967 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005968 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00005969 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005970
David Greene74a579d2011-02-10 16:57:36 +00005971 SDValue Vec = Op.getOperand(0);
5972 EVT VecVT = Vec.getValueType();
5973
5974 // If this is a 256-bit vector result, first extract the 128-bit
5975 // vector and then extract from the 128-bit vector.
5976 if (VecVT.getSizeInBits() > 128) {
5977 DebugLoc dl = Op.getNode()->getDebugLoc();
5978 unsigned NumElems = VecVT.getVectorNumElements();
5979 SDValue Idx = Op.getOperand(1);
5980
5981 if (!isa<ConstantSDNode>(Idx))
5982 return SDValue();
5983
5984 unsigned ExtractNumElems = NumElems / (VecVT.getSizeInBits() / 128);
5985 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
5986
5987 // Get the 128-bit vector.
5988 bool Upper = IdxVal >= ExtractNumElems;
5989 Vec = Extract128BitVector(Vec, Idx, DAG, dl);
5990
5991 // Extract from it.
5992 SDValue ScaledIdx = Idx;
5993 if (Upper)
5994 ScaledIdx = DAG.getNode(ISD::SUB, dl, Idx.getValueType(), Idx,
5995 DAG.getConstant(ExtractNumElems,
5996 Idx.getValueType()));
5997 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, Op.getValueType(), Vec,
5998 ScaledIdx);
5999 }
6000
6001 assert(Vec.getValueSizeInBits() <= 128 && "Unexpected vector length");
6002
Evan Cheng62a3f152008-03-24 21:52:23 +00006003 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006004 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006005 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00006006 return Res;
6007 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00006008
Owen Andersone50ed302009-08-10 22:56:29 +00006009 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006010 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006011 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006012 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00006013 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006014 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00006015 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006016 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6017 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006018 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006019 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00006020 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006021 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00006022 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00006023 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00006024 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00006025 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00006026 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006027 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006028 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006029 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006030 if (Idx == 0)
6031 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00006032
Evan Cheng0db9fe62006-04-25 20:13:52 +00006033 // SHUFPS the element to the lowest double word, then movss.
Nate Begeman9008ca62009-04-27 18:41:29 +00006034 int Mask[4] = { Idx, -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006035 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006036 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00006037 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00006038 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00006039 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00006040 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00006041 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
6042 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
6043 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006044 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006045 if (Idx == 0)
6046 return Op;
6047
6048 // UNPCKHPD the element to the lowest double word, then movsd.
6049 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
6050 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00006051 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006052 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006053 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00006054 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00006055 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00006056 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006057 }
6058
Dan Gohman475871a2008-07-27 21:46:04 +00006059 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006060}
6061
Dan Gohman475871a2008-07-27 21:46:04 +00006062SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006063X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
6064 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006065 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006066 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006067 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006068
Dan Gohman475871a2008-07-27 21:46:04 +00006069 SDValue N0 = Op.getOperand(0);
6070 SDValue N1 = Op.getOperand(1);
6071 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00006072
Dan Gohman8a55ce42009-09-23 21:02:20 +00006073 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00006074 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00006075 unsigned Opc;
6076 if (VT == MVT::v8i16)
6077 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00006078 else if (VT == MVT::v16i8)
6079 Opc = X86ISD::PINSRB;
6080 else
6081 Opc = X86ISD::PINSRB;
6082
Nate Begeman14d12ca2008-02-11 04:19:36 +00006083 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
6084 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00006085 if (N1.getValueType() != MVT::i32)
6086 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
6087 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006088 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00006089 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00006090 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00006091 // Bits [7:6] of the constant are the source select. This will always be
6092 // zero here. The DAG Combiner may combine an extract_elt index into these
6093 // bits. For example (insert (extract, 3), 2) could be matched by putting
6094 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00006095 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00006096 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00006097 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00006098 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006099 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00006100 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00006101 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00006102 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00006103 } else if (EltVT == MVT::i32 && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00006104 // PINSR* works with constant index.
6105 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00006106 }
Dan Gohman475871a2008-07-27 21:46:04 +00006107 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006108}
6109
Dan Gohman475871a2008-07-27 21:46:04 +00006110SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006111X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006112 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006113 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006114
David Greene6b381262011-02-09 15:32:06 +00006115 DebugLoc dl = Op.getDebugLoc();
6116 SDValue N0 = Op.getOperand(0);
6117 SDValue N1 = Op.getOperand(1);
6118 SDValue N2 = Op.getOperand(2);
6119
6120 // If this is a 256-bit vector result, first insert into a 128-bit
6121 // vector and then insert into the 256-bit vector.
6122 if (VT.getSizeInBits() > 128) {
6123 if (!isa<ConstantSDNode>(N2))
6124 return SDValue();
6125
6126 // Get the 128-bit vector.
6127 unsigned NumElems = VT.getVectorNumElements();
6128 unsigned IdxVal = cast<ConstantSDNode>(N2)->getZExtValue();
6129 bool Upper = IdxVal >= NumElems / 2;
6130
6131 SDValue SubN0 = Extract128BitVector(N0, N2, DAG, dl);
6132
6133 // Insert into it.
6134 SDValue ScaledN2 = N2;
6135 if (Upper)
6136 ScaledN2 = DAG.getNode(ISD::SUB, dl, N2.getValueType(), N2,
Owen Anderson95771af2011-02-25 21:41:48 +00006137 DAG.getConstant(NumElems /
David Greene6b381262011-02-09 15:32:06 +00006138 (VT.getSizeInBits() / 128),
6139 N2.getValueType()));
6140 Op = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, SubN0.getValueType(), SubN0,
6141 N1, ScaledN2);
6142
6143 // Insert the 128-bit vector
6144 // FIXME: Why UNDEF?
6145 return Insert128BitVector(N0, Op, N2, DAG, dl);
6146 }
6147
Nate Begeman14d12ca2008-02-11 04:19:36 +00006148 if (Subtarget->hasSSE41())
6149 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
6150
Dan Gohman8a55ce42009-09-23 21:02:20 +00006151 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00006152 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00006153
Dan Gohman8a55ce42009-09-23 21:02:20 +00006154 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00006155 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
6156 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00006157 if (N1.getValueType() != MVT::i32)
6158 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
6159 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006160 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00006161 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006162 }
Dan Gohman475871a2008-07-27 21:46:04 +00006163 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006164}
6165
Dan Gohman475871a2008-07-27 21:46:04 +00006166SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006167X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) const {
David Greene2fcdfb42011-02-10 23:11:29 +00006168 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006169 DebugLoc dl = Op.getDebugLoc();
David Greene2fcdfb42011-02-10 23:11:29 +00006170 EVT OpVT = Op.getValueType();
6171
6172 // If this is a 256-bit vector result, first insert into a 128-bit
6173 // vector and then insert into the 256-bit vector.
6174 if (OpVT.getSizeInBits() > 128) {
6175 // Insert into a 128-bit vector.
6176 EVT VT128 = EVT::getVectorVT(*Context,
6177 OpVT.getVectorElementType(),
6178 OpVT.getVectorNumElements() / 2);
6179
6180 Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT128, Op.getOperand(0));
6181
6182 // Insert the 128-bit vector.
6183 return Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, OpVT), Op,
6184 DAG.getConstant(0, MVT::i32),
6185 DAG, dl);
6186 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00006187
Chris Lattnerf172ecd2010-07-04 23:07:25 +00006188 if (Op.getValueType() == MVT::v1i64 &&
6189 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00006190 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00006191
Owen Anderson825b72b2009-08-11 20:47:22 +00006192 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Dale Johannesen0488fb62010-09-30 23:57:10 +00006193 assert(Op.getValueType().getSimpleVT().getSizeInBits() == 128 &&
6194 "Expected an SSE type!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006195 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(),
Dale Johannesen0488fb62010-09-30 23:57:10 +00006196 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006197}
6198
David Greene91585092011-01-26 15:38:49 +00006199// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
6200// a simple subregister reference or explicit instructions to grab
6201// upper bits of a vector.
6202SDValue
6203X86TargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
6204 if (Subtarget->hasAVX()) {
David Greenea5f26012011-02-07 19:36:54 +00006205 DebugLoc dl = Op.getNode()->getDebugLoc();
6206 SDValue Vec = Op.getNode()->getOperand(0);
6207 SDValue Idx = Op.getNode()->getOperand(1);
6208
6209 if (Op.getNode()->getValueType(0).getSizeInBits() == 128
6210 && Vec.getNode()->getValueType(0).getSizeInBits() == 256) {
6211 return Extract128BitVector(Vec, Idx, DAG, dl);
6212 }
David Greene91585092011-01-26 15:38:49 +00006213 }
6214 return SDValue();
6215}
6216
David Greenecfe33c42011-01-26 19:13:22 +00006217// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
6218// simple superregister reference or explicit instructions to insert
6219// the upper bits of a vector.
6220SDValue
6221X86TargetLowering::LowerINSERT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
6222 if (Subtarget->hasAVX()) {
6223 DebugLoc dl = Op.getNode()->getDebugLoc();
6224 SDValue Vec = Op.getNode()->getOperand(0);
6225 SDValue SubVec = Op.getNode()->getOperand(1);
6226 SDValue Idx = Op.getNode()->getOperand(2);
6227
6228 if (Op.getNode()->getValueType(0).getSizeInBits() == 256
6229 && SubVec.getNode()->getValueType(0).getSizeInBits() == 128) {
David Greenea5f26012011-02-07 19:36:54 +00006230 return Insert128BitVector(Vec, SubVec, Idx, DAG, dl);
David Greenecfe33c42011-01-26 19:13:22 +00006231 }
6232 }
6233 return SDValue();
6234}
6235
Bill Wendling056292f2008-09-16 21:48:12 +00006236// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
6237// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
6238// one of the above mentioned nodes. It has to be wrapped because otherwise
6239// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
6240// be used to form addressing mode. These wrapped nodes will be selected
6241// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00006242SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006243X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006244 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00006245
Chris Lattner41621a22009-06-26 19:22:52 +00006246 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6247 // global base reg.
6248 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006249 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006250 CodeModel::Model M = getTargetMachine().getCodeModel();
6251
Chris Lattner4f066492009-07-11 20:29:19 +00006252 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006253 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00006254 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00006255 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006256 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00006257 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006258 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00006259
Evan Cheng1606e8e2009-03-13 07:51:59 +00006260 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00006261 CP->getAlignment(),
6262 CP->getOffset(), OpFlag);
6263 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00006264 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006265 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00006266 if (OpFlag) {
6267 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00006268 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006269 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006270 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006271 }
6272
6273 return Result;
6274}
6275
Dan Gohmand858e902010-04-17 15:26:15 +00006276SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00006277 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00006278
Chris Lattner18c59872009-06-27 04:16:01 +00006279 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6280 // global base reg.
6281 unsigned char OpFlag = 0;
6282 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006283 CodeModel::Model M = getTargetMachine().getCodeModel();
6284
Chris Lattner4f066492009-07-11 20:29:19 +00006285 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006286 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00006287 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00006288 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006289 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00006290 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006291 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00006292
Chris Lattner18c59872009-06-27 04:16:01 +00006293 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
6294 OpFlag);
6295 DebugLoc DL = JT->getDebugLoc();
6296 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00006297
Chris Lattner18c59872009-06-27 04:16:01 +00006298 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00006299 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00006300 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6301 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006302 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00006303 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00006304
Chris Lattner18c59872009-06-27 04:16:01 +00006305 return Result;
6306}
6307
6308SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006309X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00006310 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00006311
Chris Lattner18c59872009-06-27 04:16:01 +00006312 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6313 // global base reg.
6314 unsigned char OpFlag = 0;
6315 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006316 CodeModel::Model M = getTargetMachine().getCodeModel();
6317
Chris Lattner4f066492009-07-11 20:29:19 +00006318 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006319 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00006320 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00006321 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006322 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00006323 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006324 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00006325
Chris Lattner18c59872009-06-27 04:16:01 +00006326 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00006327
Chris Lattner18c59872009-06-27 04:16:01 +00006328 DebugLoc DL = Op.getDebugLoc();
6329 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00006330
6331
Chris Lattner18c59872009-06-27 04:16:01 +00006332 // With PIC, the address is actually $g + Offset.
6333 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00006334 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00006335 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6336 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006337 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00006338 Result);
6339 }
Eric Christopherfd179292009-08-27 18:07:15 +00006340
Chris Lattner18c59872009-06-27 04:16:01 +00006341 return Result;
6342}
6343
Dan Gohman475871a2008-07-27 21:46:04 +00006344SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006345X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00006346 // Create the TargetBlockAddressAddress node.
6347 unsigned char OpFlags =
6348 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00006349 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00006350 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +00006351 DebugLoc dl = Op.getDebugLoc();
6352 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
6353 /*isTarget=*/true, OpFlags);
6354
Dan Gohmanf705adb2009-10-30 01:28:02 +00006355 if (Subtarget->isPICStyleRIPRel() &&
6356 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00006357 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
6358 else
6359 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00006360
Dan Gohman29cbade2009-11-20 23:18:13 +00006361 // With PIC, the address is actually $g + Offset.
6362 if (isGlobalRelativeToPICBase(OpFlags)) {
6363 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
6364 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
6365 Result);
6366 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00006367
6368 return Result;
6369}
6370
6371SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00006372X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00006373 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00006374 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00006375 // Create the TargetGlobalAddress node, folding in the constant
6376 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00006377 unsigned char OpFlags =
6378 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006379 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00006380 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006381 if (OpFlags == X86II::MO_NO_FLAG &&
6382 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00006383 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00006384 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00006385 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006386 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00006387 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00006388 }
Eric Christopherfd179292009-08-27 18:07:15 +00006389
Chris Lattner4f066492009-07-11 20:29:19 +00006390 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006391 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00006392 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
6393 else
6394 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00006395
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006396 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00006397 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00006398 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
6399 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006400 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006401 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006402
Chris Lattner36c25012009-07-10 07:34:39 +00006403 // For globals that require a load from a stub to get the address, emit the
6404 // load.
6405 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00006406 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00006407 MachinePointerInfo::getGOT(), false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006408
Dan Gohman6520e202008-10-18 02:06:02 +00006409 // If there was a non-zero offset that we didn't fold, create an explicit
6410 // addition for it.
6411 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00006412 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00006413 DAG.getConstant(Offset, getPointerTy()));
6414
Evan Cheng0db9fe62006-04-25 20:13:52 +00006415 return Result;
6416}
6417
Evan Chengda43bcf2008-09-24 00:05:32 +00006418SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006419X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00006420 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00006421 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006422 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00006423}
6424
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006425static SDValue
6426GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00006427 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerb903bed2009-06-26 21:20:29 +00006428 unsigned char OperandFlags) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006429 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006430 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006431 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00006432 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006433 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00006434 GA->getOffset(),
6435 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006436 if (InFlag) {
6437 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00006438 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006439 } else {
6440 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00006441 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006442 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006443
6444 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00006445 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006446
Rafael Espindola15f1b662009-04-24 12:59:40 +00006447 SDValue Flag = Chain.getValue(1);
6448 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006449}
6450
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006451// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00006452static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006453LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006454 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00006455 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00006456 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
6457 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006458 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006459 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006460 InFlag = Chain.getValue(1);
6461
Chris Lattnerb903bed2009-06-26 21:20:29 +00006462 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006463}
6464
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006465// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00006466static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006467LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006468 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00006469 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
6470 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006471}
6472
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006473// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
6474// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00006475static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006476 const EVT PtrVT, TLSModel::Model model,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00006477 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00006478 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00006479
Chris Lattnerf93b90c2010-09-22 04:39:11 +00006480 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
6481 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
6482 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00006483
Michael J. Spencerec38de22010-10-10 22:04:20 +00006484 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00006485 DAG.getIntPtrConstant(0),
6486 MachinePointerInfo(Ptr), false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00006487
Chris Lattnerb903bed2009-06-26 21:20:29 +00006488 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006489 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
6490 // initialexec.
6491 unsigned WrapperKind = X86ISD::Wrapper;
6492 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00006493 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00006494 } else if (is64Bit) {
6495 assert(model == TLSModel::InitialExec);
6496 OperandFlags = X86II::MO_GOTTPOFF;
6497 WrapperKind = X86ISD::WrapperRIP;
6498 } else {
6499 assert(model == TLSModel::InitialExec);
6500 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00006501 }
Eric Christopherfd179292009-08-27 18:07:15 +00006502
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006503 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
6504 // exec)
Michael J. Spencerec38de22010-10-10 22:04:20 +00006505 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00006506 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00006507 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00006508 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00006509
Rafael Espindola9a580232009-02-27 13:37:18 +00006510 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00006511 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00006512 MachinePointerInfo::getGOT(), false, false, 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00006513
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006514 // The address of the thread local variable is the add of the thread
6515 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00006516 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006517}
6518
Dan Gohman475871a2008-07-27 21:46:04 +00006519SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006520X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00006521
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006522 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00006523 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00006524
Eric Christopher30ef0e52010-06-03 04:07:48 +00006525 if (Subtarget->isTargetELF()) {
6526 // TODO: implement the "local dynamic" model
6527 // TODO: implement the "initial exec"model for pic executables
Michael J. Spencerec38de22010-10-10 22:04:20 +00006528
Eric Christopher30ef0e52010-06-03 04:07:48 +00006529 // If GV is an alias then use the aliasee for determining
6530 // thread-localness.
6531 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
6532 GV = GA->resolveAliasedGlobal(false);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006533
6534 TLSModel::Model model
Eric Christopher30ef0e52010-06-03 04:07:48 +00006535 = getTLSModel(GV, getTargetMachine().getRelocationModel());
Michael J. Spencerec38de22010-10-10 22:04:20 +00006536
Eric Christopher30ef0e52010-06-03 04:07:48 +00006537 switch (model) {
6538 case TLSModel::GeneralDynamic:
6539 case TLSModel::LocalDynamic: // not implemented
6540 if (Subtarget->is64Bit())
6541 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
6542 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Michael J. Spencerec38de22010-10-10 22:04:20 +00006543
Eric Christopher30ef0e52010-06-03 04:07:48 +00006544 case TLSModel::InitialExec:
6545 case TLSModel::LocalExec:
6546 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
6547 Subtarget->is64Bit());
6548 }
6549 } else if (Subtarget->isTargetDarwin()) {
6550 // Darwin only has one model of TLS. Lower to that.
6551 unsigned char OpFlag = 0;
6552 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
6553 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006554
Eric Christopher30ef0e52010-06-03 04:07:48 +00006555 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6556 // global base reg.
6557 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
6558 !Subtarget->is64Bit();
6559 if (PIC32)
6560 OpFlag = X86II::MO_TLVP_PIC_BASE;
6561 else
6562 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006563 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00006564 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00006565 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00006566 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00006567 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006568
Eric Christopher30ef0e52010-06-03 04:07:48 +00006569 // With PIC32, the address is actually $g + Offset.
6570 if (PIC32)
6571 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6572 DAG.getNode(X86ISD::GlobalBaseReg,
6573 DebugLoc(), getPointerTy()),
6574 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006575
Eric Christopher30ef0e52010-06-03 04:07:48 +00006576 // Lowering the machine isd will make sure everything is in the right
6577 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00006578 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006579 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00006580 SDValue Args[] = { Chain, Offset };
6581 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006582
Eric Christopher30ef0e52010-06-03 04:07:48 +00006583 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
6584 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
6585 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00006586
Eric Christopher30ef0e52010-06-03 04:07:48 +00006587 // And our return value (tls address) is in the standard call return value
6588 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00006589 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
6590 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy());
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006591 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00006592
Eric Christopher30ef0e52010-06-03 04:07:48 +00006593 assert(false &&
6594 "TLS not implemented for this target.");
Eric Christopherfd179292009-08-27 18:07:15 +00006595
Torok Edwinc23197a2009-07-14 16:55:14 +00006596 llvm_unreachable("Unreachable");
Chris Lattner5867de12009-04-01 22:14:45 +00006597 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006598}
6599
Evan Cheng0db9fe62006-04-25 20:13:52 +00006600
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006601/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00006602/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00006603SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman4c1fa612008-03-03 22:22:09 +00006604 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00006605 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006606 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006607 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006608 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00006609 SDValue ShOpLo = Op.getOperand(0);
6610 SDValue ShOpHi = Op.getOperand(1);
6611 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00006612 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00006613 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00006614 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00006615
Dan Gohman475871a2008-07-27 21:46:04 +00006616 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006617 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00006618 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
6619 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006620 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006621 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
6622 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006623 }
Evan Chenge3413162006-01-09 18:33:28 +00006624
Owen Anderson825b72b2009-08-11 20:47:22 +00006625 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
6626 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00006627 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00006628 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00006629
Dan Gohman475871a2008-07-27 21:46:04 +00006630 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00006631 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00006632 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
6633 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00006634
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006635 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00006636 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
6637 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006638 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006639 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
6640 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006641 }
6642
Dan Gohman475871a2008-07-27 21:46:04 +00006643 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00006644 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006645}
Evan Chenga3195e82006-01-12 22:54:21 +00006646
Dan Gohmand858e902010-04-17 15:26:15 +00006647SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
6648 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006649 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00006650
Dale Johannesen0488fb62010-09-30 23:57:10 +00006651 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00006652 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00006653
Owen Anderson825b72b2009-08-11 20:47:22 +00006654 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00006655 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00006656
Eli Friedman36df4992009-05-27 00:47:34 +00006657 // These are really Legal; return the operand so the caller accepts it as
6658 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00006659 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00006660 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00006661 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00006662 Subtarget->is64Bit()) {
6663 return Op;
6664 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006665
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006666 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006667 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006668 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00006669 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006670 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00006671 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00006672 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006673 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006674 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00006675 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
6676}
Evan Cheng0db9fe62006-04-25 20:13:52 +00006677
Owen Andersone50ed302009-08-10 22:56:29 +00006678SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00006679 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00006680 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006681 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00006682 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00006683 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00006684 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006685 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006686 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00006687 else
Owen Anderson825b72b2009-08-11 20:47:22 +00006688 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006689
Chris Lattner492a43e2010-09-22 01:28:21 +00006690 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006691
Chris Lattner492a43e2010-09-22 01:28:21 +00006692 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
6693 MachineMemOperand *MMO =
6694 DAG.getMachineFunction()
6695 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6696 MachineMemOperand::MOLoad, ByteSize, ByteSize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006697
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006698 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00006699 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
6700 X86ISD::FILD, DL,
6701 Tys, Ops, array_lengthof(Ops),
6702 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006703
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006704 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006705 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00006706 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006707
6708 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
6709 // shouldn't be necessary except that RFP cannot be live across
6710 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006711 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00006712 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
6713 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006714 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00006715 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006716 SDValue Ops[] = {
6717 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
6718 };
Chris Lattner492a43e2010-09-22 01:28:21 +00006719 MachineMemOperand *MMO =
6720 DAG.getMachineFunction()
6721 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00006722 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006723
Chris Lattner492a43e2010-09-22 01:28:21 +00006724 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
6725 Ops, array_lengthof(Ops),
6726 Op.getValueType(), MMO);
6727 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006728 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006729 false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006730 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006731
Evan Cheng0db9fe62006-04-25 20:13:52 +00006732 return Result;
6733}
6734
Bill Wendling8b8a6362009-01-17 03:56:04 +00006735// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00006736SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
6737 SelectionDAG &DAG) const {
Bill Wendling8b8a6362009-01-17 03:56:04 +00006738 // This algorithm is not obvious. Here it is in C code, more or less:
6739 /*
6740 double uint64_to_double( uint32_t hi, uint32_t lo ) {
6741 static const __m128i exp = { 0x4330000045300000ULL, 0 };
6742 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00006743
Bill Wendling8b8a6362009-01-17 03:56:04 +00006744 // Copy ints to xmm registers.
6745 __m128i xh = _mm_cvtsi32_si128( hi );
6746 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00006747
Bill Wendling8b8a6362009-01-17 03:56:04 +00006748 // Combine into low half of a single xmm register.
6749 __m128i x = _mm_unpacklo_epi32( xh, xl );
6750 __m128d d;
6751 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00006752
Bill Wendling8b8a6362009-01-17 03:56:04 +00006753 // Merge in appropriate exponents to give the integer bits the right
6754 // magnitude.
6755 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00006756
Bill Wendling8b8a6362009-01-17 03:56:04 +00006757 // Subtract away the biases to deal with the IEEE-754 double precision
6758 // implicit 1.
6759 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00006760
Bill Wendling8b8a6362009-01-17 03:56:04 +00006761 // All conversions up to here are exact. The correctly rounded result is
6762 // calculated using the current rounding mode using the following
6763 // horizontal add.
6764 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
6765 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
6766 // store doesn't really need to be here (except
6767 // maybe to zero the other double)
6768 return sd;
6769 }
6770 */
Dale Johannesen040225f2008-10-21 23:07:49 +00006771
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006772 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00006773 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00006774
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006775 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00006776 std::vector<Constant*> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00006777 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
6778 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
6779 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
6780 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00006781 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006782 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006783
Bill Wendling8b8a6362009-01-17 03:56:04 +00006784 std::vector<Constant*> CV1;
Owen Andersona90b3dc2009-07-15 21:51:10 +00006785 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006786 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
Owen Andersona90b3dc2009-07-15 21:51:10 +00006787 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006788 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00006789 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006790 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006791
Owen Anderson825b72b2009-08-11 20:47:22 +00006792 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6793 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00006794 Op.getOperand(0),
6795 DAG.getIntPtrConstant(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006796 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6797 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00006798 Op.getOperand(0),
6799 DAG.getIntPtrConstant(0)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006800 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
6801 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00006802 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006803 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00006804 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006805 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck2);
Owen Anderson825b72b2009-08-11 20:47:22 +00006806 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00006807 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006808 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00006809 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006810
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006811 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00006812 int ShufMask[2] = { 1, -1 };
Owen Anderson825b72b2009-08-11 20:47:22 +00006813 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
6814 DAG.getUNDEF(MVT::v2f64), ShufMask);
6815 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
6816 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006817 DAG.getIntPtrConstant(0));
6818}
6819
Bill Wendling8b8a6362009-01-17 03:56:04 +00006820// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00006821SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
6822 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006823 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00006824 // FP constant to bias correct the final result.
6825 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00006826 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006827
6828 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00006829 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6830 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Bill Wendling8b8a6362009-01-17 03:56:04 +00006831 Op.getOperand(0),
6832 DAG.getIntPtrConstant(0)));
6833
Owen Anderson825b72b2009-08-11 20:47:22 +00006834 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006835 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00006836 DAG.getIntPtrConstant(0));
6837
6838 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00006839 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006840 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00006841 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006842 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006843 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00006844 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006845 MVT::v2f64, Bias)));
6846 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006847 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00006848 DAG.getIntPtrConstant(0));
6849
6850 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00006851 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006852
6853 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00006854 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00006855
Owen Anderson825b72b2009-08-11 20:47:22 +00006856 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006857 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00006858 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00006859 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006860 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00006861 }
6862
6863 // Handle final rounding.
6864 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00006865}
6866
Dan Gohmand858e902010-04-17 15:26:15 +00006867SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
6868 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00006869 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006870 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00006871
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006872 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00006873 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
6874 // the optimization here.
6875 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00006876 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00006877
Owen Andersone50ed302009-08-10 22:56:29 +00006878 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006879 EVT DstVT = Op.getValueType();
6880 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00006881 return LowerUINT_TO_FP_i64(Op, DAG);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006882 else if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00006883 return LowerUINT_TO_FP_i32(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00006884
6885 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00006886 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006887 if (SrcVT == MVT::i32) {
6888 SDValue WordOff = DAG.getConstant(4, getPointerTy());
6889 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
6890 getPointerTy(), StackSlot, WordOff);
6891 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006892 StackSlot, MachinePointerInfo(),
6893 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006894 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006895 OffsetSlot, MachinePointerInfo(),
6896 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006897 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
6898 return Fild;
6899 }
6900
6901 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
6902 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006903 StackSlot, MachinePointerInfo(),
6904 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006905 // For i64 source, we need to add the appropriate power of 2 if the input
6906 // was negative. This is the same as the optimization in
6907 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
6908 // we must be careful to do the computation in x87 extended precision, not
6909 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00006910 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
6911 MachineMemOperand *MMO =
6912 DAG.getMachineFunction()
6913 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6914 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006915
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006916 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
6917 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00006918 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
6919 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006920
6921 APInt FF(32, 0x5F800000ULL);
6922
6923 // Check whether the sign bit is set.
6924 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
6925 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
6926 ISD::SETLT);
6927
6928 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
6929 SDValue FudgePtr = DAG.getConstantPool(
6930 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
6931 getPointerTy());
6932
6933 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
6934 SDValue Zero = DAG.getIntPtrConstant(0);
6935 SDValue Four = DAG.getIntPtrConstant(4);
6936 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
6937 Zero, Four);
6938 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
6939
6940 // Load the value out, extending it from f32 to f80.
6941 // FIXME: Avoid the extend by constructing the right constant pool?
Stuart Hastingsa9011292011-02-16 16:23:55 +00006942 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00006943 FudgePtr, MachinePointerInfo::getConstantPool(),
6944 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006945 // Extend everything to 80 bits to force it to be done on x87.
6946 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
6947 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00006948}
6949
Dan Gohman475871a2008-07-27 21:46:04 +00006950std::pair<SDValue,SDValue> X86TargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00006951FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) const {
Chris Lattner07290932010-09-22 01:05:16 +00006952 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00006953
Owen Andersone50ed302009-08-10 22:56:29 +00006954 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00006955
6956 if (!IsSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006957 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
6958 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00006959 }
6960
Owen Anderson825b72b2009-08-11 20:47:22 +00006961 assert(DstTy.getSimpleVT() <= MVT::i64 &&
6962 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00006963 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00006964
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006965 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00006966 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00006967 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00006968 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00006969 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00006970 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00006971 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00006972 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006973
Evan Cheng87c89352007-10-15 20:11:21 +00006974 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
6975 // stack slot.
6976 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00006977 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00006978 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006979 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00006980
Michael J. Spencerec38de22010-10-10 22:04:20 +00006981
6982
Evan Cheng0db9fe62006-04-25 20:13:52 +00006983 unsigned Opc;
Owen Anderson825b72b2009-08-11 20:47:22 +00006984 switch (DstTy.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00006985 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson825b72b2009-08-11 20:47:22 +00006986 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
6987 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
6988 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006989 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006990
Dan Gohman475871a2008-07-27 21:46:04 +00006991 SDValue Chain = DAG.getEntryNode();
6992 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00006993 EVT TheVT = Op.getOperand(0).getValueType();
6994 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006995 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00006996 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006997 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006998 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00006999 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00007000 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00007001 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00007002 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00007003
Chris Lattner492a43e2010-09-22 01:28:21 +00007004 MachineMemOperand *MMO =
7005 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7006 MachineMemOperand::MOLoad, MemSize, MemSize);
7007 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
7008 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007009 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00007010 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007011 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
7012 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007013
Chris Lattner07290932010-09-22 01:05:16 +00007014 MachineMemOperand *MMO =
7015 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7016 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007017
Evan Cheng0db9fe62006-04-25 20:13:52 +00007018 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00007019 SDValue Ops[] = { Chain, Value, StackSlot };
Chris Lattner07290932010-09-22 01:05:16 +00007020 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
7021 Ops, 3, DstTy, MMO);
Evan Chengd9558e02006-01-06 00:43:03 +00007022
Chris Lattner27a6c732007-11-24 07:07:01 +00007023 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007024}
7025
Dan Gohmand858e902010-04-17 15:26:15 +00007026SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
7027 SelectionDAG &DAG) const {
Dale Johannesen0488fb62010-09-30 23:57:10 +00007028 if (Op.getValueType().isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007029 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007030
Eli Friedman948e95a2009-05-23 09:59:16 +00007031 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00007032 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00007033 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
7034 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00007035
Chris Lattner27a6c732007-11-24 07:07:01 +00007036 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007037 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00007038 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00007039}
7040
Dan Gohmand858e902010-04-17 15:26:15 +00007041SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
7042 SelectionDAG &DAG) const {
Eli Friedman948e95a2009-05-23 09:59:16 +00007043 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
7044 SDValue FIST = Vals.first, StackSlot = Vals.second;
7045 assert(FIST.getNode() && "Unexpected failure");
7046
7047 // Load the result.
7048 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00007049 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007050}
7051
Dan Gohmand858e902010-04-17 15:26:15 +00007052SDValue X86TargetLowering::LowerFABS(SDValue Op,
7053 SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007054 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007055 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007056 EVT VT = Op.getValueType();
7057 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007058 if (VT.isVector())
7059 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007060 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007061 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007062 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00007063 CV.push_back(C);
7064 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007065 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007066 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00007067 CV.push_back(C);
7068 CV.push_back(C);
7069 CV.push_back(C);
7070 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007071 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007072 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007073 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007074 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007075 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007076 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007077 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007078}
7079
Dan Gohmand858e902010-04-17 15:26:15 +00007080SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007081 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007082 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007083 EVT VT = Op.getValueType();
7084 EVT EltVT = VT;
Duncan Sandsda9ad382009-09-06 19:29:07 +00007085 if (VT.isVector())
Duncan Sands83ec4b62008-06-06 12:08:01 +00007086 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007087 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007088 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007089 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00007090 CV.push_back(C);
7091 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007092 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007093 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00007094 CV.push_back(C);
7095 CV.push_back(C);
7096 CV.push_back(C);
7097 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007098 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007099 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007100 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007101 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007102 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007103 false, false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00007104 if (VT.isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007105 return DAG.getNode(ISD::BITCAST, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007106 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007107 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007108 Op.getOperand(0)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007109 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00007110 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007111 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00007112 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00007113}
7114
Dan Gohmand858e902010-04-17 15:26:15 +00007115SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007116 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00007117 SDValue Op0 = Op.getOperand(0);
7118 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007119 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007120 EVT VT = Op.getValueType();
7121 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00007122
7123 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007124 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007125 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00007126 SrcVT = VT;
7127 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007128 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007129 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007130 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007131 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007132 }
7133
7134 // At this point the operands and the result should have the same
7135 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00007136
Evan Cheng68c47cb2007-01-05 07:55:56 +00007137 // First get the sign bit of second operand.
7138 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007139 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007140 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
7141 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007142 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007143 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
7144 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7145 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7146 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007147 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007148 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007149 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007150 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007151 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007152 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007153 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00007154
7155 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007156 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007157 // Op0 is MVT::f32, Op1 is MVT::f64.
7158 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
7159 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
7160 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007161 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00007162 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00007163 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007164 }
7165
Evan Cheng73d6cf12007-01-05 21:37:56 +00007166 // Clear first operand sign bit.
7167 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00007168 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007169 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
7170 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00007171 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007172 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
7173 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7174 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7175 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00007176 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007177 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007178 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007179 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007180 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007181 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007182 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00007183
7184 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00007185 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00007186}
7187
Dan Gohman076aee32009-03-04 19:44:21 +00007188/// Emit nodes that will be selected as "test Op0,Op0", or something
7189/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00007190SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00007191 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00007192 DebugLoc dl = Op.getDebugLoc();
7193
Dan Gohman31125812009-03-07 01:58:32 +00007194 // CF and OF aren't always set the way we want. Determine which
7195 // of these we need.
7196 bool NeedCF = false;
7197 bool NeedOF = false;
7198 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007199 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00007200 case X86::COND_A: case X86::COND_AE:
7201 case X86::COND_B: case X86::COND_BE:
7202 NeedCF = true;
7203 break;
7204 case X86::COND_G: case X86::COND_GE:
7205 case X86::COND_L: case X86::COND_LE:
7206 case X86::COND_O: case X86::COND_NO:
7207 NeedOF = true;
7208 break;
Dan Gohman31125812009-03-07 01:58:32 +00007209 }
7210
Dan Gohman076aee32009-03-04 19:44:21 +00007211 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00007212 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
7213 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007214 if (Op.getResNo() != 0 || NeedOF || NeedCF)
7215 // Emit a CMP with 0, which is the TEST pattern.
7216 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
7217 DAG.getConstant(0, Op.getValueType()));
7218
7219 unsigned Opcode = 0;
7220 unsigned NumOperands = 0;
7221 switch (Op.getNode()->getOpcode()) {
7222 case ISD::ADD:
7223 // Due to an isel shortcoming, be conservative if this add is likely to be
7224 // selected as part of a load-modify-store instruction. When the root node
7225 // in a match is a store, isel doesn't know how to remap non-chain non-flag
7226 // uses of other nodes in the match, such as the ADD in this case. This
7227 // leads to the ADD being left around and reselected, with the result being
7228 // two adds in the output. Alas, even if none our users are stores, that
7229 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
7230 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
7231 // climbing the DAG back to the root, and it doesn't seem to be worth the
7232 // effort.
7233 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Dan Gohman076aee32009-03-04 19:44:21 +00007234 UE = Op.getNode()->use_end(); UI != UE; ++UI)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007235 if (UI->getOpcode() != ISD::CopyToReg && UI->getOpcode() != ISD::SETCC)
7236 goto default_case;
7237
7238 if (ConstantSDNode *C =
7239 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
7240 // An add of one will be selected as an INC.
7241 if (C->getAPIntValue() == 1) {
7242 Opcode = X86ISD::INC;
7243 NumOperands = 1;
7244 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00007245 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007246
7247 // An add of negative one (subtract of one) will be selected as a DEC.
7248 if (C->getAPIntValue().isAllOnesValue()) {
7249 Opcode = X86ISD::DEC;
7250 NumOperands = 1;
7251 break;
7252 }
Dan Gohman076aee32009-03-04 19:44:21 +00007253 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007254
7255 // Otherwise use a regular EFLAGS-setting add.
7256 Opcode = X86ISD::ADD;
7257 NumOperands = 2;
7258 break;
7259 case ISD::AND: {
7260 // If the primary and result isn't used, don't bother using X86ISD::AND,
7261 // because a TEST instruction will be better.
7262 bool NonFlagUse = false;
7263 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
7264 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
7265 SDNode *User = *UI;
7266 unsigned UOpNo = UI.getOperandNo();
7267 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
7268 // Look pass truncate.
7269 UOpNo = User->use_begin().getOperandNo();
7270 User = *User->use_begin();
7271 }
7272
7273 if (User->getOpcode() != ISD::BRCOND &&
7274 User->getOpcode() != ISD::SETCC &&
7275 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
7276 NonFlagUse = true;
7277 break;
7278 }
Dan Gohman076aee32009-03-04 19:44:21 +00007279 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007280
7281 if (!NonFlagUse)
7282 break;
7283 }
7284 // FALL THROUGH
7285 case ISD::SUB:
7286 case ISD::OR:
7287 case ISD::XOR:
7288 // Due to the ISEL shortcoming noted above, be conservative if this op is
7289 // likely to be selected as part of a load-modify-store instruction.
7290 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
7291 UE = Op.getNode()->use_end(); UI != UE; ++UI)
7292 if (UI->getOpcode() == ISD::STORE)
7293 goto default_case;
7294
7295 // Otherwise use a regular EFLAGS-setting instruction.
7296 switch (Op.getNode()->getOpcode()) {
7297 default: llvm_unreachable("unexpected operator!");
7298 case ISD::SUB: Opcode = X86ISD::SUB; break;
7299 case ISD::OR: Opcode = X86ISD::OR; break;
7300 case ISD::XOR: Opcode = X86ISD::XOR; break;
7301 case ISD::AND: Opcode = X86ISD::AND; break;
7302 }
7303
7304 NumOperands = 2;
7305 break;
7306 case X86ISD::ADD:
7307 case X86ISD::SUB:
7308 case X86ISD::INC:
7309 case X86ISD::DEC:
7310 case X86ISD::OR:
7311 case X86ISD::XOR:
7312 case X86ISD::AND:
7313 return SDValue(Op.getNode(), 1);
7314 default:
7315 default_case:
7316 break;
Dan Gohman076aee32009-03-04 19:44:21 +00007317 }
7318
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007319 if (Opcode == 0)
7320 // Emit a CMP with 0, which is the TEST pattern.
7321 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
7322 DAG.getConstant(0, Op.getValueType()));
7323
7324 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
7325 SmallVector<SDValue, 4> Ops;
7326 for (unsigned i = 0; i != NumOperands; ++i)
7327 Ops.push_back(Op.getOperand(i));
7328
7329 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
7330 DAG.ReplaceAllUsesWith(Op, New);
7331 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00007332}
7333
7334/// Emit nodes that will be selected as "cmp Op0,Op1", or something
7335/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00007336SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00007337 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00007338 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
7339 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00007340 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00007341
7342 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00007343 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00007344}
7345
Evan Chengd40d03e2010-01-06 19:38:29 +00007346/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
7347/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00007348SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
7349 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00007350 SDValue Op0 = And.getOperand(0);
7351 SDValue Op1 = And.getOperand(1);
7352 if (Op0.getOpcode() == ISD::TRUNCATE)
7353 Op0 = Op0.getOperand(0);
7354 if (Op1.getOpcode() == ISD::TRUNCATE)
7355 Op1 = Op1.getOperand(0);
7356
Evan Chengd40d03e2010-01-06 19:38:29 +00007357 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00007358 if (Op1.getOpcode() == ISD::SHL)
7359 std::swap(Op0, Op1);
7360 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00007361 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
7362 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00007363 // If we looked past a truncate, check that it's only truncating away
7364 // known zeros.
7365 unsigned BitWidth = Op0.getValueSizeInBits();
7366 unsigned AndBitWidth = And.getValueSizeInBits();
7367 if (BitWidth > AndBitWidth) {
7368 APInt Mask = APInt::getAllOnesValue(BitWidth), Zeros, Ones;
7369 DAG.ComputeMaskedBits(Op0, Mask, Zeros, Ones);
7370 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
7371 return SDValue();
7372 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007373 LHS = Op1;
7374 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00007375 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007376 } else if (Op1.getOpcode() == ISD::Constant) {
7377 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
7378 SDValue AndLHS = Op0;
Evan Chengd40d03e2010-01-06 19:38:29 +00007379 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
7380 LHS = AndLHS.getOperand(0);
7381 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00007382 }
Evan Chengd40d03e2010-01-06 19:38:29 +00007383 }
Evan Cheng0488db92007-09-25 01:57:46 +00007384
Evan Chengd40d03e2010-01-06 19:38:29 +00007385 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00007386 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00007387 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00007388 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00007389 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00007390 // Also promote i16 to i32 for performance / code size reason.
7391 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00007392 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00007393 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00007394
Evan Chengd40d03e2010-01-06 19:38:29 +00007395 // If the operand types disagree, extend the shift amount to match. Since
7396 // BT ignores high bits (like shifts) we can use anyextend.
7397 if (LHS.getValueType() != RHS.getValueType())
7398 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00007399
Evan Chengd40d03e2010-01-06 19:38:29 +00007400 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
7401 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
7402 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7403 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00007404 }
7405
Evan Cheng54de3ea2010-01-05 06:52:31 +00007406 return SDValue();
7407}
7408
Dan Gohmand858e902010-04-17 15:26:15 +00007409SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng54de3ea2010-01-05 06:52:31 +00007410 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
7411 SDValue Op0 = Op.getOperand(0);
7412 SDValue Op1 = Op.getOperand(1);
7413 DebugLoc dl = Op.getDebugLoc();
7414 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
7415
7416 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00007417 // Lower (X & (1 << N)) == 0 to BT(X, N).
7418 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
7419 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Chris Lattner481eebc2010-12-19 21:23:48 +00007420 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00007421 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00007422 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00007423 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
7424 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
7425 if (NewSetCC.getNode())
7426 return NewSetCC;
7427 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00007428
Chris Lattner481eebc2010-12-19 21:23:48 +00007429 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
7430 // these.
7431 if (Op1.getOpcode() == ISD::Constant &&
Evan Cheng2c755ba2010-02-27 07:36:59 +00007432 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
7433 cast<ConstantSDNode>(Op1)->isNullValue()) &&
7434 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007435
Chris Lattner481eebc2010-12-19 21:23:48 +00007436 // If the input is a setcc, then reuse the input setcc or use a new one with
7437 // the inverted condition.
7438 if (Op0.getOpcode() == X86ISD::SETCC) {
7439 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
7440 bool Invert = (CC == ISD::SETNE) ^
7441 cast<ConstantSDNode>(Op1)->isNullValue();
7442 if (!Invert) return Op0;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007443
Evan Cheng2c755ba2010-02-27 07:36:59 +00007444 CCode = X86::GetOppositeBranchCondition(CCode);
Chris Lattner481eebc2010-12-19 21:23:48 +00007445 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7446 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
7447 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007448 }
7449
Evan Chenge5b51ac2010-04-17 06:13:15 +00007450 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00007451 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00007452 if (X86CC == X86::COND_INVALID)
7453 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00007454
Chris Lattnerc19d1c32010-12-19 22:08:31 +00007455 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00007456 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnerc19d1c32010-12-19 22:08:31 +00007457 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
Evan Cheng0488db92007-09-25 01:57:46 +00007458}
7459
Dan Gohmand858e902010-04-17 15:26:15 +00007460SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00007461 SDValue Cond;
7462 SDValue Op0 = Op.getOperand(0);
7463 SDValue Op1 = Op.getOperand(1);
7464 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00007465 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00007466 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
7467 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007468 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00007469
7470 if (isFP) {
7471 unsigned SSECC = 8;
Owen Andersone50ed302009-08-10 22:56:29 +00007472 EVT VT0 = Op0.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00007473 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
7474 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00007475 bool Swap = false;
7476
7477 switch (SetCCOpcode) {
7478 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00007479 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00007480 case ISD::SETEQ: SSECC = 0; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00007481 case ISD::SETOGT:
Nate Begeman30a0de92008-07-17 16:51:19 +00007482 case ISD::SETGT: Swap = true; // Fallthrough
7483 case ISD::SETLT:
7484 case ISD::SETOLT: SSECC = 1; break;
7485 case ISD::SETOGE:
7486 case ISD::SETGE: Swap = true; // Fallthrough
7487 case ISD::SETLE:
7488 case ISD::SETOLE: SSECC = 2; break;
7489 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00007490 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00007491 case ISD::SETNE: SSECC = 4; break;
7492 case ISD::SETULE: Swap = true;
7493 case ISD::SETUGE: SSECC = 5; break;
7494 case ISD::SETULT: Swap = true;
7495 case ISD::SETUGT: SSECC = 6; break;
7496 case ISD::SETO: SSECC = 7; break;
7497 }
7498 if (Swap)
7499 std::swap(Op0, Op1);
7500
Nate Begemanfb8ead02008-07-25 19:05:58 +00007501 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00007502 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00007503 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00007504 SDValue UNORD, EQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00007505 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
7506 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00007507 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00007508 }
7509 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00007510 SDValue ORD, NEQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00007511 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
7512 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00007513 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00007514 }
Torok Edwinc23197a2009-07-14 16:55:14 +00007515 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00007516 }
7517 // Handle all other FP comparisons here.
Owen Anderson825b72b2009-08-11 20:47:22 +00007518 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00007519 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007520
Nate Begeman30a0de92008-07-17 16:51:19 +00007521 // We are handling one of the integer comparisons here. Since SSE only has
7522 // GT and EQ comparisons for integer, swapping operands and multiple
7523 // operations may be required for some comparisons.
7524 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
7525 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007526
Owen Anderson825b72b2009-08-11 20:47:22 +00007527 switch (VT.getSimpleVT().SimpleTy) {
Nate Begeman30a0de92008-07-17 16:51:19 +00007528 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007529 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007530 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007531 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
7532 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00007533 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007534
Nate Begeman30a0de92008-07-17 16:51:19 +00007535 switch (SetCCOpcode) {
7536 default: break;
7537 case ISD::SETNE: Invert = true;
7538 case ISD::SETEQ: Opc = EQOpc; break;
7539 case ISD::SETLT: Swap = true;
7540 case ISD::SETGT: Opc = GTOpc; break;
7541 case ISD::SETGE: Swap = true;
7542 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
7543 case ISD::SETULT: Swap = true;
7544 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
7545 case ISD::SETUGE: Swap = true;
7546 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
7547 }
7548 if (Swap)
7549 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007550
Nate Begeman30a0de92008-07-17 16:51:19 +00007551 // Since SSE has no unsigned integer comparisons, we need to flip the sign
7552 // bits of the inputs before performing those operations.
7553 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00007554 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00007555 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
7556 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00007557 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00007558 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
7559 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00007560 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
7561 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00007562 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007563
Dale Johannesenace16102009-02-03 19:33:06 +00007564 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00007565
7566 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00007567 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00007568 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00007569
Nate Begeman30a0de92008-07-17 16:51:19 +00007570 return Result;
7571}
Evan Cheng0488db92007-09-25 01:57:46 +00007572
Evan Cheng370e5342008-12-03 08:38:43 +00007573// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00007574static bool isX86LogicalCmp(SDValue Op) {
7575 unsigned Opc = Op.getNode()->getOpcode();
7576 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
7577 return true;
7578 if (Op.getResNo() == 1 &&
7579 (Opc == X86ISD::ADD ||
7580 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00007581 Opc == X86ISD::ADC ||
7582 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00007583 Opc == X86ISD::SMUL ||
7584 Opc == X86ISD::UMUL ||
7585 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00007586 Opc == X86ISD::DEC ||
7587 Opc == X86ISD::OR ||
7588 Opc == X86ISD::XOR ||
7589 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00007590 return true;
7591
Chris Lattner9637d5b2010-12-05 07:49:54 +00007592 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
7593 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007594
Dan Gohman076aee32009-03-04 19:44:21 +00007595 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00007596}
7597
Chris Lattnera2b56002010-12-05 01:23:24 +00007598static bool isZero(SDValue V) {
7599 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
7600 return C && C->isNullValue();
7601}
7602
Chris Lattner96908b12010-12-05 02:00:51 +00007603static bool isAllOnes(SDValue V) {
7604 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
7605 return C && C->isAllOnesValue();
7606}
7607
Dan Gohmand858e902010-04-17 15:26:15 +00007608SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00007609 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007610 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00007611 SDValue Op1 = Op.getOperand(1);
7612 SDValue Op2 = Op.getOperand(2);
7613 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00007614 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00007615
Dan Gohman1a492952009-10-20 16:22:37 +00007616 if (Cond.getOpcode() == ISD::SETCC) {
7617 SDValue NewCond = LowerSETCC(Cond, DAG);
7618 if (NewCond.getNode())
7619 Cond = NewCond;
7620 }
Evan Cheng734503b2006-09-11 02:19:56 +00007621
Chris Lattnera2b56002010-12-05 01:23:24 +00007622 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00007623 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00007624 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00007625 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007626 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00007627 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
7628 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007629 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007630
Chris Lattnera2b56002010-12-05 01:23:24 +00007631 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007632
7633 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00007634 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
7635 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00007636
7637 SDValue CmpOp0 = Cmp.getOperand(0);
7638 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
7639 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007640
Chris Lattner96908b12010-12-05 02:00:51 +00007641 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00007642 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
7643 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007644
Chris Lattner96908b12010-12-05 02:00:51 +00007645 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
7646 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007647
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007648 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00007649 if (N2C == 0 || !N2C->isNullValue())
7650 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
7651 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007652 }
7653 }
7654
Chris Lattnera2b56002010-12-05 01:23:24 +00007655 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00007656 if (Cond.getOpcode() == ISD::AND &&
7657 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
7658 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00007659 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00007660 Cond = Cond.getOperand(0);
7661 }
7662
Evan Cheng3f41d662007-10-08 22:16:29 +00007663 // If condition flag is set by a X86ISD::CMP, then use it as the condition
7664 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00007665 if (Cond.getOpcode() == X86ISD::SETCC ||
7666 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00007667 CC = Cond.getOperand(0);
7668
Dan Gohman475871a2008-07-27 21:46:04 +00007669 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00007670 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00007671 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00007672
Evan Cheng3f41d662007-10-08 22:16:29 +00007673 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007674 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00007675 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00007676 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00007677
Chris Lattnerd1980a52009-03-12 06:52:53 +00007678 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
7679 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00007680 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00007681 addTest = false;
7682 }
7683 }
7684
7685 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007686 // Look pass the truncate.
7687 if (Cond.getOpcode() == ISD::TRUNCATE)
7688 Cond = Cond.getOperand(0);
7689
7690 // We know the result of AND is compared against zero. Try to match
7691 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00007692 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00007693 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00007694 if (NewSetCC.getNode()) {
7695 CC = NewSetCC.getOperand(0);
7696 Cond = NewSetCC.getOperand(1);
7697 addTest = false;
7698 }
7699 }
7700 }
7701
7702 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007703 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00007704 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00007705 }
7706
Benjamin Kramere915ff32010-12-22 23:09:28 +00007707 // a < b ? -1 : 0 -> RES = ~setcc_carry
7708 // a < b ? 0 : -1 -> RES = setcc_carry
7709 // a >= b ? -1 : 0 -> RES = setcc_carry
7710 // a >= b ? 0 : -1 -> RES = ~setcc_carry
7711 if (Cond.getOpcode() == X86ISD::CMP) {
7712 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
7713
7714 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
7715 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
7716 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
7717 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
7718 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
7719 return DAG.getNOT(DL, Res, Res.getValueType());
7720 return Res;
7721 }
7722 }
7723
Evan Cheng0488db92007-09-25 01:57:46 +00007724 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
7725 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007726 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007727 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00007728 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00007729}
7730
Evan Cheng370e5342008-12-03 08:38:43 +00007731// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
7732// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
7733// from the AND / OR.
7734static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
7735 Opc = Op.getOpcode();
7736 if (Opc != ISD::OR && Opc != ISD::AND)
7737 return false;
7738 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
7739 Op.getOperand(0).hasOneUse() &&
7740 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
7741 Op.getOperand(1).hasOneUse());
7742}
7743
Evan Cheng961d6d42009-02-02 08:19:07 +00007744// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
7745// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00007746static bool isXor1OfSetCC(SDValue Op) {
7747 if (Op.getOpcode() != ISD::XOR)
7748 return false;
7749 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
7750 if (N1C && N1C->getAPIntValue() == 1) {
7751 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
7752 Op.getOperand(0).hasOneUse();
7753 }
7754 return false;
7755}
7756
Dan Gohmand858e902010-04-17 15:26:15 +00007757SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00007758 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007759 SDValue Chain = Op.getOperand(0);
7760 SDValue Cond = Op.getOperand(1);
7761 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007762 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00007763 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00007764
Dan Gohman1a492952009-10-20 16:22:37 +00007765 if (Cond.getOpcode() == ISD::SETCC) {
7766 SDValue NewCond = LowerSETCC(Cond, DAG);
7767 if (NewCond.getNode())
7768 Cond = NewCond;
7769 }
Chris Lattnere55484e2008-12-25 05:34:37 +00007770#if 0
7771 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00007772 else if (Cond.getOpcode() == X86ISD::ADD ||
7773 Cond.getOpcode() == X86ISD::SUB ||
7774 Cond.getOpcode() == X86ISD::SMUL ||
7775 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00007776 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00007777#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00007778
Evan Chengad9c0a32009-12-15 00:53:42 +00007779 // Look pass (and (setcc_carry (cmp ...)), 1).
7780 if (Cond.getOpcode() == ISD::AND &&
7781 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
7782 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00007783 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00007784 Cond = Cond.getOperand(0);
7785 }
7786
Evan Cheng3f41d662007-10-08 22:16:29 +00007787 // If condition flag is set by a X86ISD::CMP, then use it as the condition
7788 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00007789 if (Cond.getOpcode() == X86ISD::SETCC ||
7790 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00007791 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007792
Dan Gohman475871a2008-07-27 21:46:04 +00007793 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00007794 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00007795 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00007796 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00007797 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00007798 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00007799 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00007800 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00007801 default: break;
7802 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00007803 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00007804 // These can only come from an arithmetic instruction with overflow,
7805 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00007806 Cond = Cond.getNode()->getOperand(1);
7807 addTest = false;
7808 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00007809 }
Evan Cheng0488db92007-09-25 01:57:46 +00007810 }
Evan Cheng370e5342008-12-03 08:38:43 +00007811 } else {
7812 unsigned CondOpc;
7813 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
7814 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00007815 if (CondOpc == ISD::OR) {
7816 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
7817 // two branches instead of an explicit OR instruction with a
7818 // separate test.
7819 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00007820 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00007821 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007822 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00007823 Chain, Dest, CC, Cmp);
7824 CC = Cond.getOperand(1).getOperand(0);
7825 Cond = Cmp;
7826 addTest = false;
7827 }
7828 } else { // ISD::AND
7829 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
7830 // two branches instead of an explicit AND instruction with a
7831 // separate test. However, we only do this if this block doesn't
7832 // have a fall-through edge, because this requires an explicit
7833 // jmp when the condition is false.
7834 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00007835 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00007836 Op.getNode()->hasOneUse()) {
7837 X86::CondCode CCode =
7838 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
7839 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007840 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00007841 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00007842 // Look for an unconditional branch following this conditional branch.
7843 // We need this because we need to reverse the successors in order
7844 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00007845 if (User->getOpcode() == ISD::BR) {
7846 SDValue FalseBB = User->getOperand(1);
7847 SDNode *NewBR =
7848 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00007849 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00007850 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00007851 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00007852
Dale Johannesene4d209d2009-02-03 20:21:25 +00007853 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00007854 Chain, Dest, CC, Cmp);
7855 X86::CondCode CCode =
7856 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
7857 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007858 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00007859 Cond = Cmp;
7860 addTest = false;
7861 }
7862 }
Dan Gohman279c22e2008-10-21 03:29:32 +00007863 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00007864 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
7865 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
7866 // It should be transformed during dag combiner except when the condition
7867 // is set by a arithmetics with overflow node.
7868 X86::CondCode CCode =
7869 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
7870 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007871 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00007872 Cond = Cond.getOperand(0).getOperand(1);
7873 addTest = false;
Dan Gohman279c22e2008-10-21 03:29:32 +00007874 }
Evan Cheng0488db92007-09-25 01:57:46 +00007875 }
7876
7877 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007878 // Look pass the truncate.
7879 if (Cond.getOpcode() == ISD::TRUNCATE)
7880 Cond = Cond.getOperand(0);
7881
7882 // We know the result of AND is compared against zero. Try to match
7883 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00007884 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007885 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
7886 if (NewSetCC.getNode()) {
7887 CC = NewSetCC.getOperand(0);
7888 Cond = NewSetCC.getOperand(1);
7889 addTest = false;
7890 }
7891 }
7892 }
7893
7894 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007895 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00007896 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00007897 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00007898 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00007899 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00007900}
7901
Anton Korobeynikove060b532007-04-17 19:34:00 +00007902
7903// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
7904// Calls to _alloca is needed to probe the stack when allocating more than 4k
7905// bytes in one go. Touching the stack at 4K increments is necessary to ensure
7906// that the guard pages used by the OS virtual memory manager are allocated in
7907// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00007908SDValue
7909X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00007910 SelectionDAG &DAG) const {
Duncan Sands1e1ca0b2010-10-21 16:02:12 +00007911 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows()) &&
Michael J. Spencere9c253e2010-10-21 01:41:01 +00007912 "This should be used only on Windows targets");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007913 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007914
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007915 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00007916 SDValue Chain = Op.getOperand(0);
7917 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007918 // FIXME: Ensure alignment here
7919
Dan Gohman475871a2008-07-27 21:46:04 +00007920 SDValue Flag;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007921
Owen Anderson825b72b2009-08-11 20:47:22 +00007922 EVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007923
Dale Johannesendd64c412009-02-04 00:33:20 +00007924 Chain = DAG.getCopyToReg(Chain, dl, X86::EAX, Size, Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00007925 Flag = Chain.getValue(1);
7926
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007927 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00007928
Michael J. Spencere9c253e2010-10-21 01:41:01 +00007929 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00007930 Flag = Chain.getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007931
Dale Johannesendd64c412009-02-04 00:33:20 +00007932 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007933
Dan Gohman475871a2008-07-27 21:46:04 +00007934 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007935 return DAG.getMergeValues(Ops1, 2, dl);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007936}
7937
Dan Gohmand858e902010-04-17 15:26:15 +00007938SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00007939 MachineFunction &MF = DAG.getMachineFunction();
7940 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
7941
Dan Gohman69de1932008-02-06 22:27:42 +00007942 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00007943 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00007944
Anton Korobeynikove7beda12010-10-03 22:52:07 +00007945 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00007946 // vastart just stores the address of the VarArgsFrameIndex slot into the
7947 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00007948 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
7949 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00007950 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
7951 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007952 }
7953
7954 // __va_list_tag:
7955 // gp_offset (0 - 6 * 8)
7956 // fp_offset (48 - 48 + 8 * 16)
7957 // overflow_arg_area (point to parameters coming in memory).
7958 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00007959 SmallVector<SDValue, 8> MemOps;
7960 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00007961 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00007962 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00007963 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
7964 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007965 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007966 MemOps.push_back(Store);
7967
7968 // Store fp_offset
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));
Chris Lattner8026a9d2010-09-21 17:50:43 +00007971 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00007972 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
7973 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007974 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007975 MemOps.push_back(Store);
7976
7977 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +00007978 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007979 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00007980 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
7981 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00007982 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
7983 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +00007984 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007985 MemOps.push_back(Store);
7986
7987 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +00007988 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007989 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00007990 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
7991 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00007992 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
7993 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007994 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +00007995 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00007996 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00007997}
7998
Dan Gohmand858e902010-04-17 15:26:15 +00007999SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +00008000 assert(Subtarget->is64Bit() &&
8001 "LowerVAARG only handles 64-bit va_arg!");
8002 assert((Subtarget->isTargetLinux() ||
8003 Subtarget->isTargetDarwin()) &&
8004 "Unhandled target in LowerVAARG");
8005 assert(Op.getNode()->getNumOperands() == 4);
8006 SDValue Chain = Op.getOperand(0);
8007 SDValue SrcPtr = Op.getOperand(1);
8008 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
8009 unsigned Align = Op.getConstantOperandVal(3);
8010 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +00008011
Dan Gohman320afb82010-10-12 18:00:49 +00008012 EVT ArgVT = Op.getNode()->getValueType(0);
8013 const Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
8014 uint32_t ArgSize = getTargetData()->getTypeAllocSize(ArgTy);
8015 uint8_t ArgMode;
8016
8017 // Decide which area this value should be read from.
8018 // TODO: Implement the AMD64 ABI in its entirety. This simple
8019 // selection mechanism works only for the basic types.
8020 if (ArgVT == MVT::f80) {
8021 llvm_unreachable("va_arg for f80 not yet implemented");
8022 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
8023 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
8024 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
8025 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
8026 } else {
8027 llvm_unreachable("Unhandled argument type in LowerVAARG");
8028 }
8029
8030 if (ArgMode == 2) {
8031 // Sanity Check: Make sure using fp_offset makes sense.
Michael J. Spencer87b86652010-10-19 07:32:42 +00008032 assert(!UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +00008033 !(DAG.getMachineFunction()
8034 .getFunction()->hasFnAttr(Attribute::NoImplicitFloat)) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00008035 Subtarget->hasXMM());
Dan Gohman320afb82010-10-12 18:00:49 +00008036 }
8037
8038 // Insert VAARG_64 node into the DAG
8039 // VAARG_64 returns two values: Variable Argument Address, Chain
8040 SmallVector<SDValue, 11> InstOps;
8041 InstOps.push_back(Chain);
8042 InstOps.push_back(SrcPtr);
8043 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
8044 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
8045 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
8046 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
8047 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
8048 VTs, &InstOps[0], InstOps.size(),
8049 MVT::i64,
8050 MachinePointerInfo(SV),
8051 /*Align=*/0,
8052 /*Volatile=*/false,
8053 /*ReadMem=*/true,
8054 /*WriteMem=*/true);
8055 Chain = VAARG.getValue(1);
8056
8057 // Load the next argument and return it
8058 return DAG.getLoad(ArgVT, dl,
8059 Chain,
8060 VAARG,
8061 MachinePointerInfo(),
8062 false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +00008063}
8064
Dan Gohmand858e902010-04-17 15:26:15 +00008065SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) const {
Evan Chengae642192007-03-02 23:16:35 +00008066 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00008067 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00008068 SDValue Chain = Op.getOperand(0);
8069 SDValue DstPtr = Op.getOperand(1);
8070 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00008071 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
8072 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +00008073 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00008074
Chris Lattnere72f2022010-09-21 05:40:29 +00008075 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00008076 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +00008077 false,
Chris Lattnere72f2022010-09-21 05:40:29 +00008078 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +00008079}
8080
Dan Gohman475871a2008-07-27 21:46:04 +00008081SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00008082X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008083 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00008084 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00008085 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00008086 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00008087 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00008088 case Intrinsic::x86_sse_comieq_ss:
8089 case Intrinsic::x86_sse_comilt_ss:
8090 case Intrinsic::x86_sse_comile_ss:
8091 case Intrinsic::x86_sse_comigt_ss:
8092 case Intrinsic::x86_sse_comige_ss:
8093 case Intrinsic::x86_sse_comineq_ss:
8094 case Intrinsic::x86_sse_ucomieq_ss:
8095 case Intrinsic::x86_sse_ucomilt_ss:
8096 case Intrinsic::x86_sse_ucomile_ss:
8097 case Intrinsic::x86_sse_ucomigt_ss:
8098 case Intrinsic::x86_sse_ucomige_ss:
8099 case Intrinsic::x86_sse_ucomineq_ss:
8100 case Intrinsic::x86_sse2_comieq_sd:
8101 case Intrinsic::x86_sse2_comilt_sd:
8102 case Intrinsic::x86_sse2_comile_sd:
8103 case Intrinsic::x86_sse2_comigt_sd:
8104 case Intrinsic::x86_sse2_comige_sd:
8105 case Intrinsic::x86_sse2_comineq_sd:
8106 case Intrinsic::x86_sse2_ucomieq_sd:
8107 case Intrinsic::x86_sse2_ucomilt_sd:
8108 case Intrinsic::x86_sse2_ucomile_sd:
8109 case Intrinsic::x86_sse2_ucomigt_sd:
8110 case Intrinsic::x86_sse2_ucomige_sd:
8111 case Intrinsic::x86_sse2_ucomineq_sd: {
8112 unsigned Opc = 0;
8113 ISD::CondCode CC = ISD::SETCC_INVALID;
8114 switch (IntNo) {
8115 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008116 case Intrinsic::x86_sse_comieq_ss:
8117 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008118 Opc = X86ISD::COMI;
8119 CC = ISD::SETEQ;
8120 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00008121 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008122 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008123 Opc = X86ISD::COMI;
8124 CC = ISD::SETLT;
8125 break;
8126 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008127 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008128 Opc = X86ISD::COMI;
8129 CC = ISD::SETLE;
8130 break;
8131 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008132 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008133 Opc = X86ISD::COMI;
8134 CC = ISD::SETGT;
8135 break;
8136 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008137 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008138 Opc = X86ISD::COMI;
8139 CC = ISD::SETGE;
8140 break;
8141 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008142 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008143 Opc = X86ISD::COMI;
8144 CC = ISD::SETNE;
8145 break;
8146 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008147 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008148 Opc = X86ISD::UCOMI;
8149 CC = ISD::SETEQ;
8150 break;
8151 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008152 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008153 Opc = X86ISD::UCOMI;
8154 CC = ISD::SETLT;
8155 break;
8156 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008157 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008158 Opc = X86ISD::UCOMI;
8159 CC = ISD::SETLE;
8160 break;
8161 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008162 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008163 Opc = X86ISD::UCOMI;
8164 CC = ISD::SETGT;
8165 break;
8166 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008167 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008168 Opc = X86ISD::UCOMI;
8169 CC = ISD::SETGE;
8170 break;
8171 case Intrinsic::x86_sse_ucomineq_ss:
8172 case Intrinsic::x86_sse2_ucomineq_sd:
8173 Opc = X86ISD::UCOMI;
8174 CC = ISD::SETNE;
8175 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00008176 }
Evan Cheng734503b2006-09-11 02:19:56 +00008177
Dan Gohman475871a2008-07-27 21:46:04 +00008178 SDValue LHS = Op.getOperand(1);
8179 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00008180 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00008181 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00008182 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
8183 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8184 DAG.getConstant(X86CC, MVT::i8), Cond);
8185 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00008186 }
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008187 // ptest and testp intrinsics. The intrinsic these come from are designed to
8188 // return an integer value, not just an instruction so lower it to the ptest
8189 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00008190 case Intrinsic::x86_sse41_ptestz:
8191 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008192 case Intrinsic::x86_sse41_ptestnzc:
8193 case Intrinsic::x86_avx_ptestz_256:
8194 case Intrinsic::x86_avx_ptestc_256:
8195 case Intrinsic::x86_avx_ptestnzc_256:
8196 case Intrinsic::x86_avx_vtestz_ps:
8197 case Intrinsic::x86_avx_vtestc_ps:
8198 case Intrinsic::x86_avx_vtestnzc_ps:
8199 case Intrinsic::x86_avx_vtestz_pd:
8200 case Intrinsic::x86_avx_vtestc_pd:
8201 case Intrinsic::x86_avx_vtestnzc_pd:
8202 case Intrinsic::x86_avx_vtestz_ps_256:
8203 case Intrinsic::x86_avx_vtestc_ps_256:
8204 case Intrinsic::x86_avx_vtestnzc_ps_256:
8205 case Intrinsic::x86_avx_vtestz_pd_256:
8206 case Intrinsic::x86_avx_vtestc_pd_256:
8207 case Intrinsic::x86_avx_vtestnzc_pd_256: {
8208 bool IsTestPacked = false;
Eric Christopher71c67532009-07-29 00:28:05 +00008209 unsigned X86CC = 0;
8210 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00008211 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008212 case Intrinsic::x86_avx_vtestz_ps:
8213 case Intrinsic::x86_avx_vtestz_pd:
8214 case Intrinsic::x86_avx_vtestz_ps_256:
8215 case Intrinsic::x86_avx_vtestz_pd_256:
8216 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00008217 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008218 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +00008219 // ZF = 1
8220 X86CC = X86::COND_E;
8221 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008222 case Intrinsic::x86_avx_vtestc_ps:
8223 case Intrinsic::x86_avx_vtestc_pd:
8224 case Intrinsic::x86_avx_vtestc_ps_256:
8225 case Intrinsic::x86_avx_vtestc_pd_256:
8226 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00008227 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008228 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00008229 // CF = 1
8230 X86CC = X86::COND_B;
8231 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008232 case Intrinsic::x86_avx_vtestnzc_ps:
8233 case Intrinsic::x86_avx_vtestnzc_pd:
8234 case Intrinsic::x86_avx_vtestnzc_ps_256:
8235 case Intrinsic::x86_avx_vtestnzc_pd_256:
8236 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +00008237 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008238 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00008239 // ZF and CF = 0
8240 X86CC = X86::COND_A;
8241 break;
8242 }
Eric Christopherfd179292009-08-27 18:07:15 +00008243
Eric Christopher71c67532009-07-29 00:28:05 +00008244 SDValue LHS = Op.getOperand(1);
8245 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008246 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
8247 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00008248 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
8249 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
8250 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00008251 }
Evan Cheng5759f972008-05-04 09:15:50 +00008252
8253 // Fix vector shift instructions where the last operand is a non-immediate
8254 // i32 value.
8255 case Intrinsic::x86_sse2_pslli_w:
8256 case Intrinsic::x86_sse2_pslli_d:
8257 case Intrinsic::x86_sse2_pslli_q:
8258 case Intrinsic::x86_sse2_psrli_w:
8259 case Intrinsic::x86_sse2_psrli_d:
8260 case Intrinsic::x86_sse2_psrli_q:
8261 case Intrinsic::x86_sse2_psrai_w:
8262 case Intrinsic::x86_sse2_psrai_d:
8263 case Intrinsic::x86_mmx_pslli_w:
8264 case Intrinsic::x86_mmx_pslli_d:
8265 case Intrinsic::x86_mmx_pslli_q:
8266 case Intrinsic::x86_mmx_psrli_w:
8267 case Intrinsic::x86_mmx_psrli_d:
8268 case Intrinsic::x86_mmx_psrli_q:
8269 case Intrinsic::x86_mmx_psrai_w:
8270 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00008271 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00008272 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00008273 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00008274
8275 unsigned NewIntNo = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00008276 EVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00008277 switch (IntNo) {
8278 case Intrinsic::x86_sse2_pslli_w:
8279 NewIntNo = Intrinsic::x86_sse2_psll_w;
8280 break;
8281 case Intrinsic::x86_sse2_pslli_d:
8282 NewIntNo = Intrinsic::x86_sse2_psll_d;
8283 break;
8284 case Intrinsic::x86_sse2_pslli_q:
8285 NewIntNo = Intrinsic::x86_sse2_psll_q;
8286 break;
8287 case Intrinsic::x86_sse2_psrli_w:
8288 NewIntNo = Intrinsic::x86_sse2_psrl_w;
8289 break;
8290 case Intrinsic::x86_sse2_psrli_d:
8291 NewIntNo = Intrinsic::x86_sse2_psrl_d;
8292 break;
8293 case Intrinsic::x86_sse2_psrli_q:
8294 NewIntNo = Intrinsic::x86_sse2_psrl_q;
8295 break;
8296 case Intrinsic::x86_sse2_psrai_w:
8297 NewIntNo = Intrinsic::x86_sse2_psra_w;
8298 break;
8299 case Intrinsic::x86_sse2_psrai_d:
8300 NewIntNo = Intrinsic::x86_sse2_psra_d;
8301 break;
8302 default: {
Owen Anderson825b72b2009-08-11 20:47:22 +00008303 ShAmtVT = MVT::v2i32;
Evan Cheng5759f972008-05-04 09:15:50 +00008304 switch (IntNo) {
8305 case Intrinsic::x86_mmx_pslli_w:
8306 NewIntNo = Intrinsic::x86_mmx_psll_w;
8307 break;
8308 case Intrinsic::x86_mmx_pslli_d:
8309 NewIntNo = Intrinsic::x86_mmx_psll_d;
8310 break;
8311 case Intrinsic::x86_mmx_pslli_q:
8312 NewIntNo = Intrinsic::x86_mmx_psll_q;
8313 break;
8314 case Intrinsic::x86_mmx_psrli_w:
8315 NewIntNo = Intrinsic::x86_mmx_psrl_w;
8316 break;
8317 case Intrinsic::x86_mmx_psrli_d:
8318 NewIntNo = Intrinsic::x86_mmx_psrl_d;
8319 break;
8320 case Intrinsic::x86_mmx_psrli_q:
8321 NewIntNo = Intrinsic::x86_mmx_psrl_q;
8322 break;
8323 case Intrinsic::x86_mmx_psrai_w:
8324 NewIntNo = Intrinsic::x86_mmx_psra_w;
8325 break;
8326 case Intrinsic::x86_mmx_psrai_d:
8327 NewIntNo = Intrinsic::x86_mmx_psra_d;
8328 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00008329 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00008330 }
8331 break;
8332 }
8333 }
Mon P Wangefa42202009-09-03 19:56:25 +00008334
8335 // The vector shift intrinsics with scalars uses 32b shift amounts but
8336 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
8337 // to be zero.
8338 SDValue ShOps[4];
8339 ShOps[0] = ShAmt;
8340 ShOps[1] = DAG.getConstant(0, MVT::i32);
8341 if (ShAmtVT == MVT::v4i32) {
8342 ShOps[2] = DAG.getUNDEF(MVT::i32);
8343 ShOps[3] = DAG.getUNDEF(MVT::i32);
8344 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
8345 } else {
8346 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
Dale Johannesen0488fb62010-09-30 23:57:10 +00008347// FIXME this must be lowered to get rid of the invalid type.
Mon P Wangefa42202009-09-03 19:56:25 +00008348 }
8349
Owen Andersone50ed302009-08-10 22:56:29 +00008350 EVT VT = Op.getValueType();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008351 ShAmt = DAG.getNode(ISD::BITCAST, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008352 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008353 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00008354 Op.getOperand(1), ShAmt);
8355 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00008356 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008357}
Evan Cheng72261582005-12-20 06:22:03 +00008358
Dan Gohmand858e902010-04-17 15:26:15 +00008359SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
8360 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +00008361 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
8362 MFI->setReturnAddressIsTaken(true);
8363
Bill Wendling64e87322009-01-16 19:25:27 +00008364 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008365 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00008366
8367 if (Depth > 0) {
8368 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
8369 SDValue Offset =
8370 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00008371 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008372 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00008373 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008374 FrameAddr, Offset),
Chris Lattner51abfe42010-09-21 06:02:19 +00008375 MachinePointerInfo(), false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +00008376 }
8377
8378 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00008379 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00008380 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Chris Lattner51abfe42010-09-21 06:02:19 +00008381 RetAddrFI, MachinePointerInfo(), false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00008382}
8383
Dan Gohmand858e902010-04-17 15:26:15 +00008384SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +00008385 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
8386 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00008387
Owen Andersone50ed302009-08-10 22:56:29 +00008388 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008389 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00008390 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
8391 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00008392 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00008393 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +00008394 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
8395 MachinePointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +00008396 false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00008397 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00008398}
8399
Dan Gohman475871a2008-07-27 21:46:04 +00008400SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008401 SelectionDAG &DAG) const {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00008402 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008403}
8404
Dan Gohmand858e902010-04-17 15:26:15 +00008405SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008406 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00008407 SDValue Chain = Op.getOperand(0);
8408 SDValue Offset = Op.getOperand(1);
8409 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008410 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008411
Dan Gohmand8816272010-08-11 18:14:00 +00008412 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
8413 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
8414 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008415 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008416
Dan Gohmand8816272010-08-11 18:14:00 +00008417 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
8418 DAG.getIntPtrConstant(TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008419 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +00008420 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
8421 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00008422 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008423 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008424
Dale Johannesene4d209d2009-02-03 20:21:25 +00008425 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008426 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008427 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008428}
8429
Dan Gohman475871a2008-07-27 21:46:04 +00008430SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008431 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008432 SDValue Root = Op.getOperand(0);
8433 SDValue Trmp = Op.getOperand(1); // trampoline
8434 SDValue FPtr = Op.getOperand(2); // nested function
8435 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008436 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008437
Dan Gohman69de1932008-02-06 22:27:42 +00008438 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008439
8440 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00008441 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00008442
8443 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +00008444 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
8445 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +00008446
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00008447 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
8448 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00008449
8450 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
8451
8452 // Load the pointer to the nested function into R11.
8453 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00008454 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00008455 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008456 Addr, MachinePointerInfo(TrmpAddr),
8457 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008458
Owen Anderson825b72b2009-08-11 20:47:22 +00008459 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8460 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008461 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
8462 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +00008463 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00008464
8465 // Load the 'nest' parameter value into R10.
8466 // R10 is specified in X86CallingConv.td
8467 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +00008468 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8469 DAG.getConstant(10, MVT::i64));
8470 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008471 Addr, MachinePointerInfo(TrmpAddr, 10),
8472 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008473
Owen Anderson825b72b2009-08-11 20:47:22 +00008474 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8475 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008476 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
8477 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +00008478 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00008479
8480 // Jump to the nested function.
8481 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +00008482 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8483 DAG.getConstant(20, MVT::i64));
8484 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008485 Addr, MachinePointerInfo(TrmpAddr, 20),
8486 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008487
8488 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +00008489 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8490 DAG.getConstant(22, MVT::i64));
8491 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00008492 MachinePointerInfo(TrmpAddr, 22),
8493 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008494
Dan Gohman475871a2008-07-27 21:46:04 +00008495 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00008496 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008497 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008498 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00008499 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00008500 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00008501 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00008502 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008503
8504 switch (CC) {
8505 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00008506 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00008507 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00008508 case CallingConv::X86_StdCall: {
8509 // Pass 'nest' parameter in ECX.
8510 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00008511 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008512
8513 // Check that ECX wasn't needed by an 'inreg' parameter.
8514 const FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00008515 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008516
Chris Lattner58d74912008-03-12 17:45:29 +00008517 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00008518 unsigned InRegCount = 0;
8519 unsigned Idx = 1;
8520
8521 for (FunctionType::param_iterator I = FTy->param_begin(),
8522 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00008523 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00008524 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00008525 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008526
8527 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +00008528 report_fatal_error("Nest register in use - reduce number of inreg"
8529 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00008530 }
8531 }
8532 break;
8533 }
8534 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +00008535 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00008536 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00008537 // Pass 'nest' parameter in EAX.
8538 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00008539 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008540 break;
8541 }
8542
Dan Gohman475871a2008-07-27 21:46:04 +00008543 SDValue OutChains[4];
8544 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008545
Owen Anderson825b72b2009-08-11 20:47:22 +00008546 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8547 DAG.getConstant(10, MVT::i32));
8548 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008549
Chris Lattnera62fe662010-02-05 19:20:30 +00008550 // This is storing the opcode for MOV32ri.
8551 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00008552 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00008553 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008554 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008555 Trmp, MachinePointerInfo(TrmpAddr),
8556 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008557
Owen Anderson825b72b2009-08-11 20:47:22 +00008558 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8559 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008560 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
8561 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +00008562 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008563
Chris Lattnera62fe662010-02-05 19:20:30 +00008564 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +00008565 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8566 DAG.getConstant(5, MVT::i32));
8567 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00008568 MachinePointerInfo(TrmpAddr, 5),
8569 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008570
Owen Anderson825b72b2009-08-11 20:47:22 +00008571 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8572 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008573 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
8574 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +00008575 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008576
Dan Gohman475871a2008-07-27 21:46:04 +00008577 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00008578 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008579 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008580 }
8581}
8582
Dan Gohmand858e902010-04-17 15:26:15 +00008583SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
8584 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008585 /*
8586 The rounding mode is in bits 11:10 of FPSR, and has the following
8587 settings:
8588 00 Round to nearest
8589 01 Round to -inf
8590 10 Round to +inf
8591 11 Round to 0
8592
8593 FLT_ROUNDS, on the other hand, expects the following:
8594 -1 Undefined
8595 0 Round to 0
8596 1 Round to nearest
8597 2 Round to +inf
8598 3 Round to -inf
8599
8600 To perform the conversion, we do:
8601 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
8602 */
8603
8604 MachineFunction &MF = DAG.getMachineFunction();
8605 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00008606 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008607 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00008608 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +00008609 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008610
8611 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00008612 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +00008613 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008614
Michael J. Spencerec38de22010-10-10 22:04:20 +00008615
Chris Lattner2156b792010-09-22 01:11:26 +00008616 MachineMemOperand *MMO =
8617 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8618 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008619
Chris Lattner2156b792010-09-22 01:11:26 +00008620 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
8621 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
8622 DAG.getVTList(MVT::Other),
8623 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008624
8625 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +00008626 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Chris Lattner51abfe42010-09-21 06:02:19 +00008627 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008628
8629 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00008630 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +00008631 DAG.getNode(ISD::SRL, DL, MVT::i16,
8632 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00008633 CWD, DAG.getConstant(0x800, MVT::i16)),
8634 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00008635 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +00008636 DAG.getNode(ISD::SRL, DL, MVT::i16,
8637 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00008638 CWD, DAG.getConstant(0x400, MVT::i16)),
8639 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008640
Dan Gohman475871a2008-07-27 21:46:04 +00008641 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +00008642 DAG.getNode(ISD::AND, DL, MVT::i16,
8643 DAG.getNode(ISD::ADD, DL, MVT::i16,
8644 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +00008645 DAG.getConstant(1, MVT::i16)),
8646 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008647
8648
Duncan Sands83ec4b62008-06-06 12:08:01 +00008649 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +00008650 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008651}
8652
Dan Gohmand858e902010-04-17 15:26:15 +00008653SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008654 EVT VT = Op.getValueType();
8655 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008656 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008657 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00008658
8659 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008660 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00008661 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +00008662 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00008663 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008664 }
Evan Cheng18efe262007-12-14 02:13:44 +00008665
Evan Cheng152804e2007-12-14 08:30:15 +00008666 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008667 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008668 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00008669
8670 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00008671 SDValue Ops[] = {
8672 Op,
8673 DAG.getConstant(NumBits+NumBits-1, OpVT),
8674 DAG.getConstant(X86::COND_E, MVT::i8),
8675 Op.getValue(1)
8676 };
8677 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00008678
8679 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00008680 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00008681
Owen Anderson825b72b2009-08-11 20:47:22 +00008682 if (VT == MVT::i8)
8683 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008684 return Op;
8685}
8686
Dan Gohmand858e902010-04-17 15:26:15 +00008687SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008688 EVT VT = Op.getValueType();
8689 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008690 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008691 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00008692
8693 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008694 if (VT == MVT::i8) {
8695 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00008696 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008697 }
Evan Cheng152804e2007-12-14 08:30:15 +00008698
8699 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008700 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008701 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00008702
8703 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00008704 SDValue Ops[] = {
8705 Op,
8706 DAG.getConstant(NumBits, OpVT),
8707 DAG.getConstant(X86::COND_E, MVT::i8),
8708 Op.getValue(1)
8709 };
8710 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00008711
Owen Anderson825b72b2009-08-11 20:47:22 +00008712 if (VT == MVT::i8)
8713 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008714 return Op;
8715}
8716
Dan Gohmand858e902010-04-17 15:26:15 +00008717SDValue X86TargetLowering::LowerMUL_V2I64(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008718 EVT VT = Op.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00008719 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008720 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00008721
Mon P Wangaf9b9522008-12-18 21:42:19 +00008722 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
8723 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
8724 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
8725 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
8726 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
8727 //
8728 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
8729 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
8730 // return AloBlo + AloBhi + AhiBlo;
8731
8732 SDValue A = Op.getOperand(0);
8733 SDValue B = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008734
Dale Johannesene4d209d2009-02-03 20:21:25 +00008735 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008736 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8737 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008738 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008739 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8740 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008741 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008742 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008743 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008744 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008745 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008746 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008747 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008748 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008749 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008750 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008751 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8752 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008753 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008754 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8755 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008756 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
8757 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +00008758 return Res;
8759}
8760
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008761SDValue X86TargetLowering::LowerSHL(SDValue Op, SelectionDAG &DAG) const {
8762 EVT VT = Op.getValueType();
8763 DebugLoc dl = Op.getDebugLoc();
8764 SDValue R = Op.getOperand(0);
8765
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008766 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008767
Nate Begeman51409212010-07-28 00:21:48 +00008768 assert(Subtarget->hasSSE41() && "Cannot lower SHL without SSE4.1 or later");
8769
8770 if (VT == MVT::v4i32) {
8771 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8772 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
8773 Op.getOperand(1), DAG.getConstant(23, MVT::i32));
8774
8775 ConstantInt *CI = ConstantInt::get(*Context, APInt(32, 0x3f800000U));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008776
Nate Begeman51409212010-07-28 00:21:48 +00008777 std::vector<Constant*> CV(4, CI);
8778 Constant *C = ConstantVector::get(CV);
8779 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8780 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008781 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00008782 false, false, 16);
8783
8784 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008785 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +00008786 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
8787 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
8788 }
8789 if (VT == MVT::v16i8) {
8790 // a = a << 5;
8791 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8792 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
8793 Op.getOperand(1), DAG.getConstant(5, MVT::i32));
8794
8795 ConstantInt *CM1 = ConstantInt::get(*Context, APInt(8, 15));
8796 ConstantInt *CM2 = ConstantInt::get(*Context, APInt(8, 63));
8797
8798 std::vector<Constant*> CVM1(16, CM1);
8799 std::vector<Constant*> CVM2(16, CM2);
8800 Constant *C = ConstantVector::get(CVM1);
8801 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8802 SDValue M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008803 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00008804 false, false, 16);
8805
8806 // r = pblendv(r, psllw(r & (char16)15, 4), a);
8807 M = DAG.getNode(ISD::AND, dl, VT, R, M);
8808 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8809 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
8810 DAG.getConstant(4, MVT::i32));
Nate Begeman672fb622010-12-20 22:04:24 +00008811 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT, R, M, Op);
Nate Begeman51409212010-07-28 00:21:48 +00008812 // a += a
8813 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008814
Nate Begeman51409212010-07-28 00:21:48 +00008815 C = ConstantVector::get(CVM2);
8816 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8817 M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008818 MachinePointerInfo::getConstantPool(),
Chris Lattner51abfe42010-09-21 06:02:19 +00008819 false, false, 16);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008820
Nate Begeman51409212010-07-28 00:21:48 +00008821 // r = pblendv(r, psllw(r & (char16)63, 2), a);
8822 M = DAG.getNode(ISD::AND, dl, VT, R, M);
8823 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8824 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
8825 DAG.getConstant(2, MVT::i32));
Nate Begeman672fb622010-12-20 22:04:24 +00008826 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT, R, M, Op);
Nate Begeman51409212010-07-28 00:21:48 +00008827 // a += a
8828 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008829
Nate Begeman51409212010-07-28 00:21:48 +00008830 // return pblendv(r, r+r, a);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008831 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT,
Nate Begeman51409212010-07-28 00:21:48 +00008832 R, DAG.getNode(ISD::ADD, dl, VT, R, R), Op);
8833 return R;
8834 }
8835 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008836}
Mon P Wangaf9b9522008-12-18 21:42:19 +00008837
Dan Gohmand858e902010-04-17 15:26:15 +00008838SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
Bill Wendling74c37652008-12-09 22:08:41 +00008839 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
8840 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +00008841 // looks for this combo and may remove the "setcc" instruction if the "setcc"
8842 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +00008843 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +00008844 SDValue LHS = N->getOperand(0);
8845 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +00008846 unsigned BaseOp = 0;
8847 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008848 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +00008849 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008850 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +00008851 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +00008852 // A subtract of one will be selected as a INC. Note that INC doesn't
8853 // set CF, so we can't do this for UADDO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +00008854 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
8855 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +00008856 BaseOp = X86ISD::INC;
8857 Cond = X86::COND_O;
8858 break;
8859 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008860 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +00008861 Cond = X86::COND_O;
8862 break;
8863 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008864 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +00008865 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00008866 break;
8867 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +00008868 // A subtract of one will be selected as a DEC. Note that DEC doesn't
8869 // set CF, so we can't do this for USUBO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +00008870 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
8871 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +00008872 BaseOp = X86ISD::DEC;
8873 Cond = X86::COND_O;
8874 break;
8875 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008876 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +00008877 Cond = X86::COND_O;
8878 break;
8879 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008880 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +00008881 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00008882 break;
8883 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00008884 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +00008885 Cond = X86::COND_O;
8886 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008887 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
8888 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
8889 MVT::i32);
8890 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008891
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008892 SDValue SetCC =
8893 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
8894 DAG.getConstant(X86::COND_O, MVT::i32),
8895 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008896
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008897 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
8898 return Sum;
8899 }
Bill Wendling74c37652008-12-09 22:08:41 +00008900 }
Bill Wendling3fafd932008-11-26 22:37:40 +00008901
Bill Wendling61edeb52008-12-02 01:06:39 +00008902 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008903 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008904 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +00008905
Bill Wendling61edeb52008-12-02 01:06:39 +00008906 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008907 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
8908 DAG.getConstant(Cond, MVT::i32),
8909 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +00008910
Bill Wendling61edeb52008-12-02 01:06:39 +00008911 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
8912 return Sum;
Bill Wendling41ea7e72008-11-24 19:21:46 +00008913}
8914
Eric Christopher9a9d2752010-07-22 02:48:34 +00008915SDValue X86TargetLowering::LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG) const{
8916 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00008917
Eric Christopherb6729dc2010-08-04 23:03:04 +00008918 if (!Subtarget->hasSSE2()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +00008919 SDValue Chain = Op.getOperand(0);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008920 SDValue Zero = DAG.getConstant(0,
Eric Christopherb6729dc2010-08-04 23:03:04 +00008921 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +00008922 SDValue Ops[] = {
8923 DAG.getRegister(X86::ESP, MVT::i32), // Base
8924 DAG.getTargetConstant(1, MVT::i8), // Scale
8925 DAG.getRegister(0, MVT::i32), // Index
8926 DAG.getTargetConstant(0, MVT::i32), // Disp
8927 DAG.getRegister(0, MVT::i32), // Segment.
8928 Zero,
8929 Chain
8930 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00008931 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +00008932 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
8933 array_lengthof(Ops));
8934 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +00008935 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00008936
Eric Christopher9a9d2752010-07-22 02:48:34 +00008937 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +00008938 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +00008939 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008940
Chris Lattner132929a2010-08-14 17:26:09 +00008941 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
8942 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
8943 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
8944 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +00008945
Chris Lattner132929a2010-08-14 17:26:09 +00008946 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
8947 if (!Op1 && !Op2 && !Op3 && Op4)
8948 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008949
Chris Lattner132929a2010-08-14 17:26:09 +00008950 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
8951 if (Op1 && !Op2 && !Op3 && !Op4)
8952 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008953
8954 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +00008955 // (MFENCE)>;
8956 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +00008957}
8958
Dan Gohmand858e902010-04-17 15:26:15 +00008959SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008960 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008961 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +00008962 unsigned Reg = 0;
8963 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00008964 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008965 default:
8966 assert(false && "Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +00008967 case MVT::i8: Reg = X86::AL; size = 1; break;
8968 case MVT::i16: Reg = X86::AX; size = 2; break;
8969 case MVT::i32: Reg = X86::EAX; size = 4; break;
8970 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +00008971 assert(Subtarget->is64Bit() && "Node not type legal!");
8972 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00008973 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00008974 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008975 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +00008976 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +00008977 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +00008978 Op.getOperand(1),
8979 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +00008980 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +00008981 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008982 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008983 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
8984 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
8985 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +00008986 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008987 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +00008988 return cpOut;
8989}
8990
Duncan Sands1607f052008-12-01 11:39:25 +00008991SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008992 SelectionDAG &DAG) const {
Duncan Sands1607f052008-12-01 11:39:25 +00008993 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008994 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +00008995 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008996 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +00008997 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00008998 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
8999 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +00009000 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +00009001 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
9002 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +00009003 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +00009004 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +00009005 rdx.getValue(1)
9006 };
Dale Johannesene4d209d2009-02-03 20:21:25 +00009007 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009008}
9009
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009010SDValue X86TargetLowering::LowerBITCAST(SDValue Op,
Dale Johannesen7d07b482010-05-21 00:52:33 +00009011 SelectionDAG &DAG) const {
9012 EVT SrcVT = Op.getOperand(0).getValueType();
9013 EVT DstVT = Op.getValueType();
Chris Lattner2a786eb2010-12-19 20:19:20 +00009014 assert(Subtarget->is64Bit() && !Subtarget->hasSSE2() &&
9015 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +00009016 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +00009017 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009018 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +00009019 // i64 <=> MMX conversions are Legal.
9020 if (SrcVT==MVT::i64 && DstVT.isVector())
9021 return Op;
9022 if (DstVT==MVT::i64 && SrcVT.isVector())
9023 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +00009024 // MMX <=> MMX conversions are Legal.
9025 if (SrcVT.isVector() && DstVT.isVector())
9026 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +00009027 // All other conversions need to be expanded.
9028 return SDValue();
9029}
Chris Lattner5b856542010-12-20 00:59:46 +00009030
Dan Gohmand858e902010-04-17 15:26:15 +00009031SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen71d1bf52008-09-29 22:25:26 +00009032 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +00009033 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00009034 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009035 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +00009036 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009037 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009038 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +00009039 Node->getOperand(0),
9040 Node->getOperand(1), negOp,
9041 cast<AtomicSDNode>(Node)->getSrcValue(),
9042 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang63307c32008-05-05 19:05:59 +00009043}
9044
Chris Lattner5b856542010-12-20 00:59:46 +00009045static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
9046 EVT VT = Op.getNode()->getValueType(0);
9047
9048 // Let legalize expand this if it isn't a legal type yet.
9049 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
9050 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009051
Chris Lattner5b856542010-12-20 00:59:46 +00009052 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009053
Chris Lattner5b856542010-12-20 00:59:46 +00009054 unsigned Opc;
9055 bool ExtraOp = false;
9056 switch (Op.getOpcode()) {
9057 default: assert(0 && "Invalid code");
9058 case ISD::ADDC: Opc = X86ISD::ADD; break;
9059 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
9060 case ISD::SUBC: Opc = X86ISD::SUB; break;
9061 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
9062 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009063
Chris Lattner5b856542010-12-20 00:59:46 +00009064 if (!ExtraOp)
9065 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
9066 Op.getOperand(1));
9067 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
9068 Op.getOperand(1), Op.getOperand(2));
9069}
9070
Evan Cheng0db9fe62006-04-25 20:13:52 +00009071/// LowerOperation - Provide custom lowering hooks for some operations.
9072///
Dan Gohmand858e902010-04-17 15:26:15 +00009073SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00009074 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00009075 default: llvm_unreachable("Should not custom lower this!");
Eric Christopher9a9d2752010-07-22 02:48:34 +00009076 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op,DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009077 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
9078 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009079 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00009080 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009081 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
9082 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
9083 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
David Greene91585092011-01-26 15:38:49 +00009084 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
David Greenecfe33c42011-01-26 19:13:22 +00009085 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009086 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
9087 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
9088 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00009089 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +00009090 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +00009091 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009092 case ISD::SHL_PARTS:
9093 case ISD::SRA_PARTS:
9094 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
9095 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00009096 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009097 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00009098 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009099 case ISD::FABS: return LowerFABS(Op, DAG);
9100 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00009101 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00009102 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman30a0de92008-07-17 16:51:19 +00009103 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00009104 case ISD::SELECT: return LowerSELECT(Op, DAG);
9105 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009106 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009107 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +00009108 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +00009109 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009110 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00009111 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
9112 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009113 case ISD::FRAME_TO_ARGS_OFFSET:
9114 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009115 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009116 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009117 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00009118 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +00009119 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
9120 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Mon P Wangaf9b9522008-12-18 21:42:19 +00009121 case ISD::MUL: return LowerMUL_V2I64(Op, DAG);
Nate Begemanbdcb5af2010-07-27 22:37:06 +00009122 case ISD::SHL: return LowerSHL(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +00009123 case ISD::SADDO:
9124 case ISD::UADDO:
9125 case ISD::SSUBO:
9126 case ISD::USUBO:
9127 case ISD::SMULO:
9128 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00009129 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009130 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +00009131 case ISD::ADDC:
9132 case ISD::ADDE:
9133 case ISD::SUBC:
9134 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009135 }
Chris Lattner27a6c732007-11-24 07:07:01 +00009136}
9137
Duncan Sands1607f052008-12-01 11:39:25 +00009138void X86TargetLowering::
9139ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00009140 SelectionDAG &DAG, unsigned NewOp) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009141 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009142 DebugLoc dl = Node->getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00009143 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +00009144
9145 SDValue Chain = Node->getOperand(0);
9146 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00009147 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00009148 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00009149 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00009150 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +00009151 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +00009152 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +00009153 SDValue Result =
9154 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
9155 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +00009156 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +00009157 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00009158 Results.push_back(Result.getValue(2));
9159}
9160
Duncan Sands126d9072008-07-04 11:47:58 +00009161/// ReplaceNodeResults - Replace a node with an illegal result type
9162/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00009163void X86TargetLowering::ReplaceNodeResults(SDNode *N,
9164 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00009165 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +00009166 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +00009167 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +00009168 default:
Duncan Sands1607f052008-12-01 11:39:25 +00009169 assert(false && "Do not know how to custom type legalize this operation!");
9170 return;
Chris Lattner5b856542010-12-20 00:59:46 +00009171 case ISD::ADDC:
9172 case ISD::ADDE:
9173 case ISD::SUBC:
9174 case ISD::SUBE:
9175 // We don't want to expand or promote these.
9176 return;
Duncan Sands1607f052008-12-01 11:39:25 +00009177 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +00009178 std::pair<SDValue,SDValue> Vals =
9179 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +00009180 SDValue FIST = Vals.first, StackSlot = Vals.second;
9181 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +00009182 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +00009183 // Return a load from the stack slot.
Chris Lattner51abfe42010-09-21 06:02:19 +00009184 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
9185 MachinePointerInfo(), false, false, 0));
Duncan Sands1607f052008-12-01 11:39:25 +00009186 }
9187 return;
9188 }
9189 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009190 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +00009191 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009192 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00009193 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +00009194 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00009195 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00009196 eax.getValue(2));
9197 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
9198 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +00009199 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00009200 Results.push_back(edx.getValue(1));
9201 return;
9202 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009203 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +00009204 EVT T = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009205 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Duncan Sands1607f052008-12-01 11:39:25 +00009206 SDValue cpInL, cpInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00009207 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
9208 DAG.getConstant(0, MVT::i32));
9209 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
9210 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00009211 cpInL = DAG.getCopyToReg(N->getOperand(0), dl, X86::EAX, cpInL, SDValue());
9212 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl, X86::EDX, cpInH,
Duncan Sands1607f052008-12-01 11:39:25 +00009213 cpInL.getValue(1));
9214 SDValue swapInL, swapInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00009215 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
9216 DAG.getConstant(0, MVT::i32));
9217 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
9218 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00009219 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl, X86::EBX, swapInL,
Duncan Sands1607f052008-12-01 11:39:25 +00009220 cpInH.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00009221 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl, X86::ECX, swapInH,
Duncan Sands1607f052008-12-01 11:39:25 +00009222 swapInL.getValue(1));
9223 SDValue Ops[] = { swapInH.getValue(0),
9224 N->getOperand(1),
9225 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009226 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +00009227 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
9228 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG8_DAG, dl, Tys,
9229 Ops, 3, T, MMO);
Dale Johannesendd64c412009-02-04 00:33:20 +00009230 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl, X86::EAX,
Owen Anderson825b72b2009-08-11 20:47:22 +00009231 MVT::i32, Result.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00009232 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl, X86::EDX,
Owen Anderson825b72b2009-08-11 20:47:22 +00009233 MVT::i32, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +00009234 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Owen Anderson825b72b2009-08-11 20:47:22 +00009235 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00009236 Results.push_back(cpOutH.getValue(1));
9237 return;
9238 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009239 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +00009240 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
9241 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009242 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +00009243 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
9244 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009245 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +00009246 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
9247 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009248 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +00009249 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
9250 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009251 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +00009252 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
9253 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009254 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +00009255 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
9256 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009257 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +00009258 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
9259 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00009260 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00009261}
9262
Evan Cheng72261582005-12-20 06:22:03 +00009263const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
9264 switch (Opcode) {
9265 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +00009266 case X86ISD::BSF: return "X86ISD::BSF";
9267 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +00009268 case X86ISD::SHLD: return "X86ISD::SHLD";
9269 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00009270 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00009271 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00009272 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00009273 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00009274 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00009275 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00009276 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
9277 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
9278 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00009279 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00009280 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +00009281 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +00009282 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +00009283 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +00009284 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00009285 case X86ISD::COMI: return "X86ISD::COMI";
9286 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00009287 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +00009288 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Evan Cheng72261582005-12-20 06:22:03 +00009289 case X86ISD::CMOV: return "X86ISD::CMOV";
9290 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00009291 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00009292 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
9293 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +00009294 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00009295 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +00009296 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +00009297 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +00009298 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +00009299 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
9300 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +00009301 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +00009302 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Nate Begemanb65c1752010-12-17 22:55:37 +00009303 case X86ISD::PANDN: return "X86ISD::PANDN";
9304 case X86ISD::PSIGNB: return "X86ISD::PSIGNB";
9305 case X86ISD::PSIGNW: return "X86ISD::PSIGNW";
9306 case X86ISD::PSIGND: return "X86ISD::PSIGND";
Nate Begeman672fb622010-12-20 22:04:24 +00009307 case X86ISD::PBLENDVB: return "X86ISD::PBLENDVB";
Evan Cheng8ca29322006-11-10 21:43:37 +00009308 case X86ISD::FMAX: return "X86ISD::FMAX";
9309 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +00009310 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
9311 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00009312 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +00009313 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009314 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00009315 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009316 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +00009317 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
9318 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009319 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
9320 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
9321 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
9322 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
9323 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
9324 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +00009325 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
9326 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +00009327 case X86ISD::VSHL: return "X86ISD::VSHL";
9328 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +00009329 case X86ISD::CMPPD: return "X86ISD::CMPPD";
9330 case X86ISD::CMPPS: return "X86ISD::CMPPS";
9331 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
9332 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
9333 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
9334 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
9335 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
9336 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
9337 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
9338 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +00009339 case X86ISD::ADD: return "X86ISD::ADD";
9340 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +00009341 case X86ISD::ADC: return "X86ISD::ADC";
9342 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +00009343 case X86ISD::SMUL: return "X86ISD::SMUL";
9344 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +00009345 case X86ISD::INC: return "X86ISD::INC";
9346 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +00009347 case X86ISD::OR: return "X86ISD::OR";
9348 case X86ISD::XOR: return "X86ISD::XOR";
9349 case X86ISD::AND: return "X86ISD::AND";
Evan Cheng73f24c92009-03-30 21:36:47 +00009350 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +00009351 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009352 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009353 case X86ISD::PALIGN: return "X86ISD::PALIGN";
9354 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
9355 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
9356 case X86ISD::PSHUFHW_LD: return "X86ISD::PSHUFHW_LD";
9357 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
9358 case X86ISD::PSHUFLW_LD: return "X86ISD::PSHUFLW_LD";
9359 case X86ISD::SHUFPS: return "X86ISD::SHUFPS";
9360 case X86ISD::SHUFPD: return "X86ISD::SHUFPD";
9361 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009362 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00009363 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009364 case X86ISD::MOVHLPD: return "X86ISD::MOVHLPD";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00009365 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
9366 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009367 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
9368 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
9369 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
9370 case X86ISD::MOVSHDUP_LD: return "X86ISD::MOVSHDUP_LD";
9371 case X86ISD::MOVSLDUP_LD: return "X86ISD::MOVSLDUP_LD";
9372 case X86ISD::MOVSD: return "X86ISD::MOVSD";
9373 case X86ISD::MOVSS: return "X86ISD::MOVSS";
9374 case X86ISD::UNPCKLPS: return "X86ISD::UNPCKLPS";
9375 case X86ISD::UNPCKLPD: return "X86ISD::UNPCKLPD";
David Greenefbf05d32011-02-22 23:31:46 +00009376 case X86ISD::VUNPCKLPS: return "X86ISD::VUNPCKLPS";
9377 case X86ISD::VUNPCKLPD: return "X86ISD::VUNPCKLPD";
9378 case X86ISD::VUNPCKLPSY: return "X86ISD::VUNPCKLPSY";
9379 case X86ISD::VUNPCKLPDY: return "X86ISD::VUNPCKLPDY";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009380 case X86ISD::UNPCKHPS: return "X86ISD::UNPCKHPS";
9381 case X86ISD::UNPCKHPD: return "X86ISD::UNPCKHPD";
9382 case X86ISD::PUNPCKLBW: return "X86ISD::PUNPCKLBW";
9383 case X86ISD::PUNPCKLWD: return "X86ISD::PUNPCKLWD";
9384 case X86ISD::PUNPCKLDQ: return "X86ISD::PUNPCKLDQ";
9385 case X86ISD::PUNPCKLQDQ: return "X86ISD::PUNPCKLQDQ";
9386 case X86ISD::PUNPCKHBW: return "X86ISD::PUNPCKHBW";
9387 case X86ISD::PUNPCKHWD: return "X86ISD::PUNPCKHWD";
9388 case X86ISD::PUNPCKHDQ: return "X86ISD::PUNPCKHDQ";
9389 case X86ISD::PUNPCKHQDQ: return "X86ISD::PUNPCKHQDQ";
Dan Gohmand6708ea2009-08-15 01:38:56 +00009390 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +00009391 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +00009392 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Evan Cheng72261582005-12-20 06:22:03 +00009393 }
9394}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00009395
Chris Lattnerc9addb72007-03-30 23:15:24 +00009396// isLegalAddressingMode - Return true if the addressing mode represented
9397// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +00009398bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerc9addb72007-03-30 23:15:24 +00009399 const Type *Ty) const {
9400 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009401 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +00009402 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +00009403
Chris Lattnerc9addb72007-03-30 23:15:24 +00009404 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009405 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +00009406 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00009407
Chris Lattnerc9addb72007-03-30 23:15:24 +00009408 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +00009409 unsigned GVFlags =
9410 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009411
Chris Lattnerdfed4132009-07-10 07:38:24 +00009412 // If a reference to this global requires an extra load, we can't fold it.
9413 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +00009414 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009415
Chris Lattnerdfed4132009-07-10 07:38:24 +00009416 // If BaseGV requires a register for the PIC base, we cannot also have a
9417 // BaseReg specified.
9418 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +00009419 return false;
Evan Cheng52787842007-08-01 23:46:47 +00009420
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009421 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +00009422 if ((M != CodeModel::Small || R != Reloc::Static) &&
9423 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009424 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +00009425 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009426
Chris Lattnerc9addb72007-03-30 23:15:24 +00009427 switch (AM.Scale) {
9428 case 0:
9429 case 1:
9430 case 2:
9431 case 4:
9432 case 8:
9433 // These scales always work.
9434 break;
9435 case 3:
9436 case 5:
9437 case 9:
9438 // These scales are formed with basereg+scalereg. Only accept if there is
9439 // no basereg yet.
9440 if (AM.HasBaseReg)
9441 return false;
9442 break;
9443 default: // Other stuff never works.
9444 return false;
9445 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009446
Chris Lattnerc9addb72007-03-30 23:15:24 +00009447 return true;
9448}
9449
9450
Evan Cheng2bd122c2007-10-26 01:56:11 +00009451bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009452 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +00009453 return false;
Evan Chenge127a732007-10-29 07:57:50 +00009454 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
9455 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00009456 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +00009457 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00009458 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +00009459}
9460
Owen Andersone50ed302009-08-10 22:56:29 +00009461bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +00009462 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009463 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009464 unsigned NumBits1 = VT1.getSizeInBits();
9465 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00009466 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009467 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00009468 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009469}
Evan Cheng2bd122c2007-10-26 01:56:11 +00009470
Dan Gohman97121ba2009-04-08 00:15:30 +00009471bool X86TargetLowering::isZExtFree(const Type *Ty1, const Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00009472 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009473 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00009474}
9475
Owen Andersone50ed302009-08-10 22:56:29 +00009476bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00009477 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00009478 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00009479}
9480
Owen Andersone50ed302009-08-10 22:56:29 +00009481bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +00009482 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +00009483 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +00009484}
9485
Evan Cheng60c07e12006-07-05 22:17:51 +00009486/// isShuffleMaskLegal - Targets can use this to indicate that they only
9487/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
9488/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
9489/// are assumed to be legal.
9490bool
Eric Christopherfd179292009-08-27 18:07:15 +00009491X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +00009492 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +00009493 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +00009494 if (VT.getSizeInBits() == 64)
Eric Christophercff6f852010-04-15 01:40:20 +00009495 return isPALIGNRMask(M, VT, Subtarget->hasSSSE3());
Nate Begeman9008ca62009-04-27 18:41:29 +00009496
Nate Begemana09008b2009-10-19 02:17:23 +00009497 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +00009498 return (VT.getVectorNumElements() == 2 ||
9499 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
9500 isMOVLMask(M, VT) ||
9501 isSHUFPMask(M, VT) ||
9502 isPSHUFDMask(M, VT) ||
9503 isPSHUFHWMask(M, VT) ||
9504 isPSHUFLWMask(M, VT) ||
Nate Begemana09008b2009-10-19 02:17:23 +00009505 isPALIGNRMask(M, VT, Subtarget->hasSSSE3()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +00009506 isUNPCKLMask(M, VT) ||
9507 isUNPCKHMask(M, VT) ||
9508 isUNPCKL_v_undef_Mask(M, VT) ||
9509 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00009510}
9511
Dan Gohman7d8143f2008-04-09 20:09:42 +00009512bool
Nate Begeman5a5ca152009-04-29 05:20:52 +00009513X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +00009514 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00009515 unsigned NumElts = VT.getVectorNumElements();
9516 // FIXME: This collection of masks seems suspect.
9517 if (NumElts == 2)
9518 return true;
9519 if (NumElts == 4 && VT.getSizeInBits() == 128) {
9520 return (isMOVLMask(Mask, VT) ||
9521 isCommutedMOVLMask(Mask, VT, true) ||
9522 isSHUFPMask(Mask, VT) ||
9523 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00009524 }
9525 return false;
9526}
9527
9528//===----------------------------------------------------------------------===//
9529// X86 Scheduler Hooks
9530//===----------------------------------------------------------------------===//
9531
Mon P Wang63307c32008-05-05 19:05:59 +00009532// private utility function
9533MachineBasicBlock *
9534X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
9535 MachineBasicBlock *MBB,
9536 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009537 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009538 unsigned LoadOpc,
9539 unsigned CXchgOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009540 unsigned notOpc,
9541 unsigned EAXreg,
9542 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009543 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00009544 // For the atomic bitwise operator, we generate
9545 // thisMBB:
9546 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00009547 // ld t1 = [bitinstr.addr]
9548 // op t2 = t1, [bitinstr.val]
9549 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00009550 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
9551 // bz newMBB
9552 // fallthrough -->nextMBB
9553 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9554 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009555 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00009556 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009557
Mon P Wang63307c32008-05-05 19:05:59 +00009558 /// First build the CFG
9559 MachineFunction *F = MBB->getParent();
9560 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009561 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9562 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9563 F->insert(MBBIter, newMBB);
9564 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009565
Dan Gohman14152b42010-07-06 20:24:04 +00009566 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9567 nextMBB->splice(nextMBB->begin(), thisMBB,
9568 llvm::next(MachineBasicBlock::iterator(bInstr)),
9569 thisMBB->end());
9570 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009571
Mon P Wang63307c32008-05-05 19:05:59 +00009572 // Update thisMBB to fall through to newMBB
9573 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009574
Mon P Wang63307c32008-05-05 19:05:59 +00009575 // newMBB jumps to itself and fall through to nextMBB
9576 newMBB->addSuccessor(nextMBB);
9577 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009578
Mon P Wang63307c32008-05-05 19:05:59 +00009579 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009580 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009581 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +00009582 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00009583 MachineOperand& destOper = bInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009584 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00009585 int numArgs = bInstr->getNumOperands() - 1;
9586 for (int i=0; i < numArgs; ++i)
9587 argOpers[i] = &bInstr->getOperand(i+1);
9588
9589 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009590 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009591 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00009592
Dale Johannesen140be2d2008-08-19 18:47:28 +00009593 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009594 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00009595 for (int i=0; i <= lastAddrIndx; ++i)
9596 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009597
Dale Johannesen140be2d2008-08-19 18:47:28 +00009598 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009599 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00009600 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009601 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009602 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009603 tt = t1;
9604
Dale Johannesen140be2d2008-08-19 18:47:28 +00009605 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +00009606 assert((argOpers[valArgIndx]->isReg() ||
9607 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00009608 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +00009609 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009610 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00009611 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009612 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009613 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +00009614 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009615
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009616 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +00009617 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009618
Dale Johannesene4d209d2009-02-03 20:21:25 +00009619 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +00009620 for (int i=0; i <= lastAddrIndx; ++i)
9621 (*MIB).addOperand(*argOpers[i]);
9622 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +00009623 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009624 (*MIB).setMemRefs(bInstr->memoperands_begin(),
9625 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +00009626
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009627 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +00009628 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +00009629
Mon P Wang63307c32008-05-05 19:05:59 +00009630 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009631 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00009632
Dan Gohman14152b42010-07-06 20:24:04 +00009633 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00009634 return nextMBB;
9635}
9636
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00009637// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +00009638MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009639X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
9640 MachineBasicBlock *MBB,
9641 unsigned regOpcL,
9642 unsigned regOpcH,
9643 unsigned immOpcL,
9644 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009645 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009646 // For the atomic bitwise operator, we generate
9647 // thisMBB (instructions are in pairs, except cmpxchg8b)
9648 // ld t1,t2 = [bitinstr.addr]
9649 // newMBB:
9650 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
9651 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +00009652 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009653 // mov ECX, EBX <- t5, t6
9654 // mov EAX, EDX <- t1, t2
9655 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
9656 // mov t3, t4 <- EAX, EDX
9657 // bz newMBB
9658 // result in out1, out2
9659 // fallthrough -->nextMBB
9660
9661 const TargetRegisterClass *RC = X86::GR32RegisterClass;
9662 const unsigned LoadOpc = X86::MOV32rm;
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009663 const unsigned NotOpc = X86::NOT32r;
9664 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9665 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
9666 MachineFunction::iterator MBBIter = MBB;
9667 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009668
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009669 /// First build the CFG
9670 MachineFunction *F = MBB->getParent();
9671 MachineBasicBlock *thisMBB = MBB;
9672 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9673 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9674 F->insert(MBBIter, newMBB);
9675 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009676
Dan Gohman14152b42010-07-06 20:24:04 +00009677 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9678 nextMBB->splice(nextMBB->begin(), thisMBB,
9679 llvm::next(MachineBasicBlock::iterator(bInstr)),
9680 thisMBB->end());
9681 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009682
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009683 // Update thisMBB to fall through to newMBB
9684 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009685
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009686 // newMBB jumps to itself and fall through to nextMBB
9687 newMBB->addSuccessor(nextMBB);
9688 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009689
Dale Johannesene4d209d2009-02-03 20:21:25 +00009690 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009691 // Insert instructions into newMBB based on incoming instruction
9692 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009693 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009694 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009695 MachineOperand& dest1Oper = bInstr->getOperand(0);
9696 MachineOperand& dest2Oper = bInstr->getOperand(1);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009697 MachineOperand* argOpers[2 + X86::AddrNumOperands];
9698 for (int i=0; i < 2 + X86::AddrNumOperands; ++i) {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009699 argOpers[i] = &bInstr->getOperand(i+2);
9700
Dan Gohman71ea4e52010-05-14 21:01:44 +00009701 // We use some of the operands multiple times, so conservatively just
9702 // clear any kill flags that might be present.
9703 if (argOpers[i]->isReg() && argOpers[i]->isUse())
9704 argOpers[i]->setIsKill(false);
9705 }
9706
Evan Chengad5b52f2010-01-08 19:14:57 +00009707 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009708 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +00009709
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009710 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009711 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009712 for (int i=0; i <= lastAddrIndx; ++i)
9713 (*MIB).addOperand(*argOpers[i]);
9714 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009715 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +00009716 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +00009717 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009718 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +00009719 MachineOperand newOp3 = *(argOpers[3]);
9720 if (newOp3.isImm())
9721 newOp3.setImm(newOp3.getImm()+4);
9722 else
9723 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009724 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +00009725 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009726
9727 // t3/4 are defined later, at the bottom of the loop
9728 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
9729 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009730 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009731 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009732 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009733 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
9734
Evan Cheng306b4ca2010-01-08 23:41:50 +00009735 // The subsequent operations should be using the destination registers of
9736 //the PHI instructions.
Scott Michelfdc40a02009-02-17 22:15:04 +00009737 if (invSrc) {
Evan Cheng306b4ca2010-01-08 23:41:50 +00009738 t1 = F->getRegInfo().createVirtualRegister(RC);
9739 t2 = F->getRegInfo().createVirtualRegister(RC);
9740 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
9741 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009742 } else {
Evan Cheng306b4ca2010-01-08 23:41:50 +00009743 t1 = dest1Oper.getReg();
9744 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009745 }
9746
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009747 int valArgIndx = lastAddrIndx + 1;
9748 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +00009749 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009750 "invalid operand");
9751 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
9752 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009753 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009754 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009755 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009756 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +00009757 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00009758 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009759 (*MIB).addOperand(*argOpers[valArgIndx]);
9760 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00009761 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009762 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00009763 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009764 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009765 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009766 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009767 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +00009768 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00009769 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009770 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009771
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009772 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009773 MIB.addReg(t1);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009774 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009775 MIB.addReg(t2);
9776
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009777 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009778 MIB.addReg(t5);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009779 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009780 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +00009781
Dale Johannesene4d209d2009-02-03 20:21:25 +00009782 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009783 for (int i=0; i <= lastAddrIndx; ++i)
9784 (*MIB).addOperand(*argOpers[i]);
9785
9786 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009787 (*MIB).setMemRefs(bInstr->memoperands_begin(),
9788 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009789
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009790 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009791 MIB.addReg(X86::EAX);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009792 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009793 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +00009794
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009795 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009796 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009797
Dan Gohman14152b42010-07-06 20:24:04 +00009798 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009799 return nextMBB;
9800}
9801
9802// private utility function
9803MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +00009804X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
9805 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009806 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00009807 // For the atomic min/max operator, we generate
9808 // thisMBB:
9809 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00009810 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +00009811 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +00009812 // cmp t1, t2
9813 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +00009814 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00009815 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
9816 // bz newMBB
9817 // fallthrough -->nextMBB
9818 //
9819 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9820 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009821 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00009822 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009823
Mon P Wang63307c32008-05-05 19:05:59 +00009824 /// First build the CFG
9825 MachineFunction *F = MBB->getParent();
9826 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009827 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9828 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9829 F->insert(MBBIter, newMBB);
9830 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009831
Dan Gohman14152b42010-07-06 20:24:04 +00009832 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9833 nextMBB->splice(nextMBB->begin(), thisMBB,
9834 llvm::next(MachineBasicBlock::iterator(mInstr)),
9835 thisMBB->end());
9836 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009837
Mon P Wang63307c32008-05-05 19:05:59 +00009838 // Update thisMBB to fall through to newMBB
9839 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009840
Mon P Wang63307c32008-05-05 19:05:59 +00009841 // newMBB jumps to newMBB and fall through to nextMBB
9842 newMBB->addSuccessor(nextMBB);
9843 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009844
Dale Johannesene4d209d2009-02-03 20:21:25 +00009845 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00009846 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009847 assert(mInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009848 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +00009849 MachineOperand& destOper = mInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009850 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00009851 int numArgs = mInstr->getNumOperands() - 1;
9852 for (int i=0; i < numArgs; ++i)
9853 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009854
Mon P Wang63307c32008-05-05 19:05:59 +00009855 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009856 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009857 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00009858
Mon P Wangab3e7472008-05-05 22:56:23 +00009859 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009860 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00009861 for (int i=0; i <= lastAddrIndx; ++i)
9862 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +00009863
Mon P Wang63307c32008-05-05 19:05:59 +00009864 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +00009865 assert((argOpers[valArgIndx]->isReg() ||
9866 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00009867 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +00009868
9869 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +00009870 if (argOpers[valArgIndx]->isReg())
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009871 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +00009872 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009873 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00009874 (*MIB).addOperand(*argOpers[valArgIndx]);
9875
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009876 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +00009877 MIB.addReg(t1);
9878
Dale Johannesene4d209d2009-02-03 20:21:25 +00009879 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +00009880 MIB.addReg(t1);
9881 MIB.addReg(t2);
9882
9883 // Generate movc
9884 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009885 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +00009886 MIB.addReg(t2);
9887 MIB.addReg(t1);
9888
9889 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +00009890 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +00009891 for (int i=0; i <= lastAddrIndx; ++i)
9892 (*MIB).addOperand(*argOpers[i]);
9893 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +00009894 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009895 (*MIB).setMemRefs(mInstr->memoperands_begin(),
9896 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +00009897
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009898 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +00009899 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +00009900
Mon P Wang63307c32008-05-05 19:05:59 +00009901 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009902 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00009903
Dan Gohman14152b42010-07-06 20:24:04 +00009904 mInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00009905 return nextMBB;
9906}
9907
Eric Christopherf83a5de2009-08-27 18:08:16 +00009908// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009909// or XMM0_V32I8 in AVX all of this code can be replaced with that
9910// in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +00009911MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +00009912X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +00009913 unsigned numArgs, bool memArg) const {
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009914 assert((Subtarget->hasSSE42() || Subtarget->hasAVX()) &&
9915 "Target must have SSE4.2 or AVX features enabled");
9916
Eric Christopherb120ab42009-08-18 22:50:32 +00009917 DebugLoc dl = MI->getDebugLoc();
9918 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Eric Christopherb120ab42009-08-18 22:50:32 +00009919 unsigned Opc;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009920 if (!Subtarget->hasAVX()) {
9921 if (memArg)
9922 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
9923 else
9924 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
9925 } else {
9926 if (memArg)
9927 Opc = numArgs == 3 ? X86::VPCMPISTRM128rm : X86::VPCMPESTRM128rm;
9928 else
9929 Opc = numArgs == 3 ? X86::VPCMPISTRM128rr : X86::VPCMPESTRM128rr;
9930 }
Eric Christopherb120ab42009-08-18 22:50:32 +00009931
Eric Christopher41c902f2010-11-30 08:20:21 +00009932 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Eric Christopherb120ab42009-08-18 22:50:32 +00009933 for (unsigned i = 0; i < numArgs; ++i) {
9934 MachineOperand &Op = MI->getOperand(i+1);
Eric Christopherb120ab42009-08-18 22:50:32 +00009935 if (!(Op.isReg() && Op.isImplicit()))
9936 MIB.addOperand(Op);
9937 }
Eric Christopher41c902f2010-11-30 08:20:21 +00009938 BuildMI(*BB, MI, dl, TII->get(X86::MOVAPSrr), MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +00009939 .addReg(X86::XMM0);
9940
Dan Gohman14152b42010-07-06 20:24:04 +00009941 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +00009942 return BB;
9943}
9944
9945MachineBasicBlock *
Eric Christopher228232b2010-11-30 07:20:12 +00009946X86TargetLowering::EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +00009947 DebugLoc dl = MI->getDebugLoc();
9948 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009949
Eric Christopher228232b2010-11-30 07:20:12 +00009950 // Address into RAX/EAX, other two args into ECX, EDX.
9951 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
9952 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
9953 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
9954 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +00009955 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009956
Eric Christopher228232b2010-11-30 07:20:12 +00009957 unsigned ValOps = X86::AddrNumOperands;
9958 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
9959 .addReg(MI->getOperand(ValOps).getReg());
9960 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
9961 .addReg(MI->getOperand(ValOps+1).getReg());
9962
9963 // The instruction doesn't actually take any operands though.
9964 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009965
Eric Christopher228232b2010-11-30 07:20:12 +00009966 MI->eraseFromParent(); // The pseudo is gone now.
9967 return BB;
9968}
9969
9970MachineBasicBlock *
9971X86TargetLowering::EmitMwait(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +00009972 DebugLoc dl = MI->getDebugLoc();
9973 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009974
Eric Christopher228232b2010-11-30 07:20:12 +00009975 // First arg in ECX, the second in EAX.
9976 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
9977 .addReg(MI->getOperand(0).getReg());
9978 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EAX)
9979 .addReg(MI->getOperand(1).getReg());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009980
Eric Christopher228232b2010-11-30 07:20:12 +00009981 // The instruction doesn't actually take any operands though.
9982 BuildMI(*BB, MI, dl, TII->get(X86::MWAITrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009983
Eric Christopher228232b2010-11-30 07:20:12 +00009984 MI->eraseFromParent(); // The pseudo is gone now.
9985 return BB;
9986}
9987
9988MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +00009989X86TargetLowering::EmitVAARG64WithCustomInserter(
9990 MachineInstr *MI,
9991 MachineBasicBlock *MBB) const {
9992 // Emit va_arg instruction on X86-64.
9993
9994 // Operands to this pseudo-instruction:
9995 // 0 ) Output : destination address (reg)
9996 // 1-5) Input : va_list address (addr, i64mem)
9997 // 6 ) ArgSize : Size (in bytes) of vararg type
9998 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
9999 // 8 ) Align : Alignment of type
10000 // 9 ) EFLAGS (implicit-def)
10001
10002 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
10003 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
10004
10005 unsigned DestReg = MI->getOperand(0).getReg();
10006 MachineOperand &Base = MI->getOperand(1);
10007 MachineOperand &Scale = MI->getOperand(2);
10008 MachineOperand &Index = MI->getOperand(3);
10009 MachineOperand &Disp = MI->getOperand(4);
10010 MachineOperand &Segment = MI->getOperand(5);
10011 unsigned ArgSize = MI->getOperand(6).getImm();
10012 unsigned ArgMode = MI->getOperand(7).getImm();
10013 unsigned Align = MI->getOperand(8).getImm();
10014
10015 // Memory Reference
10016 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
10017 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
10018 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
10019
10020 // Machine Information
10021 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10022 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
10023 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
10024 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
10025 DebugLoc DL = MI->getDebugLoc();
10026
10027 // struct va_list {
10028 // i32 gp_offset
10029 // i32 fp_offset
10030 // i64 overflow_area (address)
10031 // i64 reg_save_area (address)
10032 // }
10033 // sizeof(va_list) = 24
10034 // alignment(va_list) = 8
10035
10036 unsigned TotalNumIntRegs = 6;
10037 unsigned TotalNumXMMRegs = 8;
10038 bool UseGPOffset = (ArgMode == 1);
10039 bool UseFPOffset = (ArgMode == 2);
10040 unsigned MaxOffset = TotalNumIntRegs * 8 +
10041 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
10042
10043 /* Align ArgSize to a multiple of 8 */
10044 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
10045 bool NeedsAlign = (Align > 8);
10046
10047 MachineBasicBlock *thisMBB = MBB;
10048 MachineBasicBlock *overflowMBB;
10049 MachineBasicBlock *offsetMBB;
10050 MachineBasicBlock *endMBB;
10051
10052 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
10053 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
10054 unsigned OffsetReg = 0;
10055
10056 if (!UseGPOffset && !UseFPOffset) {
10057 // If we only pull from the overflow region, we don't create a branch.
10058 // We don't need to alter control flow.
10059 OffsetDestReg = 0; // unused
10060 OverflowDestReg = DestReg;
10061
10062 offsetMBB = NULL;
10063 overflowMBB = thisMBB;
10064 endMBB = thisMBB;
10065 } else {
10066 // First emit code to check if gp_offset (or fp_offset) is below the bound.
10067 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
10068 // If not, pull from overflow_area. (branch to overflowMBB)
10069 //
10070 // thisMBB
10071 // | .
10072 // | .
10073 // offsetMBB overflowMBB
10074 // | .
10075 // | .
10076 // endMBB
10077
10078 // Registers for the PHI in endMBB
10079 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
10080 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
10081
10082 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
10083 MachineFunction *MF = MBB->getParent();
10084 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
10085 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
10086 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
10087
10088 MachineFunction::iterator MBBIter = MBB;
10089 ++MBBIter;
10090
10091 // Insert the new basic blocks
10092 MF->insert(MBBIter, offsetMBB);
10093 MF->insert(MBBIter, overflowMBB);
10094 MF->insert(MBBIter, endMBB);
10095
10096 // Transfer the remainder of MBB and its successor edges to endMBB.
10097 endMBB->splice(endMBB->begin(), thisMBB,
10098 llvm::next(MachineBasicBlock::iterator(MI)),
10099 thisMBB->end());
10100 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
10101
10102 // Make offsetMBB and overflowMBB successors of thisMBB
10103 thisMBB->addSuccessor(offsetMBB);
10104 thisMBB->addSuccessor(overflowMBB);
10105
10106 // endMBB is a successor of both offsetMBB and overflowMBB
10107 offsetMBB->addSuccessor(endMBB);
10108 overflowMBB->addSuccessor(endMBB);
10109
10110 // Load the offset value into a register
10111 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
10112 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
10113 .addOperand(Base)
10114 .addOperand(Scale)
10115 .addOperand(Index)
10116 .addDisp(Disp, UseFPOffset ? 4 : 0)
10117 .addOperand(Segment)
10118 .setMemRefs(MMOBegin, MMOEnd);
10119
10120 // Check if there is enough room left to pull this argument.
10121 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
10122 .addReg(OffsetReg)
10123 .addImm(MaxOffset + 8 - ArgSizeA8);
10124
10125 // Branch to "overflowMBB" if offset >= max
10126 // Fall through to "offsetMBB" otherwise
10127 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
10128 .addMBB(overflowMBB);
10129 }
10130
10131 // In offsetMBB, emit code to use the reg_save_area.
10132 if (offsetMBB) {
10133 assert(OffsetReg != 0);
10134
10135 // Read the reg_save_area address.
10136 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
10137 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
10138 .addOperand(Base)
10139 .addOperand(Scale)
10140 .addOperand(Index)
10141 .addDisp(Disp, 16)
10142 .addOperand(Segment)
10143 .setMemRefs(MMOBegin, MMOEnd);
10144
10145 // Zero-extend the offset
10146 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
10147 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
10148 .addImm(0)
10149 .addReg(OffsetReg)
10150 .addImm(X86::sub_32bit);
10151
10152 // Add the offset to the reg_save_area to get the final address.
10153 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
10154 .addReg(OffsetReg64)
10155 .addReg(RegSaveReg);
10156
10157 // Compute the offset for the next argument
10158 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
10159 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
10160 .addReg(OffsetReg)
10161 .addImm(UseFPOffset ? 16 : 8);
10162
10163 // Store it back into the va_list.
10164 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
10165 .addOperand(Base)
10166 .addOperand(Scale)
10167 .addOperand(Index)
10168 .addDisp(Disp, UseFPOffset ? 4 : 0)
10169 .addOperand(Segment)
10170 .addReg(NextOffsetReg)
10171 .setMemRefs(MMOBegin, MMOEnd);
10172
10173 // Jump to endMBB
10174 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
10175 .addMBB(endMBB);
10176 }
10177
10178 //
10179 // Emit code to use overflow area
10180 //
10181
10182 // Load the overflow_area address into a register.
10183 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
10184 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
10185 .addOperand(Base)
10186 .addOperand(Scale)
10187 .addOperand(Index)
10188 .addDisp(Disp, 8)
10189 .addOperand(Segment)
10190 .setMemRefs(MMOBegin, MMOEnd);
10191
10192 // If we need to align it, do so. Otherwise, just copy the address
10193 // to OverflowDestReg.
10194 if (NeedsAlign) {
10195 // Align the overflow address
10196 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
10197 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
10198
10199 // aligned_addr = (addr + (align-1)) & ~(align-1)
10200 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
10201 .addReg(OverflowAddrReg)
10202 .addImm(Align-1);
10203
10204 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
10205 .addReg(TmpReg)
10206 .addImm(~(uint64_t)(Align-1));
10207 } else {
10208 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
10209 .addReg(OverflowAddrReg);
10210 }
10211
10212 // Compute the next overflow address after this argument.
10213 // (the overflow address should be kept 8-byte aligned)
10214 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
10215 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
10216 .addReg(OverflowDestReg)
10217 .addImm(ArgSizeA8);
10218
10219 // Store the new overflow address.
10220 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
10221 .addOperand(Base)
10222 .addOperand(Scale)
10223 .addOperand(Index)
10224 .addDisp(Disp, 8)
10225 .addOperand(Segment)
10226 .addReg(NextAddrReg)
10227 .setMemRefs(MMOBegin, MMOEnd);
10228
10229 // If we branched, emit the PHI to the front of endMBB.
10230 if (offsetMBB) {
10231 BuildMI(*endMBB, endMBB->begin(), DL,
10232 TII->get(X86::PHI), DestReg)
10233 .addReg(OffsetDestReg).addMBB(offsetMBB)
10234 .addReg(OverflowDestReg).addMBB(overflowMBB);
10235 }
10236
10237 // Erase the pseudo instruction
10238 MI->eraseFromParent();
10239
10240 return endMBB;
10241}
10242
10243MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +000010244X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
10245 MachineInstr *MI,
10246 MachineBasicBlock *MBB) const {
10247 // Emit code to save XMM registers to the stack. The ABI says that the
10248 // number of registers to save is given in %al, so it's theoretically
10249 // possible to do an indirect jump trick to avoid saving all of them,
10250 // however this code takes a simpler approach and just executes all
10251 // of the stores if %al is non-zero. It's less code, and it's probably
10252 // easier on the hardware branch predictor, and stores aren't all that
10253 // expensive anyway.
10254
10255 // Create the new basic blocks. One block contains all the XMM stores,
10256 // and one block is the final destination regardless of whether any
10257 // stores were performed.
10258 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
10259 MachineFunction *F = MBB->getParent();
10260 MachineFunction::iterator MBBIter = MBB;
10261 ++MBBIter;
10262 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
10263 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
10264 F->insert(MBBIter, XMMSaveMBB);
10265 F->insert(MBBIter, EndMBB);
10266
Dan Gohman14152b42010-07-06 20:24:04 +000010267 // Transfer the remainder of MBB and its successor edges to EndMBB.
10268 EndMBB->splice(EndMBB->begin(), MBB,
10269 llvm::next(MachineBasicBlock::iterator(MI)),
10270 MBB->end());
10271 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
10272
Dan Gohmand6708ea2009-08-15 01:38:56 +000010273 // The original block will now fall through to the XMM save block.
10274 MBB->addSuccessor(XMMSaveMBB);
10275 // The XMMSaveMBB will fall through to the end block.
10276 XMMSaveMBB->addSuccessor(EndMBB);
10277
10278 // Now add the instructions.
10279 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10280 DebugLoc DL = MI->getDebugLoc();
10281
10282 unsigned CountReg = MI->getOperand(0).getReg();
10283 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
10284 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
10285
10286 if (!Subtarget->isTargetWin64()) {
10287 // If %al is 0, branch around the XMM save block.
10288 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +000010289 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010290 MBB->addSuccessor(EndMBB);
10291 }
10292
10293 // In the XMM save block, save all the XMM argument registers.
10294 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
10295 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +000010296 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +000010297 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +000010298 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +000010299 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +000010300 /*Size=*/16, /*Align=*/16);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010301 BuildMI(XMMSaveMBB, DL, TII->get(X86::MOVAPSmr))
10302 .addFrameIndex(RegSaveFrameIndex)
10303 .addImm(/*Scale=*/1)
10304 .addReg(/*IndexReg=*/0)
10305 .addImm(/*Disp=*/Offset)
10306 .addReg(/*Segment=*/0)
10307 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +000010308 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010309 }
10310
Dan Gohman14152b42010-07-06 20:24:04 +000010311 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +000010312
10313 return EndMBB;
10314}
Mon P Wang63307c32008-05-05 19:05:59 +000010315
Evan Cheng60c07e12006-07-05 22:17:51 +000010316MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +000010317X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010318 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +000010319 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10320 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +000010321
Chris Lattner52600972009-09-02 05:57:00 +000010322 // To "insert" a SELECT_CC instruction, we actually have to insert the
10323 // diamond control-flow pattern. The incoming instruction knows the
10324 // destination vreg to set, the condition code register to branch on, the
10325 // true/false values to select between, and a branch opcode to use.
10326 const BasicBlock *LLVM_BB = BB->getBasicBlock();
10327 MachineFunction::iterator It = BB;
10328 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +000010329
Chris Lattner52600972009-09-02 05:57:00 +000010330 // thisMBB:
10331 // ...
10332 // TrueVal = ...
10333 // cmpTY ccX, r1, r2
10334 // bCC copy1MBB
10335 // fallthrough --> copy0MBB
10336 MachineBasicBlock *thisMBB = BB;
10337 MachineFunction *F = BB->getParent();
10338 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
10339 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +000010340 F->insert(It, copy0MBB);
10341 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +000010342
Bill Wendling730c07e2010-06-25 20:48:10 +000010343 // If the EFLAGS register isn't dead in the terminator, then claim that it's
10344 // live into the sink and copy blocks.
10345 const MachineFunction *MF = BB->getParent();
10346 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
10347 BitVector ReservedRegs = TRI->getReservedRegs(*MF);
Bill Wendling730c07e2010-06-25 20:48:10 +000010348
Dan Gohman14152b42010-07-06 20:24:04 +000010349 for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) {
10350 const MachineOperand &MO = MI->getOperand(I);
10351 if (!MO.isReg() || !MO.isUse() || MO.isKill()) continue;
Bill Wendling730c07e2010-06-25 20:48:10 +000010352 unsigned Reg = MO.getReg();
10353 if (Reg != X86::EFLAGS) continue;
10354 copy0MBB->addLiveIn(Reg);
10355 sinkMBB->addLiveIn(Reg);
10356 }
10357
Dan Gohman14152b42010-07-06 20:24:04 +000010358 // Transfer the remainder of BB and its successor edges to sinkMBB.
10359 sinkMBB->splice(sinkMBB->begin(), BB,
10360 llvm::next(MachineBasicBlock::iterator(MI)),
10361 BB->end());
10362 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
10363
10364 // Add the true and fallthrough blocks as its successors.
10365 BB->addSuccessor(copy0MBB);
10366 BB->addSuccessor(sinkMBB);
10367
10368 // Create the conditional branch instruction.
10369 unsigned Opc =
10370 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
10371 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
10372
Chris Lattner52600972009-09-02 05:57:00 +000010373 // copy0MBB:
10374 // %FalseValue = ...
10375 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000010376 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000010377
Chris Lattner52600972009-09-02 05:57:00 +000010378 // sinkMBB:
10379 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
10380 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000010381 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
10382 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000010383 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
10384 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
10385
Dan Gohman14152b42010-07-06 20:24:04 +000010386 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000010387 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000010388}
10389
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010390MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010391X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010392 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010393 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10394 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010395
10396 // The lowering is pretty easy: we're just emitting the call to _alloca. The
10397 // non-trivial part is impdef of ESP.
10398 // FIXME: The code should be tweaked as soon as we'll try to do codegen for
10399 // mingw-w64.
10400
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010401 const char *StackProbeSymbol =
10402 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
10403
Dan Gohman14152b42010-07-06 20:24:04 +000010404 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010405 .addExternalSymbol(StackProbeSymbol)
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010406 .addReg(X86::EAX, RegState::Implicit)
10407 .addReg(X86::ESP, RegState::Implicit)
10408 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
Anton Korobeynikov9f7f83b2010-08-25 07:50:11 +000010409 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
10410 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010411
Dan Gohman14152b42010-07-06 20:24:04 +000010412 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010413 return BB;
10414}
Chris Lattner52600972009-09-02 05:57:00 +000010415
10416MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000010417X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
10418 MachineBasicBlock *BB) const {
10419 // This is pretty easy. We're taking the value that we received from
10420 // our load from the relocation, sticking it in either RDI (x86-64)
10421 // or EAX and doing an indirect call. The return value will then
10422 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000010423 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000010424 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000010425 DebugLoc DL = MI->getDebugLoc();
10426 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000010427
10428 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000010429 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000010430
Eric Christopher30ef0e52010-06-03 04:07:48 +000010431 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000010432 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10433 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000010434 .addReg(X86::RIP)
10435 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010436 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000010437 MI->getOperand(3).getTargetFlags())
10438 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000010439 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000010440 addDirectMem(MIB, X86::RDI);
Eric Christopher61025492010-06-15 23:08:42 +000010441 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000010442 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10443 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000010444 .addReg(0)
10445 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010446 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000010447 MI->getOperand(3).getTargetFlags())
10448 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000010449 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000010450 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010451 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000010452 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10453 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000010454 .addReg(TII->getGlobalBaseReg(F))
10455 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010456 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000010457 MI->getOperand(3).getTargetFlags())
10458 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000010459 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000010460 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010461 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000010462
Dan Gohman14152b42010-07-06 20:24:04 +000010463 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000010464 return BB;
10465}
10466
10467MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000010468X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010469 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000010470 switch (MI->getOpcode()) {
10471 default: assert(false && "Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000010472 case X86::TAILJMPd64:
10473 case X86::TAILJMPr64:
10474 case X86::TAILJMPm64:
10475 assert(!"TAILJMP64 would not be touched here.");
10476 case X86::TCRETURNdi64:
10477 case X86::TCRETURNri64:
10478 case X86::TCRETURNmi64:
10479 // Defs of TCRETURNxx64 has Win64's callee-saved registers, as subset.
10480 // On AMD64, additional defs should be added before register allocation.
10481 if (!Subtarget->isTargetWin64()) {
10482 MI->addRegisterDefined(X86::RSI);
10483 MI->addRegisterDefined(X86::RDI);
10484 MI->addRegisterDefined(X86::XMM6);
10485 MI->addRegisterDefined(X86::XMM7);
10486 MI->addRegisterDefined(X86::XMM8);
10487 MI->addRegisterDefined(X86::XMM9);
10488 MI->addRegisterDefined(X86::XMM10);
10489 MI->addRegisterDefined(X86::XMM11);
10490 MI->addRegisterDefined(X86::XMM12);
10491 MI->addRegisterDefined(X86::XMM13);
10492 MI->addRegisterDefined(X86::XMM14);
10493 MI->addRegisterDefined(X86::XMM15);
10494 }
10495 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010496 case X86::WIN_ALLOCA:
10497 return EmitLoweredWinAlloca(MI, BB);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010498 case X86::TLSCall_32:
10499 case X86::TLSCall_64:
10500 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000010501 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000010502 case X86::CMOV_FR32:
10503 case X86::CMOV_FR64:
10504 case X86::CMOV_V4F32:
10505 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000010506 case X86::CMOV_V2I64:
Chris Lattner314a1132010-03-14 18:31:44 +000010507 case X86::CMOV_GR16:
10508 case X86::CMOV_GR32:
10509 case X86::CMOV_RFP32:
10510 case X86::CMOV_RFP64:
10511 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010512 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000010513
Dale Johannesen849f2142007-07-03 00:53:03 +000010514 case X86::FP32_TO_INT16_IN_MEM:
10515 case X86::FP32_TO_INT32_IN_MEM:
10516 case X86::FP32_TO_INT64_IN_MEM:
10517 case X86::FP64_TO_INT16_IN_MEM:
10518 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000010519 case X86::FP64_TO_INT64_IN_MEM:
10520 case X86::FP80_TO_INT16_IN_MEM:
10521 case X86::FP80_TO_INT32_IN_MEM:
10522 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000010523 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10524 DebugLoc DL = MI->getDebugLoc();
10525
Evan Cheng60c07e12006-07-05 22:17:51 +000010526 // Change the floating point control register to use "round towards zero"
10527 // mode when truncating to an integer value.
10528 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000010529 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000010530 addFrameReference(BuildMI(*BB, MI, DL,
10531 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010532
10533 // Load the old value of the high byte of the control word...
10534 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +000010535 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohman14152b42010-07-06 20:24:04 +000010536 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000010537 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010538
10539 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000010540 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000010541 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000010542
10543 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000010544 addFrameReference(BuildMI(*BB, MI, DL,
10545 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010546
10547 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000010548 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000010549 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000010550
10551 // Get the X86 opcode to use.
10552 unsigned Opc;
10553 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010554 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000010555 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
10556 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
10557 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
10558 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
10559 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
10560 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000010561 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
10562 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
10563 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000010564 }
10565
10566 X86AddressMode AM;
10567 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000010568 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000010569 AM.BaseType = X86AddressMode::RegBase;
10570 AM.Base.Reg = Op.getReg();
10571 } else {
10572 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000010573 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000010574 }
10575 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000010576 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000010577 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010578 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000010579 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000010580 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010581 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000010582 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000010583 AM.GV = Op.getGlobal();
10584 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000010585 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010586 }
Dan Gohman14152b42010-07-06 20:24:04 +000010587 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010588 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000010589
10590 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000010591 addFrameReference(BuildMI(*BB, MI, DL,
10592 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010593
Dan Gohman14152b42010-07-06 20:24:04 +000010594 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000010595 return BB;
10596 }
Eric Christopherb120ab42009-08-18 22:50:32 +000010597 // String/text processing lowering.
10598 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010599 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000010600 return EmitPCMP(MI, BB, 3, false /* in-mem */);
10601 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010602 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000010603 return EmitPCMP(MI, BB, 3, true /* in-mem */);
10604 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010605 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000010606 return EmitPCMP(MI, BB, 5, false /* in mem */);
10607 case X86::PCMPESTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010608 case X86::VPCMPESTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000010609 return EmitPCMP(MI, BB, 5, true /* in mem */);
10610
Eric Christopher228232b2010-11-30 07:20:12 +000010611 // Thread synchronization.
10612 case X86::MONITOR:
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010613 return EmitMonitor(MI, BB);
Eric Christopher228232b2010-11-30 07:20:12 +000010614 case X86::MWAIT:
10615 return EmitMwait(MI, BB);
10616
Eric Christopherb120ab42009-08-18 22:50:32 +000010617 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +000010618 case X86::ATOMAND32:
10619 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010620 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010621 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010622 X86::NOT32r, X86::EAX,
10623 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000010624 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +000010625 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
10626 X86::OR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010627 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010628 X86::NOT32r, X86::EAX,
10629 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000010630 case X86::ATOMXOR32:
10631 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010632 X86::XOR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010633 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010634 X86::NOT32r, X86::EAX,
10635 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010636 case X86::ATOMNAND32:
10637 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010638 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010639 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010640 X86::NOT32r, X86::EAX,
10641 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +000010642 case X86::ATOMMIN32:
10643 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
10644 case X86::ATOMMAX32:
10645 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
10646 case X86::ATOMUMIN32:
10647 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
10648 case X86::ATOMUMAX32:
10649 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +000010650
10651 case X86::ATOMAND16:
10652 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
10653 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010654 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010655 X86::NOT16r, X86::AX,
10656 X86::GR16RegisterClass);
10657 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +000010658 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010659 X86::OR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010660 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010661 X86::NOT16r, X86::AX,
10662 X86::GR16RegisterClass);
10663 case X86::ATOMXOR16:
10664 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
10665 X86::XOR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010666 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010667 X86::NOT16r, X86::AX,
10668 X86::GR16RegisterClass);
10669 case X86::ATOMNAND16:
10670 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
10671 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010672 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010673 X86::NOT16r, X86::AX,
10674 X86::GR16RegisterClass, true);
10675 case X86::ATOMMIN16:
10676 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
10677 case X86::ATOMMAX16:
10678 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
10679 case X86::ATOMUMIN16:
10680 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
10681 case X86::ATOMUMAX16:
10682 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
10683
10684 case X86::ATOMAND8:
10685 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
10686 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010687 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010688 X86::NOT8r, X86::AL,
10689 X86::GR8RegisterClass);
10690 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +000010691 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010692 X86::OR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010693 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010694 X86::NOT8r, X86::AL,
10695 X86::GR8RegisterClass);
10696 case X86::ATOMXOR8:
10697 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
10698 X86::XOR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010699 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010700 X86::NOT8r, X86::AL,
10701 X86::GR8RegisterClass);
10702 case X86::ATOMNAND8:
10703 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
10704 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010705 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010706 X86::NOT8r, X86::AL,
10707 X86::GR8RegisterClass, true);
10708 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010709 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +000010710 case X86::ATOMAND64:
10711 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010712 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010713 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010714 X86::NOT64r, X86::RAX,
10715 X86::GR64RegisterClass);
10716 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +000010717 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
10718 X86::OR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010719 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010720 X86::NOT64r, X86::RAX,
10721 X86::GR64RegisterClass);
10722 case X86::ATOMXOR64:
10723 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010724 X86::XOR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010725 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010726 X86::NOT64r, X86::RAX,
10727 X86::GR64RegisterClass);
10728 case X86::ATOMNAND64:
10729 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
10730 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010731 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010732 X86::NOT64r, X86::RAX,
10733 X86::GR64RegisterClass, true);
10734 case X86::ATOMMIN64:
10735 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
10736 case X86::ATOMMAX64:
10737 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
10738 case X86::ATOMUMIN64:
10739 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
10740 case X86::ATOMUMAX64:
10741 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010742
10743 // This group does 64-bit operations on a 32-bit host.
10744 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010745 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010746 X86::AND32rr, X86::AND32rr,
10747 X86::AND32ri, X86::AND32ri,
10748 false);
10749 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010750 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010751 X86::OR32rr, X86::OR32rr,
10752 X86::OR32ri, X86::OR32ri,
10753 false);
10754 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010755 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010756 X86::XOR32rr, X86::XOR32rr,
10757 X86::XOR32ri, X86::XOR32ri,
10758 false);
10759 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010760 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010761 X86::AND32rr, X86::AND32rr,
10762 X86::AND32ri, X86::AND32ri,
10763 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010764 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010765 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010766 X86::ADD32rr, X86::ADC32rr,
10767 X86::ADD32ri, X86::ADC32ri,
10768 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010769 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010770 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010771 X86::SUB32rr, X86::SBB32rr,
10772 X86::SUB32ri, X86::SBB32ri,
10773 false);
Dale Johannesen880ae362008-10-03 22:25:52 +000010774 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010775 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +000010776 X86::MOV32rr, X86::MOV32rr,
10777 X86::MOV32ri, X86::MOV32ri,
10778 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010779 case X86::VASTART_SAVE_XMM_REGS:
10780 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000010781
10782 case X86::VAARG_64:
10783 return EmitVAARG64WithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000010784 }
10785}
10786
10787//===----------------------------------------------------------------------===//
10788// X86 Optimization Hooks
10789//===----------------------------------------------------------------------===//
10790
Dan Gohman475871a2008-07-27 21:46:04 +000010791void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +000010792 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000010793 APInt &KnownZero,
10794 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000010795 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000010796 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010797 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000010798 assert((Opc >= ISD::BUILTIN_OP_END ||
10799 Opc == ISD::INTRINSIC_WO_CHAIN ||
10800 Opc == ISD::INTRINSIC_W_CHAIN ||
10801 Opc == ISD::INTRINSIC_VOID) &&
10802 "Should use MaskedValueIsZero if you don't know whether Op"
10803 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010804
Dan Gohmanf4f92f52008-02-13 23:07:24 +000010805 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010806 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000010807 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000010808 case X86ISD::ADD:
10809 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000010810 case X86ISD::ADC:
10811 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000010812 case X86ISD::SMUL:
10813 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000010814 case X86ISD::INC:
10815 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000010816 case X86ISD::OR:
10817 case X86ISD::XOR:
10818 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000010819 // These nodes' second result is a boolean.
10820 if (Op.getResNo() == 0)
10821 break;
10822 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010823 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000010824 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
10825 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000010826 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010827 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010828}
Chris Lattner259e97c2006-01-31 19:43:35 +000010829
Owen Andersonbc146b02010-09-21 20:42:50 +000010830unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
10831 unsigned Depth) const {
10832 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
10833 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
10834 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000010835
Owen Andersonbc146b02010-09-21 20:42:50 +000010836 // Fallback case.
10837 return 1;
10838}
10839
Evan Cheng206ee9d2006-07-07 08:33:52 +000010840/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000010841/// node is a GlobalAddress + offset.
10842bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000010843 const GlobalValue* &GA,
10844 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000010845 if (N->getOpcode() == X86ISD::Wrapper) {
10846 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000010847 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000010848 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000010849 return true;
10850 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000010851 }
Evan Chengad4196b2008-05-12 19:56:52 +000010852 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000010853}
10854
Evan Cheng206ee9d2006-07-07 08:33:52 +000010855/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
10856/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
10857/// if the load addresses are consecutive, non-overlapping, and in the right
Nate Begemanfdea31a2010-03-24 20:49:50 +000010858/// order.
Dan Gohman475871a2008-07-27 21:46:04 +000010859static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Mon P Wanga0fd0d52010-12-19 23:55:53 +000010860 TargetLowering::DAGCombinerInfo &DCI) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000010861 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000010862 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000010863
Eli Friedman7a5e5552009-06-07 06:52:44 +000010864 if (VT.getSizeInBits() != 128)
10865 return SDValue();
10866
Mon P Wanga0fd0d52010-12-19 23:55:53 +000010867 // Don't create instructions with illegal types after legalize types has run.
10868 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10869 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
10870 return SDValue();
10871
Nate Begemanfdea31a2010-03-24 20:49:50 +000010872 SmallVector<SDValue, 16> Elts;
10873 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000010874 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000010875
Nate Begemanfdea31a2010-03-24 20:49:50 +000010876 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000010877}
Evan Chengd880b972008-05-09 21:53:03 +000010878
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000010879/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
10880/// generation and convert it from being a bunch of shuffles and extracts
10881/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010882static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
10883 const TargetLowering &TLI) {
10884 SDValue InputVector = N->getOperand(0);
10885
10886 // Only operate on vectors of 4 elements, where the alternative shuffling
10887 // gets to be more expensive.
10888 if (InputVector.getValueType() != MVT::v4i32)
10889 return SDValue();
10890
10891 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
10892 // single use which is a sign-extend or zero-extend, and all elements are
10893 // used.
10894 SmallVector<SDNode *, 4> Uses;
10895 unsigned ExtractedElements = 0;
10896 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
10897 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
10898 if (UI.getUse().getResNo() != InputVector.getResNo())
10899 return SDValue();
10900
10901 SDNode *Extract = *UI;
10902 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
10903 return SDValue();
10904
10905 if (Extract->getValueType(0) != MVT::i32)
10906 return SDValue();
10907 if (!Extract->hasOneUse())
10908 return SDValue();
10909 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
10910 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
10911 return SDValue();
10912 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
10913 return SDValue();
10914
10915 // Record which element was extracted.
10916 ExtractedElements |=
10917 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
10918
10919 Uses.push_back(Extract);
10920 }
10921
10922 // If not all the elements were used, this may not be worthwhile.
10923 if (ExtractedElements != 15)
10924 return SDValue();
10925
10926 // Ok, we've now decided to do the transformation.
10927 DebugLoc dl = InputVector.getDebugLoc();
10928
10929 // Store the value to a temporary stack slot.
10930 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000010931 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
10932 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010933
10934 // Replace each use (extract) with a load of the appropriate element.
10935 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
10936 UE = Uses.end(); UI != UE; ++UI) {
10937 SDNode *Extract = *UI;
10938
10939 // Compute the element's address.
10940 SDValue Idx = Extract->getOperand(1);
10941 unsigned EltSize =
10942 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
10943 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
10944 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
10945
Eric Christopher90eb4022010-07-22 00:26:08 +000010946 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(),
Chris Lattner51abfe42010-09-21 06:02:19 +000010947 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010948
10949 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000010950 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000010951 ScalarAddr, MachinePointerInfo(),
10952 false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010953
10954 // Replace the exact with the load.
10955 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
10956 }
10957
10958 // The replacement was made in place; don't return anything.
10959 return SDValue();
10960}
10961
Chris Lattner83e6c992006-10-04 06:57:07 +000010962/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000010963static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +000010964 const X86Subtarget *Subtarget) {
10965 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000010966 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000010967 // Get the LHS/RHS of the select.
10968 SDValue LHS = N->getOperand(1);
10969 SDValue RHS = N->getOperand(2);
Eric Christopherfd179292009-08-27 18:07:15 +000010970
Dan Gohman670e5392009-09-21 18:03:22 +000010971 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000010972 // instructions match the semantics of the common C idiom x<y?x:y but not
10973 // x<=y?x:y, because of how they handle negative zero (which can be
10974 // ignored in unsafe-math mode).
Chris Lattner83e6c992006-10-04 06:57:07 +000010975 if (Subtarget->hasSSE2() &&
Owen Anderson825b72b2009-08-11 20:47:22 +000010976 (LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64) &&
Chris Lattner47b4ce82009-03-11 05:48:52 +000010977 Cond.getOpcode() == ISD::SETCC) {
10978 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010979
Chris Lattner47b4ce82009-03-11 05:48:52 +000010980 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000010981 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000010982 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
10983 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000010984 switch (CC) {
10985 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000010986 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000010987 // Converting this to a min would handle NaNs incorrectly, and swapping
10988 // the operands would cause it to handle comparisons between positive
10989 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000010990 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000010991 if (!UnsafeFPMath &&
10992 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
10993 break;
10994 std::swap(LHS, RHS);
10995 }
Dan Gohman670e5392009-09-21 18:03:22 +000010996 Opcode = X86ISD::FMIN;
10997 break;
10998 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000010999 // Converting this to a min would handle comparisons between positive
11000 // and negative zero incorrectly.
11001 if (!UnsafeFPMath &&
11002 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
11003 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011004 Opcode = X86ISD::FMIN;
11005 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000011006 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000011007 // Converting this to a min would handle both negative zeros and NaNs
11008 // incorrectly, but we can swap the operands to fix both.
11009 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000011010 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011011 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000011012 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011013 Opcode = X86ISD::FMIN;
11014 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011015
Dan Gohman670e5392009-09-21 18:03:22 +000011016 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000011017 // Converting this to a max would handle comparisons between positive
11018 // and negative zero incorrectly.
11019 if (!UnsafeFPMath &&
11020 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(LHS))
11021 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011022 Opcode = X86ISD::FMAX;
11023 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000011024 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000011025 // Converting this to a max would handle NaNs incorrectly, and swapping
11026 // the operands would cause it to handle comparisons between positive
11027 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000011028 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000011029 if (!UnsafeFPMath &&
11030 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
11031 break;
11032 std::swap(LHS, RHS);
11033 }
Dan Gohman670e5392009-09-21 18:03:22 +000011034 Opcode = X86ISD::FMAX;
11035 break;
11036 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000011037 // Converting this to a max would handle both negative zeros and NaNs
11038 // incorrectly, but we can swap the operands to fix both.
11039 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000011040 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011041 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011042 case ISD::SETGE:
11043 Opcode = X86ISD::FMAX;
11044 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000011045 }
Dan Gohman670e5392009-09-21 18:03:22 +000011046 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000011047 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
11048 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000011049 switch (CC) {
11050 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000011051 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000011052 // Converting this to a min would handle comparisons between positive
11053 // and negative zero incorrectly, and swapping the operands would
11054 // cause it to handle NaNs incorrectly.
11055 if (!UnsafeFPMath &&
11056 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000011057 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000011058 break;
11059 std::swap(LHS, RHS);
11060 }
Dan Gohman670e5392009-09-21 18:03:22 +000011061 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000011062 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011063 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000011064 // Converting this to a min would handle NaNs incorrectly.
11065 if (!UnsafeFPMath &&
11066 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
11067 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011068 Opcode = X86ISD::FMIN;
11069 break;
11070 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000011071 // Converting this to a min would handle both negative zeros and NaNs
11072 // incorrectly, but we can swap the operands to fix both.
11073 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000011074 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011075 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011076 case ISD::SETGE:
11077 Opcode = X86ISD::FMIN;
11078 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011079
Dan Gohman670e5392009-09-21 18:03:22 +000011080 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000011081 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000011082 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000011083 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011084 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000011085 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011086 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000011087 // Converting this to a max would handle comparisons between positive
11088 // and negative zero incorrectly, and swapping the operands would
11089 // cause it to handle NaNs incorrectly.
11090 if (!UnsafeFPMath &&
11091 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000011092 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000011093 break;
11094 std::swap(LHS, RHS);
11095 }
Dan Gohman670e5392009-09-21 18:03:22 +000011096 Opcode = X86ISD::FMAX;
11097 break;
11098 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000011099 // Converting this to a max would handle both negative zeros and NaNs
11100 // incorrectly, but we can swap the operands to fix both.
11101 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000011102 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011103 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000011104 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011105 Opcode = X86ISD::FMAX;
11106 break;
11107 }
Chris Lattner83e6c992006-10-04 06:57:07 +000011108 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011109
Chris Lattner47b4ce82009-03-11 05:48:52 +000011110 if (Opcode)
11111 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000011112 }
Eric Christopherfd179292009-08-27 18:07:15 +000011113
Chris Lattnerd1980a52009-03-12 06:52:53 +000011114 // If this is a select between two integer constants, try to do some
11115 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000011116 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
11117 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000011118 // Don't do this for crazy integer types.
11119 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
11120 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000011121 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000011122 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000011123
Chris Lattnercee56e72009-03-13 05:53:31 +000011124 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000011125 // Efficiently invertible.
11126 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
11127 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
11128 isa<ConstantSDNode>(Cond.getOperand(1))))) {
11129 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000011130 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000011131 }
Eric Christopherfd179292009-08-27 18:07:15 +000011132
Chris Lattnerd1980a52009-03-12 06:52:53 +000011133 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000011134 if (FalseC->getAPIntValue() == 0 &&
11135 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000011136 if (NeedsCondInvert) // Invert the condition if needed.
11137 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
11138 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011139
Chris Lattnerd1980a52009-03-12 06:52:53 +000011140 // Zero extend the condition if needed.
11141 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011142
Chris Lattnercee56e72009-03-13 05:53:31 +000011143 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000011144 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000011145 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000011146 }
Eric Christopherfd179292009-08-27 18:07:15 +000011147
Chris Lattner97a29a52009-03-13 05:22:11 +000011148 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000011149 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000011150 if (NeedsCondInvert) // Invert the condition if needed.
11151 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
11152 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011153
Chris Lattner97a29a52009-03-13 05:22:11 +000011154 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000011155 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
11156 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000011157 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000011158 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000011159 }
Eric Christopherfd179292009-08-27 18:07:15 +000011160
Chris Lattnercee56e72009-03-13 05:53:31 +000011161 // Optimize cases that will turn into an LEA instruction. This requires
11162 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000011163 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000011164 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000011165 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000011166
Chris Lattnercee56e72009-03-13 05:53:31 +000011167 bool isFastMultiplier = false;
11168 if (Diff < 10) {
11169 switch ((unsigned char)Diff) {
11170 default: break;
11171 case 1: // result = add base, cond
11172 case 2: // result = lea base( , cond*2)
11173 case 3: // result = lea base(cond, cond*2)
11174 case 4: // result = lea base( , cond*4)
11175 case 5: // result = lea base(cond, cond*4)
11176 case 8: // result = lea base( , cond*8)
11177 case 9: // result = lea base(cond, cond*8)
11178 isFastMultiplier = true;
11179 break;
11180 }
11181 }
Eric Christopherfd179292009-08-27 18:07:15 +000011182
Chris Lattnercee56e72009-03-13 05:53:31 +000011183 if (isFastMultiplier) {
11184 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
11185 if (NeedsCondInvert) // Invert the condition if needed.
11186 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
11187 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011188
Chris Lattnercee56e72009-03-13 05:53:31 +000011189 // Zero extend the condition if needed.
11190 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
11191 Cond);
11192 // Scale the condition by the difference.
11193 if (Diff != 1)
11194 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
11195 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011196
Chris Lattnercee56e72009-03-13 05:53:31 +000011197 // Add the base if non-zero.
11198 if (FalseC->getAPIntValue() != 0)
11199 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
11200 SDValue(FalseC, 0));
11201 return Cond;
11202 }
Eric Christopherfd179292009-08-27 18:07:15 +000011203 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000011204 }
11205 }
Eric Christopherfd179292009-08-27 18:07:15 +000011206
Dan Gohman475871a2008-07-27 21:46:04 +000011207 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000011208}
11209
Chris Lattnerd1980a52009-03-12 06:52:53 +000011210/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
11211static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
11212 TargetLowering::DAGCombinerInfo &DCI) {
11213 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000011214
Chris Lattnerd1980a52009-03-12 06:52:53 +000011215 // If the flag operand isn't dead, don't touch this CMOV.
11216 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
11217 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000011218
Chris Lattnerd1980a52009-03-12 06:52:53 +000011219 // If this is a select between two integer constants, try to do some
11220 // optimizations. Note that the operands are ordered the opposite of SELECT
11221 // operands.
11222 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
11223 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
11224 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
11225 // larger than FalseC (the false value).
11226 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
Eric Christopherfd179292009-08-27 18:07:15 +000011227
Chris Lattnerd1980a52009-03-12 06:52:53 +000011228 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
11229 CC = X86::GetOppositeBranchCondition(CC);
11230 std::swap(TrueC, FalseC);
11231 }
Eric Christopherfd179292009-08-27 18:07:15 +000011232
Chris Lattnerd1980a52009-03-12 06:52:53 +000011233 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000011234 // This is efficient for any integer data type (including i8/i16) and
11235 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000011236 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
11237 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +000011238 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11239 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011240
Chris Lattnerd1980a52009-03-12 06:52:53 +000011241 // Zero extend the condition if needed.
11242 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011243
Chris Lattnerd1980a52009-03-12 06:52:53 +000011244 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
11245 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000011246 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000011247 if (N->getNumValues() == 2) // Dead flag value?
11248 return DCI.CombineTo(N, Cond, SDValue());
11249 return Cond;
11250 }
Eric Christopherfd179292009-08-27 18:07:15 +000011251
Chris Lattnercee56e72009-03-13 05:53:31 +000011252 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
11253 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000011254 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
11255 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +000011256 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11257 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011258
Chris Lattner97a29a52009-03-13 05:22:11 +000011259 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000011260 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
11261 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000011262 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
11263 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000011264
Chris Lattner97a29a52009-03-13 05:22:11 +000011265 if (N->getNumValues() == 2) // Dead flag value?
11266 return DCI.CombineTo(N, Cond, SDValue());
11267 return Cond;
11268 }
Eric Christopherfd179292009-08-27 18:07:15 +000011269
Chris Lattnercee56e72009-03-13 05:53:31 +000011270 // Optimize cases that will turn into an LEA instruction. This requires
11271 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000011272 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000011273 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000011274 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000011275
Chris Lattnercee56e72009-03-13 05:53:31 +000011276 bool isFastMultiplier = false;
11277 if (Diff < 10) {
11278 switch ((unsigned char)Diff) {
11279 default: break;
11280 case 1: // result = add base, cond
11281 case 2: // result = lea base( , cond*2)
11282 case 3: // result = lea base(cond, cond*2)
11283 case 4: // result = lea base( , cond*4)
11284 case 5: // result = lea base(cond, cond*4)
11285 case 8: // result = lea base( , cond*8)
11286 case 9: // result = lea base(cond, cond*8)
11287 isFastMultiplier = true;
11288 break;
11289 }
11290 }
Eric Christopherfd179292009-08-27 18:07:15 +000011291
Chris Lattnercee56e72009-03-13 05:53:31 +000011292 if (isFastMultiplier) {
11293 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
11294 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +000011295 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11296 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000011297 // Zero extend the condition if needed.
11298 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
11299 Cond);
11300 // Scale the condition by the difference.
11301 if (Diff != 1)
11302 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
11303 DAG.getConstant(Diff, Cond.getValueType()));
11304
11305 // Add the base if non-zero.
11306 if (FalseC->getAPIntValue() != 0)
11307 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
11308 SDValue(FalseC, 0));
11309 if (N->getNumValues() == 2) // Dead flag value?
11310 return DCI.CombineTo(N, Cond, SDValue());
11311 return Cond;
11312 }
Eric Christopherfd179292009-08-27 18:07:15 +000011313 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000011314 }
11315 }
11316 return SDValue();
11317}
11318
11319
Evan Cheng0b0cd912009-03-28 05:57:29 +000011320/// PerformMulCombine - Optimize a single multiply with constant into two
11321/// in order to implement it with two cheaper instructions, e.g.
11322/// LEA + SHL, LEA + LEA.
11323static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
11324 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000011325 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
11326 return SDValue();
11327
Owen Andersone50ed302009-08-10 22:56:29 +000011328 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000011329 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000011330 return SDValue();
11331
11332 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
11333 if (!C)
11334 return SDValue();
11335 uint64_t MulAmt = C->getZExtValue();
11336 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
11337 return SDValue();
11338
11339 uint64_t MulAmt1 = 0;
11340 uint64_t MulAmt2 = 0;
11341 if ((MulAmt % 9) == 0) {
11342 MulAmt1 = 9;
11343 MulAmt2 = MulAmt / 9;
11344 } else if ((MulAmt % 5) == 0) {
11345 MulAmt1 = 5;
11346 MulAmt2 = MulAmt / 5;
11347 } else if ((MulAmt % 3) == 0) {
11348 MulAmt1 = 3;
11349 MulAmt2 = MulAmt / 3;
11350 }
11351 if (MulAmt2 &&
11352 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
11353 DebugLoc DL = N->getDebugLoc();
11354
11355 if (isPowerOf2_64(MulAmt2) &&
11356 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
11357 // If second multiplifer is pow2, issue it first. We want the multiply by
11358 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
11359 // is an add.
11360 std::swap(MulAmt1, MulAmt2);
11361
11362 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000011363 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000011364 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000011365 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000011366 else
Evan Cheng73f24c92009-03-30 21:36:47 +000011367 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000011368 DAG.getConstant(MulAmt1, VT));
11369
Eric Christopherfd179292009-08-27 18:07:15 +000011370 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000011371 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000011372 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000011373 else
Evan Cheng73f24c92009-03-30 21:36:47 +000011374 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000011375 DAG.getConstant(MulAmt2, VT));
11376
11377 // Do not add new nodes to DAG combiner worklist.
11378 DCI.CombineTo(N, NewMul, false);
11379 }
11380 return SDValue();
11381}
11382
Evan Chengad9c0a32009-12-15 00:53:42 +000011383static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
11384 SDValue N0 = N->getOperand(0);
11385 SDValue N1 = N->getOperand(1);
11386 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
11387 EVT VT = N0.getValueType();
11388
11389 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
11390 // since the result of setcc_c is all zero's or all ones.
11391 if (N1C && N0.getOpcode() == ISD::AND &&
11392 N0.getOperand(1).getOpcode() == ISD::Constant) {
11393 SDValue N00 = N0.getOperand(0);
11394 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
11395 ((N00.getOpcode() == ISD::ANY_EXTEND ||
11396 N00.getOpcode() == ISD::ZERO_EXTEND) &&
11397 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
11398 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
11399 APInt ShAmt = N1C->getAPIntValue();
11400 Mask = Mask.shl(ShAmt);
11401 if (Mask != 0)
11402 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
11403 N00, DAG.getConstant(Mask, VT));
11404 }
11405 }
11406
11407 return SDValue();
11408}
Evan Cheng0b0cd912009-03-28 05:57:29 +000011409
Nate Begeman740ab032009-01-26 00:52:55 +000011410/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
11411/// when possible.
11412static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
11413 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000011414 EVT VT = N->getValueType(0);
11415 if (!VT.isVector() && VT.isInteger() &&
11416 N->getOpcode() == ISD::SHL)
11417 return PerformSHLCombine(N, DAG);
11418
Nate Begeman740ab032009-01-26 00:52:55 +000011419 // On X86 with SSE2 support, we can transform this to a vector shift if
11420 // all elements are shifted by the same amount. We can't do this in legalize
11421 // because the a constant vector is typically transformed to a constant pool
11422 // so we have no knowledge of the shift amount.
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011423 if (!Subtarget->hasSSE2())
11424 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000011425
Owen Anderson825b72b2009-08-11 20:47:22 +000011426 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011427 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000011428
Mon P Wang3becd092009-01-28 08:12:05 +000011429 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000011430 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000011431 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000011432 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000011433 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
11434 unsigned NumElts = VT.getVectorNumElements();
11435 unsigned i = 0;
11436 for (; i != NumElts; ++i) {
11437 SDValue Arg = ShAmtOp.getOperand(i);
11438 if (Arg.getOpcode() == ISD::UNDEF) continue;
11439 BaseShAmt = Arg;
11440 break;
11441 }
11442 for (; i != NumElts; ++i) {
11443 SDValue Arg = ShAmtOp.getOperand(i);
11444 if (Arg.getOpcode() == ISD::UNDEF) continue;
11445 if (Arg != BaseShAmt) {
11446 return SDValue();
11447 }
11448 }
11449 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000011450 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000011451 SDValue InVec = ShAmtOp.getOperand(0);
11452 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
11453 unsigned NumElts = InVec.getValueType().getVectorNumElements();
11454 unsigned i = 0;
11455 for (; i != NumElts; ++i) {
11456 SDValue Arg = InVec.getOperand(i);
11457 if (Arg.getOpcode() == ISD::UNDEF) continue;
11458 BaseShAmt = Arg;
11459 break;
11460 }
11461 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
11462 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000011463 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000011464 if (C->getZExtValue() == SplatIdx)
11465 BaseShAmt = InVec.getOperand(1);
11466 }
11467 }
11468 if (BaseShAmt.getNode() == 0)
11469 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
11470 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +000011471 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011472 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000011473
Mon P Wangefa42202009-09-03 19:56:25 +000011474 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000011475 if (EltVT.bitsGT(MVT::i32))
11476 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
11477 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000011478 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000011479
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011480 // The shift amount is identical so we can do a vector shift.
11481 SDValue ValOp = N->getOperand(0);
11482 switch (N->getOpcode()) {
11483 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000011484 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011485 break;
11486 case ISD::SHL:
Owen Anderson825b72b2009-08-11 20:47:22 +000011487 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011488 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011489 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011490 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011491 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011492 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011493 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011494 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011495 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011496 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011497 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011498 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011499 break;
11500 case ISD::SRA:
Owen Anderson825b72b2009-08-11 20:47:22 +000011501 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011502 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011503 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011504 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011505 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011506 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011507 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011508 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011509 break;
11510 case ISD::SRL:
Owen Anderson825b72b2009-08-11 20:47:22 +000011511 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011512 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011513 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011514 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011515 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011516 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011517 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011518 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011519 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011520 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011521 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011522 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011523 break;
Nate Begeman740ab032009-01-26 00:52:55 +000011524 }
11525 return SDValue();
11526}
11527
Nate Begemanb65c1752010-12-17 22:55:37 +000011528
11529static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
11530 TargetLowering::DAGCombinerInfo &DCI,
11531 const X86Subtarget *Subtarget) {
11532 if (DCI.isBeforeLegalizeOps())
11533 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011534
Nate Begemanb65c1752010-12-17 22:55:37 +000011535 // Want to form PANDN nodes, in the hopes of then easily combining them with
11536 // OR and AND nodes to form PBLEND/PSIGN.
11537 EVT VT = N->getValueType(0);
11538 if (VT != MVT::v2i64)
11539 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011540
Nate Begemanb65c1752010-12-17 22:55:37 +000011541 SDValue N0 = N->getOperand(0);
11542 SDValue N1 = N->getOperand(1);
11543 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011544
Nate Begemanb65c1752010-12-17 22:55:37 +000011545 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011546 if (N0.getOpcode() == ISD::XOR &&
Nate Begemanb65c1752010-12-17 22:55:37 +000011547 ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
11548 return DAG.getNode(X86ISD::PANDN, DL, VT, N0.getOperand(0), N1);
11549
11550 // Check RHS for vnot
11551 if (N1.getOpcode() == ISD::XOR &&
11552 ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
11553 return DAG.getNode(X86ISD::PANDN, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011554
Nate Begemanb65c1752010-12-17 22:55:37 +000011555 return SDValue();
11556}
11557
Evan Cheng760d1942010-01-04 21:22:48 +000011558static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000011559 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000011560 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +000011561 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000011562 return SDValue();
11563
Evan Cheng760d1942010-01-04 21:22:48 +000011564 EVT VT = N->getValueType(0);
Nate Begemanb65c1752010-12-17 22:55:37 +000011565 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64 && VT != MVT::v2i64)
Evan Cheng760d1942010-01-04 21:22:48 +000011566 return SDValue();
11567
Evan Cheng760d1942010-01-04 21:22:48 +000011568 SDValue N0 = N->getOperand(0);
11569 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011570
Nate Begemanb65c1752010-12-17 22:55:37 +000011571 // look for psign/blend
11572 if (Subtarget->hasSSSE3()) {
11573 if (VT == MVT::v2i64) {
11574 // Canonicalize pandn to RHS
11575 if (N0.getOpcode() == X86ISD::PANDN)
11576 std::swap(N0, N1);
11577 // or (and (m, x), (pandn m, y))
11578 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::PANDN) {
11579 SDValue Mask = N1.getOperand(0);
11580 SDValue X = N1.getOperand(1);
11581 SDValue Y;
11582 if (N0.getOperand(0) == Mask)
11583 Y = N0.getOperand(1);
11584 if (N0.getOperand(1) == Mask)
11585 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011586
Nate Begemanb65c1752010-12-17 22:55:37 +000011587 // Check to see if the mask appeared in both the AND and PANDN and
11588 if (!Y.getNode())
11589 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011590
Nate Begemanb65c1752010-12-17 22:55:37 +000011591 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
11592 if (Mask.getOpcode() != ISD::BITCAST ||
11593 X.getOpcode() != ISD::BITCAST ||
11594 Y.getOpcode() != ISD::BITCAST)
11595 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011596
Nate Begemanb65c1752010-12-17 22:55:37 +000011597 // Look through mask bitcast.
11598 Mask = Mask.getOperand(0);
11599 EVT MaskVT = Mask.getValueType();
11600
11601 // Validate that the Mask operand is a vector sra node. The sra node
11602 // will be an intrinsic.
11603 if (Mask.getOpcode() != ISD::INTRINSIC_WO_CHAIN)
11604 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011605
Nate Begemanb65c1752010-12-17 22:55:37 +000011606 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
11607 // there is no psrai.b
11608 switch (cast<ConstantSDNode>(Mask.getOperand(0))->getZExtValue()) {
11609 case Intrinsic::x86_sse2_psrai_w:
11610 case Intrinsic::x86_sse2_psrai_d:
11611 break;
11612 default: return SDValue();
11613 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011614
Nate Begemanb65c1752010-12-17 22:55:37 +000011615 // Check that the SRA is all signbits.
11616 SDValue SraC = Mask.getOperand(2);
11617 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
11618 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
11619 if ((SraAmt + 1) != EltBits)
11620 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011621
Nate Begemanb65c1752010-12-17 22:55:37 +000011622 DebugLoc DL = N->getDebugLoc();
11623
11624 // Now we know we at least have a plendvb with the mask val. See if
11625 // we can form a psignb/w/d.
11626 // psign = x.type == y.type == mask.type && y = sub(0, x);
11627 X = X.getOperand(0);
11628 Y = Y.getOperand(0);
11629 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
11630 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
11631 X.getValueType() == MaskVT && X.getValueType() == Y.getValueType()){
11632 unsigned Opc = 0;
11633 switch (EltBits) {
11634 case 8: Opc = X86ISD::PSIGNB; break;
11635 case 16: Opc = X86ISD::PSIGNW; break;
11636 case 32: Opc = X86ISD::PSIGND; break;
11637 default: break;
11638 }
11639 if (Opc) {
11640 SDValue Sign = DAG.getNode(Opc, DL, MaskVT, X, Mask.getOperand(1));
11641 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Sign);
11642 }
11643 }
11644 // PBLENDVB only available on SSE 4.1
11645 if (!Subtarget->hasSSE41())
11646 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011647
Nate Begemanb65c1752010-12-17 22:55:37 +000011648 X = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, X);
11649 Y = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Y);
11650 Mask = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Mask);
Nate Begeman672fb622010-12-20 22:04:24 +000011651 Mask = DAG.getNode(X86ISD::PBLENDVB, DL, MVT::v16i8, X, Y, Mask);
Nate Begemanb65c1752010-12-17 22:55:37 +000011652 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Mask);
11653 }
11654 }
11655 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011656
Nate Begemanb65c1752010-12-17 22:55:37 +000011657 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000011658 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
11659 std::swap(N0, N1);
11660 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
11661 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000011662 if (!N0.hasOneUse() || !N1.hasOneUse())
11663 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000011664
11665 SDValue ShAmt0 = N0.getOperand(1);
11666 if (ShAmt0.getValueType() != MVT::i8)
11667 return SDValue();
11668 SDValue ShAmt1 = N1.getOperand(1);
11669 if (ShAmt1.getValueType() != MVT::i8)
11670 return SDValue();
11671 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
11672 ShAmt0 = ShAmt0.getOperand(0);
11673 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
11674 ShAmt1 = ShAmt1.getOperand(0);
11675
11676 DebugLoc DL = N->getDebugLoc();
11677 unsigned Opc = X86ISD::SHLD;
11678 SDValue Op0 = N0.getOperand(0);
11679 SDValue Op1 = N1.getOperand(0);
11680 if (ShAmt0.getOpcode() == ISD::SUB) {
11681 Opc = X86ISD::SHRD;
11682 std::swap(Op0, Op1);
11683 std::swap(ShAmt0, ShAmt1);
11684 }
11685
Evan Cheng8b1190a2010-04-28 01:18:01 +000011686 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000011687 if (ShAmt1.getOpcode() == ISD::SUB) {
11688 SDValue Sum = ShAmt1.getOperand(0);
11689 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000011690 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
11691 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
11692 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
11693 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000011694 return DAG.getNode(Opc, DL, VT,
11695 Op0, Op1,
11696 DAG.getNode(ISD::TRUNCATE, DL,
11697 MVT::i8, ShAmt0));
11698 }
11699 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
11700 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
11701 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000011702 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000011703 return DAG.getNode(Opc, DL, VT,
11704 N0.getOperand(0), N1.getOperand(0),
11705 DAG.getNode(ISD::TRUNCATE, DL,
11706 MVT::i8, ShAmt0));
11707 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011708
Evan Cheng760d1942010-01-04 21:22:48 +000011709 return SDValue();
11710}
11711
Chris Lattner149a4e52008-02-22 02:09:43 +000011712/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000011713static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000011714 const X86Subtarget *Subtarget) {
Chris Lattner149a4e52008-02-22 02:09:43 +000011715 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
11716 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000011717 // A preferable solution to the general problem is to figure out the right
11718 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000011719
11720 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng7e2ff772008-05-08 00:57:18 +000011721 StoreSDNode *St = cast<StoreSDNode>(N);
Owen Andersone50ed302009-08-10 22:56:29 +000011722 EVT VT = St->getValue().getValueType();
Evan Cheng536e6672009-03-12 05:59:15 +000011723 if (VT.getSizeInBits() != 64)
11724 return SDValue();
11725
Devang Patel578efa92009-06-05 21:57:13 +000011726 const Function *F = DAG.getMachineFunction().getFunction();
11727 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Eric Christopherfd179292009-08-27 18:07:15 +000011728 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
Devang Patel578efa92009-06-05 21:57:13 +000011729 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +000011730 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000011731 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000011732 isa<LoadSDNode>(St->getValue()) &&
11733 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
11734 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000011735 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011736 LoadSDNode *Ld = 0;
11737 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000011738 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000011739 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011740 // Must be a store of a load. We currently handle two cases: the load
11741 // is a direct child, and it's under an intervening TokenFactor. It is
11742 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000011743 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000011744 Ld = cast<LoadSDNode>(St->getChain());
11745 else if (St->getValue().hasOneUse() &&
11746 ChainVal->getOpcode() == ISD::TokenFactor) {
11747 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000011748 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000011749 TokenFactorIndex = i;
11750 Ld = cast<LoadSDNode>(St->getValue());
11751 } else
11752 Ops.push_back(ChainVal->getOperand(i));
11753 }
11754 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000011755
Evan Cheng536e6672009-03-12 05:59:15 +000011756 if (!Ld || !ISD::isNormalLoad(Ld))
11757 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011758
Evan Cheng536e6672009-03-12 05:59:15 +000011759 // If this is not the MMX case, i.e. we are just turning i64 load/store
11760 // into f64 load/store, avoid the transformation if there are multiple
11761 // uses of the loaded value.
11762 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
11763 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011764
Evan Cheng536e6672009-03-12 05:59:15 +000011765 DebugLoc LdDL = Ld->getDebugLoc();
11766 DebugLoc StDL = N->getDebugLoc();
11767 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
11768 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
11769 // pair instead.
11770 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000011771 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000011772 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
11773 Ld->getPointerInfo(), Ld->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000011774 Ld->isNonTemporal(), Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000011775 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000011776 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000011777 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000011778 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000011779 Ops.size());
11780 }
Evan Cheng536e6672009-03-12 05:59:15 +000011781 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000011782 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000011783 St->isVolatile(), St->isNonTemporal(),
11784 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000011785 }
Evan Cheng536e6672009-03-12 05:59:15 +000011786
11787 // Otherwise, lower to two pairs of 32-bit loads / stores.
11788 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000011789 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
11790 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000011791
Owen Anderson825b72b2009-08-11 20:47:22 +000011792 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000011793 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000011794 Ld->isVolatile(), Ld->isNonTemporal(),
11795 Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000011796 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000011797 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000011798 Ld->isVolatile(), Ld->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000011799 MinAlign(Ld->getAlignment(), 4));
11800
11801 SDValue NewChain = LoLd.getValue(1);
11802 if (TokenFactorIndex != -1) {
11803 Ops.push_back(LoLd);
11804 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000011805 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000011806 Ops.size());
11807 }
11808
11809 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000011810 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
11811 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000011812
11813 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000011814 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000011815 St->isVolatile(), St->isNonTemporal(),
11816 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000011817 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000011818 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000011819 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000011820 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000011821 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000011822 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000011823 }
Dan Gohman475871a2008-07-27 21:46:04 +000011824 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000011825}
11826
Chris Lattner6cf73262008-01-25 06:14:17 +000011827/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
11828/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000011829static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000011830 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
11831 // F[X]OR(0.0, x) -> x
11832 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000011833 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
11834 if (C->getValueAPF().isPosZero())
11835 return N->getOperand(1);
11836 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
11837 if (C->getValueAPF().isPosZero())
11838 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000011839 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000011840}
11841
11842/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000011843static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000011844 // FAND(0.0, x) -> 0.0
11845 // FAND(x, 0.0) -> 0.0
11846 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
11847 if (C->getValueAPF().isPosZero())
11848 return N->getOperand(0);
11849 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
11850 if (C->getValueAPF().isPosZero())
11851 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000011852 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000011853}
11854
Dan Gohmane5af2d32009-01-29 01:59:02 +000011855static SDValue PerformBTCombine(SDNode *N,
11856 SelectionDAG &DAG,
11857 TargetLowering::DAGCombinerInfo &DCI) {
11858 // BT ignores high bits in the bit index operand.
11859 SDValue Op1 = N->getOperand(1);
11860 if (Op1.hasOneUse()) {
11861 unsigned BitWidth = Op1.getValueSizeInBits();
11862 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
11863 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000011864 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
11865 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000011866 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000011867 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
11868 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
11869 DCI.CommitTargetLoweringOpt(TLO);
11870 }
11871 return SDValue();
11872}
Chris Lattner83e6c992006-10-04 06:57:07 +000011873
Eli Friedman7a5e5552009-06-07 06:52:44 +000011874static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
11875 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011876 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000011877 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000011878 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000011879 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000011880 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000011881 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011882 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000011883 }
11884 return SDValue();
11885}
11886
Evan Cheng2e489c42009-12-16 00:53:11 +000011887static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
11888 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
11889 // (and (i32 x86isd::setcc_carry), 1)
11890 // This eliminates the zext. This transformation is necessary because
11891 // ISD::SETCC is always legalized to i8.
11892 DebugLoc dl = N->getDebugLoc();
11893 SDValue N0 = N->getOperand(0);
11894 EVT VT = N->getValueType(0);
11895 if (N0.getOpcode() == ISD::AND &&
11896 N0.hasOneUse() &&
11897 N0.getOperand(0).hasOneUse()) {
11898 SDValue N00 = N0.getOperand(0);
11899 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
11900 return SDValue();
11901 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
11902 if (!C || C->getZExtValue() != 1)
11903 return SDValue();
11904 return DAG.getNode(ISD::AND, dl, VT,
11905 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
11906 N00.getOperand(0), N00.getOperand(1)),
11907 DAG.getConstant(1, VT));
11908 }
11909
11910 return SDValue();
11911}
11912
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011913// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
11914static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG) {
11915 unsigned X86CC = N->getConstantOperandVal(0);
11916 SDValue EFLAG = N->getOperand(1);
11917 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011918
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011919 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
11920 // a zext and produces an all-ones bit which is more useful than 0/1 in some
11921 // cases.
11922 if (X86CC == X86::COND_B)
11923 return DAG.getNode(ISD::AND, DL, MVT::i8,
11924 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
11925 DAG.getConstant(X86CC, MVT::i8), EFLAG),
11926 DAG.getConstant(1, MVT::i8));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011927
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011928 return SDValue();
11929}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011930
Chris Lattner23a01992010-12-20 01:37:09 +000011931// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
11932static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
11933 X86TargetLowering::DAGCombinerInfo &DCI) {
11934 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
11935 // the result is either zero or one (depending on the input carry bit).
11936 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
11937 if (X86::isZeroNode(N->getOperand(0)) &&
11938 X86::isZeroNode(N->getOperand(1)) &&
11939 // We don't have a good way to replace an EFLAGS use, so only do this when
11940 // dead right now.
11941 SDValue(N, 1).use_empty()) {
11942 DebugLoc DL = N->getDebugLoc();
11943 EVT VT = N->getValueType(0);
11944 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
11945 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
11946 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
11947 DAG.getConstant(X86::COND_B,MVT::i8),
11948 N->getOperand(2)),
11949 DAG.getConstant(1, VT));
11950 return DCI.CombineTo(N, Res1, CarryOut);
11951 }
11952
11953 return SDValue();
11954}
11955
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000011956// fold (add Y, (sete X, 0)) -> adc 0, Y
11957// (add Y, (setne X, 0)) -> sbb -1, Y
11958// (sub (sete X, 0), Y) -> sbb 0, Y
11959// (sub (setne X, 0), Y) -> adc -1, Y
11960static SDValue OptimizeConditonalInDecrement(SDNode *N, SelectionDAG &DAG) {
11961 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011962
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000011963 // Look through ZExts.
11964 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
11965 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
11966 return SDValue();
11967
11968 SDValue SetCC = Ext.getOperand(0);
11969 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
11970 return SDValue();
11971
11972 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
11973 if (CC != X86::COND_E && CC != X86::COND_NE)
11974 return SDValue();
11975
11976 SDValue Cmp = SetCC.getOperand(1);
11977 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000011978 !X86::isZeroNode(Cmp.getOperand(1)) ||
11979 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000011980 return SDValue();
11981
11982 SDValue CmpOp0 = Cmp.getOperand(0);
11983 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
11984 DAG.getConstant(1, CmpOp0.getValueType()));
11985
11986 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
11987 if (CC == X86::COND_NE)
11988 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
11989 DL, OtherVal.getValueType(), OtherVal,
11990 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
11991 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
11992 DL, OtherVal.getValueType(), OtherVal,
11993 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
11994}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011995
Dan Gohman475871a2008-07-27 21:46:04 +000011996SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000011997 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000011998 SelectionDAG &DAG = DCI.DAG;
11999 switch (N->getOpcode()) {
12000 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012001 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerc19d1c32010-12-19 22:08:31 +000012002 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, *this);
Chris Lattneraf723b92008-01-25 05:46:26 +000012003 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +000012004 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000012005 case ISD::ADD:
12006 case ISD::SUB: return OptimizeConditonalInDecrement(N, DAG);
Chris Lattner23a01992010-12-20 01:37:09 +000012007 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000012008 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000012009 case ISD::SHL:
12010 case ISD::SRA:
12011 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000012012 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000012013 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000012014 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +000012015 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000012016 case X86ISD::FOR: return PerformFORCombine(N, DAG);
12017 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000012018 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000012019 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Evan Cheng2e489c42009-12-16 00:53:11 +000012020 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG);
Chris Lattnerc19d1c32010-12-19 22:08:31 +000012021 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000012022 case X86ISD::SHUFPS: // Handle all target specific shuffles
12023 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000012024 case X86ISD::PALIGN:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000012025 case X86ISD::PUNPCKHBW:
12026 case X86ISD::PUNPCKHWD:
12027 case X86ISD::PUNPCKHDQ:
12028 case X86ISD::PUNPCKHQDQ:
12029 case X86ISD::UNPCKHPS:
12030 case X86ISD::UNPCKHPD:
12031 case X86ISD::PUNPCKLBW:
12032 case X86ISD::PUNPCKLWD:
12033 case X86ISD::PUNPCKLDQ:
12034 case X86ISD::PUNPCKLQDQ:
12035 case X86ISD::UNPCKLPS:
12036 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +000012037 case X86ISD::VUNPCKLPS:
12038 case X86ISD::VUNPCKLPD:
12039 case X86ISD::VUNPCKLPSY:
12040 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000012041 case X86ISD::MOVHLPS:
12042 case X86ISD::MOVLHPS:
12043 case X86ISD::PSHUFD:
12044 case X86ISD::PSHUFHW:
12045 case X86ISD::PSHUFLW:
12046 case X86ISD::MOVSS:
12047 case X86ISD::MOVSD:
Mon P Wanga0fd0d52010-12-19 23:55:53 +000012048 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI);
Evan Cheng206ee9d2006-07-07 08:33:52 +000012049 }
12050
Dan Gohman475871a2008-07-27 21:46:04 +000012051 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000012052}
12053
Evan Chenge5b51ac2010-04-17 06:13:15 +000012054/// isTypeDesirableForOp - Return true if the target has native support for
12055/// the specified value type and it is 'desirable' to use the type for the
12056/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
12057/// instruction encodings are longer and some i16 instructions are slow.
12058bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
12059 if (!isTypeLegal(VT))
12060 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000012061 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000012062 return true;
12063
12064 switch (Opc) {
12065 default:
12066 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000012067 case ISD::LOAD:
12068 case ISD::SIGN_EXTEND:
12069 case ISD::ZERO_EXTEND:
12070 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000012071 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000012072 case ISD::SRL:
12073 case ISD::SUB:
12074 case ISD::ADD:
12075 case ISD::MUL:
12076 case ISD::AND:
12077 case ISD::OR:
12078 case ISD::XOR:
12079 return false;
12080 }
12081}
12082
12083/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000012084/// beneficial for dag combiner to promote the specified node. If true, it
12085/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000012086bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000012087 EVT VT = Op.getValueType();
12088 if (VT != MVT::i16)
12089 return false;
12090
Evan Cheng4c26e932010-04-19 19:29:22 +000012091 bool Promote = false;
12092 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000012093 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000012094 default: break;
12095 case ISD::LOAD: {
12096 LoadSDNode *LD = cast<LoadSDNode>(Op);
12097 // If the non-extending load has a single use and it's not live out, then it
12098 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000012099 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
12100 Op.hasOneUse()*/) {
12101 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
12102 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
12103 // The only case where we'd want to promote LOAD (rather then it being
12104 // promoted as an operand is when it's only use is liveout.
12105 if (UI->getOpcode() != ISD::CopyToReg)
12106 return false;
12107 }
12108 }
Evan Cheng4c26e932010-04-19 19:29:22 +000012109 Promote = true;
12110 break;
12111 }
12112 case ISD::SIGN_EXTEND:
12113 case ISD::ZERO_EXTEND:
12114 case ISD::ANY_EXTEND:
12115 Promote = true;
12116 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000012117 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000012118 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000012119 SDValue N0 = Op.getOperand(0);
12120 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000012121 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000012122 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000012123 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000012124 break;
12125 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000012126 case ISD::ADD:
12127 case ISD::MUL:
12128 case ISD::AND:
12129 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000012130 case ISD::XOR:
12131 Commute = true;
12132 // fallthrough
12133 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000012134 SDValue N0 = Op.getOperand(0);
12135 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000012136 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000012137 return false;
12138 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000012139 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000012140 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000012141 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000012142 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000012143 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000012144 }
12145 }
12146
12147 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000012148 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000012149}
12150
Evan Cheng60c07e12006-07-05 22:17:51 +000012151//===----------------------------------------------------------------------===//
12152// X86 Inline Assembly Support
12153//===----------------------------------------------------------------------===//
12154
Chris Lattnerb8105652009-07-20 17:51:36 +000012155bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
12156 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000012157
12158 std::string AsmStr = IA->getAsmString();
12159
12160 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000012161 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000012162 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000012163
12164 switch (AsmPieces.size()) {
12165 default: return false;
12166 case 1:
12167 AsmStr = AsmPieces[0];
12168 AsmPieces.clear();
12169 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
12170
Evan Cheng55d42002011-01-08 01:24:27 +000012171 // FIXME: this should verify that we are targetting a 486 or better. If not,
12172 // we will turn this bswap into something that will be lowered to logical ops
12173 // instead of emitting the bswap asm. For now, we don't support 486 or lower
12174 // so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000012175 // bswap $0
12176 if (AsmPieces.size() == 2 &&
12177 (AsmPieces[0] == "bswap" ||
12178 AsmPieces[0] == "bswapq" ||
12179 AsmPieces[0] == "bswapl") &&
12180 (AsmPieces[1] == "$0" ||
12181 AsmPieces[1] == "${0:q}")) {
12182 // No need to check constraints, nothing other than the equivalent of
12183 // "=r,0" would be valid here.
Evan Cheng55d42002011-01-08 01:24:27 +000012184 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
12185 if (!Ty || Ty->getBitWidth() % 16 != 0)
12186 return false;
12187 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000012188 }
12189 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000012190 if (CI->getType()->isIntegerTy(16) &&
Chris Lattnerb8105652009-07-20 17:51:36 +000012191 AsmPieces.size() == 3 &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000012192 (AsmPieces[0] == "rorw" || AsmPieces[0] == "rolw") &&
Chris Lattnerb8105652009-07-20 17:51:36 +000012193 AsmPieces[1] == "$$8," &&
12194 AsmPieces[2] == "${0:w}" &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000012195 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
12196 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000012197 const std::string &ConstraintsStr = IA->getConstraintString();
12198 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000012199 std::sort(AsmPieces.begin(), AsmPieces.end());
12200 if (AsmPieces.size() == 4 &&
12201 AsmPieces[0] == "~{cc}" &&
12202 AsmPieces[1] == "~{dirflag}" &&
12203 AsmPieces[2] == "~{flags}" &&
12204 AsmPieces[3] == "~{fpsr}") {
Evan Cheng55d42002011-01-08 01:24:27 +000012205 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
12206 if (!Ty || Ty->getBitWidth() % 16 != 0)
12207 return false;
12208 return IntrinsicLowering::LowerToByteSwap(CI);
Dan Gohman0ef701e2010-03-04 19:58:08 +000012209 }
Chris Lattnerb8105652009-07-20 17:51:36 +000012210 }
12211 break;
12212 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000012213 if (CI->getType()->isIntegerTy(32) &&
12214 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
12215 SmallVector<StringRef, 4> Words;
12216 SplitString(AsmPieces[0], Words, " \t,");
12217 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
12218 Words[2] == "${0:w}") {
12219 Words.clear();
12220 SplitString(AsmPieces[1], Words, " \t,");
12221 if (Words.size() == 3 && Words[0] == "rorl" && Words[1] == "$$16" &&
12222 Words[2] == "$0") {
12223 Words.clear();
12224 SplitString(AsmPieces[2], Words, " \t,");
12225 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
12226 Words[2] == "${0:w}") {
12227 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000012228 const std::string &ConstraintsStr = IA->getConstraintString();
12229 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Peter Collingbourne948cf022010-11-13 19:54:30 +000012230 std::sort(AsmPieces.begin(), AsmPieces.end());
12231 if (AsmPieces.size() == 4 &&
12232 AsmPieces[0] == "~{cc}" &&
12233 AsmPieces[1] == "~{dirflag}" &&
12234 AsmPieces[2] == "~{flags}" &&
12235 AsmPieces[3] == "~{fpsr}") {
Evan Cheng55d42002011-01-08 01:24:27 +000012236 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
12237 if (!Ty || Ty->getBitWidth() % 16 != 0)
12238 return false;
12239 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000012240 }
12241 }
12242 }
12243 }
12244 }
Evan Cheng55d42002011-01-08 01:24:27 +000012245
12246 if (CI->getType()->isIntegerTy(64)) {
12247 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
12248 if (Constraints.size() >= 2 &&
12249 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
12250 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
12251 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
12252 SmallVector<StringRef, 4> Words;
12253 SplitString(AsmPieces[0], Words, " \t");
12254 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
Chris Lattnerb8105652009-07-20 17:51:36 +000012255 Words.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000012256 SplitString(AsmPieces[1], Words, " \t");
12257 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
12258 Words.clear();
12259 SplitString(AsmPieces[2], Words, " \t,");
12260 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
12261 Words[2] == "%edx") {
12262 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
12263 if (!Ty || Ty->getBitWidth() % 16 != 0)
12264 return false;
12265 return IntrinsicLowering::LowerToByteSwap(CI);
12266 }
Chris Lattnerb8105652009-07-20 17:51:36 +000012267 }
12268 }
12269 }
12270 }
12271 break;
12272 }
12273 return false;
12274}
12275
12276
12277
Chris Lattnerf4dff842006-07-11 02:54:03 +000012278/// getConstraintType - Given a constraint letter, return the type of
12279/// constraint it is for this target.
12280X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000012281X86TargetLowering::getConstraintType(const std::string &Constraint) const {
12282 if (Constraint.size() == 1) {
12283 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000012284 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000012285 case 'q':
12286 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000012287 case 'f':
12288 case 't':
12289 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000012290 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000012291 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000012292 case 'Y':
12293 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000012294 case 'a':
12295 case 'b':
12296 case 'c':
12297 case 'd':
12298 case 'S':
12299 case 'D':
12300 case 'A':
12301 return C_Register;
12302 case 'I':
12303 case 'J':
12304 case 'K':
12305 case 'L':
12306 case 'M':
12307 case 'N':
12308 case 'G':
12309 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000012310 case 'e':
12311 case 'Z':
12312 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000012313 default:
12314 break;
12315 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000012316 }
Chris Lattner4234f572007-03-25 02:14:49 +000012317 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000012318}
12319
John Thompson44ab89e2010-10-29 17:29:13 +000012320/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000012321/// This object must already have been set up with the operand type
12322/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000012323TargetLowering::ConstraintWeight
12324 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000012325 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000012326 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012327 Value *CallOperandVal = info.CallOperandVal;
12328 // If we don't have a value, we can't do a match,
12329 // but allow it at the lowest weight.
12330 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000012331 return CW_Default;
12332 const Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000012333 // Look at the constraint type.
12334 switch (*constraint) {
12335 default:
John Thompson44ab89e2010-10-29 17:29:13 +000012336 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
12337 case 'R':
12338 case 'q':
12339 case 'Q':
12340 case 'a':
12341 case 'b':
12342 case 'c':
12343 case 'd':
12344 case 'S':
12345 case 'D':
12346 case 'A':
12347 if (CallOperandVal->getType()->isIntegerTy())
12348 weight = CW_SpecificReg;
12349 break;
12350 case 'f':
12351 case 't':
12352 case 'u':
12353 if (type->isFloatingPointTy())
12354 weight = CW_SpecificReg;
12355 break;
12356 case 'y':
Chris Lattner2a786eb2010-12-19 20:19:20 +000012357 if (type->isX86_MMXTy() && Subtarget->hasMMX())
John Thompson44ab89e2010-10-29 17:29:13 +000012358 weight = CW_SpecificReg;
12359 break;
12360 case 'x':
12361 case 'Y':
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012362 if ((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasXMM())
John Thompson44ab89e2010-10-29 17:29:13 +000012363 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012364 break;
12365 case 'I':
12366 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
12367 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000012368 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012369 }
12370 break;
John Thompson44ab89e2010-10-29 17:29:13 +000012371 case 'J':
12372 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12373 if (C->getZExtValue() <= 63)
12374 weight = CW_Constant;
12375 }
12376 break;
12377 case 'K':
12378 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12379 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
12380 weight = CW_Constant;
12381 }
12382 break;
12383 case 'L':
12384 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12385 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
12386 weight = CW_Constant;
12387 }
12388 break;
12389 case 'M':
12390 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12391 if (C->getZExtValue() <= 3)
12392 weight = CW_Constant;
12393 }
12394 break;
12395 case 'N':
12396 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12397 if (C->getZExtValue() <= 0xff)
12398 weight = CW_Constant;
12399 }
12400 break;
12401 case 'G':
12402 case 'C':
12403 if (dyn_cast<ConstantFP>(CallOperandVal)) {
12404 weight = CW_Constant;
12405 }
12406 break;
12407 case 'e':
12408 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12409 if ((C->getSExtValue() >= -0x80000000LL) &&
12410 (C->getSExtValue() <= 0x7fffffffLL))
12411 weight = CW_Constant;
12412 }
12413 break;
12414 case 'Z':
12415 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12416 if (C->getZExtValue() <= 0xffffffff)
12417 weight = CW_Constant;
12418 }
12419 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012420 }
12421 return weight;
12422}
12423
Dale Johannesenba2a0b92008-01-29 02:21:21 +000012424/// LowerXConstraint - try to replace an X constraint, which matches anything,
12425/// with another that has more specific requirements based on the type of the
12426/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000012427const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000012428LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000012429 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
12430 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000012431 if (ConstraintVT.isFloatingPoint()) {
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012432 if (Subtarget->hasXMMInt())
Chris Lattner5e764232008-04-26 23:02:14 +000012433 return "Y";
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012434 if (Subtarget->hasXMM())
Chris Lattner5e764232008-04-26 23:02:14 +000012435 return "x";
12436 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012437
Chris Lattner5e764232008-04-26 23:02:14 +000012438 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000012439}
12440
Chris Lattner48884cd2007-08-25 00:47:38 +000012441/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
12442/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000012443void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattner48884cd2007-08-25 00:47:38 +000012444 char Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000012445 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000012446 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000012447 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000012448
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012449 switch (Constraint) {
12450 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000012451 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000012452 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000012453 if (C->getZExtValue() <= 31) {
12454 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000012455 break;
12456 }
Devang Patel84f7fd22007-03-17 00:13:28 +000012457 }
Chris Lattner48884cd2007-08-25 00:47:38 +000012458 return;
Evan Cheng364091e2008-09-22 23:57:37 +000012459 case 'J':
12460 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000012461 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000012462 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
12463 break;
12464 }
12465 }
12466 return;
12467 case 'K':
12468 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000012469 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000012470 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
12471 break;
12472 }
12473 }
12474 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000012475 case 'N':
12476 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000012477 if (C->getZExtValue() <= 255) {
12478 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000012479 break;
12480 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000012481 }
Chris Lattner48884cd2007-08-25 00:47:38 +000012482 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000012483 case 'e': {
12484 // 32-bit signed value
12485 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000012486 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
12487 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000012488 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000012489 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000012490 break;
12491 }
12492 // FIXME gcc accepts some relocatable values here too, but only in certain
12493 // memory models; it's complicated.
12494 }
12495 return;
12496 }
12497 case 'Z': {
12498 // 32-bit unsigned value
12499 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000012500 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
12501 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000012502 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
12503 break;
12504 }
12505 }
12506 // FIXME gcc accepts some relocatable values here too, but only in certain
12507 // memory models; it's complicated.
12508 return;
12509 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012510 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012511 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000012512 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000012513 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000012514 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000012515 break;
12516 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012517
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000012518 // In any sort of PIC mode addresses need to be computed at runtime by
12519 // adding in a register or some sort of table lookup. These can't
12520 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000012521 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000012522 return;
12523
Chris Lattnerdc43a882007-05-03 16:52:29 +000012524 // If we are in non-pic codegen mode, we allow the address of a global (with
12525 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000012526 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000012527 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000012528
Chris Lattner49921962009-05-08 18:23:14 +000012529 // Match either (GA), (GA+C), (GA+C1+C2), etc.
12530 while (1) {
12531 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
12532 Offset += GA->getOffset();
12533 break;
12534 } else if (Op.getOpcode() == ISD::ADD) {
12535 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
12536 Offset += C->getZExtValue();
12537 Op = Op.getOperand(0);
12538 continue;
12539 }
12540 } else if (Op.getOpcode() == ISD::SUB) {
12541 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
12542 Offset += -C->getZExtValue();
12543 Op = Op.getOperand(0);
12544 continue;
12545 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012546 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012547
Chris Lattner49921962009-05-08 18:23:14 +000012548 // Otherwise, this isn't something we can handle, reject it.
12549 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000012550 }
Eric Christopherfd179292009-08-27 18:07:15 +000012551
Dan Gohman46510a72010-04-15 01:51:59 +000012552 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012553 // If we require an extra load to get this address, as in PIC mode, we
12554 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000012555 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
12556 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012557 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000012558
Devang Patel0d881da2010-07-06 22:08:15 +000012559 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
12560 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000012561 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012562 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012563 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012564
Gabor Greifba36cb52008-08-28 21:40:38 +000012565 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000012566 Ops.push_back(Result);
12567 return;
12568 }
Dale Johannesen1784d162010-06-25 21:55:36 +000012569 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012570}
12571
Chris Lattner259e97c2006-01-31 19:43:35 +000012572std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +000012573getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000012574 EVT VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +000012575 if (Constraint.size() == 1) {
12576 // FIXME: not handling fp-stack yet!
Chris Lattner259e97c2006-01-31 19:43:35 +000012577 switch (Constraint[0]) { // GCC X86 Constraint Letters
Chris Lattnerf4dff842006-07-11 02:54:03 +000012578 default: break; // Unknown constraint letter
Evan Cheng47e9fab2009-07-17 22:13:25 +000012579 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
12580 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000012581 if (VT == MVT::i32)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012582 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
12583 X86::ESI, X86::EDI, X86::R8D, X86::R9D,
12584 X86::R10D,X86::R11D,X86::R12D,
12585 X86::R13D,X86::R14D,X86::R15D,
12586 X86::EBP, X86::ESP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012587 else if (VT == MVT::i16)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012588 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
12589 X86::SI, X86::DI, X86::R8W,X86::R9W,
12590 X86::R10W,X86::R11W,X86::R12W,
12591 X86::R13W,X86::R14W,X86::R15W,
12592 X86::BP, X86::SP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012593 else if (VT == MVT::i8)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012594 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL,
12595 X86::SIL, X86::DIL, X86::R8B,X86::R9B,
12596 X86::R10B,X86::R11B,X86::R12B,
12597 X86::R13B,X86::R14B,X86::R15B,
12598 X86::BPL, X86::SPL, 0);
12599
Owen Anderson825b72b2009-08-11 20:47:22 +000012600 else if (VT == MVT::i64)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012601 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX,
12602 X86::RSI, X86::RDI, X86::R8, X86::R9,
12603 X86::R10, X86::R11, X86::R12,
12604 X86::R13, X86::R14, X86::R15,
12605 X86::RBP, X86::RSP, 0);
12606
12607 break;
12608 }
Eric Christopherfd179292009-08-27 18:07:15 +000012609 // 32-bit fallthrough
Chris Lattner259e97c2006-01-31 19:43:35 +000012610 case 'Q': // Q_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000012611 if (VT == MVT::i32)
Chris Lattner80a7ecc2006-05-06 00:29:37 +000012612 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012613 else if (VT == MVT::i16)
Chris Lattner80a7ecc2006-05-06 00:29:37 +000012614 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012615 else if (VT == MVT::i8)
Evan Cheng12914382007-08-13 23:27:11 +000012616 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012617 else if (VT == MVT::i64)
Chris Lattner03e6c702007-11-04 06:51:12 +000012618 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
12619 break;
Chris Lattner259e97c2006-01-31 19:43:35 +000012620 }
12621 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012622
Chris Lattner1efa40f2006-02-22 00:56:39 +000012623 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +000012624}
Chris Lattnerf76d1802006-07-31 23:26:50 +000012625
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012626std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000012627X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000012628 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000012629 // First, see if this is a constraint that directly corresponds to an LLVM
12630 // register class.
12631 if (Constraint.size() == 1) {
12632 // GCC Constraint Letters
12633 switch (Constraint[0]) {
12634 default: break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000012635 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000012636 case 'l': // INDEX_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000012637 if (VT == MVT::i8)
Chris Lattner0f65cad2007-04-09 05:49:22 +000012638 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012639 if (VT == MVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +000012640 return std::make_pair(0U, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012641 if (VT == MVT::i32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +000012642 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +000012643 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000012644 case 'R': // LEGACY_REGS
12645 if (VT == MVT::i8)
12646 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
12647 if (VT == MVT::i16)
12648 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
12649 if (VT == MVT::i32 || !Subtarget->is64Bit())
12650 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
12651 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000012652 case 'f': // FP Stack registers.
12653 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
12654 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000012655 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000012656 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012657 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000012658 return std::make_pair(0U, X86::RFP64RegisterClass);
12659 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000012660 case 'y': // MMX_REGS if MMX allowed.
12661 if (!Subtarget->hasMMX()) break;
12662 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000012663 case 'Y': // SSE_REGS if SSE2 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012664 if (!Subtarget->hasXMMInt()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000012665 // FALL THROUGH.
12666 case 'x': // SSE_REGS if SSE1 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012667 if (!Subtarget->hasXMM()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000012668
Owen Anderson825b72b2009-08-11 20:47:22 +000012669 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000012670 default: break;
12671 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000012672 case MVT::f32:
12673 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +000012674 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012675 case MVT::f64:
12676 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +000012677 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000012678 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000012679 case MVT::v16i8:
12680 case MVT::v8i16:
12681 case MVT::v4i32:
12682 case MVT::v2i64:
12683 case MVT::v4f32:
12684 case MVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +000012685 return std::make_pair(0U, X86::VR128RegisterClass);
12686 }
Chris Lattnerad043e82007-04-09 05:11:28 +000012687 break;
12688 }
12689 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012690
Chris Lattnerf76d1802006-07-31 23:26:50 +000012691 // Use the default implementation in TargetLowering to convert the register
12692 // constraint into a member of a register class.
12693 std::pair<unsigned, const TargetRegisterClass*> Res;
12694 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000012695
12696 // Not found as a standard register?
12697 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000012698 // Map st(0) -> st(7) -> ST0
12699 if (Constraint.size() == 7 && Constraint[0] == '{' &&
12700 tolower(Constraint[1]) == 's' &&
12701 tolower(Constraint[2]) == 't' &&
12702 Constraint[3] == '(' &&
12703 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
12704 Constraint[5] == ')' &&
12705 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000012706
Chris Lattner56d77c72009-09-13 22:41:48 +000012707 Res.first = X86::ST0+Constraint[4]-'0';
12708 Res.second = X86::RFP80RegisterClass;
12709 return Res;
12710 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000012711
Chris Lattner56d77c72009-09-13 22:41:48 +000012712 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000012713 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000012714 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +000012715 Res.second = X86::RFP80RegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000012716 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000012717 }
Chris Lattner56d77c72009-09-13 22:41:48 +000012718
12719 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000012720 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000012721 Res.first = X86::EFLAGS;
12722 Res.second = X86::CCRRegisterClass;
12723 return Res;
12724 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000012725
Dale Johannesen330169f2008-11-13 21:52:36 +000012726 // 'A' means EAX + EDX.
12727 if (Constraint == "A") {
12728 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +000012729 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000012730 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000012731 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000012732 return Res;
12733 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012734
Chris Lattnerf76d1802006-07-31 23:26:50 +000012735 // Otherwise, check to see if this is a register class of the wrong value
12736 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
12737 // turn into {ax},{dx}.
12738 if (Res.second->hasType(VT))
12739 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012740
Chris Lattnerf76d1802006-07-31 23:26:50 +000012741 // All of the single-register GCC register classes map their values onto
12742 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
12743 // really want an 8-bit or 32-bit register, map to the appropriate register
12744 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +000012745 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000012746 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000012747 unsigned DestReg = 0;
12748 switch (Res.first) {
12749 default: break;
12750 case X86::AX: DestReg = X86::AL; break;
12751 case X86::DX: DestReg = X86::DL; break;
12752 case X86::CX: DestReg = X86::CL; break;
12753 case X86::BX: DestReg = X86::BL; break;
12754 }
12755 if (DestReg) {
12756 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000012757 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000012758 }
Owen Anderson825b72b2009-08-11 20:47:22 +000012759 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000012760 unsigned DestReg = 0;
12761 switch (Res.first) {
12762 default: break;
12763 case X86::AX: DestReg = X86::EAX; break;
12764 case X86::DX: DestReg = X86::EDX; break;
12765 case X86::CX: DestReg = X86::ECX; break;
12766 case X86::BX: DestReg = X86::EBX; break;
12767 case X86::SI: DestReg = X86::ESI; break;
12768 case X86::DI: DestReg = X86::EDI; break;
12769 case X86::BP: DestReg = X86::EBP; break;
12770 case X86::SP: DestReg = X86::ESP; break;
12771 }
12772 if (DestReg) {
12773 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000012774 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000012775 }
Owen Anderson825b72b2009-08-11 20:47:22 +000012776 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000012777 unsigned DestReg = 0;
12778 switch (Res.first) {
12779 default: break;
12780 case X86::AX: DestReg = X86::RAX; break;
12781 case X86::DX: DestReg = X86::RDX; break;
12782 case X86::CX: DestReg = X86::RCX; break;
12783 case X86::BX: DestReg = X86::RBX; break;
12784 case X86::SI: DestReg = X86::RSI; break;
12785 case X86::DI: DestReg = X86::RDI; break;
12786 case X86::BP: DestReg = X86::RBP; break;
12787 case X86::SP: DestReg = X86::RSP; break;
12788 }
12789 if (DestReg) {
12790 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000012791 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000012792 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000012793 }
Chris Lattner6ba50a92008-08-26 06:19:02 +000012794 } else if (Res.second == X86::FR32RegisterClass ||
12795 Res.second == X86::FR64RegisterClass ||
12796 Res.second == X86::VR128RegisterClass) {
12797 // Handle references to XMM physical registers that got mapped into the
12798 // wrong class. This can happen with constraints like {xmm0} where the
12799 // target independent register mapper will just pick the first match it can
12800 // find, ignoring the required type.
Owen Anderson825b72b2009-08-11 20:47:22 +000012801 if (VT == MVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +000012802 Res.second = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +000012803 else if (VT == MVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +000012804 Res.second = X86::FR64RegisterClass;
12805 else if (X86::VR128RegisterClass->hasType(VT))
12806 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000012807 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012808
Chris Lattnerf76d1802006-07-31 23:26:50 +000012809 return Res;
12810}