blob: ea7eef7320647ac18301cd45af4425171d8fba80 [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"
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000019#include "X86ShuffleDecode.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000020#include "X86TargetMachine.h"
Chris Lattner8c6ed052009-09-16 01:46:41 +000021#include "X86TargetObjectFile.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"
Mon P Wang3c81d352008-11-23 04:37:22 +000048#include "llvm/Support/CommandLine.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000049#include "llvm/Support/Debug.h"
Bill Wendlingec041eb2010-03-12 19:20:40 +000050#include "llvm/Support/Dwarf.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000051#include "llvm/Support/ErrorHandling.h"
52#include "llvm/Support/MathExtras.h"
Torok Edwindac237e2009-07-08 20:53:28 +000053#include "llvm/Support/raw_ostream.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000054using namespace llvm;
Bill Wendlingec041eb2010-03-12 19:20:40 +000055using namespace dwarf;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000056
Evan Chengb1712452010-01-27 06:25:16 +000057STATISTIC(NumTailCalls, "Number of tail calls");
58
David Greenef125a292011-02-08 19:04:41 +000059static cl::opt<bool>
60Disable256Bit("disable-256bit", cl::Hidden,
61 cl::desc("Disable use of 256-bit vectors"));
62
Evan Cheng10e86422008-04-25 19:11:04 +000063// Forward declarations.
Owen Andersone50ed302009-08-10 22:56:29 +000064static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +000065 SDValue V2);
Evan Cheng10e86422008-04-25 19:11:04 +000066
David Greenea5f26012011-02-07 19:36:54 +000067static SDValue Insert128BitVector(SDValue Result,
68 SDValue Vec,
69 SDValue Idx,
70 SelectionDAG &DAG,
71 DebugLoc dl);
David Greenef125a292011-02-08 19:04:41 +000072
David Greenea5f26012011-02-07 19:36:54 +000073static SDValue Extract128BitVector(SDValue Vec,
74 SDValue Idx,
75 SelectionDAG &DAG,
76 DebugLoc dl);
77
David Greenef125a292011-02-08 19:04:41 +000078static SDValue ConcatVectors(SDValue Lower, SDValue Upper, SelectionDAG &DAG);
79
80
David Greenea5f26012011-02-07 19:36:54 +000081/// Generate a DAG to grab 128-bits from a vector > 128 bits. This
82/// sets things up to match to an AVX VEXTRACTF128 instruction or a
83/// simple subregister reference.
84static SDValue Extract128BitVector(SDValue Vec,
85 SDValue Idx,
86 SelectionDAG &DAG,
87 DebugLoc dl) {
88 EVT VT = Vec.getValueType();
89 assert(VT.getSizeInBits() == 256 && "Unexpected vector size!");
90
91 EVT ElVT = VT.getVectorElementType();
92
93 int Factor = VT.getSizeInBits() / 128;
94
95 EVT ResultVT = EVT::getVectorVT(*DAG.getContext(),
96 ElVT,
97 VT.getVectorNumElements() / Factor);
98
99 // Extract from UNDEF is UNDEF.
100 if (Vec.getOpcode() == ISD::UNDEF)
101 return DAG.getNode(ISD::UNDEF, dl, ResultVT);
102
103 if (isa<ConstantSDNode>(Idx)) {
104 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
105
106 // Extract the relevant 128 bits. Generate an EXTRACT_SUBVECTOR
107 // we can match to VEXTRACTF128.
108 unsigned ElemsPerChunk = 128 / ElVT.getSizeInBits();
109
110 // This is the index of the first element of the 128-bit chunk
111 // we want.
112 unsigned NormalizedIdxVal = (((IdxVal * ElVT.getSizeInBits()) / 128)
113 * ElemsPerChunk);
114
115 SDValue VecIdx = DAG.getConstant(NormalizedIdxVal, MVT::i32);
116
117 SDValue Result = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, ResultVT, Vec,
118 VecIdx);
119
120 return Result;
121 }
122
123 return SDValue();
124}
125
126/// Generate a DAG to put 128-bits into a vector > 128 bits. This
127/// sets things up to match to an AVX VINSERTF128 instruction or a
David Greene6b381262011-02-09 15:32:06 +0000128/// simple superregister reference. Idx is an index in the 128 bits
129/// we want. It need not be aligned to a 128-bit bounday. That makes
130/// lowering INSERT_VECTOR_ELT operations easier.
David Greenea5f26012011-02-07 19:36:54 +0000131static SDValue Insert128BitVector(SDValue Result,
132 SDValue Vec,
133 SDValue Idx,
134 SelectionDAG &DAG,
135 DebugLoc dl) {
136 if (isa<ConstantSDNode>(Idx)) {
137 EVT VT = Vec.getValueType();
138 assert(VT.getSizeInBits() == 128 && "Unexpected vector size!");
139
140 EVT ElVT = VT.getVectorElementType();
141
142 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
143
144 EVT ResultVT = Result.getValueType();
145
146 // Insert the relevant 128 bits.
147 unsigned ElemsPerChunk = 128 / ElVT.getSizeInBits();
148
149 // This is the index of the first element of the 128-bit chunk
150 // we want.
151 unsigned NormalizedIdxVal = (((IdxVal * ElVT.getSizeInBits()) / 128)
152 * ElemsPerChunk);
153
154 SDValue VecIdx = DAG.getConstant(NormalizedIdxVal, MVT::i32);
155
156 Result = DAG.getNode(ISD::INSERT_SUBVECTOR, dl, ResultVT, Result, Vec,
157 VecIdx);
158 return Result;
159 }
160
161 return SDValue();
162}
163
David Greenef125a292011-02-08 19:04:41 +0000164/// Given two vectors, concat them.
165static SDValue ConcatVectors(SDValue Lower, SDValue Upper, SelectionDAG &DAG) {
166 DebugLoc dl = Lower.getDebugLoc();
167
168 assert(Lower.getValueType() == Upper.getValueType() && "Mismatched vectors!");
169
170 EVT VT = EVT::getVectorVT(*DAG.getContext(),
171 Lower.getValueType().getVectorElementType(),
172 Lower.getValueType().getVectorNumElements() * 2);
173
174 // TODO: Generalize to arbitrary vector length (this assumes 256-bit vectors).
175 assert(VT.getSizeInBits() == 256 && "Unsupported vector concat!");
176
177 // Insert the upper subvector.
178 SDValue Vec = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT), Upper,
179 DAG.getConstant(
180 // This is half the length of the result
181 // vector. Start inserting the upper 128
182 // bits here.
183 Lower.getValueType().getVectorNumElements(),
184 MVT::i32),
185 DAG, dl);
186
187 // Insert the lower subvector.
188 Vec = Insert128BitVector(Vec, Lower, DAG.getConstant(0, MVT::i32), DAG, dl);
189 return Vec;
190}
191
Chris Lattnerf0144122009-07-28 03:13:23 +0000192static TargetLoweringObjectFile *createTLOF(X86TargetMachine &TM) {
Evan Cheng2bffee22011-02-01 01:14:13 +0000193 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
194 bool is64Bit = Subtarget->is64Bit();
NAKAMURA Takumi27635382011-02-05 15:10:54 +0000195
Evan Cheng2bffee22011-02-01 01:14:13 +0000196 if (Subtarget->isTargetEnvMacho()) {
Chris Lattnere019ec12010-12-19 20:07:10 +0000197 if (is64Bit)
198 return new X8664_MachoTargetObjectFile();
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000199 return new TargetLoweringObjectFileMachO();
Michael J. Spencerec38de22010-10-10 22:04:20 +0000200 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000201
Evan Cheng2bffee22011-02-01 01:14:13 +0000202 if (Subtarget->isTargetELF()) {
Chris Lattnere019ec12010-12-19 20:07:10 +0000203 if (is64Bit)
204 return new X8664_ELFTargetObjectFile(TM);
205 return new X8632_ELFTargetObjectFile(TM);
206 }
Evan Cheng2bffee22011-02-01 01:14:13 +0000207 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
Chris Lattnere019ec12010-12-19 20:07:10 +0000208 return new TargetLoweringObjectFileCOFF();
Eric Christopher62f35a22010-07-05 19:26:33 +0000209 llvm_unreachable("unknown subtarget type");
Chris Lattnerf0144122009-07-28 03:13:23 +0000210}
211
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +0000212X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
Chris Lattnerf0144122009-07-28 03:13:23 +0000213 : TargetLowering(TM, createTLOF(TM)) {
Evan Cheng559806f2006-01-27 08:10:46 +0000214 Subtarget = &TM.getSubtarget<X86Subtarget>();
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000215 X86ScalarSSEf64 = Subtarget->hasXMMInt();
216 X86ScalarSSEf32 = Subtarget->hasXMM();
Evan Cheng25ab6902006-09-08 06:48:29 +0000217 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000218
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000219 RegInfo = TM.getRegisterInfo();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000220 TD = getTargetData();
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000221
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000222 // Set up the TargetLowering object.
Chris Lattnera34b3cf2010-12-19 20:03:11 +0000223 static MVT IntVTs[] = { MVT::i8, MVT::i16, MVT::i32, MVT::i64 };
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000224
225 // X86 is weird, it always uses i8 for shift amounts and setcc results.
Owen Anderson825b72b2009-08-11 20:47:22 +0000226 setShiftAmountType(MVT::i8);
Duncan Sands03228082008-11-23 15:47:28 +0000227 setBooleanContents(ZeroOrOneBooleanContent);
Evan Cheng211ffa12010-05-19 20:19:50 +0000228 setSchedulingPreference(Sched::RegPressure);
Evan Cheng25ab6902006-09-08 06:48:29 +0000229 setStackPointerRegisterToSaveRestore(X86StackPtr);
Evan Cheng714554d2006-03-16 21:47:42 +0000230
Michael J. Spencer92bf38c2010-10-10 23:11:06 +0000231 if (Subtarget->isTargetWindows() && !Subtarget->isTargetCygMing()) {
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000232 // Setup Windows compiler runtime calls.
233 setLibcallName(RTLIB::SDIV_I64, "_alldiv");
Michael J. Spencer335b8062010-10-11 05:29:15 +0000234 setLibcallName(RTLIB::UDIV_I64, "_aulldiv");
235 setLibcallName(RTLIB::FPTOUINT_F64_I64, "_ftol2");
Michael J. Spencer94f7eeb2010-10-19 07:32:52 +0000236 setLibcallName(RTLIB::FPTOUINT_F32_I64, "_ftol2");
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000237 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::X86_StdCall);
Michael J. Spencer335b8062010-10-11 05:29:15 +0000238 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::X86_StdCall);
Michael J. Spencer6dad10e2010-10-27 18:52:38 +0000239 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I64, CallingConv::C);
240 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I64, CallingConv::C);
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000241 }
242
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000243 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +0000244 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000245 setUseUnderscoreSetJmp(false);
246 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000247 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000248 // MS runtime is weird: it exports _setjmp, but longjmp!
249 setUseUnderscoreSetJmp(true);
250 setUseUnderscoreLongJmp(false);
251 } else {
252 setUseUnderscoreSetJmp(true);
253 setUseUnderscoreLongJmp(true);
254 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000255
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000256 // Set up the register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000257 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
Dan Gohman71edb242010-04-30 18:30:26 +0000258 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000259 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
Evan Cheng25ab6902006-09-08 06:48:29 +0000260 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000261 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000262
Owen Anderson825b72b2009-08-11 20:47:22 +0000263 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Evan Chengc5484282006-10-04 00:56:09 +0000264
Scott Michelfdc40a02009-02-17 22:15:04 +0000265 // We don't accept any truncstore of integer registers.
Owen Anderson825b72b2009-08-11 20:47:22 +0000266 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000267 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000268 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000269 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000270 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
271 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
Evan Cheng7f042682008-10-15 02:05:31 +0000272
273 // SETOEQ and SETUNE require checking two conditions.
Owen Anderson825b72b2009-08-11 20:47:22 +0000274 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
275 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
276 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
277 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
278 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
279 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
Chris Lattnerddf89562008-01-17 19:59:44 +0000280
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000281 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
282 // operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000283 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
284 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
285 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +0000286
Evan Cheng25ab6902006-09-08 06:48:29 +0000287 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000288 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
289 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
Eli Friedman948e95a2009-05-23 09:59:16 +0000290 } else if (!UseSoftFloat) {
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000291 // We have an algorithm for SSE2->double, and we turn this into a
292 // 64-bit FILD followed by conditional FADD for other targets.
293 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Eli Friedman948e95a2009-05-23 09:59:16 +0000294 // We have an algorithm for SSE2, and we turn this into a 64-bit
295 // FILD for other targets.
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000296 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000297 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000298
299 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
300 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000301 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
302 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000303
Devang Patel6a784892009-06-05 18:48:29 +0000304 if (!UseSoftFloat) {
Bill Wendling105be5a2009-03-13 08:41:47 +0000305 // SSE has no i16 to fp conversion, only i32
306 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000307 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000308 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000309 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000310 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000311 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
312 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000313 }
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000314 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000315 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
316 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000317 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000318
Dale Johannesen73328d12007-09-19 23:55:34 +0000319 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
320 // are Legal, f80 is custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000321 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
322 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000323
Evan Cheng02568ff2006-01-30 22:13:22 +0000324 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
325 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000326 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
327 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
Evan Cheng02568ff2006-01-30 22:13:22 +0000328
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000329 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000330 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000331 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000332 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000333 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000334 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
335 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000336 }
337
338 // Handle FP_TO_UINT by promoting the destination to a larger signed
339 // conversion.
Owen Anderson825b72b2009-08-11 20:47:22 +0000340 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
341 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
342 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000343
Evan Cheng25ab6902006-09-08 06:48:29 +0000344 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000345 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
346 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Eli Friedman948e95a2009-05-23 09:59:16 +0000347 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000348 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000349 // Expand FP_TO_UINT into a select.
350 // FIXME: We would like to use a Custom expander here eventually to do
351 // the optimal thing for SSE vs. the default expansion in the legalizer.
Owen Anderson825b72b2009-08-11 20:47:22 +0000352 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000353 else
Eli Friedman948e95a2009-05-23 09:59:16 +0000354 // With SSE3 we can use fisttpll to convert to a signed i64; without
355 // SSE, we're stuck with a fistpll.
Owen Anderson825b72b2009-08-11 20:47:22 +0000356 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000357 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000358
Chris Lattner399610a2006-12-05 18:22:22 +0000359 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Michael J. Spencerec38de22010-10-10 22:04:20 +0000360 if (!X86ScalarSSEf64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000361 setOperationAction(ISD::BITCAST , MVT::f32 , Expand);
362 setOperationAction(ISD::BITCAST , MVT::i32 , Expand);
Dale Johannesene39859a2010-05-21 18:40:15 +0000363 if (Subtarget->is64Bit()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000364 setOperationAction(ISD::BITCAST , MVT::f64 , Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000365 // Without SSE, i64->f64 goes through memory.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000366 setOperationAction(ISD::BITCAST , MVT::i64 , Expand);
Dale Johannesen7d07b482010-05-21 00:52:33 +0000367 }
Chris Lattnerf3597a12006-12-05 18:45:06 +0000368 }
Chris Lattner21f66852005-12-23 05:15:23 +0000369
Dan Gohmanb00ee212008-02-18 19:34:53 +0000370 // Scalar integer divide and remainder are lowered to use operations that
371 // produce two results, to match the available instructions. This exposes
372 // the two-result form to trivial CSE, which is able to combine x/y and x%y
373 // into a single instruction.
374 //
375 // Scalar integer multiply-high is also lowered to use two-result
376 // operations, to match the available instructions. However, plain multiply
377 // (low) operations are left as Legal, as there are single-result
378 // instructions for this in x86. Using the two-result multiply instructions
379 // when both high and low results are needed must be arranged by dagcombine.
Chris Lattnere019ec12010-12-19 20:07:10 +0000380 for (unsigned i = 0, e = 4; i != e; ++i) {
381 MVT VT = IntVTs[i];
382 setOperationAction(ISD::MULHS, VT, Expand);
383 setOperationAction(ISD::MULHU, VT, Expand);
384 setOperationAction(ISD::SDIV, VT, Expand);
385 setOperationAction(ISD::UDIV, VT, Expand);
386 setOperationAction(ISD::SREM, VT, Expand);
387 setOperationAction(ISD::UREM, VT, Expand);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000388
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000389 // Add/Sub overflow ops with MVT::Glues are lowered to EFLAGS dependences.
Chris Lattnerd8ff7ec2010-12-20 01:03:27 +0000390 setOperationAction(ISD::ADDC, VT, Custom);
391 setOperationAction(ISD::ADDE, VT, Custom);
392 setOperationAction(ISD::SUBC, VT, Custom);
393 setOperationAction(ISD::SUBE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000394 }
Dan Gohmana37c9f72007-09-25 18:23:27 +0000395
Owen Anderson825b72b2009-08-11 20:47:22 +0000396 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
397 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
398 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
399 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000400 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000401 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
402 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
403 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
404 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
405 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
406 setOperationAction(ISD::FREM , MVT::f32 , Expand);
407 setOperationAction(ISD::FREM , MVT::f64 , Expand);
408 setOperationAction(ISD::FREM , MVT::f80 , Expand);
409 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000410
Owen Anderson825b72b2009-08-11 20:47:22 +0000411 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
412 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000413 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
414 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000415 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
416 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000417 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000418 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
419 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000420 }
421
Benjamin Kramer1292c222010-12-04 20:32:23 +0000422 if (Subtarget->hasPOPCNT()) {
423 setOperationAction(ISD::CTPOP , MVT::i8 , Promote);
424 } else {
425 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
426 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
427 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
428 if (Subtarget->is64Bit())
429 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
430 }
431
Owen Anderson825b72b2009-08-11 20:47:22 +0000432 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
433 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000434
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000435 // These should be promoted to a larger select which is supported.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000436 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000437 // X86 wants to expand cmov itself.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000438 setOperationAction(ISD::SELECT , MVT::i8 , Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000439 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000440 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
441 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
442 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
443 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
444 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000445 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000446 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
447 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
448 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
449 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000450 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000451 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
452 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000453 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000454 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000455
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000456 // Darwin ABI issue.
Owen Anderson825b72b2009-08-11 20:47:22 +0000457 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
458 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
459 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
460 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000461 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000462 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
463 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000464 setOperationAction(ISD::BlockAddress , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000465 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000466 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
467 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
468 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
469 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000470 setOperationAction(ISD::BlockAddress , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000471 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000472 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Owen Anderson825b72b2009-08-11 20:47:22 +0000473 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
474 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
475 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000476 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000477 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
478 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
479 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000480 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000481
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000482 if (Subtarget->hasXMM())
Owen Anderson825b72b2009-08-11 20:47:22 +0000483 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000484
Eric Christopher9a9d2752010-07-22 02:48:34 +0000485 // We may not have a libcall for MEMBARRIER so we should lower this.
486 setOperationAction(ISD::MEMBARRIER , MVT::Other, Custom);
Michael J. Spencerec38de22010-10-10 22:04:20 +0000487
Jim Grosbachf1ab49e2010-06-23 16:25:07 +0000488 // On X86 and X86-64, atomic operations are lowered to locked instructions.
489 // Locked instructions, in turn, have implicit fence semantics (all memory
490 // operations are flushed before issuing the locked instruction, and they
491 // are not buffered), so we can fold away the common pattern of
492 // fence-atomic-fence.
493 setShouldFoldAtomicFences(true);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000494
Mon P Wang63307c32008-05-05 19:05:59 +0000495 // Expand certain atomics
Chris Lattnere019ec12010-12-19 20:07:10 +0000496 for (unsigned i = 0, e = 4; i != e; ++i) {
497 MVT VT = IntVTs[i];
498 setOperationAction(ISD::ATOMIC_CMP_SWAP, VT, Custom);
499 setOperationAction(ISD::ATOMIC_LOAD_SUB, VT, Custom);
500 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000501
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000502 if (!Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000503 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
504 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
505 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
506 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
507 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
508 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
509 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000510 }
511
Evan Cheng3c992d22006-03-07 02:02:57 +0000512 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000513 if (!Subtarget->isTargetDarwin() &&
514 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000515 !Subtarget->isTargetCygMing()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000516 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Dan Gohman44066042008-07-01 00:05:16 +0000517 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000518
Owen Anderson825b72b2009-08-11 20:47:22 +0000519 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
520 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
521 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
522 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000523 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000524 setExceptionPointerRegister(X86::RAX);
525 setExceptionSelectorRegister(X86::RDX);
526 } else {
527 setExceptionPointerRegister(X86::EAX);
528 setExceptionSelectorRegister(X86::EDX);
529 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000530 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
531 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000532
Owen Anderson825b72b2009-08-11 20:47:22 +0000533 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000534
Owen Anderson825b72b2009-08-11 20:47:22 +0000535 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000536
Nate Begemanacc398c2006-01-25 18:21:52 +0000537 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000538 setOperationAction(ISD::VASTART , MVT::Other, Custom);
539 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000540 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000541 setOperationAction(ISD::VAARG , MVT::Other, Custom);
542 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000543 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000544 setOperationAction(ISD::VAARG , MVT::Other, Expand);
545 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000546 }
Evan Chengae642192007-03-02 23:16:35 +0000547
Owen Anderson825b72b2009-08-11 20:47:22 +0000548 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
549 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000550 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000551 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Michael J. Spencere9c253e2010-10-21 01:41:01 +0000552 if (Subtarget->isTargetCygMing() || Subtarget->isTargetWindows())
Owen Anderson825b72b2009-08-11 20:47:22 +0000553 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +0000554 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000555 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000556
Evan Chengc7ce29b2009-02-13 22:36:38 +0000557 if (!UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000558 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000559 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000560 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
561 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000562
Evan Cheng223547a2006-01-31 22:28:30 +0000563 // Use ANDPD to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000564 setOperationAction(ISD::FABS , MVT::f64, Custom);
565 setOperationAction(ISD::FABS , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000566
567 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000568 setOperationAction(ISD::FNEG , MVT::f64, Custom);
569 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000570
Evan Cheng68c47cb2007-01-05 07:55:56 +0000571 // Use ANDPD and ORPD to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000572 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
573 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000574
Evan Chengd25e9e82006-02-02 00:28:23 +0000575 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000576 setOperationAction(ISD::FSIN , MVT::f64, Expand);
577 setOperationAction(ISD::FCOS , MVT::f64, Expand);
578 setOperationAction(ISD::FSIN , MVT::f32, Expand);
579 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000580
Chris Lattnera54aa942006-01-29 06:26:08 +0000581 // Expand FP immediates into loads from the stack, except for the special
582 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000583 addLegalFPImmediate(APFloat(+0.0)); // xorpd
584 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Evan Chengc7ce29b2009-02-13 22:36:38 +0000585 } else if (!UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000586 // Use SSE for f32, x87 for f64.
587 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000588 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
589 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000590
591 // Use ANDPS to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000592 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000593
594 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000595 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000596
Owen Anderson825b72b2009-08-11 20:47:22 +0000597 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000598
599 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000600 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
601 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000602
603 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000604 setOperationAction(ISD::FSIN , MVT::f32, Expand);
605 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000606
Nate Begemane1795842008-02-14 08:57:00 +0000607 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000608 addLegalFPImmediate(APFloat(+0.0f)); // xorps
609 addLegalFPImmediate(APFloat(+0.0)); // FLD0
610 addLegalFPImmediate(APFloat(+1.0)); // FLD1
611 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
612 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
613
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000614 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000615 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
616 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000617 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000618 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000619 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000620 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000621 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
622 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000623
Owen Anderson825b72b2009-08-11 20:47:22 +0000624 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
625 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
626 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
627 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000628
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000629 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000630 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
631 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000632 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000633 addLegalFPImmediate(APFloat(+0.0)); // FLD0
634 addLegalFPImmediate(APFloat(+1.0)); // FLD1
635 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
636 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000637 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
638 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
639 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
640 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000641 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000642
Dale Johannesen59a58732007-08-05 18:49:15 +0000643 // Long double always uses X87.
Evan Cheng92722532009-03-26 23:06:32 +0000644 if (!UseSoftFloat) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000645 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
646 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
647 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000648 {
Benjamin Kramer98383962010-12-04 14:22:24 +0000649 APFloat TmpFlt = APFloat::getZero(APFloat::x87DoubleExtended);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000650 addLegalFPImmediate(TmpFlt); // FLD0
651 TmpFlt.changeSign();
652 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
Benjamin Kramer98383962010-12-04 14:22:24 +0000653
654 bool ignored;
Evan Chengc7ce29b2009-02-13 22:36:38 +0000655 APFloat TmpFlt2(+1.0);
656 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
657 &ignored);
658 addLegalFPImmediate(TmpFlt2); // FLD1
659 TmpFlt2.changeSign();
660 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
661 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000662
Evan Chengc7ce29b2009-02-13 22:36:38 +0000663 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000664 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
665 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000666 }
Dale Johannesen2f429012007-09-26 21:10:55 +0000667 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000668
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000669 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000670 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
671 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
672 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000673
Owen Anderson825b72b2009-08-11 20:47:22 +0000674 setOperationAction(ISD::FLOG, MVT::f80, Expand);
675 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
676 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
677 setOperationAction(ISD::FEXP, MVT::f80, Expand);
678 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000679
Mon P Wangf007a8b2008-11-06 05:31:54 +0000680 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000681 // (for widening) or expand (for scalarization). Then we will selectively
682 // turn on ones that can be effectively codegen'd.
Owen Anderson825b72b2009-08-11 20:47:22 +0000683 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
684 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
685 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
686 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
687 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
688 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
689 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
690 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
691 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
692 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
693 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
694 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
695 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
696 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
697 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
698 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
699 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000700 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
David Greenecfe33c42011-01-26 19:13:22 +0000701 setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
702 setOperationAction(ISD::INSERT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000703 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
704 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
705 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
706 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
707 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
708 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
709 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
710 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
711 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
712 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
713 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
714 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
715 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
716 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
717 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
718 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
719 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
720 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
721 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
722 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
723 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
724 setOperationAction(ISD::VSETCC, (MVT::SimpleValueType)VT, Expand);
725 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
726 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
727 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
728 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
729 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
730 setOperationAction(ISD::FP_TO_UINT, (MVT::SimpleValueType)VT, Expand);
731 setOperationAction(ISD::FP_TO_SINT, (MVT::SimpleValueType)VT, Expand);
732 setOperationAction(ISD::UINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
733 setOperationAction(ISD::SINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
Dan Gohman87862e72009-12-11 21:31:27 +0000734 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000735 setOperationAction(ISD::TRUNCATE, (MVT::SimpleValueType)VT, Expand);
736 setOperationAction(ISD::SIGN_EXTEND, (MVT::SimpleValueType)VT, Expand);
737 setOperationAction(ISD::ZERO_EXTEND, (MVT::SimpleValueType)VT, Expand);
738 setOperationAction(ISD::ANY_EXTEND, (MVT::SimpleValueType)VT, Expand);
739 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
740 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
741 setTruncStoreAction((MVT::SimpleValueType)VT,
742 (MVT::SimpleValueType)InnerVT, Expand);
743 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
744 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
745 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000746 }
747
Evan Chengc7ce29b2009-02-13 22:36:38 +0000748 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
749 // with -msoft-float, disable use of MMX as well.
Chris Lattner2a786eb2010-12-19 20:19:20 +0000750 if (!UseSoftFloat && Subtarget->hasMMX()) {
Dale Johannesene93d99c2010-10-20 21:32:10 +0000751 addRegisterClass(MVT::x86mmx, X86::VR64RegisterClass);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000752 // No operations on x86mmx supported, everything uses intrinsics.
Evan Cheng470a6ad2006-02-22 02:26:30 +0000753 }
754
Dale Johannesen0488fb62010-09-30 23:57:10 +0000755 // MMX-sized vectors (other than x86mmx) are expected to be expanded
756 // into smaller operations.
757 setOperationAction(ISD::MULHS, MVT::v8i8, Expand);
758 setOperationAction(ISD::MULHS, MVT::v4i16, Expand);
759 setOperationAction(ISD::MULHS, MVT::v2i32, Expand);
760 setOperationAction(ISD::MULHS, MVT::v1i64, Expand);
761 setOperationAction(ISD::AND, MVT::v8i8, Expand);
762 setOperationAction(ISD::AND, MVT::v4i16, Expand);
763 setOperationAction(ISD::AND, MVT::v2i32, Expand);
764 setOperationAction(ISD::AND, MVT::v1i64, Expand);
765 setOperationAction(ISD::OR, MVT::v8i8, Expand);
766 setOperationAction(ISD::OR, MVT::v4i16, Expand);
767 setOperationAction(ISD::OR, MVT::v2i32, Expand);
768 setOperationAction(ISD::OR, MVT::v1i64, Expand);
769 setOperationAction(ISD::XOR, MVT::v8i8, Expand);
770 setOperationAction(ISD::XOR, MVT::v4i16, Expand);
771 setOperationAction(ISD::XOR, MVT::v2i32, Expand);
772 setOperationAction(ISD::XOR, MVT::v1i64, Expand);
773 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Expand);
774 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Expand);
775 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Expand);
776 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Expand);
777 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v1i64, Expand);
778 setOperationAction(ISD::SELECT, MVT::v8i8, Expand);
779 setOperationAction(ISD::SELECT, MVT::v4i16, Expand);
780 setOperationAction(ISD::SELECT, MVT::v2i32, Expand);
781 setOperationAction(ISD::SELECT, MVT::v1i64, Expand);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000782 setOperationAction(ISD::BITCAST, MVT::v8i8, Expand);
783 setOperationAction(ISD::BITCAST, MVT::v4i16, Expand);
784 setOperationAction(ISD::BITCAST, MVT::v2i32, Expand);
785 setOperationAction(ISD::BITCAST, MVT::v1i64, Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000786
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000787 if (!UseSoftFloat && Subtarget->hasXMM()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000788 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000789
Owen Anderson825b72b2009-08-11 20:47:22 +0000790 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
791 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
792 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
793 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
794 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
795 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
796 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
797 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
798 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
799 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
800 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
801 setOperationAction(ISD::VSETCC, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000802 }
803
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000804 if (!UseSoftFloat && Subtarget->hasXMMInt()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000805 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000806
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000807 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
808 // registers cannot be used even for integer operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000809 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
810 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
811 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
812 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000813
Owen Anderson825b72b2009-08-11 20:47:22 +0000814 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
815 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
816 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
817 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
818 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
819 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
820 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
821 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
822 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
823 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
824 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
825 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
826 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
827 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
828 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
829 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000830
Owen Anderson825b72b2009-08-11 20:47:22 +0000831 setOperationAction(ISD::VSETCC, MVT::v2f64, Custom);
832 setOperationAction(ISD::VSETCC, MVT::v16i8, Custom);
833 setOperationAction(ISD::VSETCC, MVT::v8i16, Custom);
834 setOperationAction(ISD::VSETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000835
Owen Anderson825b72b2009-08-11 20:47:22 +0000836 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
837 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
838 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
839 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
840 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000841
Mon P Wangeb38ebf2010-01-24 00:05:03 +0000842 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2f64, Custom);
843 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2i64, Custom);
844 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i8, Custom);
845 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i16, Custom);
846 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Custom);
847
Evan Cheng2c3ae372006-04-12 21:21:57 +0000848 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Owen Anderson825b72b2009-08-11 20:47:22 +0000849 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
850 EVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000851 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000852 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000853 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000854 // Do not attempt to custom lower non-128-bit vectors
855 if (!VT.is128BitVector())
856 continue;
Owen Anderson825b72b2009-08-11 20:47:22 +0000857 setOperationAction(ISD::BUILD_VECTOR,
858 VT.getSimpleVT().SimpleTy, Custom);
859 setOperationAction(ISD::VECTOR_SHUFFLE,
860 VT.getSimpleVT().SimpleTy, Custom);
861 setOperationAction(ISD::EXTRACT_VECTOR_ELT,
862 VT.getSimpleVT().SimpleTy, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000863 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000864
Owen Anderson825b72b2009-08-11 20:47:22 +0000865 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
866 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
867 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
868 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
869 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
870 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000871
Nate Begemancdd1eec2008-02-12 22:51:28 +0000872 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000873 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
874 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000875 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000876
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000877 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000878 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; i++) {
879 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
Owen Andersone50ed302009-08-10 22:56:29 +0000880 EVT VT = SVT;
David Greene9b9838d2009-06-29 16:47:10 +0000881
882 // Do not attempt to promote non-128-bit vectors
Chris Lattner32b4b5a2010-07-05 05:53:14 +0000883 if (!VT.is128BitVector())
David Greene9b9838d2009-06-29 16:47:10 +0000884 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +0000885
Owen Andersond6662ad2009-08-10 20:46:15 +0000886 setOperationAction(ISD::AND, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000887 AddPromotedToType (ISD::AND, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000888 setOperationAction(ISD::OR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000889 AddPromotedToType (ISD::OR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000890 setOperationAction(ISD::XOR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000891 AddPromotedToType (ISD::XOR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000892 setOperationAction(ISD::LOAD, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000893 AddPromotedToType (ISD::LOAD, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000894 setOperationAction(ISD::SELECT, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000895 AddPromotedToType (ISD::SELECT, SVT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000896 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000897
Owen Anderson825b72b2009-08-11 20:47:22 +0000898 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000899
Evan Cheng2c3ae372006-04-12 21:21:57 +0000900 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000901 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
902 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
903 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
904 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000905
Owen Anderson825b72b2009-08-11 20:47:22 +0000906 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
907 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000908 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000909
Nate Begeman14d12ca2008-02-11 04:19:36 +0000910 if (Subtarget->hasSSE41()) {
Dale Johannesen54feef22010-05-27 20:12:41 +0000911 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
912 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
913 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
914 setOperationAction(ISD::FRINT, MVT::f32, Legal);
915 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
916 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
917 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
918 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
919 setOperationAction(ISD::FRINT, MVT::f64, Legal);
920 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
921
Nate Begeman14d12ca2008-02-11 04:19:36 +0000922 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +0000923 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000924
Nate Begemanbdcb5af2010-07-27 22:37:06 +0000925 // Can turn SHL into an integer multiply.
926 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
Nate Begeman51409212010-07-28 00:21:48 +0000927 setOperationAction(ISD::SHL, MVT::v16i8, Custom);
Nate Begemanbdcb5af2010-07-27 22:37:06 +0000928
Nate Begeman14d12ca2008-02-11 04:19:36 +0000929 // i8 and i16 vectors are custom , because the source register and source
930 // source memory operand types are not the same width. f32 vectors are
931 // custom since the immediate controlling the insert encodes additional
932 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +0000933 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
934 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
935 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
936 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000937
Owen Anderson825b72b2009-08-11 20:47:22 +0000938 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
939 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
940 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
941 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000942
943 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000944 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
945 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000946 }
947 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000948
Chris Lattnera34b3cf2010-12-19 20:03:11 +0000949 if (Subtarget->hasSSE42())
Owen Anderson825b72b2009-08-11 20:47:22 +0000950 setOperationAction(ISD::VSETCC, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000951
David Greene9b9838d2009-06-29 16:47:10 +0000952 if (!UseSoftFloat && Subtarget->hasAVX()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000953 addRegisterClass(MVT::v8f32, X86::VR256RegisterClass);
954 addRegisterClass(MVT::v4f64, X86::VR256RegisterClass);
955 addRegisterClass(MVT::v8i32, X86::VR256RegisterClass);
956 addRegisterClass(MVT::v4i64, X86::VR256RegisterClass);
Bruno Cardoso Lopes405f11b2010-08-10 01:43:16 +0000957 addRegisterClass(MVT::v32i8, X86::VR256RegisterClass);
David Greened94c1012009-06-29 22:50:51 +0000958
Owen Anderson825b72b2009-08-11 20:47:22 +0000959 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
960 setOperationAction(ISD::LOAD, MVT::v8i32, Legal);
961 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
962 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
David Greene54d8eba2011-01-27 22:38:56 +0000963
Owen Anderson825b72b2009-08-11 20:47:22 +0000964 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
965 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
966 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
967 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
968 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
969 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000970
Owen Anderson825b72b2009-08-11 20:47:22 +0000971 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
972 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
973 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
974 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
975 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
976 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000977
David Greene54d8eba2011-01-27 22:38:56 +0000978 // Custom lower build_vector, vector_shuffle, scalar_to_vector,
979 // insert_vector_elt extract_subvector and extract_vector_elt for
980 // 256-bit types.
981 for (unsigned i = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
982 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE;
983 ++i) {
984 MVT::SimpleValueType VT = (MVT::SimpleValueType)i;
985 // Do not attempt to custom lower non-256-bit vectors
986 if (!isPowerOf2_32(MVT(VT).getVectorNumElements())
987 || (MVT(VT).getSizeInBits() < 256))
David Greene9b9838d2009-06-29 16:47:10 +0000988 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000989 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
990 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
David Greene54d8eba2011-01-27 22:38:56 +0000991 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000992 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
David Greene54d8eba2011-01-27 22:38:56 +0000993 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000994 }
David Greene54d8eba2011-01-27 22:38:56 +0000995 // Custom-lower insert_subvector and extract_subvector based on
996 // the result type.
997 for (unsigned i = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
998 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE;
999 ++i) {
1000 MVT::SimpleValueType VT = (MVT::SimpleValueType)i;
1001 // Do not attempt to custom lower non-256-bit vectors
1002 if (!isPowerOf2_32(MVT(VT).getVectorNumElements()))
David Greene9b9838d2009-06-29 16:47:10 +00001003 continue;
David Greene54d8eba2011-01-27 22:38:56 +00001004
1005 if (MVT(VT).getSizeInBits() == 128) {
1006 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001007 }
David Greene54d8eba2011-01-27 22:38:56 +00001008 else if (MVT(VT).getSizeInBits() == 256) {
1009 setOperationAction(ISD::INSERT_SUBVECTOR, VT, Custom);
1010 }
David Greene9b9838d2009-06-29 16:47:10 +00001011 }
1012
David Greene54d8eba2011-01-27 22:38:56 +00001013 // Promote v32i8, v16i16, v8i32 select, and, or, xor to v4i64.
1014 // Don't promote loads because we need them for VPERM vector index versions.
1015
1016 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
1017 VT != (unsigned)MVT::LAST_VECTOR_VALUETYPE;
1018 VT++) {
1019 if (!isPowerOf2_32(MVT((MVT::SimpleValueType)VT).getVectorNumElements())
1020 || (MVT((MVT::SimpleValueType)VT).getSizeInBits() < 256))
1021 continue;
1022 setOperationAction(ISD::AND, (MVT::SimpleValueType)VT, Promote);
1023 AddPromotedToType (ISD::AND, (MVT::SimpleValueType)VT, MVT::v4i64);
1024 setOperationAction(ISD::OR, (MVT::SimpleValueType)VT, Promote);
1025 AddPromotedToType (ISD::OR, (MVT::SimpleValueType)VT, MVT::v4i64);
1026 setOperationAction(ISD::XOR, (MVT::SimpleValueType)VT, Promote);
1027 AddPromotedToType (ISD::XOR, (MVT::SimpleValueType)VT, MVT::v4i64);
1028 //setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Promote);
1029 //AddPromotedToType (ISD::LOAD, (MVT::SimpleValueType)VT, MVT::v4i64);
1030 setOperationAction(ISD::SELECT, (MVT::SimpleValueType)VT, Promote);
1031 AddPromotedToType (ISD::SELECT, (MVT::SimpleValueType)VT, MVT::v4i64);
1032 }
David Greene9b9838d2009-06-29 16:47:10 +00001033 }
1034
Evan Cheng6be2c582006-04-05 23:38:46 +00001035 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +00001036 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +00001037
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001038
Eli Friedman962f5492010-06-02 19:35:46 +00001039 // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
1040 // handle type legalization for these operations here.
Dan Gohman71c62a22010-06-02 19:13:40 +00001041 //
Eli Friedman962f5492010-06-02 19:35:46 +00001042 // FIXME: We really should do custom legalization for addition and
1043 // subtraction on x86-32 once PR3203 is fixed. We really can't do much better
1044 // than generic legalization for 64-bit multiplication-with-overflow, though.
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001045 for (unsigned i = 0, e = 3+Subtarget->is64Bit(); i != e; ++i) {
1046 // Add/Sub/Mul with overflow operations are custom lowered.
1047 MVT VT = IntVTs[i];
1048 setOperationAction(ISD::SADDO, VT, Custom);
1049 setOperationAction(ISD::UADDO, VT, Custom);
1050 setOperationAction(ISD::SSUBO, VT, Custom);
1051 setOperationAction(ISD::USUBO, VT, Custom);
1052 setOperationAction(ISD::SMULO, VT, Custom);
1053 setOperationAction(ISD::UMULO, VT, Custom);
Eli Friedmana993f0a2010-06-02 00:27:18 +00001054 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001055
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001056 // There are no 8-bit 3-address imul/mul instructions
1057 setOperationAction(ISD::SMULO, MVT::i8, Expand);
1058 setOperationAction(ISD::UMULO, MVT::i8, Expand);
Bill Wendling41ea7e72008-11-24 19:21:46 +00001059
Evan Chengd54f2d52009-03-31 19:38:51 +00001060 if (!Subtarget->is64Bit()) {
1061 // These libcalls are not available in 32-bit.
1062 setLibcallName(RTLIB::SHL_I128, 0);
1063 setLibcallName(RTLIB::SRL_I128, 0);
1064 setLibcallName(RTLIB::SRA_I128, 0);
1065 }
1066
Evan Cheng206ee9d2006-07-07 08:33:52 +00001067 // We have target-specific dag combine patterns for the following nodes:
1068 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00001069 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Evan Chengd880b972008-05-09 21:53:03 +00001070 setTargetDAGCombine(ISD::BUILD_VECTOR);
Chris Lattner83e6c992006-10-04 06:57:07 +00001071 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +00001072 setTargetDAGCombine(ISD::SHL);
1073 setTargetDAGCombine(ISD::SRA);
1074 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +00001075 setTargetDAGCombine(ISD::OR);
Nate Begemanb65c1752010-12-17 22:55:37 +00001076 setTargetDAGCombine(ISD::AND);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001077 setTargetDAGCombine(ISD::ADD);
1078 setTargetDAGCombine(ISD::SUB);
Chris Lattner149a4e52008-02-22 02:09:43 +00001079 setTargetDAGCombine(ISD::STORE);
Evan Cheng2e489c42009-12-16 00:53:11 +00001080 setTargetDAGCombine(ISD::ZERO_EXTEND);
Evan Cheng0b0cd912009-03-28 05:57:29 +00001081 if (Subtarget->is64Bit())
1082 setTargetDAGCombine(ISD::MUL);
Evan Cheng206ee9d2006-07-07 08:33:52 +00001083
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001084 computeRegisterProperties();
1085
Evan Cheng05219282011-01-06 06:52:41 +00001086 // On Darwin, -Os means optimize for size without hurting performance,
1087 // do not reduce the limit.
Dan Gohman87060f52008-06-30 21:00:56 +00001088 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001089 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 16 : 8;
Evan Cheng255f20f2010-04-01 06:04:33 +00001090 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001091 maxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
1092 maxStoresPerMemmove = 8; // For @llvm.memmove -> sequence of stores
1093 maxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Evan Chengfb8075d2008-02-28 00:43:03 +00001094 setPrefLoopAlignment(16);
Evan Cheng6ebf7bc2009-05-13 21:42:09 +00001095 benefitFromCodePlacementOpt = true;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001096}
1097
Scott Michel5b8f82e2008-03-10 15:42:14 +00001098
Owen Anderson825b72b2009-08-11 20:47:22 +00001099MVT::SimpleValueType X86TargetLowering::getSetCCResultType(EVT VT) const {
1100 return MVT::i8;
Scott Michel5b8f82e2008-03-10 15:42:14 +00001101}
1102
1103
Evan Cheng29286502008-01-23 23:17:41 +00001104/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1105/// the desired ByVal argument alignment.
1106static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
1107 if (MaxAlign == 16)
1108 return;
1109 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
1110 if (VTy->getBitWidth() == 128)
1111 MaxAlign = 16;
Evan Cheng29286502008-01-23 23:17:41 +00001112 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
1113 unsigned EltAlign = 0;
1114 getMaxByValAlign(ATy->getElementType(), EltAlign);
1115 if (EltAlign > MaxAlign)
1116 MaxAlign = EltAlign;
1117 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
1118 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1119 unsigned EltAlign = 0;
1120 getMaxByValAlign(STy->getElementType(i), EltAlign);
1121 if (EltAlign > MaxAlign)
1122 MaxAlign = EltAlign;
1123 if (MaxAlign == 16)
1124 break;
1125 }
1126 }
1127 return;
1128}
1129
1130/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1131/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001132/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1133/// are at 4-byte boundaries.
Evan Cheng29286502008-01-23 23:17:41 +00001134unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001135 if (Subtarget->is64Bit()) {
1136 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001137 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001138 if (TyAlign > 8)
1139 return TyAlign;
1140 return 8;
1141 }
1142
Evan Cheng29286502008-01-23 23:17:41 +00001143 unsigned Align = 4;
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001144 if (Subtarget->hasXMM())
Dale Johannesen0c191872008-02-08 19:48:20 +00001145 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001146 return Align;
1147}
Chris Lattner2b02a442007-02-25 08:29:00 +00001148
Evan Chengf0df0312008-05-15 08:39:06 +00001149/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001150/// and store operations as a result of memset, memcpy, and memmove
1151/// lowering. If DstAlign is zero that means it's safe to destination
1152/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1153/// means there isn't a need to check it against alignment requirement,
1154/// probably because the source does not need to be loaded. If
1155/// 'NonScalarIntSafe' is true, that means it's safe to return a
1156/// non-scalar-integer type, e.g. empty string source, constant, or loaded
1157/// from memory. 'MemcpyStrSrc' indicates whether the memcpy source is
1158/// constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001159/// It returns EVT::Other if the type should be determined using generic
1160/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001161EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001162X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1163 unsigned DstAlign, unsigned SrcAlign,
Evan Chengf28f8bc2010-04-02 19:36:14 +00001164 bool NonScalarIntSafe,
Evan Chengc3b0c342010-04-08 07:37:57 +00001165 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001166 MachineFunction &MF) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001167 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1168 // linux. This is because the stack realignment code can't handle certain
1169 // cases like PR2962. This should be removed when PR2962 is fixed.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001170 const Function *F = MF.getFunction();
Evan Chenga5e13622011-01-07 19:35:30 +00001171 if (NonScalarIntSafe &&
1172 !F->hasFnAttr(Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001173 if (Size >= 16 &&
Evan Chenga5e13622011-01-07 19:35:30 +00001174 (Subtarget->isUnalignedMemAccessFast() ||
1175 ((DstAlign == 0 || DstAlign >= 16) &&
1176 (SrcAlign == 0 || SrcAlign >= 16))) &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001177 Subtarget->getStackAlignment() >= 16) {
1178 if (Subtarget->hasSSE2())
1179 return MVT::v4i32;
Evan Chengf28f8bc2010-04-02 19:36:14 +00001180 if (Subtarget->hasSSE1())
Evan Cheng255f20f2010-04-01 06:04:33 +00001181 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001182 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001183 !Subtarget->is64Bit() &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001184 Subtarget->getStackAlignment() >= 8 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001185 Subtarget->hasXMMInt()) {
Evan Chengc3b0c342010-04-08 07:37:57 +00001186 // Do not use f64 to lower memcpy if source is string constant. It's
1187 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001188 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001189 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001190 }
Evan Chengf0df0312008-05-15 08:39:06 +00001191 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001192 return MVT::i64;
1193 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001194}
1195
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001196/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1197/// current function. The returned value is a member of the
1198/// MachineJumpTableInfo::JTEntryKind enum.
1199unsigned X86TargetLowering::getJumpTableEncoding() const {
1200 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1201 // symbol.
1202 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1203 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001204 return MachineJumpTableInfo::EK_Custom32;
Michael J. Spencerec38de22010-10-10 22:04:20 +00001205
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001206 // Otherwise, use the normal jump table encoding heuristics.
1207 return TargetLowering::getJumpTableEncoding();
1208}
1209
Chris Lattnerc64daab2010-01-26 05:02:42 +00001210const MCExpr *
1211X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1212 const MachineBasicBlock *MBB,
1213 unsigned uid,MCContext &Ctx) const{
1214 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1215 Subtarget->isPICStyleGOT());
1216 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1217 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001218 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1219 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001220}
1221
Evan Chengcc415862007-11-09 01:32:10 +00001222/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1223/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001224SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001225 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001226 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001227 // This doesn't have DebugLoc associated with it, but is not really the
1228 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001229 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001230 return Table;
1231}
1232
Chris Lattner589c6f62010-01-26 06:28:43 +00001233/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1234/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1235/// MCExpr.
1236const MCExpr *X86TargetLowering::
1237getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1238 MCContext &Ctx) const {
1239 // X86-64 uses RIP relative addressing based on the jump table label.
1240 if (Subtarget->isPICStyleRIPRel())
1241 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1242
1243 // Otherwise, the reference is relative to the PIC base.
Chris Lattner142b5312010-11-14 22:48:15 +00001244 return MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
Chris Lattner589c6f62010-01-26 06:28:43 +00001245}
1246
Bill Wendlingb4202b82009-07-01 18:50:55 +00001247/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +00001248unsigned X86TargetLowering::getFunctionAlignment(const Function *F) const {
Dan Gohman25103a22009-08-18 00:20:06 +00001249 return F->hasFnAttr(Attribute::OptimizeForSize) ? 0 : 4;
Bill Wendling20c568f2009-06-30 22:38:32 +00001250}
1251
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001252// FIXME: Why this routine is here? Move to RegInfo!
Evan Chengdee81012010-07-26 21:50:05 +00001253std::pair<const TargetRegisterClass*, uint8_t>
1254X86TargetLowering::findRepresentativeClass(EVT VT) const{
1255 const TargetRegisterClass *RRC = 0;
1256 uint8_t Cost = 1;
1257 switch (VT.getSimpleVT().SimpleTy) {
1258 default:
1259 return TargetLowering::findRepresentativeClass(VT);
1260 case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
1261 RRC = (Subtarget->is64Bit()
1262 ? X86::GR64RegisterClass : X86::GR32RegisterClass);
1263 break;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001264 case MVT::x86mmx:
Evan Chengdee81012010-07-26 21:50:05 +00001265 RRC = X86::VR64RegisterClass;
1266 break;
1267 case MVT::f32: case MVT::f64:
1268 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1269 case MVT::v4f32: case MVT::v2f64:
1270 case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1271 case MVT::v4f64:
1272 RRC = X86::VR128RegisterClass;
1273 break;
1274 }
1275 return std::make_pair(RRC, Cost);
1276}
1277
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001278// FIXME: Why this routine is here? Move to RegInfo!
Evan Cheng70017e42010-07-24 00:39:05 +00001279unsigned
1280X86TargetLowering::getRegPressureLimit(const TargetRegisterClass *RC,
1281 MachineFunction &MF) const {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001282 const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
Anton Korobeynikovd0c38172010-11-18 21:19:35 +00001283
1284 unsigned FPDiff = TFI->hasFP(MF) ? 1 : 0;
Evan Cheng70017e42010-07-24 00:39:05 +00001285 switch (RC->getID()) {
1286 default:
1287 return 0;
1288 case X86::GR32RegClassID:
1289 return 4 - FPDiff;
1290 case X86::GR64RegClassID:
1291 return 8 - FPDiff;
1292 case X86::VR128RegClassID:
1293 return Subtarget->is64Bit() ? 10 : 4;
1294 case X86::VR64RegClassID:
1295 return 4;
1296 }
1297}
1298
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001299bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1300 unsigned &Offset) const {
1301 if (!Subtarget->isTargetLinux())
1302 return false;
1303
1304 if (Subtarget->is64Bit()) {
1305 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1306 Offset = 0x28;
1307 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1308 AddressSpace = 256;
1309 else
1310 AddressSpace = 257;
1311 } else {
1312 // %gs:0x14 on i386
1313 Offset = 0x14;
1314 AddressSpace = 256;
1315 }
1316 return true;
1317}
1318
1319
Chris Lattner2b02a442007-02-25 08:29:00 +00001320//===----------------------------------------------------------------------===//
1321// Return Value Calling Convention Implementation
1322//===----------------------------------------------------------------------===//
1323
Chris Lattner59ed56b2007-02-28 04:55:35 +00001324#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001325
Michael J. Spencerec38de22010-10-10 22:04:20 +00001326bool
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001327X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001328 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001329 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001330 SmallVector<CCValAssign, 16> RVLocs;
1331 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001332 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001333 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001334}
1335
Dan Gohman98ca4f22009-08-05 01:29:28 +00001336SDValue
1337X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001338 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001339 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001340 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001341 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001342 MachineFunction &MF = DAG.getMachineFunction();
1343 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001344
Chris Lattner9774c912007-02-27 05:28:59 +00001345 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001346 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1347 RVLocs, *DAG.getContext());
1348 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001349
Evan Chengdcea1632010-02-04 02:40:39 +00001350 // Add the regs to the liveout set for the function.
1351 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1352 for (unsigned i = 0; i != RVLocs.size(); ++i)
1353 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1354 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001355
Dan Gohman475871a2008-07-27 21:46:04 +00001356 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001357
Dan Gohman475871a2008-07-27 21:46:04 +00001358 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001359 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1360 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001361 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1362 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001363
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001364 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001365 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1366 CCValAssign &VA = RVLocs[i];
1367 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001368 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001369 EVT ValVT = ValToCopy.getValueType();
1370
Dale Johannesenc4510512010-09-24 19:05:48 +00001371 // If this is x86-64, and we disabled SSE, we can't return FP values,
1372 // or SSE or MMX vectors.
1373 if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
1374 VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001375 (Subtarget->is64Bit() && !Subtarget->hasXMM())) {
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001376 report_fatal_error("SSE register return with SSE disabled");
1377 }
1378 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1379 // llvm-gcc has never done it right and no one has noticed, so this
1380 // should be OK for now.
1381 if (ValVT == MVT::f64 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001382 (Subtarget->is64Bit() && !Subtarget->hasXMMInt()))
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001383 report_fatal_error("SSE2 register return with SSE2 disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001384
Chris Lattner447ff682008-03-11 03:23:40 +00001385 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1386 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001387 if (VA.getLocReg() == X86::ST0 ||
1388 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001389 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1390 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001391 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001392 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001393 RetOps.push_back(ValToCopy);
1394 // Don't emit a copytoreg.
1395 continue;
1396 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001397
Evan Cheng242b38b2009-02-23 09:03:22 +00001398 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1399 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001400 if (Subtarget->is64Bit()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001401 if (ValVT == MVT::x86mmx) {
Chris Lattner97a2a562010-08-26 05:24:29 +00001402 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001403 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ValToCopy);
Eric Christopher90eb4022010-07-22 00:26:08 +00001404 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1405 ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001406 // If we don't have SSE2 available, convert to v4f32 so the generated
1407 // register is legal.
1408 if (!Subtarget->hasSSE2())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001409 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32,ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001410 }
Evan Cheng242b38b2009-02-23 09:03:22 +00001411 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001412 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00001413
Dale Johannesendd64c412009-02-04 00:33:20 +00001414 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001415 Flag = Chain.getValue(1);
1416 }
Dan Gohman61a92132008-04-21 23:59:07 +00001417
1418 // The x86-64 ABI for returning structs by value requires that we copy
1419 // the sret argument into %rax for the return. We saved the argument into
1420 // a virtual register in the entry block, so now we copy the value out
1421 // and into %rax.
1422 if (Subtarget->is64Bit() &&
1423 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1424 MachineFunction &MF = DAG.getMachineFunction();
1425 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1426 unsigned Reg = FuncInfo->getSRetReturnReg();
Michael J. Spencerec38de22010-10-10 22:04:20 +00001427 assert(Reg &&
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001428 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001429 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001430
Dale Johannesendd64c412009-02-04 00:33:20 +00001431 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001432 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001433
1434 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001435 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001436 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001437
Chris Lattner447ff682008-03-11 03:23:40 +00001438 RetOps[0] = Chain; // Update chain.
1439
1440 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001441 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001442 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001443
1444 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001445 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001446}
1447
Evan Cheng3d2125c2010-11-30 23:55:39 +00001448bool X86TargetLowering::isUsedByReturnOnly(SDNode *N) const {
1449 if (N->getNumValues() != 1)
1450 return false;
1451 if (!N->hasNUsesOfValue(1, 0))
1452 return false;
1453
1454 SDNode *Copy = *N->use_begin();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001455 if (Copy->getOpcode() != ISD::CopyToReg &&
1456 Copy->getOpcode() != ISD::FP_EXTEND)
Evan Cheng3d2125c2010-11-30 23:55:39 +00001457 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001458
1459 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001460 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001461 UI != UE; ++UI) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001462 if (UI->getOpcode() != X86ISD::RET_FLAG)
1463 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001464 HasRet = true;
1465 }
Evan Cheng3d2125c2010-11-30 23:55:39 +00001466
Evan Cheng1bf891a2010-12-01 22:59:46 +00001467 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001468}
1469
Dan Gohman98ca4f22009-08-05 01:29:28 +00001470/// LowerCallResult - Lower the result values of a call into the
1471/// appropriate copies out of appropriate physical registers.
1472///
1473SDValue
1474X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001475 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001476 const SmallVectorImpl<ISD::InputArg> &Ins,
1477 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001478 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001479
Chris Lattnere32bbf62007-02-28 07:09:55 +00001480 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001481 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001482 bool Is64Bit = Subtarget->is64Bit();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001483 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +00001484 RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001485 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001486
Chris Lattner3085e152007-02-25 08:59:22 +00001487 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001488 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001489 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001490 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001491
Torok Edwin3f142c32009-02-01 18:15:56 +00001492 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001493 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001494 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasXMM())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001495 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001496 }
1497
Evan Cheng79fb3b42009-02-20 20:43:02 +00001498 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001499
1500 // If this is a call to a function that returns an fp value on the floating
1501 // point stack, we must guarantee the the value is popped from the stack, so
1502 // a CopyFromReg is not good enough - the copy instruction may be eliminated
1503 // if the return value is not used. We use the FpGET_ST0 instructions
1504 // instead.
1505 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1506 // If we prefer to use the value in xmm registers, copy it out as f80 and
1507 // use a truncate to move it from fp stack reg to xmm reg.
1508 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
1509 bool isST0 = VA.getLocReg() == X86::ST0;
1510 unsigned Opc = 0;
1511 if (CopyVT == MVT::f32) Opc = isST0 ? X86::FpGET_ST0_32:X86::FpGET_ST1_32;
1512 if (CopyVT == MVT::f64) Opc = isST0 ? X86::FpGET_ST0_64:X86::FpGET_ST1_64;
1513 if (CopyVT == MVT::f80) Opc = isST0 ? X86::FpGET_ST0_80:X86::FpGET_ST1_80;
1514 SDValue Ops[] = { Chain, InFlag };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001515 Chain = SDValue(DAG.getMachineNode(Opc, dl, CopyVT, MVT::Other, MVT::Glue,
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001516 Ops, 2), 1);
1517 Val = Chain.getValue(0);
1518
1519 // Round the f80 to the right size, which also moves it to the appropriate
1520 // xmm register.
1521 if (CopyVT != VA.getValVT())
1522 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1523 // This truncation won't change the value.
1524 DAG.getIntPtrConstant(1));
1525 } else if (Is64Bit && CopyVT.isVector() && CopyVT.getSizeInBits() == 64) {
Evan Cheng242b38b2009-02-23 09:03:22 +00001526 // For x86-64, MMX values are returned in XMM0 / XMM1 except for v1i64.
1527 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
1528 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001529 MVT::v2i64, InFlag).getValue(1);
Evan Cheng242b38b2009-02-23 09:03:22 +00001530 Val = Chain.getValue(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00001531 Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1532 Val, DAG.getConstant(0, MVT::i64));
Evan Cheng242b38b2009-02-23 09:03:22 +00001533 } else {
1534 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001535 MVT::i64, InFlag).getValue(1);
Evan Cheng242b38b2009-02-23 09:03:22 +00001536 Val = Chain.getValue(0);
1537 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001538 Val = DAG.getNode(ISD::BITCAST, dl, CopyVT, Val);
Evan Cheng79fb3b42009-02-20 20:43:02 +00001539 } else {
1540 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1541 CopyVT, InFlag).getValue(1);
1542 Val = Chain.getValue(0);
1543 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001544 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001545 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001546 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001547
Dan Gohman98ca4f22009-08-05 01:29:28 +00001548 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001549}
1550
1551
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001552//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001553// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001554//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001555// StdCall calling convention seems to be standard for many Windows' API
1556// routines and around. It differs from C calling convention just a little:
1557// callee should clean up the stack, not caller. Symbols should be also
1558// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001559// For info on fast calling convention see Fast Calling Convention (tail call)
1560// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001561
Dan Gohman98ca4f22009-08-05 01:29:28 +00001562/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001563/// semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001564static bool CallIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
1565 if (Outs.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 Outs[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001569}
1570
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001571/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001572/// return semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001573static bool
1574ArgsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
1575 if (Ins.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001576 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001577
Dan Gohman98ca4f22009-08-05 01:29:28 +00001578 return Ins[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001579}
1580
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001581/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1582/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001583/// the specific parameter attribute. The copy will be passed as a byval
1584/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001585static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001586CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001587 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1588 DebugLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00001589 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Michael J. Spencerec38de22010-10-10 22:04:20 +00001590
Dale Johannesendd64c412009-02-04 00:33:20 +00001591 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Mon P Wang20adc9d2010-04-04 03:10:48 +00001592 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001593 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001594}
1595
Chris Lattner29689432010-03-11 00:22:57 +00001596/// IsTailCallConvention - Return true if the calling convention is one that
1597/// supports tail call optimization.
1598static bool IsTailCallConvention(CallingConv::ID CC) {
1599 return (CC == CallingConv::Fast || CC == CallingConv::GHC);
1600}
1601
Evan Cheng0c439eb2010-01-27 00:07:07 +00001602/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1603/// a tailcall target by changing its ABI.
1604static bool FuncIsMadeTailCallSafe(CallingConv::ID CC) {
Chris Lattner29689432010-03-11 00:22:57 +00001605 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001606}
1607
Dan Gohman98ca4f22009-08-05 01:29:28 +00001608SDValue
1609X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001610 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001611 const SmallVectorImpl<ISD::InputArg> &Ins,
1612 DebugLoc dl, SelectionDAG &DAG,
1613 const CCValAssign &VA,
1614 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001615 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001616 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001617 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Evan Cheng0c439eb2010-01-27 00:07:07 +00001618 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001619 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001620 EVT ValVT;
1621
1622 // If value is passed by pointer we have address passed instead of the value
1623 // itself.
1624 if (VA.getLocInfo() == CCValAssign::Indirect)
1625 ValVT = VA.getLocVT();
1626 else
1627 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001628
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001629 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001630 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001631 // In case of tail call optimization mark all arguments mutable. Since they
1632 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001633 if (Flags.isByVal()) {
1634 int FI = MFI->CreateFixedObject(Flags.getByValSize(),
Evan Chenged2ae132010-07-03 00:40:23 +00001635 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001636 return DAG.getFrameIndex(FI, getPointerTy());
1637 } else {
1638 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001639 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001640 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1641 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001642 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00001643 false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001644 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001645}
1646
Dan Gohman475871a2008-07-27 21:46:04 +00001647SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001648X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001649 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001650 bool isVarArg,
1651 const SmallVectorImpl<ISD::InputArg> &Ins,
1652 DebugLoc dl,
1653 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001654 SmallVectorImpl<SDValue> &InVals)
1655 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001656 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001657 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001658
Gordon Henriksen86737662008-01-05 16:56:59 +00001659 const Function* Fn = MF.getFunction();
1660 if (Fn->hasExternalLinkage() &&
1661 Subtarget->isTargetCygMing() &&
1662 Fn->getName() == "main")
1663 FuncInfo->setForceFramePointer(true);
1664
Evan Cheng1bc78042006-04-26 01:20:17 +00001665 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001666 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001667 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001668
Chris Lattner29689432010-03-11 00:22:57 +00001669 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1670 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001671
Chris Lattner638402b2007-02-28 07:00:42 +00001672 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001673 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001674 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1675 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001676
1677 // Allocate shadow area for Win64
1678 if (IsWin64) {
1679 CCInfo.AllocateStack(32, 8);
1680 }
1681
Duncan Sands45907662010-10-31 13:21:44 +00001682 CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001683
Chris Lattnerf39f7712007-02-28 05:46:49 +00001684 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001685 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001686 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1687 CCValAssign &VA = ArgLocs[i];
1688 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1689 // places.
1690 assert(VA.getValNo() != LastVal &&
1691 "Don't support value assigned to multiple locs yet");
1692 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001693
Chris Lattnerf39f7712007-02-28 05:46:49 +00001694 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001695 EVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001696 TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001697 if (RegVT == MVT::i32)
Chris Lattnerf39f7712007-02-28 05:46:49 +00001698 RC = X86::GR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001699 else if (Is64Bit && RegVT == MVT::i64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001700 RC = X86::GR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001701 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001702 RC = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001703 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001704 RC = X86::FR64RegisterClass;
Bruno Cardoso Lopesac098352010-08-05 23:35:51 +00001705 else if (RegVT.isVector() && RegVT.getSizeInBits() == 256)
1706 RC = X86::VR256RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001707 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001708 RC = X86::VR128RegisterClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001709 else if (RegVT == MVT::x86mmx)
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001710 RC = X86::VR64RegisterClass;
1711 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001712 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001713
Devang Patele9a7ea62011-01-31 21:38:14 +00001714 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC, dl);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001715 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001716
Chris Lattnerf39f7712007-02-28 05:46:49 +00001717 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1718 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1719 // right size.
1720 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001721 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001722 DAG.getValueType(VA.getValVT()));
1723 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001724 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001725 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001726 else if (VA.getLocInfo() == CCValAssign::BCvt)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001727 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001728
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001729 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001730 // Handle MMX values passed in XMM regs.
1731 if (RegVT.isVector()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001732 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(),
1733 ArgValue);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001734 } else
1735 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001736 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001737 } else {
1738 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001739 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001740 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001741
1742 // If value is passed via pointer - do a load.
1743 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00001744 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
1745 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001746
Dan Gohman98ca4f22009-08-05 01:29:28 +00001747 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001748 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001749
Dan Gohman61a92132008-04-21 23:59:07 +00001750 // The x86-64 ABI for returning structs by value requires that we copy
1751 // the sret argument into %rax for the return. Save the argument into
1752 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001753 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001754 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1755 unsigned Reg = FuncInfo->getSRetReturnReg();
1756 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001757 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001758 FuncInfo->setSRetReturnReg(Reg);
1759 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001760 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001761 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001762 }
1763
Chris Lattnerf39f7712007-02-28 05:46:49 +00001764 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001765 // Align stack specially for tail calls.
1766 if (FuncIsMadeTailCallSafe(CallConv))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001767 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001768
Evan Cheng1bc78042006-04-26 01:20:17 +00001769 // If the function takes variable number of arguments, make a frame index for
1770 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001771 if (isVarArg) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001772 if (!IsWin64 && (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
1773 CallConv != CallingConv::X86_ThisCall))) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00001774 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00001775 }
1776 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001777 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1778
1779 // FIXME: We should really autogenerate these arrays
1780 static const unsigned GPR64ArgRegsWin64[] = {
1781 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001782 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001783 static const unsigned GPR64ArgRegs64Bit[] = {
1784 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1785 };
1786 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001787 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1788 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1789 };
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001790 const unsigned *GPR64ArgRegs;
1791 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001792
1793 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001794 // The XMM registers which might contain var arg parameters are shadowed
1795 // in their paired GPR. So we only need to save the GPR to their home
1796 // slots.
1797 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001798 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001799 } else {
1800 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1801 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001802
1803 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit, TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001804 }
1805 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1806 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001807
Devang Patel578efa92009-06-05 21:57:13 +00001808 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001809 assert(!(NumXMMRegs && !Subtarget->hasXMM()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001810 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001811 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001812 "SSE register cannot be used when SSE is disabled!");
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001813 if (UseSoftFloat || NoImplicitFloatOps || !Subtarget->hasXMM())
Torok Edwin3f142c32009-02-01 18:15:56 +00001814 // Kernel mode asks for SSE to be disabled, so don't push them
1815 // on the stack.
1816 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001817
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001818 if (IsWin64) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001819 const TargetFrameLowering &TFI = *getTargetMachine().getFrameLowering();
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001820 // Get to the caller-allocated home save location. Add 8 to account
1821 // for the return address.
1822 int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001823 FuncInfo->setRegSaveFrameIndex(
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001824 MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001825 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
1826 } else {
1827 // For X86-64, if there are vararg parameters that are passed via
1828 // registers, then we must store them to their spots on the stack so they
1829 // may be loaded by deferencing the result of va_next.
1830 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
1831 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
1832 FuncInfo->setRegSaveFrameIndex(
1833 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00001834 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001835 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001836
Gordon Henriksen86737662008-01-05 16:56:59 +00001837 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001838 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00001839 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
1840 getPointerTy());
1841 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001842 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00001843 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
1844 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00001845 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
Devang Patele9a7ea62011-01-31 21:38:14 +00001846 X86::GR64RegisterClass, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00001847 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001848 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001849 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001850 MachinePointerInfo::getFixedStack(
1851 FuncInfo->getRegSaveFrameIndex(), Offset),
1852 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001853 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001854 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001855 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001856
Dan Gohmanface41a2009-08-16 21:24:25 +00001857 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
1858 // Now store the XMM (fp + vector) parameter registers.
1859 SmallVector<SDValue, 11> SaveXMMOps;
1860 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001861
Devang Patele9a7ea62011-01-31 21:38:14 +00001862 unsigned AL = MF.addLiveIn(X86::AL, X86::GR8RegisterClass, dl);
Dan Gohmanface41a2009-08-16 21:24:25 +00001863 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
1864 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001865
Dan Gohman1e93df62010-04-17 14:41:14 +00001866 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1867 FuncInfo->getRegSaveFrameIndex()));
1868 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1869 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00001870
Dan Gohmanface41a2009-08-16 21:24:25 +00001871 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001872 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Devang Patele9a7ea62011-01-31 21:38:14 +00001873 X86::VR128RegisterClass, dl);
Dan Gohmanface41a2009-08-16 21:24:25 +00001874 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
1875 SaveXMMOps.push_back(Val);
1876 }
1877 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
1878 MVT::Other,
1879 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00001880 }
Dan Gohmanface41a2009-08-16 21:24:25 +00001881
1882 if (!MemOps.empty())
1883 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1884 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001885 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001886 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001887
Gordon Henriksen86737662008-01-05 16:56:59 +00001888 // Some CCs need callee pop.
Dan Gohman4d3d6e12010-05-27 18:43:40 +00001889 if (Subtarget->IsCalleePop(isVarArg, CallConv)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001890 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001891 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00001892 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001893 // If this is an sret function, the return should pop the hidden pointer.
Chris Lattner29689432010-03-11 00:22:57 +00001894 if (!Is64Bit && !IsTailCallConvention(CallConv) && ArgsAreStructReturn(Ins))
Dan Gohman1e93df62010-04-17 14:41:14 +00001895 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001896 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001897
Gordon Henriksen86737662008-01-05 16:56:59 +00001898 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001899 // RegSaveFrameIndex is X86-64 only.
1900 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001901 if (CallConv == CallingConv::X86_FastCall ||
1902 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00001903 // fastcc functions can't have varargs.
1904 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00001905 }
Evan Cheng25caf632006-05-23 21:06:34 +00001906
Dan Gohman98ca4f22009-08-05 01:29:28 +00001907 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001908}
1909
Dan Gohman475871a2008-07-27 21:46:04 +00001910SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001911X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
1912 SDValue StackPtr, SDValue Arg,
1913 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00001914 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001915 ISD::ArgFlagsTy Flags) const {
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001916 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00001917 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00001918 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001919 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00001920 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001921
1922 return DAG.getStore(Chain, dl, Arg, PtrOff,
1923 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00001924 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00001925}
1926
Bill Wendling64e87322009-01-16 19:25:27 +00001927/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001928/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00001929SDValue
1930X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00001931 SDValue &OutRetAddr, SDValue Chain,
1932 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00001933 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001934 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00001935 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001936 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00001937
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001938 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00001939 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
1940 false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001941 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001942}
1943
1944/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1945/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00001946static SDValue
1947EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00001948 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00001949 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001950 // Store the return address to the appropriate stack slot.
1951 if (!FPDiff) return Chain;
1952 // Calculate the new stack slot for the return address.
1953 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00001954 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00001955 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00001956 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00001957 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001958 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00001959 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00001960 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001961 return Chain;
1962}
1963
Dan Gohman98ca4f22009-08-05 01:29:28 +00001964SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001965X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001966 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001967 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001968 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001969 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001970 const SmallVectorImpl<ISD::InputArg> &Ins,
1971 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001972 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001973 MachineFunction &MF = DAG.getMachineFunction();
1974 bool Is64Bit = Subtarget->is64Bit();
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00001975 bool IsWin64 = Subtarget->isTargetWin64();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001976 bool IsStructRet = CallIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00001977 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001978
Evan Cheng5f941932010-02-05 02:21:12 +00001979 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00001980 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00001981 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1982 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001983 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00001984
1985 // Sibcalls are automatically detected tailcalls which do not require
1986 // ABI changes.
Dan Gohman1797ed52010-02-08 20:27:50 +00001987 if (!GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00001988 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00001989
1990 if (isTailCall)
1991 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00001992 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00001993
Chris Lattner29689432010-03-11 00:22:57 +00001994 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1995 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001996
Chris Lattner638402b2007-02-28 07:00:42 +00001997 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00001998 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001999 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
2000 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002001
2002 // Allocate shadow area for Win64
2003 if (IsWin64) {
2004 CCInfo.AllocateStack(32, 8);
2005 }
2006
Duncan Sands45907662010-10-31 13:21:44 +00002007 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00002008
Chris Lattner423c5f42007-02-28 05:31:48 +00002009 // Get a count of how many bytes are to be pushed on the stack.
2010 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00002011 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00002012 // This is a sibcall. The memory operands are available in caller's
2013 // own caller's stack.
2014 NumBytes = 0;
Chris Lattner29689432010-03-11 00:22:57 +00002015 else if (GuaranteedTailCallOpt && IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00002016 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002017
Gordon Henriksen86737662008-01-05 16:56:59 +00002018 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00002019 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002020 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00002021 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00002022 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
2023 FPDiff = NumBytesCallerPushed - NumBytes;
2024
2025 // Set the delta of movement of the returnaddr stackslot.
2026 // But only set if delta is greater than previous delta.
2027 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
2028 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
2029 }
2030
Evan Chengf22f9b32010-02-06 03:28:46 +00002031 if (!IsSibcall)
2032 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002033
Dan Gohman475871a2008-07-27 21:46:04 +00002034 SDValue RetAddrFrIdx;
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002035 // Load return adress for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00002036 if (isTailCall && FPDiff)
2037 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
2038 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002039
Dan Gohman475871a2008-07-27 21:46:04 +00002040 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
2041 SmallVector<SDValue, 8> MemOpChains;
2042 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00002043
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002044 // Walk the register/memloc assignments, inserting copies/loads. In the case
2045 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00002046 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2047 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00002048 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00002049 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002050 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00002051 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00002052
Chris Lattner423c5f42007-02-28 05:31:48 +00002053 // Promote the value if needed.
2054 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002055 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00002056 case CCValAssign::Full: break;
2057 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002058 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002059 break;
2060 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002061 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002062 break;
2063 case CCValAssign::AExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002064 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
2065 // Special case: passing MMX values in XMM registers.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002066 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00002067 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
2068 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002069 } else
2070 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
2071 break;
2072 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002073 Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002074 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002075 case CCValAssign::Indirect: {
2076 // Store the argument.
2077 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00002078 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002079 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00002080 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002081 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002082 Arg = SpillSlot;
2083 break;
2084 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00002085 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002086
Chris Lattner423c5f42007-02-28 05:31:48 +00002087 if (VA.isRegLoc()) {
2088 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002089 if (isVarArg && IsWin64) {
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002090 // Win64 ABI requires argument XMM reg to be copied to the corresponding
2091 // shadow reg if callee is a varargs function.
2092 unsigned ShadowReg = 0;
2093 switch (VA.getLocReg()) {
2094 case X86::XMM0: ShadowReg = X86::RCX; break;
2095 case X86::XMM1: ShadowReg = X86::RDX; break;
2096 case X86::XMM2: ShadowReg = X86::R8; break;
2097 case X86::XMM3: ShadowReg = X86::R9; break;
2098 }
2099 if (ShadowReg)
2100 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
2101 }
Evan Chengf22f9b32010-02-06 03:28:46 +00002102 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00002103 assert(VA.isMemLoc());
2104 if (StackPtr.getNode() == 0)
2105 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
2106 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
2107 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002108 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002109 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002110
Evan Cheng32fe1032006-05-25 00:59:30 +00002111 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002112 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002113 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002114
Evan Cheng347d5f72006-04-28 21:29:37 +00002115 // Build a sequence of copy-to-reg nodes chained together with token chain
2116 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002117 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002118 // Tail call byval lowering might overwrite argument registers so in case of
2119 // tail call optimization the copies to registers are lowered later.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002120 if (!isTailCall)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002121 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002122 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002123 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002124 InFlag = Chain.getValue(1);
2125 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002126
Chris Lattner88e1fd52009-07-09 04:24:46 +00002127 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002128 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2129 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002130 if (!isTailCall) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002131 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
2132 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00002133 DebugLoc(), getPointerTy()),
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002134 InFlag);
2135 InFlag = Chain.getValue(1);
2136 } else {
2137 // If we are tail calling and generating PIC/GOT style code load the
2138 // address of the callee into ECX. The value in ecx is used as target of
2139 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2140 // for tail calls on PIC/GOT architectures. Normally we would just put the
2141 // address of GOT into ebx and then call target@PLT. But for tail calls
2142 // ebx would be restored (since ebx is callee saved) before jumping to the
2143 // target@PLT.
2144
2145 // Note: The actual moving to ECX is done further down.
2146 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2147 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2148 !G->getGlobal()->hasProtectedVisibility())
2149 Callee = LowerGlobalAddress(Callee, DAG);
2150 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002151 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002152 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002153 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002154
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002155 if (Is64Bit && isVarArg && !IsWin64) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002156 // From AMD64 ABI document:
2157 // For calls that may call functions that use varargs or stdargs
2158 // (prototype-less calls or calls to functions containing ellipsis (...) in
2159 // the declaration) %al is used as hidden argument to specify the number
2160 // of SSE registers used. The contents of %al do not need to match exactly
2161 // the number of registers, but must be an ubound on the number of SSE
2162 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002163
Gordon Henriksen86737662008-01-05 16:56:59 +00002164 // Count the number of XMM registers allocated.
2165 static const unsigned XMMArgRegs[] = {
2166 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2167 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2168 };
2169 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00002170 assert((Subtarget->hasXMM() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002171 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002172
Dale Johannesendd64c412009-02-04 00:33:20 +00002173 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Owen Anderson825b72b2009-08-11 20:47:22 +00002174 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002175 InFlag = Chain.getValue(1);
2176 }
2177
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002178
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002179 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002180 if (isTailCall) {
2181 // Force all the incoming stack arguments to be loaded from the stack
2182 // before any new outgoing arguments are stored to the stack, because the
2183 // outgoing stack slots may alias the incoming argument stack slots, and
2184 // the alias isn't otherwise explicit. This is slightly more conservative
2185 // than necessary, because it means that each store effectively depends
2186 // on every argument instead of just those arguments it would clobber.
2187 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2188
Dan Gohman475871a2008-07-27 21:46:04 +00002189 SmallVector<SDValue, 8> MemOpChains2;
2190 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002191 int FI = 0;
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002192 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00002193 InFlag = SDValue();
Dan Gohman1797ed52010-02-08 20:27:50 +00002194 if (GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002195 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2196 CCValAssign &VA = ArgLocs[i];
2197 if (VA.isRegLoc())
2198 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002199 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002200 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002201 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002202 // Create frame index.
2203 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002204 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002205 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002206 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002207
Duncan Sands276dcbd2008-03-21 09:14:45 +00002208 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002209 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002210 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002211 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002212 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00002213 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002214 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002215
Dan Gohman98ca4f22009-08-05 01:29:28 +00002216 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2217 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002218 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002219 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002220 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002221 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002222 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002223 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002224 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002225 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002226 }
2227 }
2228
2229 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002230 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002231 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002232
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002233 // Copy arguments to their registers.
2234 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002235 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002236 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002237 InFlag = Chain.getValue(1);
2238 }
Dan Gohman475871a2008-07-27 21:46:04 +00002239 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002240
Gordon Henriksen86737662008-01-05 16:56:59 +00002241 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002242 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002243 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002244 }
2245
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002246 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2247 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2248 // In the 64-bit large code model, we have to make all calls
2249 // through a register, since the call instruction's 32-bit
2250 // pc-relative offset may not be large enough to hold the whole
2251 // address.
2252 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002253 // If the callee is a GlobalAddress node (quite common, every direct call
2254 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2255 // it.
2256
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002257 // We should use extra load for direct calls to dllimported functions in
2258 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002259 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002260 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002261 unsigned char OpFlags = 0;
Eric Christopherfd179292009-08-27 18:07:15 +00002262
Chris Lattner48a7d022009-07-09 05:02:21 +00002263 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2264 // external symbols most go through the PLT in PIC mode. If the symbol
2265 // has hidden or protected visibility, or if it is static or local, then
2266 // we don't need to use the PLT - we can directly call it.
2267 if (Subtarget->isTargetELF() &&
2268 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002269 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002270 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002271 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002272 (GV->isDeclaration() || GV->isWeakForLinker()) &&
2273 Subtarget->getDarwinVers() < 9) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002274 // PC-relative references to external symbols should go through $stub,
2275 // unless we're building with the leopard linker or later, which
2276 // automatically synthesizes these stubs.
2277 OpFlags = X86II::MO_DARWIN_STUB;
2278 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002279
Devang Patel0d881da2010-07-06 22:08:15 +00002280 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002281 G->getOffset(), OpFlags);
2282 }
Bill Wendling056292f2008-09-16 21:48:12 +00002283 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002284 unsigned char OpFlags = 0;
2285
Evan Cheng1bf891a2010-12-01 22:59:46 +00002286 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2287 // external symbols should go through the PLT.
2288 if (Subtarget->isTargetELF() &&
2289 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2290 OpFlags = X86II::MO_PLT;
2291 } else if (Subtarget->isPICStyleStubAny() &&
2292 Subtarget->getDarwinVers() < 9) {
2293 // PC-relative references to external symbols should go through $stub,
2294 // unless we're building with the leopard linker or later, which
2295 // automatically synthesizes these stubs.
2296 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002297 }
Eric Christopherfd179292009-08-27 18:07:15 +00002298
Chris Lattner48a7d022009-07-09 05:02:21 +00002299 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2300 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002301 }
2302
Chris Lattnerd96d0722007-02-25 06:40:16 +00002303 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002304 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002305 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002306
Evan Chengf22f9b32010-02-06 03:28:46 +00002307 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002308 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2309 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002310 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002311 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002312
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002313 Ops.push_back(Chain);
2314 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002315
Dan Gohman98ca4f22009-08-05 01:29:28 +00002316 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002317 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002318
Gordon Henriksen86737662008-01-05 16:56:59 +00002319 // Add argument registers to the end of the list so that they are known live
2320 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002321 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2322 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2323 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002324
Evan Cheng586ccac2008-03-18 23:36:35 +00002325 // Add an implicit use GOT pointer in EBX.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002326 if (!isTailCall && Subtarget->isPICStyleGOT())
Evan Cheng586ccac2008-03-18 23:36:35 +00002327 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
2328
Anton Korobeynikov3a1e54a2010-08-17 21:06:07 +00002329 // Add an implicit use of AL for non-Windows x86 64-bit vararg functions.
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002330 if (Is64Bit && isVarArg && !IsWin64)
Owen Anderson825b72b2009-08-11 20:47:22 +00002331 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
Evan Cheng586ccac2008-03-18 23:36:35 +00002332
Gabor Greifba36cb52008-08-28 21:40:38 +00002333 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002334 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002335
Dan Gohman98ca4f22009-08-05 01:29:28 +00002336 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002337 // We used to do:
2338 //// If this is the first return lowered for this function, add the regs
2339 //// to the liveout set for the function.
2340 // This isn't right, although it's probably harmless on x86; liveouts
2341 // should be computed from returns not tail calls. Consider a void
2342 // function making a tail call to a function returning int.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002343 return DAG.getNode(X86ISD::TC_RETURN, dl,
2344 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002345 }
2346
Dale Johannesenace16102009-02-03 19:33:06 +00002347 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002348 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002349
Chris Lattner2d297092006-05-23 18:50:38 +00002350 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002351 unsigned NumBytesForCalleeToPush;
Dan Gohman4d3d6e12010-05-27 18:43:40 +00002352 if (Subtarget->IsCalleePop(isVarArg, CallConv))
Gordon Henriksen86737662008-01-05 16:56:59 +00002353 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Chris Lattner29689432010-03-11 00:22:57 +00002354 else if (!Is64Bit && !IsTailCallConvention(CallConv) && IsStructRet)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002355 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002356 // pops the hidden struct pointer, so we have to push it back.
2357 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002358 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002359 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002360 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002361
Gordon Henriksenae636f82008-01-03 16:47:34 +00002362 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002363 if (!IsSibcall) {
2364 Chain = DAG.getCALLSEQ_END(Chain,
2365 DAG.getIntPtrConstant(NumBytes, true),
2366 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2367 true),
2368 InFlag);
2369 InFlag = Chain.getValue(1);
2370 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002371
Chris Lattner3085e152007-02-25 08:59:22 +00002372 // Handle result values, copying them out of physregs into vregs that we
2373 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002374 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2375 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002376}
2377
Evan Cheng25ab6902006-09-08 06:48:29 +00002378
2379//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002380// Fast Calling Convention (tail call) implementation
2381//===----------------------------------------------------------------------===//
2382
2383// Like std call, callee cleans arguments, convention except that ECX is
2384// reserved for storing the tail called function address. Only 2 registers are
2385// free for argument passing (inreg). Tail call optimization is performed
2386// provided:
2387// * tailcallopt is enabled
2388// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002389// On X86_64 architecture with GOT-style position independent code only local
2390// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002391// To keep the stack aligned according to platform abi the function
2392// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2393// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002394// If a tail called function callee has more arguments than the caller the
2395// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002396// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002397// original REtADDR, but before the saved framepointer or the spilled registers
2398// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2399// stack layout:
2400// arg1
2401// arg2
2402// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002403// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002404// move area ]
2405// (possible EBP)
2406// ESI
2407// EDI
2408// local1 ..
2409
2410/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2411/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002412unsigned
2413X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2414 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002415 MachineFunction &MF = DAG.getMachineFunction();
2416 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002417 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002418 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002419 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002420 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002421 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002422 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2423 // Number smaller than 12 so just add the difference.
2424 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2425 } else {
2426 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002427 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002428 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002429 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002430 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002431}
2432
Evan Cheng5f941932010-02-05 02:21:12 +00002433/// MatchingStackOffset - Return true if the given stack call argument is
2434/// already available in the same position (relatively) of the caller's
2435/// incoming argument stack.
2436static
2437bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2438 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2439 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002440 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2441 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002442 if (Arg.getOpcode() == ISD::CopyFromReg) {
2443 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00002444 if (!TargetRegisterInfo::isVirtualRegister(VR))
Evan Cheng5f941932010-02-05 02:21:12 +00002445 return false;
2446 MachineInstr *Def = MRI->getVRegDef(VR);
2447 if (!Def)
2448 return false;
2449 if (!Flags.isByVal()) {
2450 if (!TII->isLoadFromStackSlot(Def, FI))
2451 return false;
2452 } else {
2453 unsigned Opcode = Def->getOpcode();
2454 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2455 Def->getOperand(1).isFI()) {
2456 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002457 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002458 } else
2459 return false;
2460 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002461 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2462 if (Flags.isByVal())
2463 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002464 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002465 // define @foo(%struct.X* %A) {
2466 // tail call @bar(%struct.X* byval %A)
2467 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002468 return false;
2469 SDValue Ptr = Ld->getBasePtr();
2470 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2471 if (!FINode)
2472 return false;
2473 FI = FINode->getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002474 } else
2475 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002476
Evan Cheng4cae1332010-03-05 08:38:04 +00002477 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002478 if (!MFI->isFixedObjectIndex(FI))
2479 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002480 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002481}
2482
Dan Gohman98ca4f22009-08-05 01:29:28 +00002483/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2484/// for tail call optimization. Targets which want to do tail call
2485/// optimization should implement this function.
2486bool
2487X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002488 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002489 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002490 bool isCalleeStructRet,
2491 bool isCallerStructRet,
Evan Chengb1712452010-01-27 06:25:16 +00002492 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002493 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002494 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002495 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002496 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002497 CalleeCC != CallingConv::C)
2498 return false;
2499
Evan Cheng7096ae42010-01-29 06:45:59 +00002500 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002501 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002502 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Cheng13617962010-04-30 01:12:32 +00002503 CallingConv::ID CallerCC = CallerF->getCallingConv();
2504 bool CCMatch = CallerCC == CalleeCC;
2505
Dan Gohman1797ed52010-02-08 20:27:50 +00002506 if (GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002507 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002508 return true;
2509 return false;
2510 }
2511
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002512 // Look for obvious safe cases to perform tail call optimization that do not
2513 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002514
Evan Cheng2c12cb42010-03-26 16:26:03 +00002515 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2516 // emit a special epilogue.
2517 if (RegInfo->needsStackRealignment(MF))
2518 return false;
2519
Eric Christopher90eb4022010-07-22 00:26:08 +00002520 // Do not sibcall optimize vararg calls unless the call site is not passing
2521 // any arguments.
Evan Cheng3c262ee2010-03-26 02:13:13 +00002522 if (isVarArg && !Outs.empty())
Evan Cheng843bd692010-01-31 06:44:49 +00002523 return false;
2524
Evan Chenga375d472010-03-15 18:54:48 +00002525 // Also avoid sibcall optimization if either caller or callee uses struct
2526 // return semantics.
2527 if (isCalleeStructRet || isCallerStructRet)
2528 return false;
2529
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002530 // If the call result is in ST0 / ST1, it needs to be popped off the x87 stack.
2531 // Therefore if it's not used by the call it is not safe to optimize this into
2532 // a sibcall.
2533 bool Unused = false;
2534 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2535 if (!Ins[i].Used) {
2536 Unused = true;
2537 break;
2538 }
2539 }
2540 if (Unused) {
2541 SmallVector<CCValAssign, 16> RVLocs;
2542 CCState CCInfo(CalleeCC, false, getTargetMachine(),
2543 RVLocs, *DAG.getContext());
2544 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002545 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002546 CCValAssign &VA = RVLocs[i];
2547 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2548 return false;
2549 }
2550 }
2551
Evan Cheng13617962010-04-30 01:12:32 +00002552 // If the calling conventions do not match, then we'd better make sure the
2553 // results are returned in the same way as what the caller expects.
2554 if (!CCMatch) {
2555 SmallVector<CCValAssign, 16> RVLocs1;
2556 CCState CCInfo1(CalleeCC, false, getTargetMachine(),
2557 RVLocs1, *DAG.getContext());
2558 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2559
2560 SmallVector<CCValAssign, 16> RVLocs2;
2561 CCState CCInfo2(CallerCC, false, getTargetMachine(),
2562 RVLocs2, *DAG.getContext());
2563 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2564
2565 if (RVLocs1.size() != RVLocs2.size())
2566 return false;
2567 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2568 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2569 return false;
2570 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2571 return false;
2572 if (RVLocs1[i].isRegLoc()) {
2573 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2574 return false;
2575 } else {
2576 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2577 return false;
2578 }
2579 }
2580 }
2581
Evan Chenga6bff982010-01-30 01:22:00 +00002582 // If the callee takes no arguments then go on to check the results of the
2583 // call.
2584 if (!Outs.empty()) {
2585 // Check if stack adjustment is needed. For now, do not do this if any
2586 // argument is passed on the stack.
2587 SmallVector<CCValAssign, 16> ArgLocs;
2588 CCState CCInfo(CalleeCC, isVarArg, getTargetMachine(),
2589 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002590
2591 // Allocate shadow area for Win64
2592 if (Subtarget->isTargetWin64()) {
2593 CCInfo.AllocateStack(32, 8);
2594 }
2595
Duncan Sands45907662010-10-31 13:21:44 +00002596 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Evan Chengb2c92902010-02-02 02:22:50 +00002597 if (CCInfo.getNextStackOffset()) {
2598 MachineFunction &MF = DAG.getMachineFunction();
2599 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2600 return false;
Evan Chengb2c92902010-02-02 02:22:50 +00002601
2602 // Check if the arguments are already laid out in the right way as
2603 // the caller's fixed stack objects.
2604 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002605 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2606 const X86InstrInfo *TII =
2607 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002608 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2609 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002610 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002611 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002612 if (VA.getLocInfo() == CCValAssign::Indirect)
2613 return false;
2614 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002615 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2616 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002617 return false;
2618 }
2619 }
2620 }
Evan Cheng9c044672010-05-29 01:35:22 +00002621
2622 // If the tailcall address may be in a register, then make sure it's
2623 // possible to register allocate for it. In 32-bit, the call address can
2624 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002625 // callee-saved registers are restored. These happen to be the same
2626 // registers used to pass 'inreg' arguments so watch out for those.
2627 if (!Subtarget->is64Bit() &&
2628 !isa<GlobalAddressSDNode>(Callee) &&
Evan Cheng9c044672010-05-29 01:35:22 +00002629 !isa<ExternalSymbolSDNode>(Callee)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002630 unsigned NumInRegs = 0;
2631 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2632 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002633 if (!VA.isRegLoc())
2634 continue;
2635 unsigned Reg = VA.getLocReg();
2636 switch (Reg) {
2637 default: break;
2638 case X86::EAX: case X86::EDX: case X86::ECX:
2639 if (++NumInRegs == 3)
Evan Cheng9c044672010-05-29 01:35:22 +00002640 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002641 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002642 }
2643 }
2644 }
Evan Chenga6bff982010-01-30 01:22:00 +00002645 }
Evan Chengb1712452010-01-27 06:25:16 +00002646
Dale Johannesend155d7e2010-10-25 22:17:05 +00002647 // An stdcall caller is expected to clean up its arguments; the callee
Dale Johannesen0e034562010-11-12 00:43:18 +00002648 // isn't going to do that.
Dale Johannesend155d7e2010-10-25 22:17:05 +00002649 if (!CCMatch && CallerCC==CallingConv::X86_StdCall)
2650 return false;
2651
Evan Cheng86809cc2010-02-03 03:28:02 +00002652 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002653}
2654
Dan Gohman3df24e62008-09-03 23:12:08 +00002655FastISel *
Dan Gohmana4160c32010-07-07 16:29:44 +00002656X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
2657 return X86::createFastISel(funcInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00002658}
2659
2660
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002661//===----------------------------------------------------------------------===//
2662// Other Lowering Hooks
2663//===----------------------------------------------------------------------===//
2664
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00002665static bool MayFoldLoad(SDValue Op) {
2666 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
2667}
2668
2669static bool MayFoldIntoStore(SDValue Op) {
2670 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
2671}
2672
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002673static bool isTargetShuffle(unsigned Opcode) {
2674 switch(Opcode) {
2675 default: return false;
2676 case X86ISD::PSHUFD:
2677 case X86ISD::PSHUFHW:
2678 case X86ISD::PSHUFLW:
2679 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002680 case X86ISD::PALIGN:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002681 case X86ISD::SHUFPS:
2682 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002683 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002684 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002685 case X86ISD::MOVLPS:
2686 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002687 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002688 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002689 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002690 case X86ISD::MOVSS:
2691 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002692 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002693 case X86ISD::UNPCKLPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002694 case X86ISD::PUNPCKLWD:
2695 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002696 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002697 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002698 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002699 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002700 case X86ISD::PUNPCKHWD:
2701 case X86ISD::PUNPCKHBW:
2702 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002703 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002704 return true;
2705 }
2706 return false;
2707}
2708
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002709static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002710 SDValue V1, SelectionDAG &DAG) {
2711 switch(Opc) {
2712 default: llvm_unreachable("Unknown x86 shuffle node");
2713 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002714 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002715 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002716 return DAG.getNode(Opc, dl, VT, V1);
2717 }
2718
2719 return SDValue();
2720}
2721
2722static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00002723 SDValue V1, unsigned TargetMask, SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002724 switch(Opc) {
2725 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002726 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002727 case X86ISD::PSHUFHW:
2728 case X86ISD::PSHUFLW:
2729 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
2730 }
2731
2732 return SDValue();
2733}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002734
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002735static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2736 SDValue V1, SDValue V2, unsigned TargetMask, SelectionDAG &DAG) {
2737 switch(Opc) {
2738 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002739 case X86ISD::PALIGN:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002740 case X86ISD::SHUFPD:
2741 case X86ISD::SHUFPS:
2742 return DAG.getNode(Opc, dl, VT, V1, V2,
2743 DAG.getConstant(TargetMask, MVT::i8));
2744 }
2745 return SDValue();
2746}
2747
2748static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2749 SDValue V1, SDValue V2, SelectionDAG &DAG) {
2750 switch(Opc) {
2751 default: llvm_unreachable("Unknown x86 shuffle node");
2752 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00002753 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002754 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002755 case X86ISD::MOVLPS:
2756 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002757 case X86ISD::MOVSS:
2758 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002759 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002760 case X86ISD::UNPCKLPD:
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
Evan Cheng5ced1d82006-04-06 23:23:56 +00003172 if (NumElems != 2 && NumElems != 4)
3173 return false;
3174
Evan Chengc5cdff22006-04-07 21:53:05 +00003175 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003176 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003177 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003178
Nate Begeman9008ca62009-04-27 18:41:29 +00003179 for (unsigned i = 0; i < NumElems/2; ++i)
3180 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003181 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003182
3183 return true;
3184}
3185
Evan Cheng0038e592006-03-28 00:39:58 +00003186/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3187/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Owen Andersone50ed302009-08-10 22:56:29 +00003188static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003189 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003190 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003191 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng0038e592006-03-28 00:39:58 +00003192 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003193
Nate Begeman9008ca62009-04-27 18:41:29 +00003194 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
3195 int BitI = Mask[i];
3196 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00003197 if (!isUndefOrEqual(BitI, j))
3198 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00003199 if (V2IsSplat) {
Mon P Wang7bcaefa2009-02-04 01:16:59 +00003200 if (!isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003201 return false;
3202 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00003203 if (!isUndefOrEqual(BitI1, j + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003204 return false;
3205 }
Evan Cheng0038e592006-03-28 00:39:58 +00003206 }
Evan Cheng0038e592006-03-28 00:39:58 +00003207 return true;
3208}
3209
Nate Begeman9008ca62009-04-27 18:41:29 +00003210bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3211 SmallVector<int, 8> M;
3212 N->getMask(M);
3213 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003214}
3215
Evan Cheng4fcb9222006-03-28 02:43:26 +00003216/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3217/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Eric Christopherfd179292009-08-27 18:07:15 +00003218static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003219 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003220 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003221 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng4fcb9222006-03-28 02:43:26 +00003222 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003223
Nate Begeman9008ca62009-04-27 18:41:29 +00003224 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
3225 int BitI = Mask[i];
3226 int BitI1 = Mask[i+1];
Chris Lattner5a88b832007-02-25 07:10:00 +00003227 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengc5cdff22006-04-07 21:53:05 +00003228 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00003229 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00003230 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003231 return false;
3232 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00003233 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003234 return false;
3235 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003236 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003237 return true;
3238}
3239
Nate Begeman9008ca62009-04-27 18:41:29 +00003240bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3241 SmallVector<int, 8> M;
3242 N->getMask(M);
3243 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003244}
3245
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003246/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3247/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3248/// <0, 0, 1, 1>
Owen Andersone50ed302009-08-10 22:56:29 +00003249static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003250 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003251 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003252 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003253
Nate Begeman9008ca62009-04-27 18:41:29 +00003254 for (int i = 0, j = 0; i != NumElems; i += 2, ++j) {
3255 int BitI = Mask[i];
3256 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00003257 if (!isUndefOrEqual(BitI, j))
3258 return false;
3259 if (!isUndefOrEqual(BitI1, j))
3260 return false;
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003261 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003262 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003263}
3264
Nate Begeman9008ca62009-04-27 18:41:29 +00003265bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
3266 SmallVector<int, 8> M;
3267 N->getMask(M);
3268 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
3269}
3270
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003271/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3272/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3273/// <2, 2, 3, 3>
Owen Andersone50ed302009-08-10 22:56:29 +00003274static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003275 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003276 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
3277 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003278
Nate Begeman9008ca62009-04-27 18:41:29 +00003279 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
3280 int BitI = Mask[i];
3281 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003282 if (!isUndefOrEqual(BitI, j))
3283 return false;
3284 if (!isUndefOrEqual(BitI1, j))
3285 return false;
3286 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003287 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003288}
3289
Nate Begeman9008ca62009-04-27 18:41:29 +00003290bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
3291 SmallVector<int, 8> M;
3292 N->getMask(M);
3293 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
3294}
3295
Evan Cheng017dcc62006-04-21 01:05:10 +00003296/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3297/// specifies a shuffle of elements that is suitable for input to MOVSS,
3298/// MOVSD, and MOVD, i.e. setting the lowest element.
Owen Andersone50ed302009-08-10 22:56:29 +00003299static bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003300 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003301 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003302
3303 int NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003304
Nate Begeman9008ca62009-04-27 18:41:29 +00003305 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003306 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003307
Nate Begeman9008ca62009-04-27 18:41:29 +00003308 for (int i = 1; i < NumElts; ++i)
3309 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003310 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003311
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003312 return true;
3313}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003314
Nate Begeman9008ca62009-04-27 18:41:29 +00003315bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
3316 SmallVector<int, 8> M;
3317 N->getMask(M);
3318 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003319}
3320
Evan Cheng017dcc62006-04-21 01:05:10 +00003321/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
3322/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003323/// element of vector 2 and the other elements to come from vector 1 in order.
Owen Andersone50ed302009-08-10 22:56:29 +00003324static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003325 bool V2IsSplat = false, bool V2IsUndef = false) {
3326 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003327 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003328 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003329
Nate Begeman9008ca62009-04-27 18:41:29 +00003330 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003331 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003332
Nate Begeman9008ca62009-04-27 18:41:29 +00003333 for (int i = 1; i < NumOps; ++i)
3334 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3335 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3336 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003337 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003338
Evan Cheng39623da2006-04-20 08:58:49 +00003339 return true;
3340}
3341
Nate Begeman9008ca62009-04-27 18:41:29 +00003342static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00003343 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003344 SmallVector<int, 8> M;
3345 N->getMask(M);
3346 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00003347}
3348
Evan Chengd9539472006-04-14 21:59:03 +00003349/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3350/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003351bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N) {
3352 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003353 return false;
3354
3355 // Expect 1, 1, 3, 3
Rafael Espindola15684b22009-04-24 12:40:33 +00003356 for (unsigned i = 0; i < 2; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003357 int Elt = N->getMaskElt(i);
3358 if (Elt >= 0 && Elt != 1)
3359 return false;
Rafael Espindola15684b22009-04-24 12:40:33 +00003360 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003361
3362 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003363 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003364 int Elt = N->getMaskElt(i);
3365 if (Elt >= 0 && Elt != 3)
3366 return false;
3367 if (Elt == 3)
3368 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003369 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003370 // Don't use movshdup if it can be done with a shufps.
Nate Begeman9008ca62009-04-27 18:41:29 +00003371 // FIXME: verify that matching u, u, 3, 3 is what we want.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003372 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003373}
3374
3375/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3376/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003377bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N) {
3378 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003379 return false;
3380
3381 // Expect 0, 0, 2, 2
Nate Begeman9008ca62009-04-27 18:41:29 +00003382 for (unsigned i = 0; i < 2; ++i)
3383 if (N->getMaskElt(i) > 0)
3384 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003385
3386 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003387 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003388 int Elt = N->getMaskElt(i);
3389 if (Elt >= 0 && Elt != 2)
3390 return false;
3391 if (Elt == 2)
3392 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003393 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003394 // Don't use movsldup if it can be done with a shufps.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003395 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003396}
3397
Evan Cheng0b457f02008-09-25 20:50:48 +00003398/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3399/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003400bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
3401 int e = N->getValueType(0).getVectorNumElements() / 2;
Eric Christopherfd179292009-08-27 18:07:15 +00003402
Nate Begeman9008ca62009-04-27 18:41:29 +00003403 for (int i = 0; i < e; ++i)
3404 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003405 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003406 for (int i = 0; i < e; ++i)
3407 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003408 return false;
3409 return true;
3410}
3411
David Greenec38a03e2011-02-03 15:50:00 +00003412/// isVEXTRACTF128Index - Return true if the specified
3413/// EXTRACT_SUBVECTOR operand specifies a vector extract that is
3414/// suitable for input to VEXTRACTF128.
3415bool X86::isVEXTRACTF128Index(SDNode *N) {
3416 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3417 return false;
3418
3419 // The index should be aligned on a 128-bit boundary.
3420 uint64_t Index =
3421 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
3422
3423 unsigned VL = N->getValueType(0).getVectorNumElements();
3424 unsigned VBits = N->getValueType(0).getSizeInBits();
3425 unsigned ElSize = VBits / VL;
3426 bool Result = (Index * ElSize) % 128 == 0;
3427
3428 return Result;
3429}
3430
David Greeneccacdc12011-02-04 16:08:29 +00003431/// isVINSERTF128Index - Return true if the specified INSERT_SUBVECTOR
3432/// operand specifies a subvector insert that is suitable for input to
3433/// VINSERTF128.
3434bool X86::isVINSERTF128Index(SDNode *N) {
3435 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
3436 return false;
3437
3438 // The index should be aligned on a 128-bit boundary.
3439 uint64_t Index =
3440 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
3441
3442 unsigned VL = N->getValueType(0).getVectorNumElements();
3443 unsigned VBits = N->getValueType(0).getSizeInBits();
3444 unsigned ElSize = VBits / VL;
3445 bool Result = (Index * ElSize) % 128 == 0;
3446
3447 return Result;
3448}
3449
Evan Cheng63d33002006-03-22 08:01:21 +00003450/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003451/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Evan Cheng63d33002006-03-22 08:01:21 +00003452unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003453 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3454 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
3455
Evan Chengb9df0ca2006-03-22 02:53:00 +00003456 unsigned Shift = (NumOperands == 4) ? 2 : 1;
3457 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00003458 for (int i = 0; i < NumOperands; ++i) {
3459 int Val = SVOp->getMaskElt(NumOperands-i-1);
3460 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00003461 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00003462 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00003463 if (i != NumOperands - 1)
3464 Mask <<= Shift;
3465 }
Evan Cheng63d33002006-03-22 08:01:21 +00003466 return Mask;
3467}
3468
Evan Cheng506d3df2006-03-29 23:07:14 +00003469/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003470/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003471unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003472 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003473 unsigned Mask = 0;
3474 // 8 nodes, but we only care about the last 4.
3475 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003476 int Val = SVOp->getMaskElt(i);
3477 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00003478 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00003479 if (i != 4)
3480 Mask <<= 2;
3481 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003482 return Mask;
3483}
3484
3485/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003486/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003487unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003488 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003489 unsigned Mask = 0;
3490 // 8 nodes, but we only care about the first 4.
3491 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003492 int Val = SVOp->getMaskElt(i);
3493 if (Val >= 0)
3494 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00003495 if (i != 0)
3496 Mask <<= 2;
3497 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003498 return Mask;
3499}
3500
Nate Begemana09008b2009-10-19 02:17:23 +00003501/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
3502/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
3503unsigned X86::getShufflePALIGNRImmediate(SDNode *N) {
3504 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3505 EVT VVT = N->getValueType(0);
3506 unsigned EltSize = VVT.getVectorElementType().getSizeInBits() >> 3;
3507 int Val = 0;
3508
3509 unsigned i, e;
3510 for (i = 0, e = VVT.getVectorNumElements(); i != e; ++i) {
3511 Val = SVOp->getMaskElt(i);
3512 if (Val >= 0)
3513 break;
3514 }
3515 return (Val - i) * EltSize;
3516}
3517
David Greenec38a03e2011-02-03 15:50:00 +00003518/// getExtractVEXTRACTF128Immediate - Return the appropriate immediate
3519/// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF128
3520/// instructions.
3521unsigned X86::getExtractVEXTRACTF128Immediate(SDNode *N) {
3522 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3523 llvm_unreachable("Illegal extract subvector for VEXTRACTF128");
3524
3525 uint64_t Index =
3526 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
3527
3528 EVT VecVT = N->getOperand(0).getValueType();
3529 EVT ElVT = VecVT.getVectorElementType();
3530
3531 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
3532
3533 return Index / NumElemsPerChunk;
3534}
3535
David Greeneccacdc12011-02-04 16:08:29 +00003536/// getInsertVINSERTF128Immediate - Return the appropriate immediate
3537/// to insert at the specified INSERT_SUBVECTOR index with VINSERTF128
3538/// instructions.
3539unsigned X86::getInsertVINSERTF128Immediate(SDNode *N) {
3540 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
3541 llvm_unreachable("Illegal insert subvector for VINSERTF128");
3542
3543 uint64_t Index =
NAKAMURA Takumi27635382011-02-05 15:10:54 +00003544 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
David Greeneccacdc12011-02-04 16:08:29 +00003545
3546 EVT VecVT = N->getValueType(0);
3547 EVT ElVT = VecVT.getVectorElementType();
3548
3549 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
3550
3551 return Index / NumElemsPerChunk;
3552}
3553
Evan Cheng37b73872009-07-30 08:33:02 +00003554/// isZeroNode - Returns true if Elt is a constant zero or a floating point
3555/// constant +0.0.
3556bool X86::isZeroNode(SDValue Elt) {
3557 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00003558 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00003559 (isa<ConstantFPSDNode>(Elt) &&
3560 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
3561}
3562
Nate Begeman9008ca62009-04-27 18:41:29 +00003563/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
3564/// their permute mask.
3565static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
3566 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003567 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003568 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00003569 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00003570
Nate Begeman5a5ca152009-04-29 05:20:52 +00003571 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003572 int idx = SVOp->getMaskElt(i);
3573 if (idx < 0)
3574 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003575 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003576 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003577 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003578 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003579 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003580 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
3581 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003582}
3583
Evan Cheng779ccea2007-12-07 21:30:01 +00003584/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3585/// the two vector operands have swapped position.
Owen Andersone50ed302009-08-10 22:56:29 +00003586static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00003587 unsigned NumElems = VT.getVectorNumElements();
3588 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003589 int idx = Mask[i];
3590 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003591 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003592 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003593 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003594 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003595 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003596 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003597}
3598
Evan Cheng533a0aa2006-04-19 20:35:22 +00003599/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
3600/// match movhlps. The lower half elements should come from upper half of
3601/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003602/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00003603static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
3604 if (Op->getValueType(0).getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00003605 return false;
3606 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003607 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003608 return false;
3609 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003610 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003611 return false;
3612 return true;
3613}
3614
Evan Cheng5ced1d82006-04-06 23:23:56 +00003615/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00003616/// is promoted to a vector. It also returns the LoadSDNode by reference if
3617/// required.
3618static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00003619 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
3620 return false;
3621 N = N->getOperand(0).getNode();
3622 if (!ISD::isNON_EXTLoad(N))
3623 return false;
3624 if (LD)
3625 *LD = cast<LoadSDNode>(N);
3626 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003627}
3628
Evan Cheng533a0aa2006-04-19 20:35:22 +00003629/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
3630/// match movlp{s|d}. The lower half elements should come from lower half of
3631/// V1 (and in order), and the upper half elements should come from the upper
3632/// half of V2 (and in order). And since V1 will become the source of the
3633/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003634static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
3635 ShuffleVectorSDNode *Op) {
Evan Cheng466685d2006-10-09 20:57:25 +00003636 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003637 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00003638 // Is V2 is a vector load, don't do this transformation. We will try to use
3639 // load folding shufps op.
3640 if (ISD::isNON_EXTLoad(V2))
3641 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003642
Nate Begeman5a5ca152009-04-29 05:20:52 +00003643 unsigned NumElems = Op->getValueType(0).getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003644
Evan Cheng533a0aa2006-04-19 20:35:22 +00003645 if (NumElems != 2 && NumElems != 4)
3646 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003647 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003648 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003649 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003650 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003651 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003652 return false;
3653 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003654}
3655
Evan Cheng39623da2006-04-20 08:58:49 +00003656/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
3657/// all the same.
3658static bool isSplatVector(SDNode *N) {
3659 if (N->getOpcode() != ISD::BUILD_VECTOR)
3660 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003661
Dan Gohman475871a2008-07-27 21:46:04 +00003662 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00003663 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
3664 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003665 return false;
3666 return true;
3667}
3668
Evan Cheng213d2cf2007-05-17 18:45:50 +00003669/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00003670/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00003671/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00003672static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00003673 SDValue V1 = N->getOperand(0);
3674 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003675 unsigned NumElems = N->getValueType(0).getVectorNumElements();
3676 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003677 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003678 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003679 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00003680 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
3681 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003682 if (Opc != ISD::BUILD_VECTOR ||
3683 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00003684 return false;
3685 } else if (Idx >= 0) {
3686 unsigned Opc = V1.getOpcode();
3687 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
3688 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003689 if (Opc != ISD::BUILD_VECTOR ||
3690 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00003691 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00003692 }
3693 }
3694 return true;
3695}
3696
3697/// getZeroVector - Returns a vector of specified type with all zero elements.
3698///
Owen Andersone50ed302009-08-10 22:56:29 +00003699static SDValue getZeroVector(EVT VT, bool HasSSE2, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00003700 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003701 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003702
Dale Johannesen0488fb62010-09-30 23:57:10 +00003703 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003704 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00003705 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00003706 if (VT.getSizeInBits() == 128) { // SSE
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003707 if (HasSSE2) { // SSE2
3708 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3709 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
3710 } else { // SSE1
3711 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
3712 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
3713 }
3714 } else if (VT.getSizeInBits() == 256) { // AVX
3715 // 256-bit logic and arithmetic instructions in AVX are
3716 // all floating-point, no support for integer ops. Default
3717 // to emitting fp zeroed vectors then.
Owen Anderson825b72b2009-08-11 20:47:22 +00003718 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003719 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
3720 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
Evan Chengf0df0312008-05-15 08:39:06 +00003721 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003722 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00003723}
3724
Chris Lattner8a594482007-11-25 00:24:49 +00003725/// getOnesVector - Returns a vector of specified type with all bits set.
3726///
Owen Andersone50ed302009-08-10 22:56:29 +00003727static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003728 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003729
Chris Lattner8a594482007-11-25 00:24:49 +00003730 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
3731 // type. This ensures they get CSE'd.
Owen Anderson825b72b2009-08-11 20:47:22 +00003732 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00003733 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00003734 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003735 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00003736}
3737
3738
Evan Cheng39623da2006-04-20 08:58:49 +00003739/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
3740/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00003741static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003742 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003743 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003744
Evan Cheng39623da2006-04-20 08:58:49 +00003745 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003746 SmallVector<int, 8> MaskVec;
3747 SVOp->getMask(MaskVec);
Eric Christopherfd179292009-08-27 18:07:15 +00003748
Nate Begeman5a5ca152009-04-29 05:20:52 +00003749 for (unsigned i = 0; i != NumElems; ++i) {
3750 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003751 MaskVec[i] = NumElems;
3752 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00003753 }
Evan Cheng39623da2006-04-20 08:58:49 +00003754 }
Evan Cheng39623da2006-04-20 08:58:49 +00003755 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00003756 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
3757 SVOp->getOperand(1), &MaskVec[0]);
3758 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00003759}
3760
Evan Cheng017dcc62006-04-21 01:05:10 +00003761/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
3762/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00003763static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003764 SDValue V2) {
3765 unsigned NumElems = VT.getVectorNumElements();
3766 SmallVector<int, 8> Mask;
3767 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00003768 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003769 Mask.push_back(i);
3770 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00003771}
3772
Nate Begeman9008ca62009-04-27 18:41:29 +00003773/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003774static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003775 SDValue V2) {
3776 unsigned NumElems = VT.getVectorNumElements();
3777 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00003778 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003779 Mask.push_back(i);
3780 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00003781 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003782 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00003783}
3784
Nate Begeman9008ca62009-04-27 18:41:29 +00003785/// getUnpackhMask - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003786static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003787 SDValue V2) {
3788 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00003789 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00003790 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00003791 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003792 Mask.push_back(i + Half);
3793 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00003794 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003795 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003796}
3797
Bruno Cardoso Lopesbb0a9482010-08-13 17:50:47 +00003798/// PromoteSplat - Promote a splat of v4i32, v8i16 or v16i8 to v4f32.
3799static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003800 EVT PVT = MVT::v4f32;
Owen Andersone50ed302009-08-10 22:56:29 +00003801 EVT VT = SV->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00003802 DebugLoc dl = SV->getDebugLoc();
3803 SDValue V1 = SV->getOperand(0);
3804 int NumElems = VT.getVectorNumElements();
3805 int EltNo = SV->getSplatIndex();
Rafael Espindola15684b22009-04-24 12:40:33 +00003806
Nate Begeman9008ca62009-04-27 18:41:29 +00003807 // unpack elements to the correct location
3808 while (NumElems > 4) {
3809 if (EltNo < NumElems/2) {
3810 V1 = getUnpackl(DAG, dl, VT, V1, V1);
3811 } else {
3812 V1 = getUnpackh(DAG, dl, VT, V1, V1);
3813 EltNo -= NumElems/2;
3814 }
3815 NumElems >>= 1;
3816 }
Eric Christopherfd179292009-08-27 18:07:15 +00003817
Nate Begeman9008ca62009-04-27 18:41:29 +00003818 // Perform the splat.
3819 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003820 V1 = DAG.getNode(ISD::BITCAST, dl, PVT, V1);
Nate Begeman9008ca62009-04-27 18:41:29 +00003821 V1 = DAG.getVectorShuffle(PVT, dl, V1, DAG.getUNDEF(PVT), &SplatMask[0]);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003822 return DAG.getNode(ISD::BITCAST, dl, VT, V1);
Evan Chengc575ca22006-04-17 20:43:08 +00003823}
3824
Evan Chengba05f722006-04-21 23:03:30 +00003825/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00003826/// vector of zero or undef vector. This produces a shuffle where the low
3827/// element of V2 is swizzled into the zero/undef vector, landing at element
3828/// 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 +00003829static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Chengf0df0312008-05-15 08:39:06 +00003830 bool isZero, bool HasSSE2,
3831 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003832 EVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00003833 SDValue V1 = isZero
Nate Begeman9008ca62009-04-27 18:41:29 +00003834 ? getZeroVector(VT, HasSSE2, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
3835 unsigned NumElems = VT.getVectorNumElements();
3836 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00003837 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003838 // If this is the insertion idx, put the low elt of V2 here.
3839 MaskVec.push_back(i == Idx ? NumElems : i);
3840 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00003841}
3842
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003843/// getShuffleScalarElt - Returns the scalar element that will make up the ith
3844/// element of the result of the vector shuffle.
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003845SDValue getShuffleScalarElt(SDNode *N, int Index, SelectionDAG &DAG,
3846 unsigned Depth) {
3847 if (Depth == 6)
3848 return SDValue(); // Limit search depth.
3849
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003850 SDValue V = SDValue(N, 0);
3851 EVT VT = V.getValueType();
3852 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003853
3854 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
3855 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
3856 Index = SV->getMaskElt(Index);
3857
3858 if (Index < 0)
3859 return DAG.getUNDEF(VT.getVectorElementType());
3860
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003861 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003862 SDValue NewV = (Index < NumElems) ? SV->getOperand(0) : SV->getOperand(1);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003863 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00003864 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003865
3866 // Recurse into target specific vector shuffles to find scalars.
3867 if (isTargetShuffle(Opcode)) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003868 int NumElems = VT.getVectorNumElements();
3869 SmallVector<unsigned, 16> ShuffleMask;
3870 SDValue ImmN;
3871
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003872 switch(Opcode) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003873 case X86ISD::SHUFPS:
3874 case X86ISD::SHUFPD:
3875 ImmN = N->getOperand(N->getNumOperands()-1);
3876 DecodeSHUFPSMask(NumElems,
3877 cast<ConstantSDNode>(ImmN)->getZExtValue(),
3878 ShuffleMask);
3879 break;
3880 case X86ISD::PUNPCKHBW:
3881 case X86ISD::PUNPCKHWD:
3882 case X86ISD::PUNPCKHDQ:
3883 case X86ISD::PUNPCKHQDQ:
3884 DecodePUNPCKHMask(NumElems, ShuffleMask);
3885 break;
3886 case X86ISD::UNPCKHPS:
3887 case X86ISD::UNPCKHPD:
3888 DecodeUNPCKHPMask(NumElems, ShuffleMask);
3889 break;
3890 case X86ISD::PUNPCKLBW:
3891 case X86ISD::PUNPCKLWD:
3892 case X86ISD::PUNPCKLDQ:
3893 case X86ISD::PUNPCKLQDQ:
3894 DecodePUNPCKLMask(NumElems, ShuffleMask);
3895 break;
3896 case X86ISD::UNPCKLPS:
3897 case X86ISD::UNPCKLPD:
3898 DecodeUNPCKLPMask(NumElems, ShuffleMask);
3899 break;
3900 case X86ISD::MOVHLPS:
3901 DecodeMOVHLPSMask(NumElems, ShuffleMask);
3902 break;
3903 case X86ISD::MOVLHPS:
3904 DecodeMOVLHPSMask(NumElems, ShuffleMask);
3905 break;
3906 case X86ISD::PSHUFD:
3907 ImmN = N->getOperand(N->getNumOperands()-1);
3908 DecodePSHUFMask(NumElems,
3909 cast<ConstantSDNode>(ImmN)->getZExtValue(),
3910 ShuffleMask);
3911 break;
3912 case X86ISD::PSHUFHW:
3913 ImmN = N->getOperand(N->getNumOperands()-1);
3914 DecodePSHUFHWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
3915 ShuffleMask);
3916 break;
3917 case X86ISD::PSHUFLW:
3918 ImmN = N->getOperand(N->getNumOperands()-1);
3919 DecodePSHUFLWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
3920 ShuffleMask);
3921 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003922 case X86ISD::MOVSS:
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00003923 case X86ISD::MOVSD: {
3924 // The index 0 always comes from the first element of the second source,
3925 // this is why MOVSS and MOVSD are used in the first place. The other
3926 // elements come from the other positions of the first source vector.
3927 unsigned OpNum = (Index == 0) ? 1 : 0;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003928 return getShuffleScalarElt(V.getOperand(OpNum).getNode(), Index, DAG,
3929 Depth+1);
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00003930 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003931 default:
3932 assert("not implemented for target shuffle node");
3933 return SDValue();
3934 }
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003935
3936 Index = ShuffleMask[Index];
3937 if (Index < 0)
3938 return DAG.getUNDEF(VT.getVectorElementType());
3939
3940 SDValue NewV = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
3941 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG,
3942 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003943 }
3944
3945 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003946 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003947 V = V.getOperand(0);
3948 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003949 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003950
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003951 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003952 return SDValue();
3953 }
3954
3955 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
3956 return (Index == 0) ? V.getOperand(0)
3957 : DAG.getUNDEF(VT.getVectorElementType());
3958
3959 if (V.getOpcode() == ISD::BUILD_VECTOR)
3960 return V.getOperand(Index);
3961
3962 return SDValue();
3963}
3964
3965/// getNumOfConsecutiveZeros - Return the number of elements of a vector
3966/// shuffle operation which come from a consecutively from a zero. The
3967/// search can start in two diferent directions, from left or right.
3968static
3969unsigned getNumOfConsecutiveZeros(SDNode *N, int NumElems,
3970 bool ZerosFromLeft, SelectionDAG &DAG) {
3971 int i = 0;
3972
3973 while (i < NumElems) {
3974 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003975 SDValue Elt = getShuffleScalarElt(N, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003976 if (!(Elt.getNode() &&
3977 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
3978 break;
3979 ++i;
3980 }
3981
3982 return i;
3983}
3984
3985/// isShuffleMaskConsecutive - Check if the shuffle mask indicies from MaskI to
3986/// MaskE correspond consecutively to elements from one of the vector operands,
3987/// starting from its index OpIdx. Also tell OpNum which source vector operand.
3988static
3989bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp, int MaskI, int MaskE,
3990 int OpIdx, int NumElems, unsigned &OpNum) {
3991 bool SeenV1 = false;
3992 bool SeenV2 = false;
3993
3994 for (int i = MaskI; i <= MaskE; ++i, ++OpIdx) {
3995 int Idx = SVOp->getMaskElt(i);
3996 // Ignore undef indicies
3997 if (Idx < 0)
3998 continue;
3999
4000 if (Idx < NumElems)
4001 SeenV1 = true;
4002 else
4003 SeenV2 = true;
4004
4005 // Only accept consecutive elements from the same vector
4006 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
4007 return false;
4008 }
4009
4010 OpNum = SeenV1 ? 0 : 1;
4011 return true;
4012}
4013
4014/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
4015/// logical left shift of a vector.
4016static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4017 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4018 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4019 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4020 false /* check zeros from right */, DAG);
4021 unsigned OpSrc;
4022
4023 if (!NumZeros)
4024 return false;
4025
4026 // Considering the elements in the mask that are not consecutive zeros,
4027 // check if they consecutively come from only one of the source vectors.
4028 //
4029 // V1 = {X, A, B, C} 0
4030 // \ \ \ /
4031 // vector_shuffle V1, V2 <1, 2, 3, X>
4032 //
4033 if (!isShuffleMaskConsecutive(SVOp,
4034 0, // Mask Start Index
4035 NumElems-NumZeros-1, // Mask End Index
4036 NumZeros, // Where to start looking in the src vector
4037 NumElems, // Number of elements in vector
4038 OpSrc)) // Which source operand ?
4039 return false;
4040
4041 isLeft = false;
4042 ShAmt = NumZeros;
4043 ShVal = SVOp->getOperand(OpSrc);
4044 return true;
4045}
4046
4047/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
4048/// logical left shift of a vector.
4049static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4050 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4051 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4052 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4053 true /* check zeros from left */, DAG);
4054 unsigned OpSrc;
4055
4056 if (!NumZeros)
4057 return false;
4058
4059 // Considering the elements in the mask that are not consecutive zeros,
4060 // check if they consecutively come from only one of the source vectors.
4061 //
4062 // 0 { A, B, X, X } = V2
4063 // / \ / /
4064 // vector_shuffle V1, V2 <X, X, 4, 5>
4065 //
4066 if (!isShuffleMaskConsecutive(SVOp,
4067 NumZeros, // Mask Start Index
4068 NumElems-1, // Mask End Index
4069 0, // Where to start looking in the src vector
4070 NumElems, // Number of elements in vector
4071 OpSrc)) // Which source operand ?
4072 return false;
4073
4074 isLeft = true;
4075 ShAmt = NumZeros;
4076 ShVal = SVOp->getOperand(OpSrc);
4077 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004078}
4079
4080/// isVectorShift - Returns true if the shuffle can be implemented as a
4081/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004082static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00004083 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004084 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
4085 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
4086 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004087
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004088 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00004089}
4090
Evan Chengc78d3b42006-04-24 18:01:45 +00004091/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
4092///
Dan Gohman475871a2008-07-27 21:46:04 +00004093static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00004094 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00004095 SelectionDAG &DAG,
4096 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004097 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00004098 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004099
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004100 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004101 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004102 bool First = true;
4103 for (unsigned i = 0; i < 16; ++i) {
4104 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
4105 if (ThisIsNonZero && First) {
4106 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004107 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004108 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004109 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004110 First = false;
4111 }
4112
4113 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00004114 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004115 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
4116 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004117 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004118 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00004119 }
4120 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004121 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
4122 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
4123 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00004124 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004125 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00004126 } else
4127 ThisElt = LastElt;
4128
Gabor Greifba36cb52008-08-28 21:40:38 +00004129 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00004130 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00004131 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00004132 }
4133 }
4134
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004135 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00004136}
4137
Bill Wendlinga348c562007-03-22 18:42:45 +00004138/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00004139///
Dan Gohman475871a2008-07-27 21:46:04 +00004140static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00004141 unsigned NumNonZero, unsigned NumZero,
4142 SelectionDAG &DAG,
4143 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004144 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00004145 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004146
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004147 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004148 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004149 bool First = true;
4150 for (unsigned i = 0; i < 8; ++i) {
4151 bool isNonZero = (NonZeros & (1 << i)) != 0;
4152 if (isNonZero) {
4153 if (First) {
4154 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004155 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004156 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004157 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004158 First = false;
4159 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004160 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004161 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00004162 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00004163 }
4164 }
4165
4166 return V;
4167}
4168
Evan Chengf26ffe92008-05-29 08:22:04 +00004169/// getVShift - Return a vector logical shift node.
4170///
Owen Andersone50ed302009-08-10 22:56:29 +00004171static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00004172 unsigned NumBits, SelectionDAG &DAG,
4173 const TargetLowering &TLI, DebugLoc dl) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004174 EVT ShVT = MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00004175 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004176 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
4177 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004178 DAG.getNode(Opc, dl, ShVT, SrcOp,
Gabor Greif327ef032008-08-28 23:19:51 +00004179 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
Evan Chengf26ffe92008-05-29 08:22:04 +00004180}
4181
Dan Gohman475871a2008-07-27 21:46:04 +00004182SDValue
Evan Chengc3630942009-12-09 21:00:30 +00004183X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00004184 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00004185
Evan Chengc3630942009-12-09 21:00:30 +00004186 // Check if the scalar load can be widened into a vector load. And if
4187 // the address is "base + cst" see if the cst can be "absorbed" into
4188 // the shuffle mask.
4189 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
4190 SDValue Ptr = LD->getBasePtr();
4191 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
4192 return SDValue();
4193 EVT PVT = LD->getValueType(0);
4194 if (PVT != MVT::i32 && PVT != MVT::f32)
4195 return SDValue();
4196
4197 int FI = -1;
4198 int64_t Offset = 0;
4199 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
4200 FI = FINode->getIndex();
4201 Offset = 0;
4202 } else if (Ptr.getOpcode() == ISD::ADD &&
4203 isa<ConstantSDNode>(Ptr.getOperand(1)) &&
4204 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4205 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4206 Offset = Ptr.getConstantOperandVal(1);
4207 Ptr = Ptr.getOperand(0);
4208 } else {
4209 return SDValue();
4210 }
4211
4212 SDValue Chain = LD->getChain();
4213 // Make sure the stack object alignment is at least 16.
4214 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
4215 if (DAG.InferPtrAlignment(Ptr) < 16) {
4216 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00004217 // Can't change the alignment. FIXME: It's possible to compute
4218 // the exact stack offset and reference FI + adjust offset instead.
4219 // If someone *really* cares about this. That's the way to implement it.
4220 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004221 } else {
4222 MFI->setObjectAlignment(FI, 16);
4223 }
4224 }
4225
4226 // (Offset % 16) must be multiple of 4. Then address is then
4227 // Ptr + (Offset & ~15).
4228 if (Offset < 0)
4229 return SDValue();
4230 if ((Offset % 16) & 3)
4231 return SDValue();
4232 int64_t StartOffset = Offset & ~15;
4233 if (StartOffset)
4234 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
4235 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
4236
4237 int EltNo = (Offset - StartOffset) >> 2;
4238 int Mask[4] = { EltNo, EltNo, EltNo, EltNo };
4239 EVT VT = (PVT == MVT::i32) ? MVT::v4i32 : MVT::v4f32;
Chris Lattner51abfe42010-09-21 06:02:19 +00004240 SDValue V1 = DAG.getLoad(VT, dl, Chain, Ptr,
4241 LD->getPointerInfo().getWithOffset(StartOffset),
David Greene67c9d422010-02-15 16:53:33 +00004242 false, false, 0);
Evan Chengc3630942009-12-09 21:00:30 +00004243 // Canonicalize it to a v4i32 shuffle.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004244 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, V1);
4245 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Chengc3630942009-12-09 21:00:30 +00004246 DAG.getVectorShuffle(MVT::v4i32, dl, V1,
Chris Lattner51abfe42010-09-21 06:02:19 +00004247 DAG.getUNDEF(MVT::v4i32),&Mask[0]));
Evan Chengc3630942009-12-09 21:00:30 +00004248 }
4249
4250 return SDValue();
4251}
4252
Michael J. Spencerec38de22010-10-10 22:04:20 +00004253/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
4254/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00004255/// load which has the same value as a build_vector whose operands are 'elts'.
4256///
4257/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00004258///
Nate Begeman1449f292010-03-24 22:19:06 +00004259/// FIXME: we'd also like to handle the case where the last elements are zero
4260/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
4261/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004262static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00004263 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004264 EVT EltVT = VT.getVectorElementType();
4265 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00004266
Nate Begemanfdea31a2010-03-24 20:49:50 +00004267 LoadSDNode *LDBase = NULL;
4268 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004269
Nate Begeman1449f292010-03-24 22:19:06 +00004270 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00004271 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00004272 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004273 for (unsigned i = 0; i < NumElems; ++i) {
4274 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00004275
Nate Begemanfdea31a2010-03-24 20:49:50 +00004276 if (!Elt.getNode() ||
4277 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
4278 return SDValue();
4279 if (!LDBase) {
4280 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
4281 return SDValue();
4282 LDBase = cast<LoadSDNode>(Elt.getNode());
4283 LastLoadedElt = i;
4284 continue;
4285 }
4286 if (Elt.getOpcode() == ISD::UNDEF)
4287 continue;
4288
4289 LoadSDNode *LD = cast<LoadSDNode>(Elt);
4290 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
4291 return SDValue();
4292 LastLoadedElt = i;
4293 }
Nate Begeman1449f292010-03-24 22:19:06 +00004294
4295 // If we have found an entire vector of loads and undefs, then return a large
4296 // load of the entire vector width starting at the base pointer. If we found
4297 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004298 if (LastLoadedElt == NumElems - 1) {
4299 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00004300 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004301 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004302 LDBase->isVolatile(), LDBase->isNonTemporal(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00004303 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004304 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004305 LDBase->isVolatile(), LDBase->isNonTemporal(),
4306 LDBase->getAlignment());
4307 } else if (NumElems == 4 && LastLoadedElt == 1) {
4308 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
4309 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Chris Lattner88641552010-09-22 00:34:38 +00004310 SDValue ResNode = DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys,
4311 Ops, 2, MVT::i32,
4312 LDBase->getMemOperand());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004313 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00004314 }
4315 return SDValue();
4316}
4317
Evan Chengc3630942009-12-09 21:00:30 +00004318SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004319X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004320 DebugLoc dl = Op.getDebugLoc();
David Greenea5f26012011-02-07 19:36:54 +00004321
David Greenef125a292011-02-08 19:04:41 +00004322 EVT VT = Op.getValueType();
4323 EVT ExtVT = VT.getVectorElementType();
4324
4325 unsigned NumElems = Op.getNumOperands();
4326
4327 // For AVX-length vectors, build the individual 128-bit pieces and
4328 // use shuffles to put them in place.
4329 if (VT.getSizeInBits() > 256 &&
4330 Subtarget->hasAVX() &&
4331 !Disable256Bit &&
4332 !ISD::isBuildVectorAllZeros(Op.getNode())) {
4333 SmallVector<SDValue, 8> V;
4334 V.resize(NumElems);
4335 for (unsigned i = 0; i < NumElems; ++i) {
4336 V[i] = Op.getOperand(i);
4337 }
4338
4339 EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElems/2);
4340
4341 // Build the lower subvector.
4342 SDValue Lower = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[0], NumElems/2);
4343 // Build the upper subvector.
4344 SDValue Upper = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[NumElems / 2],
4345 NumElems/2);
4346
4347 return ConcatVectors(Lower, Upper, DAG);
4348 }
4349
Chris Lattner6e80e442010-08-28 17:15:43 +00004350 // All zero's are handled with pxor in SSE2 and above, xorps in SSE1.
4351 // All one's are handled with pcmpeqd. In AVX, zero's are handled with
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004352 // vpxor in 128-bit and xor{pd,ps} in 256-bit, but no 256 version of pcmpeqd
4353 // is present, so AllOnes is ignored.
4354 if (ISD::isBuildVectorAllZeros(Op.getNode()) ||
4355 (Op.getValueType().getSizeInBits() != 256 &&
4356 ISD::isBuildVectorAllOnes(Op.getNode()))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004357 // Canonicalize this to <4 x i32> (SSE) to
Chris Lattner8a594482007-11-25 00:24:49 +00004358 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
4359 // eliminated on x86-32 hosts.
Dale Johannesen0488fb62010-09-30 23:57:10 +00004360 if (Op.getValueType() == MVT::v4i32)
Chris Lattner8a594482007-11-25 00:24:49 +00004361 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004362
Gabor Greifba36cb52008-08-28 21:40:38 +00004363 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00004364 return getOnesVector(Op.getValueType(), DAG, dl);
4365 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00004366 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004367
Owen Andersone50ed302009-08-10 22:56:29 +00004368 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004369
Evan Cheng0db9fe62006-04-25 20:13:52 +00004370 unsigned NumZero = 0;
4371 unsigned NumNonZero = 0;
4372 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004373 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00004374 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004375 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00004376 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00004377 if (Elt.getOpcode() == ISD::UNDEF)
4378 continue;
4379 Values.insert(Elt);
4380 if (Elt.getOpcode() != ISD::Constant &&
4381 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004382 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00004383 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00004384 NumZero++;
4385 else {
4386 NonZeros |= (1 << i);
4387 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004388 }
4389 }
4390
Chris Lattner97a2a562010-08-26 05:24:29 +00004391 // All undef vector. Return an UNDEF. All zero vectors were handled above.
4392 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00004393 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004394
Chris Lattner67f453a2008-03-09 05:42:06 +00004395 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00004396 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004397 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00004398 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00004399
Chris Lattner62098042008-03-09 01:05:04 +00004400 // If this is an insertion of an i64 value on x86-32, and if the top bits of
4401 // the value are obviously zero, truncate the value to i32 and do the
4402 // insertion that way. Only do this if the value is non-constant or if the
4403 // value is a constant being inserted into element 0. It is cheaper to do
4404 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00004405 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00004406 (!IsAllConstants || Idx == 0)) {
4407 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004408 // Handle SSE only.
4409 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
4410 EVT VecVT = MVT::v4i32;
4411 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00004412
Chris Lattner62098042008-03-09 01:05:04 +00004413 // Truncate the value (which may itself be a constant) to i32, and
4414 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00004415 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00004416 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00004417 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
4418 Subtarget->hasSSE2(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00004419
Chris Lattner62098042008-03-09 01:05:04 +00004420 // Now we have our 32-bit value zero extended in the low element of
4421 // a vector. If Idx != 0, swizzle it into place.
4422 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004423 SmallVector<int, 4> Mask;
4424 Mask.push_back(Idx);
4425 for (unsigned i = 1; i != VecElts; ++i)
4426 Mask.push_back(i);
4427 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopherfd179292009-08-27 18:07:15 +00004428 DAG.getUNDEF(Item.getValueType()),
Nate Begeman9008ca62009-04-27 18:41:29 +00004429 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004430 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004431 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00004432 }
4433 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004434
Chris Lattner19f79692008-03-08 22:59:52 +00004435 // If we have a constant or non-constant insertion into the low element of
4436 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
4437 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00004438 // depending on what the source datatype is.
4439 if (Idx == 0) {
4440 if (NumZero == 0) {
4441 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Owen Anderson825b72b2009-08-11 20:47:22 +00004442 } else if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
4443 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Eli Friedman10415532009-06-06 06:05:10 +00004444 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
4445 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
4446 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget->hasSSE2(),
4447 DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00004448 } else if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
4449 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Dale Johannesen0488fb62010-09-30 23:57:10 +00004450 assert(VT.getSizeInBits() == 128 && "Expected an SSE value type!");
4451 EVT MiddleVT = MVT::v4i32;
Eli Friedman10415532009-06-06 06:05:10 +00004452 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
4453 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
4454 Subtarget->hasSSE2(), DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004455 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00004456 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004457 }
Evan Chengf26ffe92008-05-29 08:22:04 +00004458
4459 // Is it a vector logical left shift?
4460 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00004461 X86::isZeroNode(Op.getOperand(0)) &&
4462 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004463 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00004464 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00004465 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004466 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00004467 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004468 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004469
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004470 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00004471 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004472
Chris Lattner19f79692008-03-08 22:59:52 +00004473 // Otherwise, if this is a vector with i32 or f32 elements, and the element
4474 // is a non-constant being inserted into an element other than the low one,
4475 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
4476 // movd/movss) to move this into the low element, then shuffle it into
4477 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004478 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00004479 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00004480
Evan Cheng0db9fe62006-04-25 20:13:52 +00004481 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00004482 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
4483 Subtarget->hasSSE2(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00004484 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004485 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00004486 MaskVec.push_back(i == Idx ? 0 : 1);
4487 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004488 }
4489 }
4490
Chris Lattner67f453a2008-03-09 05:42:06 +00004491 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00004492 if (Values.size() == 1) {
4493 if (EVTBits == 32) {
4494 // Instead of a shuffle like this:
4495 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
4496 // Check if it's possible to issue this instead.
4497 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
4498 unsigned Idx = CountTrailingZeros_32(NonZeros);
4499 SDValue Item = Op.getOperand(Idx);
4500 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
4501 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
4502 }
Dan Gohman475871a2008-07-27 21:46:04 +00004503 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004504 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004505
Dan Gohmana3941172007-07-24 22:55:08 +00004506 // A vector full of immediates; various special cases are already
4507 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004508 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00004509 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00004510
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00004511 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00004512 if (EVTBits == 64) {
4513 if (NumNonZero == 1) {
4514 // One half is zero or undef.
4515 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00004516 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00004517 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00004518 return getShuffleVectorZeroOrUndef(V2, Idx, true,
4519 Subtarget->hasSSE2(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00004520 }
Dan Gohman475871a2008-07-27 21:46:04 +00004521 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00004522 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004523
4524 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00004525 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00004526 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00004527 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004528 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004529 }
4530
Bill Wendling826f36f2007-03-28 00:57:11 +00004531 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00004532 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Chris Lattner97a2a562010-08-26 05:24:29 +00004533 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004534 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004535 }
4536
4537 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00004538 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00004539 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004540 if (NumElems == 4 && NumZero > 0) {
4541 for (unsigned i = 0; i < 4; ++i) {
4542 bool isZero = !(NonZeros & (1 << i));
4543 if (isZero)
Dale Johannesenace16102009-02-03 19:33:06 +00004544 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004545 else
Dale Johannesenace16102009-02-03 19:33:06 +00004546 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004547 }
4548
4549 for (unsigned i = 0; i < 2; ++i) {
4550 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
4551 default: break;
4552 case 0:
4553 V[i] = V[i*2]; // Must be a zero vector.
4554 break;
4555 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00004556 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004557 break;
4558 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00004559 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004560 break;
4561 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00004562 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004563 break;
4564 }
4565 }
4566
Nate Begeman9008ca62009-04-27 18:41:29 +00004567 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004568 bool Reverse = (NonZeros & 0x3) == 2;
4569 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004570 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004571 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
4572 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004573 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
4574 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004575 }
4576
Nate Begemanfdea31a2010-03-24 20:49:50 +00004577 if (Values.size() > 1 && VT.getSizeInBits() == 128) {
4578 // Check for a build vector of consecutive loads.
4579 for (unsigned i = 0; i < NumElems; ++i)
4580 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00004581
Nate Begemanfdea31a2010-03-24 20:49:50 +00004582 // Check for elements which are consecutive loads.
4583 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
4584 if (LD.getNode())
4585 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004586
4587 // For SSE 4.1, use insertps to put the high elements into the low element.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004588 if (getSubtarget()->hasSSE41()) {
Chris Lattner24faf612010-08-28 17:59:08 +00004589 SDValue Result;
4590 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
4591 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
4592 else
4593 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00004594
Chris Lattner24faf612010-08-28 17:59:08 +00004595 for (unsigned i = 1; i < NumElems; ++i) {
4596 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
4597 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00004598 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00004599 }
4600 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00004601 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00004602
Chris Lattner6e80e442010-08-28 17:15:43 +00004603 // Otherwise, expand into a number of unpckl*, start by extending each of
4604 // our (non-undef) elements to the full vector width with the element in the
4605 // bottom slot of the vector (which generates no code for SSE).
4606 for (unsigned i = 0; i < NumElems; ++i) {
4607 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
4608 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
4609 else
4610 V[i] = DAG.getUNDEF(VT);
4611 }
4612
4613 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004614 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
4615 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
4616 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00004617 unsigned EltStride = NumElems >> 1;
4618 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00004619 for (unsigned i = 0; i < EltStride; ++i) {
4620 // If V[i+EltStride] is undef and this is the first round of mixing,
4621 // then it is safe to just drop this shuffle: V[i] is already in the
4622 // right place, the one element (since it's the first round) being
4623 // inserted as undef can be dropped. This isn't safe for successive
4624 // rounds because they will permute elements within both vectors.
4625 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
4626 EltStride == NumElems/2)
4627 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004628
Chris Lattner6e80e442010-08-28 17:15:43 +00004629 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00004630 }
Chris Lattner6e80e442010-08-28 17:15:43 +00004631 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004632 }
4633 return V[0];
4634 }
Dan Gohman475871a2008-07-27 21:46:04 +00004635 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004636}
4637
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004638SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004639X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const {
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004640 // We support concatenate two MMX registers and place them in a MMX
4641 // register. This is better than doing a stack convert.
4642 DebugLoc dl = Op.getDebugLoc();
4643 EVT ResVT = Op.getValueType();
4644 assert(Op.getNumOperands() == 2);
4645 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
4646 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
4647 int Mask[2];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004648 SDValue InVec = DAG.getNode(ISD::BITCAST,dl, MVT::v1i64, Op.getOperand(0));
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004649 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4650 InVec = Op.getOperand(1);
4651 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4652 unsigned NumElts = ResVT.getVectorNumElements();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004653 VecOp = DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004654 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
4655 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
4656 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004657 InVec = DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, InVec);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004658 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4659 Mask[0] = 0; Mask[1] = 2;
4660 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
4661 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004662 return DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004663}
4664
Nate Begemanb9a47b82009-02-23 08:49:38 +00004665// v8i16 shuffles - Prefer shuffles in the following order:
4666// 1. [all] pshuflw, pshufhw, optional move
4667// 2. [ssse3] 1 x pshufb
4668// 3. [ssse3] 2 x pshufb + 1 x por
4669// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004670SDValue
4671X86TargetLowering::LowerVECTOR_SHUFFLEv8i16(SDValue Op,
4672 SelectionDAG &DAG) const {
4673 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00004674 SDValue V1 = SVOp->getOperand(0);
4675 SDValue V2 = SVOp->getOperand(1);
4676 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004677 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00004678
Nate Begemanb9a47b82009-02-23 08:49:38 +00004679 // Determine if more than 1 of the words in each of the low and high quadwords
4680 // of the result come from the same quadword of one of the two inputs. Undef
4681 // mask values count as coming from any quadword, for better codegen.
4682 SmallVector<unsigned, 4> LoQuad(4);
4683 SmallVector<unsigned, 4> HiQuad(4);
4684 BitVector InputQuads(4);
4685 for (unsigned i = 0; i < 8; ++i) {
4686 SmallVectorImpl<unsigned> &Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00004687 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004688 MaskVals.push_back(EltIdx);
4689 if (EltIdx < 0) {
4690 ++Quad[0];
4691 ++Quad[1];
4692 ++Quad[2];
4693 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00004694 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004695 }
4696 ++Quad[EltIdx / 4];
4697 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00004698 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004699
Nate Begemanb9a47b82009-02-23 08:49:38 +00004700 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004701 unsigned MaxQuad = 1;
4702 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004703 if (LoQuad[i] > MaxQuad) {
4704 BestLoQuad = i;
4705 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004706 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004707 }
4708
Nate Begemanb9a47b82009-02-23 08:49:38 +00004709 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004710 MaxQuad = 1;
4711 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004712 if (HiQuad[i] > MaxQuad) {
4713 BestHiQuad = i;
4714 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004715 }
4716 }
4717
Nate Begemanb9a47b82009-02-23 08:49:38 +00004718 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00004719 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00004720 // single pshufb instruction is necessary. If There are more than 2 input
4721 // quads, disable the next transformation since it does not help SSSE3.
4722 bool V1Used = InputQuads[0] || InputQuads[1];
4723 bool V2Used = InputQuads[2] || InputQuads[3];
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004724 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004725 if (InputQuads.count() == 2 && V1Used && V2Used) {
4726 BestLoQuad = InputQuads.find_first();
4727 BestHiQuad = InputQuads.find_next(BestLoQuad);
4728 }
4729 if (InputQuads.count() > 2) {
4730 BestLoQuad = -1;
4731 BestHiQuad = -1;
4732 }
4733 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004734
Nate Begemanb9a47b82009-02-23 08:49:38 +00004735 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
4736 // the shuffle mask. If a quad is scored as -1, that means that it contains
4737 // words from all 4 input quadwords.
4738 SDValue NewV;
4739 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004740 SmallVector<int, 8> MaskV;
4741 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
4742 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Eric Christopherfd179292009-08-27 18:07:15 +00004743 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004744 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
4745 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
4746 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00004747
Nate Begemanb9a47b82009-02-23 08:49:38 +00004748 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
4749 // source words for the shuffle, to aid later transformations.
4750 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00004751 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00004752 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004753 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00004754 if (idx != (int)i)
4755 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004756 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00004757 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004758 AllWordsInNewV = false;
4759 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00004760 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004761
Nate Begemanb9a47b82009-02-23 08:49:38 +00004762 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
4763 if (AllWordsInNewV) {
4764 for (int i = 0; i != 8; ++i) {
4765 int idx = MaskVals[i];
4766 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00004767 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004768 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004769 if ((idx != i) && idx < 4)
4770 pshufhw = false;
4771 if ((idx != i) && idx > 3)
4772 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00004773 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00004774 V1 = NewV;
4775 V2Used = false;
4776 BestLoQuad = 0;
4777 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004778 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004779
Nate Begemanb9a47b82009-02-23 08:49:38 +00004780 // If we've eliminated the use of V2, and the new mask is a pshuflw or
4781 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00004782 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00004783 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
4784 unsigned TargetMask = 0;
4785 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00004786 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00004787 TargetMask = pshufhw ? X86::getShufflePSHUFHWImmediate(NewV.getNode()):
4788 X86::getShufflePSHUFLWImmediate(NewV.getNode());
4789 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004790 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00004791 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004792 }
Eric Christopherfd179292009-08-27 18:07:15 +00004793
Nate Begemanb9a47b82009-02-23 08:49:38 +00004794 // If we have SSSE3, and all words of the result are from 1 input vector,
4795 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
4796 // is present, fall back to case 4.
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004797 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004798 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004799
Nate Begemanb9a47b82009-02-23 08:49:38 +00004800 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00004801 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00004802 // mask, and elements that come from V1 in the V2 mask, so that the two
4803 // results can be OR'd together.
4804 bool TwoInputs = V1Used && V2Used;
4805 for (unsigned i = 0; i != 8; ++i) {
4806 int EltIdx = MaskVals[i] * 2;
4807 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004808 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4809 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004810 continue;
4811 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004812 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
4813 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004814 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004815 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004816 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004817 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004818 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004819 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004820 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004821
Nate Begemanb9a47b82009-02-23 08:49:38 +00004822 // Calculate the shuffle mask for the second input, shuffle it, and
4823 // OR it with the first shuffled input.
4824 pshufbMask.clear();
4825 for (unsigned i = 0; i != 8; ++i) {
4826 int EltIdx = MaskVals[i] * 2;
4827 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004828 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4829 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004830 continue;
4831 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004832 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
4833 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004834 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004835 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00004836 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00004837 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004838 MVT::v16i8, &pshufbMask[0], 16));
4839 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004840 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004841 }
4842
4843 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
4844 // and update MaskVals with new element order.
4845 BitVector InOrder(8);
4846 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004847 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004848 for (int i = 0; i != 4; ++i) {
4849 int idx = MaskVals[i];
4850 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004851 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004852 InOrder.set(i);
4853 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004854 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004855 InOrder.set(i);
4856 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004857 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004858 }
4859 }
4860 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004861 MaskV.push_back(i);
Owen Anderson825b72b2009-08-11 20:47:22 +00004862 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004863 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004864
4865 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
4866 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
4867 NewV.getOperand(0),
4868 X86::getShufflePSHUFLWImmediate(NewV.getNode()),
4869 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004870 }
Eric Christopherfd179292009-08-27 18:07:15 +00004871
Nate Begemanb9a47b82009-02-23 08:49:38 +00004872 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
4873 // and update MaskVals with the new element order.
4874 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004875 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004876 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004877 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004878 for (unsigned i = 4; i != 8; ++i) {
4879 int idx = MaskVals[i];
4880 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004881 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004882 InOrder.set(i);
4883 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004884 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004885 InOrder.set(i);
4886 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004887 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004888 }
4889 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004890 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004891 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004892
4893 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
4894 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
4895 NewV.getOperand(0),
4896 X86::getShufflePSHUFHWImmediate(NewV.getNode()),
4897 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004898 }
Eric Christopherfd179292009-08-27 18:07:15 +00004899
Nate Begemanb9a47b82009-02-23 08:49:38 +00004900 // In case BestHi & BestLo were both -1, which means each quadword has a word
4901 // from each of the four input quadwords, calculate the InOrder bitvector now
4902 // before falling through to the insert/extract cleanup.
4903 if (BestLoQuad == -1 && BestHiQuad == -1) {
4904 NewV = V1;
4905 for (int i = 0; i != 8; ++i)
4906 if (MaskVals[i] < 0 || MaskVals[i] == i)
4907 InOrder.set(i);
4908 }
Eric Christopherfd179292009-08-27 18:07:15 +00004909
Nate Begemanb9a47b82009-02-23 08:49:38 +00004910 // The other elements are put in the right place using pextrw and pinsrw.
4911 for (unsigned i = 0; i != 8; ++i) {
4912 if (InOrder[i])
4913 continue;
4914 int EltIdx = MaskVals[i];
4915 if (EltIdx < 0)
4916 continue;
4917 SDValue ExtOp = (EltIdx < 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00004918 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004919 DAG.getIntPtrConstant(EltIdx))
Owen Anderson825b72b2009-08-11 20:47:22 +00004920 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004921 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00004922 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004923 DAG.getIntPtrConstant(i));
4924 }
4925 return NewV;
4926}
4927
4928// v16i8 shuffles - Prefer shuffles in the following order:
4929// 1. [ssse3] 1 x pshufb
4930// 2. [ssse3] 2 x pshufb + 1 x por
4931// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
4932static
Nate Begeman9008ca62009-04-27 18:41:29 +00004933SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00004934 SelectionDAG &DAG,
4935 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004936 SDValue V1 = SVOp->getOperand(0);
4937 SDValue V2 = SVOp->getOperand(1);
4938 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004939 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00004940 SVOp->getMask(MaskVals);
Eric Christopherfd179292009-08-27 18:07:15 +00004941
Nate Begemanb9a47b82009-02-23 08:49:38 +00004942 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00004943 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00004944 // present, fall back to case 3.
4945 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
4946 bool V1Only = true;
4947 bool V2Only = true;
4948 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004949 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00004950 if (EltIdx < 0)
4951 continue;
4952 if (EltIdx < 16)
4953 V2Only = false;
4954 else
4955 V1Only = false;
4956 }
Eric Christopherfd179292009-08-27 18:07:15 +00004957
Nate Begemanb9a47b82009-02-23 08:49:38 +00004958 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
4959 if (TLI.getSubtarget()->hasSSSE3()) {
4960 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004961
Nate Begemanb9a47b82009-02-23 08:49:38 +00004962 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00004963 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00004964 //
4965 // Otherwise, we have elements from both input vectors, and must zero out
4966 // elements that come from V2 in the first mask, and V1 in the second mask
4967 // so that we can OR them together.
4968 bool TwoInputs = !(V1Only || V2Only);
4969 for (unsigned i = 0; i != 16; ++i) {
4970 int EltIdx = MaskVals[i];
4971 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004972 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004973 continue;
4974 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004975 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004976 }
4977 // If all the elements are from V2, assign it to V1 and return after
4978 // building the first pshufb.
4979 if (V2Only)
4980 V1 = V2;
Owen Anderson825b72b2009-08-11 20:47:22 +00004981 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004982 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004983 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004984 if (!TwoInputs)
4985 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00004986
Nate Begemanb9a47b82009-02-23 08:49:38 +00004987 // Calculate the shuffle mask for the second input, shuffle it, and
4988 // OR it with the first shuffled input.
4989 pshufbMask.clear();
4990 for (unsigned i = 0; i != 16; ++i) {
4991 int EltIdx = MaskVals[i];
4992 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004993 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004994 continue;
4995 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004996 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004997 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004998 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00004999 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005000 MVT::v16i8, &pshufbMask[0], 16));
5001 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005002 }
Eric Christopherfd179292009-08-27 18:07:15 +00005003
Nate Begemanb9a47b82009-02-23 08:49:38 +00005004 // No SSSE3 - Calculate in place words and then fix all out of place words
5005 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
5006 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005007 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
5008 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005009 SDValue NewV = V2Only ? V2 : V1;
5010 for (int i = 0; i != 8; ++i) {
5011 int Elt0 = MaskVals[i*2];
5012 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00005013
Nate Begemanb9a47b82009-02-23 08:49:38 +00005014 // This word of the result is all undef, skip it.
5015 if (Elt0 < 0 && Elt1 < 0)
5016 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005017
Nate Begemanb9a47b82009-02-23 08:49:38 +00005018 // This word of the result is already in the correct place, skip it.
5019 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
5020 continue;
5021 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
5022 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005023
Nate Begemanb9a47b82009-02-23 08:49:38 +00005024 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
5025 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
5026 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00005027
5028 // If Elt0 and Elt1 are defined, are consecutive, and can be load
5029 // using a single extract together, load it and store it.
5030 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005031 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005032 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00005033 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005034 DAG.getIntPtrConstant(i));
5035 continue;
5036 }
5037
Nate Begemanb9a47b82009-02-23 08:49:38 +00005038 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00005039 // source byte is not also odd, shift the extracted word left 8 bits
5040 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005041 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005042 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005043 DAG.getIntPtrConstant(Elt1 / 2));
5044 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005045 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005046 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005047 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005048 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
5049 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005050 }
5051 // If Elt0 is defined, extract it from the appropriate source. If the
5052 // source byte is not also even, shift the extracted word right 8 bits. If
5053 // Elt1 was also defined, OR the extracted values together before
5054 // inserting them in the result.
5055 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005056 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005057 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
5058 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005059 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005060 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005061 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005062 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
5063 DAG.getConstant(0x00FF, MVT::i16));
5064 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00005065 : InsElt0;
5066 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005067 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005068 DAG.getIntPtrConstant(i));
5069 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005070 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005071}
5072
Evan Cheng7a831ce2007-12-15 03:00:47 +00005073/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005074/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00005075/// done when every pair / quad of shuffle mask elements point to elements in
5076/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005077/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00005078static
Nate Begeman9008ca62009-04-27 18:41:29 +00005079SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005080 SelectionDAG &DAG, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00005081 EVT VT = SVOp->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00005082 SDValue V1 = SVOp->getOperand(0);
5083 SDValue V2 = SVOp->getOperand(1);
5084 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00005085 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005086 EVT NewVT;
Owen Anderson825b72b2009-08-11 20:47:22 +00005087 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005088 default: assert(false && "Unexpected!");
Owen Anderson825b72b2009-08-11 20:47:22 +00005089 case MVT::v4f32: NewVT = MVT::v2f64; break;
5090 case MVT::v4i32: NewVT = MVT::v2i64; break;
5091 case MVT::v8i16: NewVT = MVT::v4i32; break;
5092 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00005093 }
5094
Nate Begeman9008ca62009-04-27 18:41:29 +00005095 int Scale = NumElems / NewWidth;
5096 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00005097 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005098 int StartIdx = -1;
5099 for (int j = 0; j < Scale; ++j) {
5100 int EltIdx = SVOp->getMaskElt(i+j);
5101 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005102 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00005103 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00005104 StartIdx = EltIdx - (EltIdx % Scale);
5105 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00005106 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00005107 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005108 if (StartIdx == -1)
5109 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00005110 else
Nate Begeman9008ca62009-04-27 18:41:29 +00005111 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005112 }
5113
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005114 V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, V1);
5115 V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00005116 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005117}
5118
Evan Chengd880b972008-05-09 21:53:03 +00005119/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005120///
Owen Andersone50ed302009-08-10 22:56:29 +00005121static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00005122 SDValue SrcOp, SelectionDAG &DAG,
5123 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005124 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005125 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00005126 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00005127 LD = dyn_cast<LoadSDNode>(SrcOp);
5128 if (!LD) {
5129 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
5130 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00005131 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00005132 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00005133 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005134 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00005135 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005136 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00005137 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005138 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005139 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
5140 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
5141 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00005142 SrcOp.getOperand(0)
5143 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005144 }
5145 }
5146 }
5147
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005148 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005149 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005150 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00005151 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005152}
5153
Evan Chengace3c172008-07-22 21:13:36 +00005154/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
5155/// shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00005156static SDValue
Nate Begeman9008ca62009-04-27 18:41:29 +00005157LowerVECTOR_SHUFFLE_4wide(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
5158 SDValue V1 = SVOp->getOperand(0);
5159 SDValue V2 = SVOp->getOperand(1);
5160 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005161 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00005162
Evan Chengace3c172008-07-22 21:13:36 +00005163 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00005164 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00005165 SmallVector<int, 8> Mask1(4U, -1);
5166 SmallVector<int, 8> PermMask;
5167 SVOp->getMask(PermMask);
5168
Evan Chengace3c172008-07-22 21:13:36 +00005169 unsigned NumHi = 0;
5170 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00005171 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005172 int Idx = PermMask[i];
5173 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00005174 Locs[i] = std::make_pair(-1, -1);
5175 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005176 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
5177 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00005178 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00005179 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005180 NumLo++;
5181 } else {
5182 Locs[i] = std::make_pair(1, NumHi);
5183 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00005184 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005185 NumHi++;
5186 }
5187 }
5188 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005189
Evan Chengace3c172008-07-22 21:13:36 +00005190 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005191 // If no more than two elements come from either vector. This can be
5192 // implemented with two shuffles. First shuffle gather the elements.
5193 // The second shuffle, which takes the first shuffle as both of its
5194 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00005195 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005196
Nate Begeman9008ca62009-04-27 18:41:29 +00005197 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopherfd179292009-08-27 18:07:15 +00005198
Evan Chengace3c172008-07-22 21:13:36 +00005199 for (unsigned i = 0; i != 4; ++i) {
5200 if (Locs[i].first == -1)
5201 continue;
5202 else {
5203 unsigned Idx = (i < 2) ? 0 : 4;
5204 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00005205 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005206 }
5207 }
5208
Nate Begeman9008ca62009-04-27 18:41:29 +00005209 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005210 } else if (NumLo == 3 || NumHi == 3) {
5211 // Otherwise, we must have three elements from one vector, call it X, and
5212 // one element from the other, call it Y. First, use a shufps to build an
5213 // intermediate vector with the one element from Y and the element from X
5214 // that will be in the same half in the final destination (the indexes don't
5215 // matter). Then, use a shufps to build the final vector, taking the half
5216 // containing the element from Y from the intermediate, and the other half
5217 // from X.
5218 if (NumHi == 3) {
5219 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00005220 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005221 std::swap(V1, V2);
5222 }
5223
5224 // Find the element from V2.
5225 unsigned HiIndex;
5226 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005227 int Val = PermMask[HiIndex];
5228 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005229 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005230 if (Val >= 4)
5231 break;
5232 }
5233
Nate Begeman9008ca62009-04-27 18:41:29 +00005234 Mask1[0] = PermMask[HiIndex];
5235 Mask1[1] = -1;
5236 Mask1[2] = PermMask[HiIndex^1];
5237 Mask1[3] = -1;
5238 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005239
5240 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005241 Mask1[0] = PermMask[0];
5242 Mask1[1] = PermMask[1];
5243 Mask1[2] = HiIndex & 1 ? 6 : 4;
5244 Mask1[3] = HiIndex & 1 ? 4 : 6;
5245 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005246 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005247 Mask1[0] = HiIndex & 1 ? 2 : 0;
5248 Mask1[1] = HiIndex & 1 ? 0 : 2;
5249 Mask1[2] = PermMask[2];
5250 Mask1[3] = PermMask[3];
5251 if (Mask1[2] >= 0)
5252 Mask1[2] += 4;
5253 if (Mask1[3] >= 0)
5254 Mask1[3] += 4;
5255 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005256 }
Evan Chengace3c172008-07-22 21:13:36 +00005257 }
5258
5259 // Break it into (shuffle shuffle_hi, shuffle_lo).
5260 Locs.clear();
Nate Begeman9008ca62009-04-27 18:41:29 +00005261 SmallVector<int,8> LoMask(4U, -1);
5262 SmallVector<int,8> HiMask(4U, -1);
5263
5264 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00005265 unsigned MaskIdx = 0;
5266 unsigned LoIdx = 0;
5267 unsigned HiIdx = 2;
5268 for (unsigned i = 0; i != 4; ++i) {
5269 if (i == 2) {
5270 MaskPtr = &HiMask;
5271 MaskIdx = 1;
5272 LoIdx = 0;
5273 HiIdx = 2;
5274 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005275 int Idx = PermMask[i];
5276 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00005277 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00005278 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00005279 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00005280 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005281 LoIdx++;
5282 } else {
5283 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00005284 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005285 HiIdx++;
5286 }
5287 }
5288
Nate Begeman9008ca62009-04-27 18:41:29 +00005289 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
5290 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
5291 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00005292 for (unsigned i = 0; i != 4; ++i) {
5293 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005294 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00005295 } else {
5296 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00005297 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00005298 }
5299 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005300 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00005301}
5302
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005303static bool MayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005304 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005305 V = V.getOperand(0);
5306 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
5307 V = V.getOperand(0);
5308 if (MayFoldLoad(V))
5309 return true;
5310 return false;
5311}
5312
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005313// FIXME: the version above should always be used. Since there's
5314// a bug where several vector shuffles can't be folded because the
5315// DAG is not updated during lowering and a node claims to have two
5316// uses while it only has one, use this version, and let isel match
5317// another instruction if the load really happens to have more than
5318// one use. Remove this version after this bug get fixed.
Evan Cheng835580f2010-10-07 20:50:20 +00005319// rdar://8434668, PR8156
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005320static bool RelaxedMayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005321 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005322 V = V.getOperand(0);
5323 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
5324 V = V.getOperand(0);
5325 if (ISD::isNormalLoad(V.getNode()))
5326 return true;
5327 return false;
5328}
5329
5330/// CanFoldShuffleIntoVExtract - Check if the current shuffle is used by
5331/// a vector extract, and if both can be later optimized into a single load.
5332/// This is done in visitEXTRACT_VECTOR_ELT and the conditions are checked
5333/// here because otherwise a target specific shuffle node is going to be
5334/// emitted for this shuffle, and the optimization not done.
5335/// FIXME: This is probably not the best approach, but fix the problem
5336/// until the right path is decided.
5337static
5338bool CanXFormVExtractWithShuffleIntoLoad(SDValue V, SelectionDAG &DAG,
5339 const TargetLowering &TLI) {
5340 EVT VT = V.getValueType();
5341 ShuffleVectorSDNode *SVOp = dyn_cast<ShuffleVectorSDNode>(V);
5342
5343 // Be sure that the vector shuffle is present in a pattern like this:
5344 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), c) -> (f32 load $addr)
5345 if (!V.hasOneUse())
5346 return false;
5347
5348 SDNode *N = *V.getNode()->use_begin();
5349 if (N->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
5350 return false;
5351
5352 SDValue EltNo = N->getOperand(1);
5353 if (!isa<ConstantSDNode>(EltNo))
5354 return false;
5355
5356 // If the bit convert changed the number of elements, it is unsafe
5357 // to examine the mask.
5358 bool HasShuffleIntoBitcast = false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005359 if (V.getOpcode() == ISD::BITCAST) {
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005360 EVT SrcVT = V.getOperand(0).getValueType();
5361 if (SrcVT.getVectorNumElements() != VT.getVectorNumElements())
5362 return false;
5363 V = V.getOperand(0);
5364 HasShuffleIntoBitcast = true;
5365 }
5366
5367 // Select the input vector, guarding against out of range extract vector.
5368 unsigned NumElems = VT.getVectorNumElements();
5369 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
5370 int Idx = (Elt > NumElems) ? -1 : SVOp->getMaskElt(Elt);
5371 V = (Idx < (int)NumElems) ? V.getOperand(0) : V.getOperand(1);
5372
5373 // Skip one more bit_convert if necessary
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005374 if (V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005375 V = V.getOperand(0);
5376
5377 if (ISD::isNormalLoad(V.getNode())) {
5378 // Is the original load suitable?
5379 LoadSDNode *LN0 = cast<LoadSDNode>(V);
5380
5381 // FIXME: avoid the multi-use bug that is preventing lots of
5382 // of foldings to be detected, this is still wrong of course, but
5383 // give the temporary desired behavior, and if it happens that
5384 // the load has real more uses, during isel it will not fold, and
5385 // will generate poor code.
5386 if (!LN0 || LN0->isVolatile()) // || !LN0->hasOneUse()
5387 return false;
5388
5389 if (!HasShuffleIntoBitcast)
5390 return true;
5391
5392 // If there's a bitcast before the shuffle, check if the load type and
5393 // alignment is valid.
5394 unsigned Align = LN0->getAlignment();
5395 unsigned NewAlign =
5396 TLI.getTargetData()->getABITypeAlignment(
5397 VT.getTypeForEVT(*DAG.getContext()));
5398
5399 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
5400 return false;
5401 }
5402
5403 return true;
5404}
5405
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005406static
Evan Cheng835580f2010-10-07 20:50:20 +00005407SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
5408 EVT VT = Op.getValueType();
5409
5410 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005411 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
5412 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00005413 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
5414 V1, DAG));
5415}
5416
5417static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005418SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
5419 bool HasSSE2) {
5420 SDValue V1 = Op.getOperand(0);
5421 SDValue V2 = Op.getOperand(1);
5422 EVT VT = Op.getValueType();
5423
5424 assert(VT != MVT::v2i64 && "unsupported shuffle type");
5425
5426 if (HasSSE2 && VT == MVT::v2f64)
5427 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
5428
5429 // v4f32 or v4i32
5430 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V2, DAG);
5431}
5432
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00005433static
5434SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
5435 SDValue V1 = Op.getOperand(0);
5436 SDValue V2 = Op.getOperand(1);
5437 EVT VT = Op.getValueType();
5438
5439 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
5440 "unsupported shuffle type");
5441
5442 if (V2.getOpcode() == ISD::UNDEF)
5443 V2 = V1;
5444
5445 // v4i32 or v4f32
5446 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
5447}
5448
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005449static
5450SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasSSE2) {
5451 SDValue V1 = Op.getOperand(0);
5452 SDValue V2 = Op.getOperand(1);
5453 EVT VT = Op.getValueType();
5454 unsigned NumElems = VT.getVectorNumElements();
5455
5456 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
5457 // operand of these instructions is only memory, so check if there's a
5458 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
5459 // same masks.
5460 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005461
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00005462 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005463 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005464 CanFoldLoad = true;
5465
5466 // When V1 is a load, it can be folded later into a store in isel, example:
5467 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
5468 // turns into:
5469 // (MOVLPSmr addr:$src1, VR128:$src2)
5470 // So, recognize this potential and also use MOVLPS or MOVLPD
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005471 if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005472 CanFoldLoad = true;
5473
5474 if (CanFoldLoad) {
5475 if (HasSSE2 && NumElems == 2)
5476 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
5477
5478 if (NumElems == 4)
5479 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
5480 }
5481
5482 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5483 // movl and movlp will both match v2i64, but v2i64 is never matched by
5484 // movl earlier because we make it strict to avoid messing with the movlp load
5485 // folding logic (see the code above getMOVLP call). Match it here then,
5486 // this is horrible, but will stay like this until we move all shuffle
5487 // matching to x86 specific nodes. Note that for the 1st condition all
5488 // types are matched with movsd.
5489 if ((HasSSE2 && NumElems == 2) || !X86::isMOVLMask(SVOp))
5490 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
5491 else if (HasSSE2)
5492 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
5493
5494
5495 assert(VT != MVT::v4i32 && "unsupported shuffle type");
5496
5497 // Invert the operand order and use SHUFPS to match it.
5498 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V2, V1,
5499 X86::getShuffleSHUFImmediate(SVOp), DAG);
5500}
5501
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005502static inline unsigned getUNPCKLOpcode(EVT VT) {
5503 switch(VT.getSimpleVT().SimpleTy) {
5504 case MVT::v4i32: return X86ISD::PUNPCKLDQ;
5505 case MVT::v2i64: return X86ISD::PUNPCKLQDQ;
5506 case MVT::v4f32: return X86ISD::UNPCKLPS;
5507 case MVT::v2f64: return X86ISD::UNPCKLPD;
5508 case MVT::v16i8: return X86ISD::PUNPCKLBW;
5509 case MVT::v8i16: return X86ISD::PUNPCKLWD;
5510 default:
5511 llvm_unreachable("Unknow type for unpckl");
5512 }
5513 return 0;
5514}
5515
5516static inline unsigned getUNPCKHOpcode(EVT VT) {
5517 switch(VT.getSimpleVT().SimpleTy) {
5518 case MVT::v4i32: return X86ISD::PUNPCKHDQ;
5519 case MVT::v2i64: return X86ISD::PUNPCKHQDQ;
5520 case MVT::v4f32: return X86ISD::UNPCKHPS;
5521 case MVT::v2f64: return X86ISD::UNPCKHPD;
5522 case MVT::v16i8: return X86ISD::PUNPCKHBW;
5523 case MVT::v8i16: return X86ISD::PUNPCKHWD;
5524 default:
5525 llvm_unreachable("Unknow type for unpckh");
5526 }
5527 return 0;
5528}
5529
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005530static
5531SDValue NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG,
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005532 const TargetLowering &TLI,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005533 const X86Subtarget *Subtarget) {
5534 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5535 EVT VT = Op.getValueType();
5536 DebugLoc dl = Op.getDebugLoc();
5537 SDValue V1 = Op.getOperand(0);
5538 SDValue V2 = Op.getOperand(1);
5539
5540 if (isZeroShuffle(SVOp))
5541 return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
5542
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005543 // Handle splat operations
5544 if (SVOp->isSplat()) {
5545 // Special case, this is the only place now where it's
5546 // allowed to return a vector_shuffle operation without
5547 // using a target specific node, because *hopefully* it
5548 // will be optimized away by the dag combiner.
5549 if (VT.getVectorNumElements() <= 4 &&
5550 CanXFormVExtractWithShuffleIntoLoad(Op, DAG, TLI))
5551 return Op;
5552
5553 // Handle splats by matching through known masks
5554 if (VT.getVectorNumElements() <= 4)
5555 return SDValue();
5556
Evan Cheng835580f2010-10-07 20:50:20 +00005557 // Canonicalize all of the remaining to v4f32.
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005558 return PromoteSplat(SVOp, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005559 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005560
5561 // If the shuffle can be profitably rewritten as a narrower shuffle, then
5562 // do it!
5563 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
5564 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5565 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005566 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005567 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
5568 // FIXME: Figure out a cleaner way to do this.
5569 // Try to make use of movq to zero out the top part.
5570 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
5571 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5572 if (NewOp.getNode()) {
5573 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
5574 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
5575 DAG, Subtarget, dl);
5576 }
5577 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
5578 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5579 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
5580 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
5581 DAG, Subtarget, dl);
5582 }
5583 }
5584 return SDValue();
5585}
5586
Dan Gohman475871a2008-07-27 21:46:04 +00005587SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005588X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00005589 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00005590 SDValue V1 = Op.getOperand(0);
5591 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00005592 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005593 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00005594 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005595 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005596 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
5597 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00005598 bool V1IsSplat = false;
5599 bool V2IsSplat = false;
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005600 bool HasSSE2 = Subtarget->hasSSE2() || Subtarget->hasAVX();
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00005601 bool HasSSE3 = Subtarget->hasSSE3() || Subtarget->hasAVX();
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00005602 bool HasSSSE3 = Subtarget->hasSSSE3() || Subtarget->hasAVX();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005603 MachineFunction &MF = DAG.getMachineFunction();
5604 bool OptForSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005605
Dale Johannesen0488fb62010-09-30 23:57:10 +00005606 // Shuffle operations on MMX not supported.
5607 if (isMMX)
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00005608 return Op;
5609
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005610 // Vector shuffle lowering takes 3 steps:
5611 //
5612 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
5613 // narrowing and commutation of operands should be handled.
5614 // 2) Matching of shuffles with known shuffle masks to x86 target specific
5615 // shuffle nodes.
5616 // 3) Rewriting of unmatched masks into new generic shuffle operations,
5617 // so the shuffle can be broken into other shuffles and the legalizer can
5618 // try the lowering again.
5619 //
5620 // The general ideia is that no vector_shuffle operation should be left to
5621 // be matched during isel, all of them must be converted to a target specific
5622 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00005623
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005624 // Normalize the input vectors. Here splats, zeroed vectors, profitable
5625 // narrowing and commutation of operands should be handled. The actual code
5626 // doesn't include all of those, work in progress...
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005627 SDValue NewOp = NormalizeVectorShuffle(Op, DAG, *this, Subtarget);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005628 if (NewOp.getNode())
5629 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00005630
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005631 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
5632 // unpckh_undef). Only use pshufd if speed is more important than size.
5633 if (OptForSize && X86::isUNPCKL_v_undef_Mask(SVOp))
5634 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5635 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V1, DAG);
5636 if (OptForSize && X86::isUNPCKH_v_undef_Mask(SVOp))
5637 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5638 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00005639
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005640 if (X86::isMOVDDUPMask(SVOp) && HasSSE3 && V2IsUndef &&
Dale Johannesen0488fb62010-09-30 23:57:10 +00005641 RelaxedMayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00005642 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005643
Dale Johannesen0488fb62010-09-30 23:57:10 +00005644 if (X86::isMOVHLPS_v_undef_Mask(SVOp))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005645 return getMOVHighToLow(Op, dl, DAG);
5646
5647 // Use to match splats
5648 if (HasSSE2 && X86::isUNPCKHMask(SVOp) && V2IsUndef &&
5649 (VT == MVT::v2f64 || VT == MVT::v2i64))
5650 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
5651
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005652 if (X86::isPSHUFDMask(SVOp)) {
5653 // The actual implementation will match the mask in the if above and then
5654 // during isel it can match several different instructions, not only pshufd
5655 // as its name says, sad but true, emulate the behavior for now...
5656 if (X86::isMOVDDUPMask(SVOp) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
5657 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
5658
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005659 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
5660
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005661 if (HasSSE2 && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005662 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
5663
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005664 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005665 return getTargetShuffleNode(X86ISD::SHUFPD, dl, VT, V1, V1,
5666 TargetMask, DAG);
5667
5668 if (VT == MVT::v4f32)
5669 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V1, V1,
5670 TargetMask, DAG);
5671 }
Eric Christopherfd179292009-08-27 18:07:15 +00005672
Evan Chengf26ffe92008-05-29 08:22:04 +00005673 // Check if this can be converted into a logical shift.
5674 bool isLeft = false;
5675 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00005676 SDValue ShVal;
Nate Begeman9008ca62009-04-27 18:41:29 +00005677 bool isShift = getSubtarget()->hasSSE2() &&
Evan Chengc3630942009-12-09 21:00:30 +00005678 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00005679 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00005680 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00005681 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00005682 EVT EltVT = VT.getVectorElementType();
5683 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00005684 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005685 }
Eric Christopherfd179292009-08-27 18:07:15 +00005686
Nate Begeman9008ca62009-04-27 18:41:29 +00005687 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005688 if (V1IsUndef)
5689 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00005690 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00005691 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Dale Johannesen0488fb62010-09-30 23:57:10 +00005692 if (!X86::isMOVLPMask(SVOp)) {
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005693 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00005694 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
5695
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005696 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00005697 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
5698 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00005699 }
Eric Christopherfd179292009-08-27 18:07:15 +00005700
Nate Begeman9008ca62009-04-27 18:41:29 +00005701 // FIXME: fold these into legal mask.
Dale Johannesen0488fb62010-09-30 23:57:10 +00005702 if (X86::isMOVLHPSMask(SVOp) && !X86::isUNPCKLMask(SVOp))
5703 return getMOVLowToHigh(Op, dl, DAG, HasSSE2);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005704
Dale Johannesen0488fb62010-09-30 23:57:10 +00005705 if (X86::isMOVHLPSMask(SVOp))
5706 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00005707
Dale Johannesen0488fb62010-09-30 23:57:10 +00005708 if (X86::isMOVSHDUPMask(SVOp) && HasSSE3 && V2IsUndef && NumElems == 4)
5709 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00005710
Dale Johannesen0488fb62010-09-30 23:57:10 +00005711 if (X86::isMOVSLDUPMask(SVOp) && HasSSE3 && V2IsUndef && NumElems == 4)
5712 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00005713
Dale Johannesen0488fb62010-09-30 23:57:10 +00005714 if (X86::isMOVLPMask(SVOp))
5715 return getMOVLP(Op, dl, DAG, HasSSE2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005716
Nate Begeman9008ca62009-04-27 18:41:29 +00005717 if (ShouldXformToMOVHLPS(SVOp) ||
5718 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
5719 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005720
Evan Chengf26ffe92008-05-29 08:22:04 +00005721 if (isShift) {
5722 // No better options. Use a vshl / vsrl.
Dan Gohman8a55ce42009-09-23 21:02:20 +00005723 EVT EltVT = VT.getVectorElementType();
5724 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00005725 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005726 }
Eric Christopherfd179292009-08-27 18:07:15 +00005727
Evan Cheng9eca5e82006-10-25 21:49:50 +00005728 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00005729 // FIXME: This should also accept a bitcast of a splat? Be careful, not
5730 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00005731 V1IsSplat = isSplatVector(V1.getNode());
5732 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00005733
Chris Lattner8a594482007-11-25 00:24:49 +00005734 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00005735 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005736 Op = CommuteVectorShuffle(SVOp, DAG);
5737 SVOp = cast<ShuffleVectorSDNode>(Op);
5738 V1 = SVOp->getOperand(0);
5739 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00005740 std::swap(V1IsSplat, V2IsSplat);
5741 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00005742 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00005743 }
5744
Nate Begeman9008ca62009-04-27 18:41:29 +00005745 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
5746 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00005747 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00005748 return V1;
5749 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
5750 // the instruction selector will not match, so get a canonical MOVL with
5751 // swapped operands to undo the commute.
5752 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00005753 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005754
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005755 if (X86::isUNPCKLMask(SVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005756 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005757
5758 if (X86::isUNPCKHMask(SVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005759 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00005760
Evan Cheng9bbbb982006-10-25 20:48:19 +00005761 if (V2IsSplat) {
5762 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005763 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00005764 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00005765 SDValue NewMask = NormalizeMask(SVOp, DAG);
5766 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
5767 if (NSVOp != SVOp) {
5768 if (X86::isUNPCKLMask(NSVOp, true)) {
5769 return NewMask;
5770 } else if (X86::isUNPCKHMask(NSVOp, true)) {
5771 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005772 }
5773 }
5774 }
5775
Evan Cheng9eca5e82006-10-25 21:49:50 +00005776 if (Commuted) {
5777 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00005778 // FIXME: this seems wrong.
5779 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
5780 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005781
5782 if (X86::isUNPCKLMask(NewSVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005783 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V2, V1, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005784
5785 if (X86::isUNPCKHMask(NewSVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005786 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V2, V1, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00005787 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005788
Nate Begeman9008ca62009-04-27 18:41:29 +00005789 // Normalize the node to match x86 shuffle ops if needed
Dale Johannesen0488fb62010-09-30 23:57:10 +00005790 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
Nate Begeman9008ca62009-04-27 18:41:29 +00005791 return CommuteVectorShuffle(SVOp, DAG);
5792
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00005793 // The checks below are all present in isShuffleMaskLegal, but they are
5794 // inlined here right now to enable us to directly emit target specific
5795 // nodes, and remove one by one until they don't return Op anymore.
5796 SmallVector<int, 16> M;
5797 SVOp->getMask(M);
5798
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00005799 if (isPALIGNRMask(M, VT, HasSSSE3))
5800 return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
5801 X86::getShufflePALIGNRImmediate(SVOp),
5802 DAG);
5803
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00005804 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
5805 SVOp->getSplatIndex() == 0 && V2IsUndef) {
5806 if (VT == MVT::v2f64)
5807 return getTargetShuffleNode(X86ISD::UNPCKLPD, dl, VT, V1, V1, DAG);
5808 if (VT == MVT::v2i64)
5809 return getTargetShuffleNode(X86ISD::PUNPCKLQDQ, dl, VT, V1, V1, DAG);
5810 }
5811
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00005812 if (isPSHUFHWMask(M, VT))
5813 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
5814 X86::getShufflePSHUFHWImmediate(SVOp),
5815 DAG);
5816
5817 if (isPSHUFLWMask(M, VT))
5818 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
5819 X86::getShufflePSHUFLWImmediate(SVOp),
5820 DAG);
5821
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00005822 if (isSHUFPMask(M, VT)) {
5823 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
5824 if (VT == MVT::v4f32 || VT == MVT::v4i32)
5825 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V1, V2,
5826 TargetMask, DAG);
5827 if (VT == MVT::v2f64 || VT == MVT::v2i64)
5828 return getTargetShuffleNode(X86ISD::SHUFPD, dl, VT, V1, V2,
5829 TargetMask, DAG);
5830 }
5831
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005832 if (X86::isUNPCKL_v_undef_Mask(SVOp))
5833 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5834 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V1, DAG);
5835 if (X86::isUNPCKH_v_undef_Mask(SVOp))
5836 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5837 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
5838
Evan Cheng14b32e12007-12-11 01:46:18 +00005839 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
Owen Anderson825b72b2009-08-11 20:47:22 +00005840 if (VT == MVT::v8i16) {
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005841 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00005842 if (NewOp.getNode())
Evan Cheng14b32e12007-12-11 01:46:18 +00005843 return NewOp;
5844 }
5845
Owen Anderson825b72b2009-08-11 20:47:22 +00005846 if (VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005847 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005848 if (NewOp.getNode())
5849 return NewOp;
5850 }
Eric Christopherfd179292009-08-27 18:07:15 +00005851
Dale Johannesen0488fb62010-09-30 23:57:10 +00005852 // Handle all 4 wide cases with a number of shuffles.
5853 if (NumElems == 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00005854 return LowerVECTOR_SHUFFLE_4wide(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005855
Dan Gohman475871a2008-07-27 21:46:04 +00005856 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005857}
5858
Dan Gohman475871a2008-07-27 21:46:04 +00005859SDValue
5860X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00005861 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005862 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005863 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005864 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005865 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005866 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00005867 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005868 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00005869 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005870 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00005871 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
5872 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
5873 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005874 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
5875 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005876 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005877 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00005878 Op.getOperand(0)),
5879 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00005880 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005881 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00005882 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005883 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00005884 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Anderson825b72b2009-08-11 20:47:22 +00005885 } else if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00005886 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
5887 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00005888 // result has a single use which is a store or a bitcast to i32. And in
5889 // the case of a store, it's not worth it if the index is a constant 0,
5890 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00005891 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00005892 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00005893 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00005894 if ((User->getOpcode() != ISD::STORE ||
5895 (isa<ConstantSDNode>(Op.getOperand(1)) &&
5896 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005897 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00005898 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00005899 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00005900 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005901 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00005902 Op.getOperand(0)),
5903 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005904 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Owen Anderson825b72b2009-08-11 20:47:22 +00005905 } else if (VT == MVT::i32) {
Mon P Wangf0fcdd82009-01-15 21:10:20 +00005906 // ExtractPS works with constant index.
5907 if (isa<ConstantSDNode>(Op.getOperand(1)))
5908 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00005909 }
Dan Gohman475871a2008-07-27 21:46:04 +00005910 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005911}
5912
5913
Dan Gohman475871a2008-07-27 21:46:04 +00005914SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005915X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
5916 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005917 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00005918 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005919
Evan Cheng62a3f152008-03-24 21:52:23 +00005920 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005921 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00005922 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00005923 return Res;
5924 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00005925
Owen Andersone50ed302009-08-10 22:56:29 +00005926 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005927 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005928 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005929 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00005930 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005931 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00005932 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005933 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
5934 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005935 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005936 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00005937 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005938 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00005939 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00005940 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00005941 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00005942 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00005943 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00005944 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005945 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005946 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005947 if (Idx == 0)
5948 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00005949
Evan Cheng0db9fe62006-04-25 20:13:52 +00005950 // SHUFPS the element to the lowest double word, then movss.
Nate Begeman9008ca62009-04-27 18:41:29 +00005951 int Mask[4] = { Idx, -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00005952 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00005953 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00005954 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00005955 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00005956 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00005957 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00005958 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
5959 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
5960 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005961 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005962 if (Idx == 0)
5963 return Op;
5964
5965 // UNPCKHPD the element to the lowest double word, then movsd.
5966 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
5967 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00005968 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00005969 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00005970 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00005971 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00005972 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00005973 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005974 }
5975
Dan Gohman475871a2008-07-27 21:46:04 +00005976 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005977}
5978
Dan Gohman475871a2008-07-27 21:46:04 +00005979SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005980X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
5981 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005982 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00005983 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005984 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005985
Dan Gohman475871a2008-07-27 21:46:04 +00005986 SDValue N0 = Op.getOperand(0);
5987 SDValue N1 = Op.getOperand(1);
5988 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00005989
Dan Gohman8a55ce42009-09-23 21:02:20 +00005990 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00005991 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00005992 unsigned Opc;
5993 if (VT == MVT::v8i16)
5994 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00005995 else if (VT == MVT::v16i8)
5996 Opc = X86ISD::PINSRB;
5997 else
5998 Opc = X86ISD::PINSRB;
5999
Nate Begeman14d12ca2008-02-11 04:19:36 +00006000 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
6001 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00006002 if (N1.getValueType() != MVT::i32)
6003 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
6004 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006005 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00006006 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00006007 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00006008 // Bits [7:6] of the constant are the source select. This will always be
6009 // zero here. The DAG Combiner may combine an extract_elt index into these
6010 // bits. For example (insert (extract, 3), 2) could be matched by putting
6011 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00006012 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00006013 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00006014 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00006015 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006016 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00006017 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00006018 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00006019 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00006020 } else if (EltVT == MVT::i32 && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00006021 // PINSR* works with constant index.
6022 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00006023 }
Dan Gohman475871a2008-07-27 21:46:04 +00006024 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006025}
6026
Dan Gohman475871a2008-07-27 21:46:04 +00006027SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006028X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006029 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006030 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006031
David Greene6b381262011-02-09 15:32:06 +00006032 DebugLoc dl = Op.getDebugLoc();
6033 SDValue N0 = Op.getOperand(0);
6034 SDValue N1 = Op.getOperand(1);
6035 SDValue N2 = Op.getOperand(2);
6036
6037 // If this is a 256-bit vector result, first insert into a 128-bit
6038 // vector and then insert into the 256-bit vector.
6039 if (VT.getSizeInBits() > 128) {
6040 if (!isa<ConstantSDNode>(N2))
6041 return SDValue();
6042
6043 // Get the 128-bit vector.
6044 unsigned NumElems = VT.getVectorNumElements();
6045 unsigned IdxVal = cast<ConstantSDNode>(N2)->getZExtValue();
6046 bool Upper = IdxVal >= NumElems / 2;
6047
6048 SDValue SubN0 = Extract128BitVector(N0, N2, DAG, dl);
6049
6050 // Insert into it.
6051 SDValue ScaledN2 = N2;
6052 if (Upper)
6053 ScaledN2 = DAG.getNode(ISD::SUB, dl, N2.getValueType(), N2,
6054 DAG.getConstant(NumElems /
6055 (VT.getSizeInBits() / 128),
6056 N2.getValueType()));
6057 Op = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, SubN0.getValueType(), SubN0,
6058 N1, ScaledN2);
6059
6060 // Insert the 128-bit vector
6061 // FIXME: Why UNDEF?
6062 return Insert128BitVector(N0, Op, N2, DAG, dl);
6063 }
6064
Nate Begeman14d12ca2008-02-11 04:19:36 +00006065 if (Subtarget->hasSSE41())
6066 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
6067
Dan Gohman8a55ce42009-09-23 21:02:20 +00006068 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00006069 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00006070
Dan Gohman8a55ce42009-09-23 21:02:20 +00006071 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00006072 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
6073 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00006074 if (N1.getValueType() != MVT::i32)
6075 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
6076 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006077 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00006078 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006079 }
Dan Gohman475871a2008-07-27 21:46:04 +00006080 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006081}
6082
Dan Gohman475871a2008-07-27 21:46:04 +00006083SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006084X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006085 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00006086
Chris Lattnerf172ecd2010-07-04 23:07:25 +00006087 if (Op.getValueType() == MVT::v1i64 &&
6088 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00006089 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00006090
Owen Anderson825b72b2009-08-11 20:47:22 +00006091 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Dale Johannesen0488fb62010-09-30 23:57:10 +00006092 assert(Op.getValueType().getSimpleVT().getSizeInBits() == 128 &&
6093 "Expected an SSE type!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006094 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(),
Dale Johannesen0488fb62010-09-30 23:57:10 +00006095 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006096}
6097
David Greene91585092011-01-26 15:38:49 +00006098// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
6099// a simple subregister reference or explicit instructions to grab
6100// upper bits of a vector.
6101SDValue
6102X86TargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
6103 if (Subtarget->hasAVX()) {
David Greenea5f26012011-02-07 19:36:54 +00006104 DebugLoc dl = Op.getNode()->getDebugLoc();
6105 SDValue Vec = Op.getNode()->getOperand(0);
6106 SDValue Idx = Op.getNode()->getOperand(1);
6107
6108 if (Op.getNode()->getValueType(0).getSizeInBits() == 128
6109 && Vec.getNode()->getValueType(0).getSizeInBits() == 256) {
6110 return Extract128BitVector(Vec, Idx, DAG, dl);
6111 }
David Greene91585092011-01-26 15:38:49 +00006112 }
6113 return SDValue();
6114}
6115
David Greenecfe33c42011-01-26 19:13:22 +00006116// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
6117// simple superregister reference or explicit instructions to insert
6118// the upper bits of a vector.
6119SDValue
6120X86TargetLowering::LowerINSERT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
6121 if (Subtarget->hasAVX()) {
6122 DebugLoc dl = Op.getNode()->getDebugLoc();
6123 SDValue Vec = Op.getNode()->getOperand(0);
6124 SDValue SubVec = Op.getNode()->getOperand(1);
6125 SDValue Idx = Op.getNode()->getOperand(2);
6126
6127 if (Op.getNode()->getValueType(0).getSizeInBits() == 256
6128 && SubVec.getNode()->getValueType(0).getSizeInBits() == 128) {
David Greenea5f26012011-02-07 19:36:54 +00006129 return Insert128BitVector(Vec, SubVec, Idx, DAG, dl);
David Greenecfe33c42011-01-26 19:13:22 +00006130 }
6131 }
6132 return SDValue();
6133}
6134
Bill Wendling056292f2008-09-16 21:48:12 +00006135// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
6136// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
6137// one of the above mentioned nodes. It has to be wrapped because otherwise
6138// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
6139// be used to form addressing mode. These wrapped nodes will be selected
6140// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00006141SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006142X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006143 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00006144
Chris Lattner41621a22009-06-26 19:22:52 +00006145 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6146 // global base reg.
6147 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006148 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006149 CodeModel::Model M = getTargetMachine().getCodeModel();
6150
Chris Lattner4f066492009-07-11 20:29:19 +00006151 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006152 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00006153 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00006154 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006155 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00006156 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006157 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00006158
Evan Cheng1606e8e2009-03-13 07:51:59 +00006159 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00006160 CP->getAlignment(),
6161 CP->getOffset(), OpFlag);
6162 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00006163 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006164 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00006165 if (OpFlag) {
6166 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00006167 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006168 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006169 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006170 }
6171
6172 return Result;
6173}
6174
Dan Gohmand858e902010-04-17 15:26:15 +00006175SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00006176 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00006177
Chris Lattner18c59872009-06-27 04:16:01 +00006178 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6179 // global base reg.
6180 unsigned char OpFlag = 0;
6181 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006182 CodeModel::Model M = getTargetMachine().getCodeModel();
6183
Chris Lattner4f066492009-07-11 20:29:19 +00006184 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006185 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00006186 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00006187 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006188 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00006189 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006190 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00006191
Chris Lattner18c59872009-06-27 04:16:01 +00006192 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
6193 OpFlag);
6194 DebugLoc DL = JT->getDebugLoc();
6195 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00006196
Chris Lattner18c59872009-06-27 04:16:01 +00006197 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00006198 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00006199 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6200 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006201 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00006202 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00006203
Chris Lattner18c59872009-06-27 04:16:01 +00006204 return Result;
6205}
6206
6207SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006208X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00006209 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00006210
Chris Lattner18c59872009-06-27 04:16:01 +00006211 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6212 // global base reg.
6213 unsigned char OpFlag = 0;
6214 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006215 CodeModel::Model M = getTargetMachine().getCodeModel();
6216
Chris Lattner4f066492009-07-11 20:29:19 +00006217 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006218 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00006219 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00006220 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006221 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00006222 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006223 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00006224
Chris Lattner18c59872009-06-27 04:16:01 +00006225 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00006226
Chris Lattner18c59872009-06-27 04:16:01 +00006227 DebugLoc DL = Op.getDebugLoc();
6228 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00006229
6230
Chris Lattner18c59872009-06-27 04:16:01 +00006231 // With PIC, the address is actually $g + Offset.
6232 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00006233 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00006234 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6235 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006236 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00006237 Result);
6238 }
Eric Christopherfd179292009-08-27 18:07:15 +00006239
Chris Lattner18c59872009-06-27 04:16:01 +00006240 return Result;
6241}
6242
Dan Gohman475871a2008-07-27 21:46:04 +00006243SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006244X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00006245 // Create the TargetBlockAddressAddress node.
6246 unsigned char OpFlags =
6247 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00006248 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00006249 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +00006250 DebugLoc dl = Op.getDebugLoc();
6251 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
6252 /*isTarget=*/true, OpFlags);
6253
Dan Gohmanf705adb2009-10-30 01:28:02 +00006254 if (Subtarget->isPICStyleRIPRel() &&
6255 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00006256 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
6257 else
6258 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00006259
Dan Gohman29cbade2009-11-20 23:18:13 +00006260 // With PIC, the address is actually $g + Offset.
6261 if (isGlobalRelativeToPICBase(OpFlags)) {
6262 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
6263 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
6264 Result);
6265 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00006266
6267 return Result;
6268}
6269
6270SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00006271X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00006272 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00006273 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00006274 // Create the TargetGlobalAddress node, folding in the constant
6275 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00006276 unsigned char OpFlags =
6277 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006278 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00006279 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006280 if (OpFlags == X86II::MO_NO_FLAG &&
6281 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00006282 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00006283 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00006284 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006285 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00006286 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00006287 }
Eric Christopherfd179292009-08-27 18:07:15 +00006288
Chris Lattner4f066492009-07-11 20:29:19 +00006289 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006290 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00006291 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
6292 else
6293 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00006294
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006295 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00006296 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00006297 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
6298 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006299 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006300 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006301
Chris Lattner36c25012009-07-10 07:34:39 +00006302 // For globals that require a load from a stub to get the address, emit the
6303 // load.
6304 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00006305 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00006306 MachinePointerInfo::getGOT(), false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006307
Dan Gohman6520e202008-10-18 02:06:02 +00006308 // If there was a non-zero offset that we didn't fold, create an explicit
6309 // addition for it.
6310 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00006311 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00006312 DAG.getConstant(Offset, getPointerTy()));
6313
Evan Cheng0db9fe62006-04-25 20:13:52 +00006314 return Result;
6315}
6316
Evan Chengda43bcf2008-09-24 00:05:32 +00006317SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006318X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00006319 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00006320 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006321 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00006322}
6323
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006324static SDValue
6325GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00006326 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerb903bed2009-06-26 21:20:29 +00006327 unsigned char OperandFlags) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006328 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006329 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006330 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00006331 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006332 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00006333 GA->getOffset(),
6334 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006335 if (InFlag) {
6336 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00006337 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006338 } else {
6339 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00006340 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006341 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006342
6343 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00006344 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006345
Rafael Espindola15f1b662009-04-24 12:59:40 +00006346 SDValue Flag = Chain.getValue(1);
6347 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006348}
6349
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006350// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00006351static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006352LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006353 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00006354 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00006355 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
6356 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006357 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006358 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006359 InFlag = Chain.getValue(1);
6360
Chris Lattnerb903bed2009-06-26 21:20:29 +00006361 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006362}
6363
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006364// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00006365static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006366LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006367 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00006368 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
6369 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006370}
6371
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006372// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
6373// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00006374static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006375 const EVT PtrVT, TLSModel::Model model,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00006376 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00006377 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00006378
Chris Lattnerf93b90c2010-09-22 04:39:11 +00006379 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
6380 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
6381 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00006382
Michael J. Spencerec38de22010-10-10 22:04:20 +00006383 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00006384 DAG.getIntPtrConstant(0),
6385 MachinePointerInfo(Ptr), false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00006386
Chris Lattnerb903bed2009-06-26 21:20:29 +00006387 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006388 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
6389 // initialexec.
6390 unsigned WrapperKind = X86ISD::Wrapper;
6391 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00006392 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00006393 } else if (is64Bit) {
6394 assert(model == TLSModel::InitialExec);
6395 OperandFlags = X86II::MO_GOTTPOFF;
6396 WrapperKind = X86ISD::WrapperRIP;
6397 } else {
6398 assert(model == TLSModel::InitialExec);
6399 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00006400 }
Eric Christopherfd179292009-08-27 18:07:15 +00006401
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006402 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
6403 // exec)
Michael J. Spencerec38de22010-10-10 22:04:20 +00006404 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00006405 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00006406 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00006407 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00006408
Rafael Espindola9a580232009-02-27 13:37:18 +00006409 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00006410 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00006411 MachinePointerInfo::getGOT(), false, false, 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00006412
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006413 // The address of the thread local variable is the add of the thread
6414 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00006415 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006416}
6417
Dan Gohman475871a2008-07-27 21:46:04 +00006418SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006419X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00006420
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006421 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00006422 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00006423
Eric Christopher30ef0e52010-06-03 04:07:48 +00006424 if (Subtarget->isTargetELF()) {
6425 // TODO: implement the "local dynamic" model
6426 // TODO: implement the "initial exec"model for pic executables
Michael J. Spencerec38de22010-10-10 22:04:20 +00006427
Eric Christopher30ef0e52010-06-03 04:07:48 +00006428 // If GV is an alias then use the aliasee for determining
6429 // thread-localness.
6430 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
6431 GV = GA->resolveAliasedGlobal(false);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006432
6433 TLSModel::Model model
Eric Christopher30ef0e52010-06-03 04:07:48 +00006434 = getTLSModel(GV, getTargetMachine().getRelocationModel());
Michael J. Spencerec38de22010-10-10 22:04:20 +00006435
Eric Christopher30ef0e52010-06-03 04:07:48 +00006436 switch (model) {
6437 case TLSModel::GeneralDynamic:
6438 case TLSModel::LocalDynamic: // not implemented
6439 if (Subtarget->is64Bit())
6440 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
6441 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Michael J. Spencerec38de22010-10-10 22:04:20 +00006442
Eric Christopher30ef0e52010-06-03 04:07:48 +00006443 case TLSModel::InitialExec:
6444 case TLSModel::LocalExec:
6445 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
6446 Subtarget->is64Bit());
6447 }
6448 } else if (Subtarget->isTargetDarwin()) {
6449 // Darwin only has one model of TLS. Lower to that.
6450 unsigned char OpFlag = 0;
6451 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
6452 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006453
Eric Christopher30ef0e52010-06-03 04:07:48 +00006454 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6455 // global base reg.
6456 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
6457 !Subtarget->is64Bit();
6458 if (PIC32)
6459 OpFlag = X86II::MO_TLVP_PIC_BASE;
6460 else
6461 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006462 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00006463 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00006464 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00006465 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00006466 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006467
Eric Christopher30ef0e52010-06-03 04:07:48 +00006468 // With PIC32, the address is actually $g + Offset.
6469 if (PIC32)
6470 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6471 DAG.getNode(X86ISD::GlobalBaseReg,
6472 DebugLoc(), getPointerTy()),
6473 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006474
Eric Christopher30ef0e52010-06-03 04:07:48 +00006475 // Lowering the machine isd will make sure everything is in the right
6476 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00006477 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006478 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00006479 SDValue Args[] = { Chain, Offset };
6480 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006481
Eric Christopher30ef0e52010-06-03 04:07:48 +00006482 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
6483 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
6484 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00006485
Eric Christopher30ef0e52010-06-03 04:07:48 +00006486 // And our return value (tls address) is in the standard call return value
6487 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00006488 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
6489 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy());
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006490 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00006491
Eric Christopher30ef0e52010-06-03 04:07:48 +00006492 assert(false &&
6493 "TLS not implemented for this target.");
Eric Christopherfd179292009-08-27 18:07:15 +00006494
Torok Edwinc23197a2009-07-14 16:55:14 +00006495 llvm_unreachable("Unreachable");
Chris Lattner5867de12009-04-01 22:14:45 +00006496 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006497}
6498
Evan Cheng0db9fe62006-04-25 20:13:52 +00006499
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006500/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00006501/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00006502SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman4c1fa612008-03-03 22:22:09 +00006503 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00006504 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006505 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006506 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006507 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00006508 SDValue ShOpLo = Op.getOperand(0);
6509 SDValue ShOpHi = Op.getOperand(1);
6510 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00006511 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00006512 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00006513 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00006514
Dan Gohman475871a2008-07-27 21:46:04 +00006515 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006516 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00006517 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
6518 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006519 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006520 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
6521 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006522 }
Evan Chenge3413162006-01-09 18:33:28 +00006523
Owen Anderson825b72b2009-08-11 20:47:22 +00006524 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
6525 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00006526 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00006527 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00006528
Dan Gohman475871a2008-07-27 21:46:04 +00006529 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00006530 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00006531 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
6532 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00006533
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006534 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00006535 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
6536 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006537 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006538 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
6539 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006540 }
6541
Dan Gohman475871a2008-07-27 21:46:04 +00006542 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00006543 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006544}
Evan Chenga3195e82006-01-12 22:54:21 +00006545
Dan Gohmand858e902010-04-17 15:26:15 +00006546SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
6547 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006548 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00006549
Dale Johannesen0488fb62010-09-30 23:57:10 +00006550 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00006551 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00006552
Owen Anderson825b72b2009-08-11 20:47:22 +00006553 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00006554 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00006555
Eli Friedman36df4992009-05-27 00:47:34 +00006556 // These are really Legal; return the operand so the caller accepts it as
6557 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00006558 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00006559 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00006560 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00006561 Subtarget->is64Bit()) {
6562 return Op;
6563 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006564
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006565 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006566 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006567 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00006568 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006569 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00006570 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00006571 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006572 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006573 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00006574 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
6575}
Evan Cheng0db9fe62006-04-25 20:13:52 +00006576
Owen Andersone50ed302009-08-10 22:56:29 +00006577SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00006578 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00006579 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006580 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00006581 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00006582 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00006583 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006584 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006585 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00006586 else
Owen Anderson825b72b2009-08-11 20:47:22 +00006587 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006588
Chris Lattner492a43e2010-09-22 01:28:21 +00006589 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006590
Chris Lattner492a43e2010-09-22 01:28:21 +00006591 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
6592 MachineMemOperand *MMO =
6593 DAG.getMachineFunction()
6594 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6595 MachineMemOperand::MOLoad, ByteSize, ByteSize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006596
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006597 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00006598 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
6599 X86ISD::FILD, DL,
6600 Tys, Ops, array_lengthof(Ops),
6601 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006602
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006603 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006604 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00006605 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006606
6607 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
6608 // shouldn't be necessary except that RFP cannot be live across
6609 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006610 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00006611 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
6612 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006613 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00006614 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006615 SDValue Ops[] = {
6616 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
6617 };
Chris Lattner492a43e2010-09-22 01:28:21 +00006618 MachineMemOperand *MMO =
6619 DAG.getMachineFunction()
6620 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00006621 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006622
Chris Lattner492a43e2010-09-22 01:28:21 +00006623 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
6624 Ops, array_lengthof(Ops),
6625 Op.getValueType(), MMO);
6626 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006627 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006628 false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006629 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006630
Evan Cheng0db9fe62006-04-25 20:13:52 +00006631 return Result;
6632}
6633
Bill Wendling8b8a6362009-01-17 03:56:04 +00006634// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00006635SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
6636 SelectionDAG &DAG) const {
Bill Wendling8b8a6362009-01-17 03:56:04 +00006637 // This algorithm is not obvious. Here it is in C code, more or less:
6638 /*
6639 double uint64_to_double( uint32_t hi, uint32_t lo ) {
6640 static const __m128i exp = { 0x4330000045300000ULL, 0 };
6641 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00006642
Bill Wendling8b8a6362009-01-17 03:56:04 +00006643 // Copy ints to xmm registers.
6644 __m128i xh = _mm_cvtsi32_si128( hi );
6645 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00006646
Bill Wendling8b8a6362009-01-17 03:56:04 +00006647 // Combine into low half of a single xmm register.
6648 __m128i x = _mm_unpacklo_epi32( xh, xl );
6649 __m128d d;
6650 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00006651
Bill Wendling8b8a6362009-01-17 03:56:04 +00006652 // Merge in appropriate exponents to give the integer bits the right
6653 // magnitude.
6654 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00006655
Bill Wendling8b8a6362009-01-17 03:56:04 +00006656 // Subtract away the biases to deal with the IEEE-754 double precision
6657 // implicit 1.
6658 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00006659
Bill Wendling8b8a6362009-01-17 03:56:04 +00006660 // All conversions up to here are exact. The correctly rounded result is
6661 // calculated using the current rounding mode using the following
6662 // horizontal add.
6663 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
6664 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
6665 // store doesn't really need to be here (except
6666 // maybe to zero the other double)
6667 return sd;
6668 }
6669 */
Dale Johannesen040225f2008-10-21 23:07:49 +00006670
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006671 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00006672 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00006673
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006674 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00006675 std::vector<Constant*> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00006676 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
6677 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
6678 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
6679 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00006680 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006681 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006682
Bill Wendling8b8a6362009-01-17 03:56:04 +00006683 std::vector<Constant*> CV1;
Owen Andersona90b3dc2009-07-15 21:51:10 +00006684 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006685 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
Owen Andersona90b3dc2009-07-15 21:51:10 +00006686 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006687 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00006688 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006689 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006690
Owen Anderson825b72b2009-08-11 20:47:22 +00006691 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6692 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00006693 Op.getOperand(0),
6694 DAG.getIntPtrConstant(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006695 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6696 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00006697 Op.getOperand(0),
6698 DAG.getIntPtrConstant(0)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006699 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
6700 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00006701 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006702 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00006703 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006704 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck2);
Owen Anderson825b72b2009-08-11 20:47:22 +00006705 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00006706 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006707 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00006708 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006709
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006710 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00006711 int ShufMask[2] = { 1, -1 };
Owen Anderson825b72b2009-08-11 20:47:22 +00006712 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
6713 DAG.getUNDEF(MVT::v2f64), ShufMask);
6714 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
6715 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006716 DAG.getIntPtrConstant(0));
6717}
6718
Bill Wendling8b8a6362009-01-17 03:56:04 +00006719// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00006720SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
6721 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006722 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00006723 // FP constant to bias correct the final result.
6724 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00006725 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006726
6727 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00006728 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6729 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Bill Wendling8b8a6362009-01-17 03:56:04 +00006730 Op.getOperand(0),
6731 DAG.getIntPtrConstant(0)));
6732
Owen Anderson825b72b2009-08-11 20:47:22 +00006733 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006734 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00006735 DAG.getIntPtrConstant(0));
6736
6737 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00006738 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006739 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00006740 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006741 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006742 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00006743 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006744 MVT::v2f64, Bias)));
6745 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006746 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00006747 DAG.getIntPtrConstant(0));
6748
6749 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00006750 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006751
6752 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00006753 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00006754
Owen Anderson825b72b2009-08-11 20:47:22 +00006755 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006756 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00006757 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00006758 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006759 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00006760 }
6761
6762 // Handle final rounding.
6763 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00006764}
6765
Dan Gohmand858e902010-04-17 15:26:15 +00006766SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
6767 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00006768 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006769 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00006770
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006771 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00006772 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
6773 // the optimization here.
6774 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00006775 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00006776
Owen Andersone50ed302009-08-10 22:56:29 +00006777 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006778 EVT DstVT = Op.getValueType();
6779 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00006780 return LowerUINT_TO_FP_i64(Op, DAG);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006781 else if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00006782 return LowerUINT_TO_FP_i32(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00006783
6784 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00006785 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006786 if (SrcVT == MVT::i32) {
6787 SDValue WordOff = DAG.getConstant(4, getPointerTy());
6788 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
6789 getPointerTy(), StackSlot, WordOff);
6790 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006791 StackSlot, MachinePointerInfo(),
6792 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006793 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006794 OffsetSlot, MachinePointerInfo(),
6795 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006796 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
6797 return Fild;
6798 }
6799
6800 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
6801 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006802 StackSlot, MachinePointerInfo(),
6803 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006804 // For i64 source, we need to add the appropriate power of 2 if the input
6805 // was negative. This is the same as the optimization in
6806 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
6807 // we must be careful to do the computation in x87 extended precision, not
6808 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00006809 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
6810 MachineMemOperand *MMO =
6811 DAG.getMachineFunction()
6812 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6813 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006814
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006815 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
6816 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00006817 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
6818 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006819
6820 APInt FF(32, 0x5F800000ULL);
6821
6822 // Check whether the sign bit is set.
6823 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
6824 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
6825 ISD::SETLT);
6826
6827 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
6828 SDValue FudgePtr = DAG.getConstantPool(
6829 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
6830 getPointerTy());
6831
6832 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
6833 SDValue Zero = DAG.getIntPtrConstant(0);
6834 SDValue Four = DAG.getIntPtrConstant(4);
6835 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
6836 Zero, Four);
6837 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
6838
6839 // Load the value out, extending it from f32 to f80.
6840 // FIXME: Avoid the extend by constructing the right constant pool?
Evan Chengbcc80172010-07-07 22:15:37 +00006841 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, MVT::f80, dl, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00006842 FudgePtr, MachinePointerInfo::getConstantPool(),
6843 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006844 // Extend everything to 80 bits to force it to be done on x87.
6845 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
6846 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00006847}
6848
Dan Gohman475871a2008-07-27 21:46:04 +00006849std::pair<SDValue,SDValue> X86TargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00006850FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) const {
Chris Lattner07290932010-09-22 01:05:16 +00006851 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00006852
Owen Andersone50ed302009-08-10 22:56:29 +00006853 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00006854
6855 if (!IsSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006856 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
6857 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00006858 }
6859
Owen Anderson825b72b2009-08-11 20:47:22 +00006860 assert(DstTy.getSimpleVT() <= MVT::i64 &&
6861 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00006862 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00006863
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006864 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00006865 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00006866 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00006867 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00006868 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00006869 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00006870 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00006871 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006872
Evan Cheng87c89352007-10-15 20:11:21 +00006873 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
6874 // stack slot.
6875 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00006876 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00006877 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006878 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00006879
Michael J. Spencerec38de22010-10-10 22:04:20 +00006880
6881
Evan Cheng0db9fe62006-04-25 20:13:52 +00006882 unsigned Opc;
Owen Anderson825b72b2009-08-11 20:47:22 +00006883 switch (DstTy.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00006884 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson825b72b2009-08-11 20:47:22 +00006885 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
6886 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
6887 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006888 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006889
Dan Gohman475871a2008-07-27 21:46:04 +00006890 SDValue Chain = DAG.getEntryNode();
6891 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00006892 EVT TheVT = Op.getOperand(0).getValueType();
6893 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006894 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00006895 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006896 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006897 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00006898 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00006899 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00006900 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00006901 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00006902
Chris Lattner492a43e2010-09-22 01:28:21 +00006903 MachineMemOperand *MMO =
6904 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6905 MachineMemOperand::MOLoad, MemSize, MemSize);
6906 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
6907 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006908 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00006909 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006910 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
6911 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00006912
Chris Lattner07290932010-09-22 01:05:16 +00006913 MachineMemOperand *MMO =
6914 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6915 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006916
Evan Cheng0db9fe62006-04-25 20:13:52 +00006917 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00006918 SDValue Ops[] = { Chain, Value, StackSlot };
Chris Lattner07290932010-09-22 01:05:16 +00006919 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
6920 Ops, 3, DstTy, MMO);
Evan Chengd9558e02006-01-06 00:43:03 +00006921
Chris Lattner27a6c732007-11-24 07:07:01 +00006922 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006923}
6924
Dan Gohmand858e902010-04-17 15:26:15 +00006925SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
6926 SelectionDAG &DAG) const {
Dale Johannesen0488fb62010-09-30 23:57:10 +00006927 if (Op.getValueType().isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00006928 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00006929
Eli Friedman948e95a2009-05-23 09:59:16 +00006930 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00006931 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00006932 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
6933 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00006934
Chris Lattner27a6c732007-11-24 07:07:01 +00006935 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006936 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00006937 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00006938}
6939
Dan Gohmand858e902010-04-17 15:26:15 +00006940SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
6941 SelectionDAG &DAG) const {
Eli Friedman948e95a2009-05-23 09:59:16 +00006942 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
6943 SDValue FIST = Vals.first, StackSlot = Vals.second;
6944 assert(FIST.getNode() && "Unexpected failure");
6945
6946 // Load the result.
6947 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00006948 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00006949}
6950
Dan Gohmand858e902010-04-17 15:26:15 +00006951SDValue X86TargetLowering::LowerFABS(SDValue Op,
6952 SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00006953 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006954 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00006955 EVT VT = Op.getValueType();
6956 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006957 if (VT.isVector())
6958 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006959 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00006960 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006961 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00006962 CV.push_back(C);
6963 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006964 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006965 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00006966 CV.push_back(C);
6967 CV.push_back(C);
6968 CV.push_back(C);
6969 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006970 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00006971 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006972 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006973 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00006974 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006975 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006976 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006977}
6978
Dan Gohmand858e902010-04-17 15:26:15 +00006979SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00006980 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006981 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00006982 EVT VT = Op.getValueType();
6983 EVT EltVT = VT;
Duncan Sandsda9ad382009-09-06 19:29:07 +00006984 if (VT.isVector())
Duncan Sands83ec4b62008-06-06 12:08:01 +00006985 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006986 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00006987 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006988 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00006989 CV.push_back(C);
6990 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006991 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006992 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00006993 CV.push_back(C);
6994 CV.push_back(C);
6995 CV.push_back(C);
6996 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006997 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00006998 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006999 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007000 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007001 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007002 false, false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00007003 if (VT.isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007004 return DAG.getNode(ISD::BITCAST, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007005 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007006 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007007 Op.getOperand(0)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007008 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00007009 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007010 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00007011 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00007012}
7013
Dan Gohmand858e902010-04-17 15:26:15 +00007014SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007015 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00007016 SDValue Op0 = Op.getOperand(0);
7017 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007018 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007019 EVT VT = Op.getValueType();
7020 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00007021
7022 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007023 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007024 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00007025 SrcVT = VT;
7026 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007027 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007028 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007029 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007030 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007031 }
7032
7033 // At this point the operands and the result should have the same
7034 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00007035
Evan Cheng68c47cb2007-01-05 07:55:56 +00007036 // First get the sign bit of second operand.
7037 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007038 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007039 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
7040 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007041 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007042 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
7043 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7044 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7045 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007046 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007047 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007048 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007049 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007050 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007051 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007052 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00007053
7054 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007055 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007056 // Op0 is MVT::f32, Op1 is MVT::f64.
7057 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
7058 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
7059 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007060 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00007061 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00007062 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007063 }
7064
Evan Cheng73d6cf12007-01-05 21:37:56 +00007065 // Clear first operand sign bit.
7066 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00007067 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007068 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
7069 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00007070 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007071 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
7072 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7073 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7074 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00007075 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007076 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007077 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007078 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007079 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007080 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007081 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00007082
7083 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00007084 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00007085}
7086
Dan Gohman076aee32009-03-04 19:44:21 +00007087/// Emit nodes that will be selected as "test Op0,Op0", or something
7088/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00007089SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00007090 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00007091 DebugLoc dl = Op.getDebugLoc();
7092
Dan Gohman31125812009-03-07 01:58:32 +00007093 // CF and OF aren't always set the way we want. Determine which
7094 // of these we need.
7095 bool NeedCF = false;
7096 bool NeedOF = false;
7097 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007098 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00007099 case X86::COND_A: case X86::COND_AE:
7100 case X86::COND_B: case X86::COND_BE:
7101 NeedCF = true;
7102 break;
7103 case X86::COND_G: case X86::COND_GE:
7104 case X86::COND_L: case X86::COND_LE:
7105 case X86::COND_O: case X86::COND_NO:
7106 NeedOF = true;
7107 break;
Dan Gohman31125812009-03-07 01:58:32 +00007108 }
7109
Dan Gohman076aee32009-03-04 19:44:21 +00007110 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00007111 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
7112 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007113 if (Op.getResNo() != 0 || NeedOF || NeedCF)
7114 // Emit a CMP with 0, which is the TEST pattern.
7115 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
7116 DAG.getConstant(0, Op.getValueType()));
7117
7118 unsigned Opcode = 0;
7119 unsigned NumOperands = 0;
7120 switch (Op.getNode()->getOpcode()) {
7121 case ISD::ADD:
7122 // Due to an isel shortcoming, be conservative if this add is likely to be
7123 // selected as part of a load-modify-store instruction. When the root node
7124 // in a match is a store, isel doesn't know how to remap non-chain non-flag
7125 // uses of other nodes in the match, such as the ADD in this case. This
7126 // leads to the ADD being left around and reselected, with the result being
7127 // two adds in the output. Alas, even if none our users are stores, that
7128 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
7129 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
7130 // climbing the DAG back to the root, and it doesn't seem to be worth the
7131 // effort.
7132 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Dan Gohman076aee32009-03-04 19:44:21 +00007133 UE = Op.getNode()->use_end(); UI != UE; ++UI)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007134 if (UI->getOpcode() != ISD::CopyToReg && UI->getOpcode() != ISD::SETCC)
7135 goto default_case;
7136
7137 if (ConstantSDNode *C =
7138 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
7139 // An add of one will be selected as an INC.
7140 if (C->getAPIntValue() == 1) {
7141 Opcode = X86ISD::INC;
7142 NumOperands = 1;
7143 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00007144 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007145
7146 // An add of negative one (subtract of one) will be selected as a DEC.
7147 if (C->getAPIntValue().isAllOnesValue()) {
7148 Opcode = X86ISD::DEC;
7149 NumOperands = 1;
7150 break;
7151 }
Dan Gohman076aee32009-03-04 19:44:21 +00007152 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007153
7154 // Otherwise use a regular EFLAGS-setting add.
7155 Opcode = X86ISD::ADD;
7156 NumOperands = 2;
7157 break;
7158 case ISD::AND: {
7159 // If the primary and result isn't used, don't bother using X86ISD::AND,
7160 // because a TEST instruction will be better.
7161 bool NonFlagUse = false;
7162 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
7163 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
7164 SDNode *User = *UI;
7165 unsigned UOpNo = UI.getOperandNo();
7166 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
7167 // Look pass truncate.
7168 UOpNo = User->use_begin().getOperandNo();
7169 User = *User->use_begin();
7170 }
7171
7172 if (User->getOpcode() != ISD::BRCOND &&
7173 User->getOpcode() != ISD::SETCC &&
7174 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
7175 NonFlagUse = true;
7176 break;
7177 }
Dan Gohman076aee32009-03-04 19:44:21 +00007178 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007179
7180 if (!NonFlagUse)
7181 break;
7182 }
7183 // FALL THROUGH
7184 case ISD::SUB:
7185 case ISD::OR:
7186 case ISD::XOR:
7187 // Due to the ISEL shortcoming noted above, be conservative if this op is
7188 // likely to be selected as part of a load-modify-store instruction.
7189 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
7190 UE = Op.getNode()->use_end(); UI != UE; ++UI)
7191 if (UI->getOpcode() == ISD::STORE)
7192 goto default_case;
7193
7194 // Otherwise use a regular EFLAGS-setting instruction.
7195 switch (Op.getNode()->getOpcode()) {
7196 default: llvm_unreachable("unexpected operator!");
7197 case ISD::SUB: Opcode = X86ISD::SUB; break;
7198 case ISD::OR: Opcode = X86ISD::OR; break;
7199 case ISD::XOR: Opcode = X86ISD::XOR; break;
7200 case ISD::AND: Opcode = X86ISD::AND; break;
7201 }
7202
7203 NumOperands = 2;
7204 break;
7205 case X86ISD::ADD:
7206 case X86ISD::SUB:
7207 case X86ISD::INC:
7208 case X86ISD::DEC:
7209 case X86ISD::OR:
7210 case X86ISD::XOR:
7211 case X86ISD::AND:
7212 return SDValue(Op.getNode(), 1);
7213 default:
7214 default_case:
7215 break;
Dan Gohman076aee32009-03-04 19:44:21 +00007216 }
7217
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007218 if (Opcode == 0)
7219 // Emit a CMP with 0, which is the TEST pattern.
7220 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
7221 DAG.getConstant(0, Op.getValueType()));
7222
7223 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
7224 SmallVector<SDValue, 4> Ops;
7225 for (unsigned i = 0; i != NumOperands; ++i)
7226 Ops.push_back(Op.getOperand(i));
7227
7228 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
7229 DAG.ReplaceAllUsesWith(Op, New);
7230 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00007231}
7232
7233/// Emit nodes that will be selected as "cmp Op0,Op1", or something
7234/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00007235SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00007236 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00007237 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
7238 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00007239 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00007240
7241 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00007242 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00007243}
7244
Evan Chengd40d03e2010-01-06 19:38:29 +00007245/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
7246/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00007247SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
7248 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00007249 SDValue Op0 = And.getOperand(0);
7250 SDValue Op1 = And.getOperand(1);
7251 if (Op0.getOpcode() == ISD::TRUNCATE)
7252 Op0 = Op0.getOperand(0);
7253 if (Op1.getOpcode() == ISD::TRUNCATE)
7254 Op1 = Op1.getOperand(0);
7255
Evan Chengd40d03e2010-01-06 19:38:29 +00007256 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00007257 if (Op1.getOpcode() == ISD::SHL)
7258 std::swap(Op0, Op1);
7259 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00007260 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
7261 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00007262 // If we looked past a truncate, check that it's only truncating away
7263 // known zeros.
7264 unsigned BitWidth = Op0.getValueSizeInBits();
7265 unsigned AndBitWidth = And.getValueSizeInBits();
7266 if (BitWidth > AndBitWidth) {
7267 APInt Mask = APInt::getAllOnesValue(BitWidth), Zeros, Ones;
7268 DAG.ComputeMaskedBits(Op0, Mask, Zeros, Ones);
7269 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
7270 return SDValue();
7271 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007272 LHS = Op1;
7273 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00007274 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007275 } else if (Op1.getOpcode() == ISD::Constant) {
7276 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
7277 SDValue AndLHS = Op0;
Evan Chengd40d03e2010-01-06 19:38:29 +00007278 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
7279 LHS = AndLHS.getOperand(0);
7280 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00007281 }
Evan Chengd40d03e2010-01-06 19:38:29 +00007282 }
Evan Cheng0488db92007-09-25 01:57:46 +00007283
Evan Chengd40d03e2010-01-06 19:38:29 +00007284 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00007285 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00007286 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00007287 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00007288 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00007289 // Also promote i16 to i32 for performance / code size reason.
7290 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00007291 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00007292 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00007293
Evan Chengd40d03e2010-01-06 19:38:29 +00007294 // If the operand types disagree, extend the shift amount to match. Since
7295 // BT ignores high bits (like shifts) we can use anyextend.
7296 if (LHS.getValueType() != RHS.getValueType())
7297 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00007298
Evan Chengd40d03e2010-01-06 19:38:29 +00007299 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
7300 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
7301 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7302 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00007303 }
7304
Evan Cheng54de3ea2010-01-05 06:52:31 +00007305 return SDValue();
7306}
7307
Dan Gohmand858e902010-04-17 15:26:15 +00007308SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng54de3ea2010-01-05 06:52:31 +00007309 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
7310 SDValue Op0 = Op.getOperand(0);
7311 SDValue Op1 = Op.getOperand(1);
7312 DebugLoc dl = Op.getDebugLoc();
7313 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
7314
7315 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00007316 // Lower (X & (1 << N)) == 0 to BT(X, N).
7317 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
7318 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Chris Lattner481eebc2010-12-19 21:23:48 +00007319 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00007320 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00007321 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00007322 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
7323 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
7324 if (NewSetCC.getNode())
7325 return NewSetCC;
7326 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00007327
Chris Lattner481eebc2010-12-19 21:23:48 +00007328 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
7329 // these.
7330 if (Op1.getOpcode() == ISD::Constant &&
Evan Cheng2c755ba2010-02-27 07:36:59 +00007331 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
7332 cast<ConstantSDNode>(Op1)->isNullValue()) &&
7333 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007334
Chris Lattner481eebc2010-12-19 21:23:48 +00007335 // If the input is a setcc, then reuse the input setcc or use a new one with
7336 // the inverted condition.
7337 if (Op0.getOpcode() == X86ISD::SETCC) {
7338 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
7339 bool Invert = (CC == ISD::SETNE) ^
7340 cast<ConstantSDNode>(Op1)->isNullValue();
7341 if (!Invert) return Op0;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007342
Evan Cheng2c755ba2010-02-27 07:36:59 +00007343 CCode = X86::GetOppositeBranchCondition(CCode);
Chris Lattner481eebc2010-12-19 21:23:48 +00007344 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7345 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
7346 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007347 }
7348
Evan Chenge5b51ac2010-04-17 06:13:15 +00007349 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00007350 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00007351 if (X86CC == X86::COND_INVALID)
7352 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00007353
Chris Lattnerc19d1c32010-12-19 22:08:31 +00007354 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00007355 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnerc19d1c32010-12-19 22:08:31 +00007356 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
Evan Cheng0488db92007-09-25 01:57:46 +00007357}
7358
Dan Gohmand858e902010-04-17 15:26:15 +00007359SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00007360 SDValue Cond;
7361 SDValue Op0 = Op.getOperand(0);
7362 SDValue Op1 = Op.getOperand(1);
7363 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00007364 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00007365 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
7366 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007367 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00007368
7369 if (isFP) {
7370 unsigned SSECC = 8;
Owen Andersone50ed302009-08-10 22:56:29 +00007371 EVT VT0 = Op0.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00007372 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
7373 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00007374 bool Swap = false;
7375
7376 switch (SetCCOpcode) {
7377 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00007378 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00007379 case ISD::SETEQ: SSECC = 0; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00007380 case ISD::SETOGT:
Nate Begeman30a0de92008-07-17 16:51:19 +00007381 case ISD::SETGT: Swap = true; // Fallthrough
7382 case ISD::SETLT:
7383 case ISD::SETOLT: SSECC = 1; break;
7384 case ISD::SETOGE:
7385 case ISD::SETGE: Swap = true; // Fallthrough
7386 case ISD::SETLE:
7387 case ISD::SETOLE: SSECC = 2; break;
7388 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00007389 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00007390 case ISD::SETNE: SSECC = 4; break;
7391 case ISD::SETULE: Swap = true;
7392 case ISD::SETUGE: SSECC = 5; break;
7393 case ISD::SETULT: Swap = true;
7394 case ISD::SETUGT: SSECC = 6; break;
7395 case ISD::SETO: SSECC = 7; break;
7396 }
7397 if (Swap)
7398 std::swap(Op0, Op1);
7399
Nate Begemanfb8ead02008-07-25 19:05:58 +00007400 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00007401 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00007402 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00007403 SDValue UNORD, EQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00007404 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
7405 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00007406 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00007407 }
7408 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00007409 SDValue ORD, NEQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00007410 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
7411 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00007412 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00007413 }
Torok Edwinc23197a2009-07-14 16:55:14 +00007414 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00007415 }
7416 // Handle all other FP comparisons here.
Owen Anderson825b72b2009-08-11 20:47:22 +00007417 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00007418 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007419
Nate Begeman30a0de92008-07-17 16:51:19 +00007420 // We are handling one of the integer comparisons here. Since SSE only has
7421 // GT and EQ comparisons for integer, swapping operands and multiple
7422 // operations may be required for some comparisons.
7423 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
7424 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007425
Owen Anderson825b72b2009-08-11 20:47:22 +00007426 switch (VT.getSimpleVT().SimpleTy) {
Nate Begeman30a0de92008-07-17 16:51:19 +00007427 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007428 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007429 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007430 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
7431 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00007432 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007433
Nate Begeman30a0de92008-07-17 16:51:19 +00007434 switch (SetCCOpcode) {
7435 default: break;
7436 case ISD::SETNE: Invert = true;
7437 case ISD::SETEQ: Opc = EQOpc; break;
7438 case ISD::SETLT: Swap = true;
7439 case ISD::SETGT: Opc = GTOpc; break;
7440 case ISD::SETGE: Swap = true;
7441 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
7442 case ISD::SETULT: Swap = true;
7443 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
7444 case ISD::SETUGE: Swap = true;
7445 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
7446 }
7447 if (Swap)
7448 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007449
Nate Begeman30a0de92008-07-17 16:51:19 +00007450 // Since SSE has no unsigned integer comparisons, we need to flip the sign
7451 // bits of the inputs before performing those operations.
7452 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00007453 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00007454 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
7455 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00007456 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00007457 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
7458 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00007459 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
7460 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00007461 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007462
Dale Johannesenace16102009-02-03 19:33:06 +00007463 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00007464
7465 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00007466 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00007467 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00007468
Nate Begeman30a0de92008-07-17 16:51:19 +00007469 return Result;
7470}
Evan Cheng0488db92007-09-25 01:57:46 +00007471
Evan Cheng370e5342008-12-03 08:38:43 +00007472// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00007473static bool isX86LogicalCmp(SDValue Op) {
7474 unsigned Opc = Op.getNode()->getOpcode();
7475 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
7476 return true;
7477 if (Op.getResNo() == 1 &&
7478 (Opc == X86ISD::ADD ||
7479 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00007480 Opc == X86ISD::ADC ||
7481 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00007482 Opc == X86ISD::SMUL ||
7483 Opc == X86ISD::UMUL ||
7484 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00007485 Opc == X86ISD::DEC ||
7486 Opc == X86ISD::OR ||
7487 Opc == X86ISD::XOR ||
7488 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00007489 return true;
7490
Chris Lattner9637d5b2010-12-05 07:49:54 +00007491 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
7492 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007493
Dan Gohman076aee32009-03-04 19:44:21 +00007494 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00007495}
7496
Chris Lattnera2b56002010-12-05 01:23:24 +00007497static bool isZero(SDValue V) {
7498 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
7499 return C && C->isNullValue();
7500}
7501
Chris Lattner96908b12010-12-05 02:00:51 +00007502static bool isAllOnes(SDValue V) {
7503 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
7504 return C && C->isAllOnesValue();
7505}
7506
Dan Gohmand858e902010-04-17 15:26:15 +00007507SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00007508 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007509 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00007510 SDValue Op1 = Op.getOperand(1);
7511 SDValue Op2 = Op.getOperand(2);
7512 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00007513 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00007514
Dan Gohman1a492952009-10-20 16:22:37 +00007515 if (Cond.getOpcode() == ISD::SETCC) {
7516 SDValue NewCond = LowerSETCC(Cond, DAG);
7517 if (NewCond.getNode())
7518 Cond = NewCond;
7519 }
Evan Cheng734503b2006-09-11 02:19:56 +00007520
Chris Lattnera2b56002010-12-05 01:23:24 +00007521 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00007522 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00007523 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00007524 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007525 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00007526 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
7527 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007528 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007529
Chris Lattnera2b56002010-12-05 01:23:24 +00007530 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007531
7532 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00007533 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
7534 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00007535
7536 SDValue CmpOp0 = Cmp.getOperand(0);
7537 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
7538 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007539
Chris Lattner96908b12010-12-05 02:00:51 +00007540 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00007541 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
7542 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007543
Chris Lattner96908b12010-12-05 02:00:51 +00007544 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
7545 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007546
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007547 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00007548 if (N2C == 0 || !N2C->isNullValue())
7549 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
7550 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007551 }
7552 }
7553
Chris Lattnera2b56002010-12-05 01:23:24 +00007554 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00007555 if (Cond.getOpcode() == ISD::AND &&
7556 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
7557 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00007558 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00007559 Cond = Cond.getOperand(0);
7560 }
7561
Evan Cheng3f41d662007-10-08 22:16:29 +00007562 // If condition flag is set by a X86ISD::CMP, then use it as the condition
7563 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00007564 if (Cond.getOpcode() == X86ISD::SETCC ||
7565 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00007566 CC = Cond.getOperand(0);
7567
Dan Gohman475871a2008-07-27 21:46:04 +00007568 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00007569 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00007570 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00007571
Evan Cheng3f41d662007-10-08 22:16:29 +00007572 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007573 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00007574 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00007575 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00007576
Chris Lattnerd1980a52009-03-12 06:52:53 +00007577 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
7578 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00007579 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00007580 addTest = false;
7581 }
7582 }
7583
7584 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007585 // Look pass the truncate.
7586 if (Cond.getOpcode() == ISD::TRUNCATE)
7587 Cond = Cond.getOperand(0);
7588
7589 // We know the result of AND is compared against zero. Try to match
7590 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00007591 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00007592 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00007593 if (NewSetCC.getNode()) {
7594 CC = NewSetCC.getOperand(0);
7595 Cond = NewSetCC.getOperand(1);
7596 addTest = false;
7597 }
7598 }
7599 }
7600
7601 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007602 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00007603 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00007604 }
7605
Benjamin Kramere915ff32010-12-22 23:09:28 +00007606 // a < b ? -1 : 0 -> RES = ~setcc_carry
7607 // a < b ? 0 : -1 -> RES = setcc_carry
7608 // a >= b ? -1 : 0 -> RES = setcc_carry
7609 // a >= b ? 0 : -1 -> RES = ~setcc_carry
7610 if (Cond.getOpcode() == X86ISD::CMP) {
7611 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
7612
7613 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
7614 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
7615 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
7616 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
7617 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
7618 return DAG.getNOT(DL, Res, Res.getValueType());
7619 return Res;
7620 }
7621 }
7622
Evan Cheng0488db92007-09-25 01:57:46 +00007623 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
7624 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007625 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007626 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00007627 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00007628}
7629
Evan Cheng370e5342008-12-03 08:38:43 +00007630// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
7631// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
7632// from the AND / OR.
7633static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
7634 Opc = Op.getOpcode();
7635 if (Opc != ISD::OR && Opc != ISD::AND)
7636 return false;
7637 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
7638 Op.getOperand(0).hasOneUse() &&
7639 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
7640 Op.getOperand(1).hasOneUse());
7641}
7642
Evan Cheng961d6d42009-02-02 08:19:07 +00007643// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
7644// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00007645static bool isXor1OfSetCC(SDValue Op) {
7646 if (Op.getOpcode() != ISD::XOR)
7647 return false;
7648 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
7649 if (N1C && N1C->getAPIntValue() == 1) {
7650 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
7651 Op.getOperand(0).hasOneUse();
7652 }
7653 return false;
7654}
7655
Dan Gohmand858e902010-04-17 15:26:15 +00007656SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00007657 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007658 SDValue Chain = Op.getOperand(0);
7659 SDValue Cond = Op.getOperand(1);
7660 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007661 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00007662 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00007663
Dan Gohman1a492952009-10-20 16:22:37 +00007664 if (Cond.getOpcode() == ISD::SETCC) {
7665 SDValue NewCond = LowerSETCC(Cond, DAG);
7666 if (NewCond.getNode())
7667 Cond = NewCond;
7668 }
Chris Lattnere55484e2008-12-25 05:34:37 +00007669#if 0
7670 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00007671 else if (Cond.getOpcode() == X86ISD::ADD ||
7672 Cond.getOpcode() == X86ISD::SUB ||
7673 Cond.getOpcode() == X86ISD::SMUL ||
7674 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00007675 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00007676#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00007677
Evan Chengad9c0a32009-12-15 00:53:42 +00007678 // Look pass (and (setcc_carry (cmp ...)), 1).
7679 if (Cond.getOpcode() == ISD::AND &&
7680 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
7681 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00007682 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00007683 Cond = Cond.getOperand(0);
7684 }
7685
Evan Cheng3f41d662007-10-08 22:16:29 +00007686 // If condition flag is set by a X86ISD::CMP, then use it as the condition
7687 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00007688 if (Cond.getOpcode() == X86ISD::SETCC ||
7689 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00007690 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007691
Dan Gohman475871a2008-07-27 21:46:04 +00007692 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00007693 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00007694 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00007695 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00007696 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00007697 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00007698 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00007699 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00007700 default: break;
7701 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00007702 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00007703 // These can only come from an arithmetic instruction with overflow,
7704 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00007705 Cond = Cond.getNode()->getOperand(1);
7706 addTest = false;
7707 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00007708 }
Evan Cheng0488db92007-09-25 01:57:46 +00007709 }
Evan Cheng370e5342008-12-03 08:38:43 +00007710 } else {
7711 unsigned CondOpc;
7712 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
7713 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00007714 if (CondOpc == ISD::OR) {
7715 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
7716 // two branches instead of an explicit OR instruction with a
7717 // separate test.
7718 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00007719 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00007720 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007721 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00007722 Chain, Dest, CC, Cmp);
7723 CC = Cond.getOperand(1).getOperand(0);
7724 Cond = Cmp;
7725 addTest = false;
7726 }
7727 } else { // ISD::AND
7728 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
7729 // two branches instead of an explicit AND instruction with a
7730 // separate test. However, we only do this if this block doesn't
7731 // have a fall-through edge, because this requires an explicit
7732 // jmp when the condition is false.
7733 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00007734 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00007735 Op.getNode()->hasOneUse()) {
7736 X86::CondCode CCode =
7737 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
7738 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007739 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00007740 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00007741 // Look for an unconditional branch following this conditional branch.
7742 // We need this because we need to reverse the successors in order
7743 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00007744 if (User->getOpcode() == ISD::BR) {
7745 SDValue FalseBB = User->getOperand(1);
7746 SDNode *NewBR =
7747 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00007748 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00007749 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00007750 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00007751
Dale Johannesene4d209d2009-02-03 20:21:25 +00007752 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00007753 Chain, Dest, CC, Cmp);
7754 X86::CondCode CCode =
7755 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
7756 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007757 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00007758 Cond = Cmp;
7759 addTest = false;
7760 }
7761 }
Dan Gohman279c22e2008-10-21 03:29:32 +00007762 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00007763 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
7764 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
7765 // It should be transformed during dag combiner except when the condition
7766 // is set by a arithmetics with overflow node.
7767 X86::CondCode CCode =
7768 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
7769 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007770 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00007771 Cond = Cond.getOperand(0).getOperand(1);
7772 addTest = false;
Dan Gohman279c22e2008-10-21 03:29:32 +00007773 }
Evan Cheng0488db92007-09-25 01:57:46 +00007774 }
7775
7776 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007777 // Look pass the truncate.
7778 if (Cond.getOpcode() == ISD::TRUNCATE)
7779 Cond = Cond.getOperand(0);
7780
7781 // We know the result of AND is compared against zero. Try to match
7782 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00007783 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007784 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
7785 if (NewSetCC.getNode()) {
7786 CC = NewSetCC.getOperand(0);
7787 Cond = NewSetCC.getOperand(1);
7788 addTest = false;
7789 }
7790 }
7791 }
7792
7793 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007794 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00007795 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00007796 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00007797 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00007798 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00007799}
7800
Anton Korobeynikove060b532007-04-17 19:34:00 +00007801
7802// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
7803// Calls to _alloca is needed to probe the stack when allocating more than 4k
7804// bytes in one go. Touching the stack at 4K increments is necessary to ensure
7805// that the guard pages used by the OS virtual memory manager are allocated in
7806// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00007807SDValue
7808X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00007809 SelectionDAG &DAG) const {
Duncan Sands1e1ca0b2010-10-21 16:02:12 +00007810 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows()) &&
Michael J. Spencere9c253e2010-10-21 01:41:01 +00007811 "This should be used only on Windows targets");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007812 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007813
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007814 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00007815 SDValue Chain = Op.getOperand(0);
7816 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007817 // FIXME: Ensure alignment here
7818
Dan Gohman475871a2008-07-27 21:46:04 +00007819 SDValue Flag;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007820
Owen Anderson825b72b2009-08-11 20:47:22 +00007821 EVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007822
Dale Johannesendd64c412009-02-04 00:33:20 +00007823 Chain = DAG.getCopyToReg(Chain, dl, X86::EAX, Size, Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00007824 Flag = Chain.getValue(1);
7825
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007826 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00007827
Michael J. Spencere9c253e2010-10-21 01:41:01 +00007828 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00007829 Flag = Chain.getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007830
Dale Johannesendd64c412009-02-04 00:33:20 +00007831 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007832
Dan Gohman475871a2008-07-27 21:46:04 +00007833 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007834 return DAG.getMergeValues(Ops1, 2, dl);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007835}
7836
Dan Gohmand858e902010-04-17 15:26:15 +00007837SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00007838 MachineFunction &MF = DAG.getMachineFunction();
7839 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
7840
Dan Gohman69de1932008-02-06 22:27:42 +00007841 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00007842 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00007843
Anton Korobeynikove7beda12010-10-03 22:52:07 +00007844 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00007845 // vastart just stores the address of the VarArgsFrameIndex slot into the
7846 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00007847 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
7848 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00007849 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
7850 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007851 }
7852
7853 // __va_list_tag:
7854 // gp_offset (0 - 6 * 8)
7855 // fp_offset (48 - 48 + 8 * 16)
7856 // overflow_arg_area (point to parameters coming in memory).
7857 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00007858 SmallVector<SDValue, 8> MemOps;
7859 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00007860 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00007861 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00007862 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
7863 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007864 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007865 MemOps.push_back(Store);
7866
7867 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00007868 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007869 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +00007870 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00007871 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
7872 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007873 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007874 MemOps.push_back(Store);
7875
7876 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +00007877 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007878 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00007879 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
7880 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00007881 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
7882 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +00007883 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007884 MemOps.push_back(Store);
7885
7886 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +00007887 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007888 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00007889 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
7890 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00007891 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
7892 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007893 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +00007894 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00007895 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00007896}
7897
Dan Gohmand858e902010-04-17 15:26:15 +00007898SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +00007899 assert(Subtarget->is64Bit() &&
7900 "LowerVAARG only handles 64-bit va_arg!");
7901 assert((Subtarget->isTargetLinux() ||
7902 Subtarget->isTargetDarwin()) &&
7903 "Unhandled target in LowerVAARG");
7904 assert(Op.getNode()->getNumOperands() == 4);
7905 SDValue Chain = Op.getOperand(0);
7906 SDValue SrcPtr = Op.getOperand(1);
7907 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
7908 unsigned Align = Op.getConstantOperandVal(3);
7909 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +00007910
Dan Gohman320afb82010-10-12 18:00:49 +00007911 EVT ArgVT = Op.getNode()->getValueType(0);
7912 const Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
7913 uint32_t ArgSize = getTargetData()->getTypeAllocSize(ArgTy);
7914 uint8_t ArgMode;
7915
7916 // Decide which area this value should be read from.
7917 // TODO: Implement the AMD64 ABI in its entirety. This simple
7918 // selection mechanism works only for the basic types.
7919 if (ArgVT == MVT::f80) {
7920 llvm_unreachable("va_arg for f80 not yet implemented");
7921 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
7922 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
7923 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
7924 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
7925 } else {
7926 llvm_unreachable("Unhandled argument type in LowerVAARG");
7927 }
7928
7929 if (ArgMode == 2) {
7930 // Sanity Check: Make sure using fp_offset makes sense.
Michael J. Spencer87b86652010-10-19 07:32:42 +00007931 assert(!UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +00007932 !(DAG.getMachineFunction()
7933 .getFunction()->hasFnAttr(Attribute::NoImplicitFloat)) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00007934 Subtarget->hasXMM());
Dan Gohman320afb82010-10-12 18:00:49 +00007935 }
7936
7937 // Insert VAARG_64 node into the DAG
7938 // VAARG_64 returns two values: Variable Argument Address, Chain
7939 SmallVector<SDValue, 11> InstOps;
7940 InstOps.push_back(Chain);
7941 InstOps.push_back(SrcPtr);
7942 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
7943 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
7944 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
7945 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
7946 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
7947 VTs, &InstOps[0], InstOps.size(),
7948 MVT::i64,
7949 MachinePointerInfo(SV),
7950 /*Align=*/0,
7951 /*Volatile=*/false,
7952 /*ReadMem=*/true,
7953 /*WriteMem=*/true);
7954 Chain = VAARG.getValue(1);
7955
7956 // Load the next argument and return it
7957 return DAG.getLoad(ArgVT, dl,
7958 Chain,
7959 VAARG,
7960 MachinePointerInfo(),
7961 false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +00007962}
7963
Dan Gohmand858e902010-04-17 15:26:15 +00007964SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) const {
Evan Chengae642192007-03-02 23:16:35 +00007965 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00007966 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00007967 SDValue Chain = Op.getOperand(0);
7968 SDValue DstPtr = Op.getOperand(1);
7969 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00007970 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
7971 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +00007972 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00007973
Chris Lattnere72f2022010-09-21 05:40:29 +00007974 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00007975 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +00007976 false,
Chris Lattnere72f2022010-09-21 05:40:29 +00007977 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +00007978}
7979
Dan Gohman475871a2008-07-27 21:46:04 +00007980SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007981X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007982 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007983 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007984 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00007985 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00007986 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00007987 case Intrinsic::x86_sse_comieq_ss:
7988 case Intrinsic::x86_sse_comilt_ss:
7989 case Intrinsic::x86_sse_comile_ss:
7990 case Intrinsic::x86_sse_comigt_ss:
7991 case Intrinsic::x86_sse_comige_ss:
7992 case Intrinsic::x86_sse_comineq_ss:
7993 case Intrinsic::x86_sse_ucomieq_ss:
7994 case Intrinsic::x86_sse_ucomilt_ss:
7995 case Intrinsic::x86_sse_ucomile_ss:
7996 case Intrinsic::x86_sse_ucomigt_ss:
7997 case Intrinsic::x86_sse_ucomige_ss:
7998 case Intrinsic::x86_sse_ucomineq_ss:
7999 case Intrinsic::x86_sse2_comieq_sd:
8000 case Intrinsic::x86_sse2_comilt_sd:
8001 case Intrinsic::x86_sse2_comile_sd:
8002 case Intrinsic::x86_sse2_comigt_sd:
8003 case Intrinsic::x86_sse2_comige_sd:
8004 case Intrinsic::x86_sse2_comineq_sd:
8005 case Intrinsic::x86_sse2_ucomieq_sd:
8006 case Intrinsic::x86_sse2_ucomilt_sd:
8007 case Intrinsic::x86_sse2_ucomile_sd:
8008 case Intrinsic::x86_sse2_ucomigt_sd:
8009 case Intrinsic::x86_sse2_ucomige_sd:
8010 case Intrinsic::x86_sse2_ucomineq_sd: {
8011 unsigned Opc = 0;
8012 ISD::CondCode CC = ISD::SETCC_INVALID;
8013 switch (IntNo) {
8014 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008015 case Intrinsic::x86_sse_comieq_ss:
8016 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008017 Opc = X86ISD::COMI;
8018 CC = ISD::SETEQ;
8019 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00008020 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008021 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008022 Opc = X86ISD::COMI;
8023 CC = ISD::SETLT;
8024 break;
8025 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008026 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008027 Opc = X86ISD::COMI;
8028 CC = ISD::SETLE;
8029 break;
8030 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008031 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008032 Opc = X86ISD::COMI;
8033 CC = ISD::SETGT;
8034 break;
8035 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008036 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008037 Opc = X86ISD::COMI;
8038 CC = ISD::SETGE;
8039 break;
8040 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008041 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008042 Opc = X86ISD::COMI;
8043 CC = ISD::SETNE;
8044 break;
8045 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008046 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008047 Opc = X86ISD::UCOMI;
8048 CC = ISD::SETEQ;
8049 break;
8050 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008051 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008052 Opc = X86ISD::UCOMI;
8053 CC = ISD::SETLT;
8054 break;
8055 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008056 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008057 Opc = X86ISD::UCOMI;
8058 CC = ISD::SETLE;
8059 break;
8060 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008061 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008062 Opc = X86ISD::UCOMI;
8063 CC = ISD::SETGT;
8064 break;
8065 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008066 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008067 Opc = X86ISD::UCOMI;
8068 CC = ISD::SETGE;
8069 break;
8070 case Intrinsic::x86_sse_ucomineq_ss:
8071 case Intrinsic::x86_sse2_ucomineq_sd:
8072 Opc = X86ISD::UCOMI;
8073 CC = ISD::SETNE;
8074 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00008075 }
Evan Cheng734503b2006-09-11 02:19:56 +00008076
Dan Gohman475871a2008-07-27 21:46:04 +00008077 SDValue LHS = Op.getOperand(1);
8078 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00008079 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00008080 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00008081 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
8082 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8083 DAG.getConstant(X86CC, MVT::i8), Cond);
8084 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00008085 }
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008086 // ptest and testp intrinsics. The intrinsic these come from are designed to
8087 // return an integer value, not just an instruction so lower it to the ptest
8088 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00008089 case Intrinsic::x86_sse41_ptestz:
8090 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008091 case Intrinsic::x86_sse41_ptestnzc:
8092 case Intrinsic::x86_avx_ptestz_256:
8093 case Intrinsic::x86_avx_ptestc_256:
8094 case Intrinsic::x86_avx_ptestnzc_256:
8095 case Intrinsic::x86_avx_vtestz_ps:
8096 case Intrinsic::x86_avx_vtestc_ps:
8097 case Intrinsic::x86_avx_vtestnzc_ps:
8098 case Intrinsic::x86_avx_vtestz_pd:
8099 case Intrinsic::x86_avx_vtestc_pd:
8100 case Intrinsic::x86_avx_vtestnzc_pd:
8101 case Intrinsic::x86_avx_vtestz_ps_256:
8102 case Intrinsic::x86_avx_vtestc_ps_256:
8103 case Intrinsic::x86_avx_vtestnzc_ps_256:
8104 case Intrinsic::x86_avx_vtestz_pd_256:
8105 case Intrinsic::x86_avx_vtestc_pd_256:
8106 case Intrinsic::x86_avx_vtestnzc_pd_256: {
8107 bool IsTestPacked = false;
Eric Christopher71c67532009-07-29 00:28:05 +00008108 unsigned X86CC = 0;
8109 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00008110 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008111 case Intrinsic::x86_avx_vtestz_ps:
8112 case Intrinsic::x86_avx_vtestz_pd:
8113 case Intrinsic::x86_avx_vtestz_ps_256:
8114 case Intrinsic::x86_avx_vtestz_pd_256:
8115 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00008116 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008117 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +00008118 // ZF = 1
8119 X86CC = X86::COND_E;
8120 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008121 case Intrinsic::x86_avx_vtestc_ps:
8122 case Intrinsic::x86_avx_vtestc_pd:
8123 case Intrinsic::x86_avx_vtestc_ps_256:
8124 case Intrinsic::x86_avx_vtestc_pd_256:
8125 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00008126 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008127 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00008128 // CF = 1
8129 X86CC = X86::COND_B;
8130 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008131 case Intrinsic::x86_avx_vtestnzc_ps:
8132 case Intrinsic::x86_avx_vtestnzc_pd:
8133 case Intrinsic::x86_avx_vtestnzc_ps_256:
8134 case Intrinsic::x86_avx_vtestnzc_pd_256:
8135 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +00008136 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008137 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00008138 // ZF and CF = 0
8139 X86CC = X86::COND_A;
8140 break;
8141 }
Eric Christopherfd179292009-08-27 18:07:15 +00008142
Eric Christopher71c67532009-07-29 00:28:05 +00008143 SDValue LHS = Op.getOperand(1);
8144 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008145 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
8146 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00008147 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
8148 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
8149 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00008150 }
Evan Cheng5759f972008-05-04 09:15:50 +00008151
8152 // Fix vector shift instructions where the last operand is a non-immediate
8153 // i32 value.
8154 case Intrinsic::x86_sse2_pslli_w:
8155 case Intrinsic::x86_sse2_pslli_d:
8156 case Intrinsic::x86_sse2_pslli_q:
8157 case Intrinsic::x86_sse2_psrli_w:
8158 case Intrinsic::x86_sse2_psrli_d:
8159 case Intrinsic::x86_sse2_psrli_q:
8160 case Intrinsic::x86_sse2_psrai_w:
8161 case Intrinsic::x86_sse2_psrai_d:
8162 case Intrinsic::x86_mmx_pslli_w:
8163 case Intrinsic::x86_mmx_pslli_d:
8164 case Intrinsic::x86_mmx_pslli_q:
8165 case Intrinsic::x86_mmx_psrli_w:
8166 case Intrinsic::x86_mmx_psrli_d:
8167 case Intrinsic::x86_mmx_psrli_q:
8168 case Intrinsic::x86_mmx_psrai_w:
8169 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00008170 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00008171 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00008172 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00008173
8174 unsigned NewIntNo = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00008175 EVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00008176 switch (IntNo) {
8177 case Intrinsic::x86_sse2_pslli_w:
8178 NewIntNo = Intrinsic::x86_sse2_psll_w;
8179 break;
8180 case Intrinsic::x86_sse2_pslli_d:
8181 NewIntNo = Intrinsic::x86_sse2_psll_d;
8182 break;
8183 case Intrinsic::x86_sse2_pslli_q:
8184 NewIntNo = Intrinsic::x86_sse2_psll_q;
8185 break;
8186 case Intrinsic::x86_sse2_psrli_w:
8187 NewIntNo = Intrinsic::x86_sse2_psrl_w;
8188 break;
8189 case Intrinsic::x86_sse2_psrli_d:
8190 NewIntNo = Intrinsic::x86_sse2_psrl_d;
8191 break;
8192 case Intrinsic::x86_sse2_psrli_q:
8193 NewIntNo = Intrinsic::x86_sse2_psrl_q;
8194 break;
8195 case Intrinsic::x86_sse2_psrai_w:
8196 NewIntNo = Intrinsic::x86_sse2_psra_w;
8197 break;
8198 case Intrinsic::x86_sse2_psrai_d:
8199 NewIntNo = Intrinsic::x86_sse2_psra_d;
8200 break;
8201 default: {
Owen Anderson825b72b2009-08-11 20:47:22 +00008202 ShAmtVT = MVT::v2i32;
Evan Cheng5759f972008-05-04 09:15:50 +00008203 switch (IntNo) {
8204 case Intrinsic::x86_mmx_pslli_w:
8205 NewIntNo = Intrinsic::x86_mmx_psll_w;
8206 break;
8207 case Intrinsic::x86_mmx_pslli_d:
8208 NewIntNo = Intrinsic::x86_mmx_psll_d;
8209 break;
8210 case Intrinsic::x86_mmx_pslli_q:
8211 NewIntNo = Intrinsic::x86_mmx_psll_q;
8212 break;
8213 case Intrinsic::x86_mmx_psrli_w:
8214 NewIntNo = Intrinsic::x86_mmx_psrl_w;
8215 break;
8216 case Intrinsic::x86_mmx_psrli_d:
8217 NewIntNo = Intrinsic::x86_mmx_psrl_d;
8218 break;
8219 case Intrinsic::x86_mmx_psrli_q:
8220 NewIntNo = Intrinsic::x86_mmx_psrl_q;
8221 break;
8222 case Intrinsic::x86_mmx_psrai_w:
8223 NewIntNo = Intrinsic::x86_mmx_psra_w;
8224 break;
8225 case Intrinsic::x86_mmx_psrai_d:
8226 NewIntNo = Intrinsic::x86_mmx_psra_d;
8227 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00008228 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00008229 }
8230 break;
8231 }
8232 }
Mon P Wangefa42202009-09-03 19:56:25 +00008233
8234 // The vector shift intrinsics with scalars uses 32b shift amounts but
8235 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
8236 // to be zero.
8237 SDValue ShOps[4];
8238 ShOps[0] = ShAmt;
8239 ShOps[1] = DAG.getConstant(0, MVT::i32);
8240 if (ShAmtVT == MVT::v4i32) {
8241 ShOps[2] = DAG.getUNDEF(MVT::i32);
8242 ShOps[3] = DAG.getUNDEF(MVT::i32);
8243 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
8244 } else {
8245 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
Dale Johannesen0488fb62010-09-30 23:57:10 +00008246// FIXME this must be lowered to get rid of the invalid type.
Mon P Wangefa42202009-09-03 19:56:25 +00008247 }
8248
Owen Andersone50ed302009-08-10 22:56:29 +00008249 EVT VT = Op.getValueType();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008250 ShAmt = DAG.getNode(ISD::BITCAST, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008251 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008252 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00008253 Op.getOperand(1), ShAmt);
8254 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00008255 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008256}
Evan Cheng72261582005-12-20 06:22:03 +00008257
Dan Gohmand858e902010-04-17 15:26:15 +00008258SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
8259 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +00008260 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
8261 MFI->setReturnAddressIsTaken(true);
8262
Bill Wendling64e87322009-01-16 19:25:27 +00008263 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008264 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00008265
8266 if (Depth > 0) {
8267 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
8268 SDValue Offset =
8269 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00008270 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008271 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00008272 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008273 FrameAddr, Offset),
Chris Lattner51abfe42010-09-21 06:02:19 +00008274 MachinePointerInfo(), false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +00008275 }
8276
8277 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00008278 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00008279 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Chris Lattner51abfe42010-09-21 06:02:19 +00008280 RetAddrFI, MachinePointerInfo(), false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00008281}
8282
Dan Gohmand858e902010-04-17 15:26:15 +00008283SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +00008284 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
8285 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00008286
Owen Andersone50ed302009-08-10 22:56:29 +00008287 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008288 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00008289 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
8290 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00008291 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00008292 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +00008293 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
8294 MachinePointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +00008295 false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00008296 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00008297}
8298
Dan Gohman475871a2008-07-27 21:46:04 +00008299SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008300 SelectionDAG &DAG) const {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00008301 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008302}
8303
Dan Gohmand858e902010-04-17 15:26:15 +00008304SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008305 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00008306 SDValue Chain = Op.getOperand(0);
8307 SDValue Offset = Op.getOperand(1);
8308 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008309 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008310
Dan Gohmand8816272010-08-11 18:14:00 +00008311 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
8312 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
8313 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008314 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008315
Dan Gohmand8816272010-08-11 18:14:00 +00008316 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
8317 DAG.getIntPtrConstant(TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008318 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +00008319 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
8320 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00008321 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008322 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008323
Dale Johannesene4d209d2009-02-03 20:21:25 +00008324 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008325 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008326 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008327}
8328
Dan Gohman475871a2008-07-27 21:46:04 +00008329SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008330 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008331 SDValue Root = Op.getOperand(0);
8332 SDValue Trmp = Op.getOperand(1); // trampoline
8333 SDValue FPtr = Op.getOperand(2); // nested function
8334 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008335 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008336
Dan Gohman69de1932008-02-06 22:27:42 +00008337 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008338
8339 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00008340 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00008341
8342 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +00008343 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
8344 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +00008345
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00008346 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
8347 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00008348
8349 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
8350
8351 // Load the pointer to the nested function into R11.
8352 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00008353 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00008354 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008355 Addr, MachinePointerInfo(TrmpAddr),
8356 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008357
Owen Anderson825b72b2009-08-11 20:47:22 +00008358 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8359 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008360 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
8361 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +00008362 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00008363
8364 // Load the 'nest' parameter value into R10.
8365 // R10 is specified in X86CallingConv.td
8366 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +00008367 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8368 DAG.getConstant(10, MVT::i64));
8369 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008370 Addr, MachinePointerInfo(TrmpAddr, 10),
8371 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008372
Owen Anderson825b72b2009-08-11 20:47:22 +00008373 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8374 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008375 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
8376 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +00008377 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00008378
8379 // Jump to the nested function.
8380 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +00008381 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8382 DAG.getConstant(20, MVT::i64));
8383 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008384 Addr, MachinePointerInfo(TrmpAddr, 20),
8385 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008386
8387 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +00008388 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8389 DAG.getConstant(22, MVT::i64));
8390 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00008391 MachinePointerInfo(TrmpAddr, 22),
8392 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008393
Dan Gohman475871a2008-07-27 21:46:04 +00008394 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00008395 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008396 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008397 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00008398 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00008399 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00008400 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00008401 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008402
8403 switch (CC) {
8404 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00008405 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00008406 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00008407 case CallingConv::X86_StdCall: {
8408 // Pass 'nest' parameter in ECX.
8409 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00008410 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008411
8412 // Check that ECX wasn't needed by an 'inreg' parameter.
8413 const FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00008414 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008415
Chris Lattner58d74912008-03-12 17:45:29 +00008416 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00008417 unsigned InRegCount = 0;
8418 unsigned Idx = 1;
8419
8420 for (FunctionType::param_iterator I = FTy->param_begin(),
8421 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00008422 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00008423 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00008424 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008425
8426 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +00008427 report_fatal_error("Nest register in use - reduce number of inreg"
8428 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00008429 }
8430 }
8431 break;
8432 }
8433 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +00008434 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00008435 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00008436 // Pass 'nest' parameter in EAX.
8437 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00008438 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008439 break;
8440 }
8441
Dan Gohman475871a2008-07-27 21:46:04 +00008442 SDValue OutChains[4];
8443 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008444
Owen Anderson825b72b2009-08-11 20:47:22 +00008445 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8446 DAG.getConstant(10, MVT::i32));
8447 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008448
Chris Lattnera62fe662010-02-05 19:20:30 +00008449 // This is storing the opcode for MOV32ri.
8450 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00008451 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00008452 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008453 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008454 Trmp, MachinePointerInfo(TrmpAddr),
8455 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008456
Owen Anderson825b72b2009-08-11 20:47:22 +00008457 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8458 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008459 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
8460 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +00008461 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008462
Chris Lattnera62fe662010-02-05 19:20:30 +00008463 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +00008464 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8465 DAG.getConstant(5, MVT::i32));
8466 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00008467 MachinePointerInfo(TrmpAddr, 5),
8468 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008469
Owen Anderson825b72b2009-08-11 20:47:22 +00008470 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8471 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008472 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
8473 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +00008474 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008475
Dan Gohman475871a2008-07-27 21:46:04 +00008476 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00008477 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008478 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008479 }
8480}
8481
Dan Gohmand858e902010-04-17 15:26:15 +00008482SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
8483 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008484 /*
8485 The rounding mode is in bits 11:10 of FPSR, and has the following
8486 settings:
8487 00 Round to nearest
8488 01 Round to -inf
8489 10 Round to +inf
8490 11 Round to 0
8491
8492 FLT_ROUNDS, on the other hand, expects the following:
8493 -1 Undefined
8494 0 Round to 0
8495 1 Round to nearest
8496 2 Round to +inf
8497 3 Round to -inf
8498
8499 To perform the conversion, we do:
8500 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
8501 */
8502
8503 MachineFunction &MF = DAG.getMachineFunction();
8504 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00008505 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008506 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00008507 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +00008508 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008509
8510 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00008511 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +00008512 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008513
Michael J. Spencerec38de22010-10-10 22:04:20 +00008514
Chris Lattner2156b792010-09-22 01:11:26 +00008515 MachineMemOperand *MMO =
8516 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8517 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008518
Chris Lattner2156b792010-09-22 01:11:26 +00008519 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
8520 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
8521 DAG.getVTList(MVT::Other),
8522 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008523
8524 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +00008525 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Chris Lattner51abfe42010-09-21 06:02:19 +00008526 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008527
8528 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00008529 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +00008530 DAG.getNode(ISD::SRL, DL, MVT::i16,
8531 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00008532 CWD, DAG.getConstant(0x800, MVT::i16)),
8533 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00008534 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +00008535 DAG.getNode(ISD::SRL, DL, MVT::i16,
8536 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00008537 CWD, DAG.getConstant(0x400, MVT::i16)),
8538 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008539
Dan Gohman475871a2008-07-27 21:46:04 +00008540 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +00008541 DAG.getNode(ISD::AND, DL, MVT::i16,
8542 DAG.getNode(ISD::ADD, DL, MVT::i16,
8543 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +00008544 DAG.getConstant(1, MVT::i16)),
8545 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008546
8547
Duncan Sands83ec4b62008-06-06 12:08:01 +00008548 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +00008549 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008550}
8551
Dan Gohmand858e902010-04-17 15:26:15 +00008552SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008553 EVT VT = Op.getValueType();
8554 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008555 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008556 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00008557
8558 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008559 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00008560 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +00008561 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00008562 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008563 }
Evan Cheng18efe262007-12-14 02:13:44 +00008564
Evan Cheng152804e2007-12-14 08:30:15 +00008565 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008566 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008567 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00008568
8569 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00008570 SDValue Ops[] = {
8571 Op,
8572 DAG.getConstant(NumBits+NumBits-1, OpVT),
8573 DAG.getConstant(X86::COND_E, MVT::i8),
8574 Op.getValue(1)
8575 };
8576 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00008577
8578 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00008579 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00008580
Owen Anderson825b72b2009-08-11 20:47:22 +00008581 if (VT == MVT::i8)
8582 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008583 return Op;
8584}
8585
Dan Gohmand858e902010-04-17 15:26:15 +00008586SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008587 EVT VT = Op.getValueType();
8588 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008589 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008590 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00008591
8592 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008593 if (VT == MVT::i8) {
8594 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00008595 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008596 }
Evan Cheng152804e2007-12-14 08:30:15 +00008597
8598 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008599 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008600 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00008601
8602 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00008603 SDValue Ops[] = {
8604 Op,
8605 DAG.getConstant(NumBits, OpVT),
8606 DAG.getConstant(X86::COND_E, MVT::i8),
8607 Op.getValue(1)
8608 };
8609 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00008610
Owen Anderson825b72b2009-08-11 20:47:22 +00008611 if (VT == MVT::i8)
8612 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008613 return Op;
8614}
8615
Dan Gohmand858e902010-04-17 15:26:15 +00008616SDValue X86TargetLowering::LowerMUL_V2I64(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008617 EVT VT = Op.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00008618 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008619 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00008620
Mon P Wangaf9b9522008-12-18 21:42:19 +00008621 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
8622 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
8623 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
8624 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
8625 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
8626 //
8627 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
8628 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
8629 // return AloBlo + AloBhi + AhiBlo;
8630
8631 SDValue A = Op.getOperand(0);
8632 SDValue B = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008633
Dale Johannesene4d209d2009-02-03 20:21:25 +00008634 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008635 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8636 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008637 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008638 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8639 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008640 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008641 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008642 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008643 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008644 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008645 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008646 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008647 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008648 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008649 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008650 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8651 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008652 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008653 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8654 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008655 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
8656 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +00008657 return Res;
8658}
8659
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008660SDValue X86TargetLowering::LowerSHL(SDValue Op, SelectionDAG &DAG) const {
8661 EVT VT = Op.getValueType();
8662 DebugLoc dl = Op.getDebugLoc();
8663 SDValue R = Op.getOperand(0);
8664
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008665 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008666
Nate Begeman51409212010-07-28 00:21:48 +00008667 assert(Subtarget->hasSSE41() && "Cannot lower SHL without SSE4.1 or later");
8668
8669 if (VT == MVT::v4i32) {
8670 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8671 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
8672 Op.getOperand(1), DAG.getConstant(23, MVT::i32));
8673
8674 ConstantInt *CI = ConstantInt::get(*Context, APInt(32, 0x3f800000U));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008675
Nate Begeman51409212010-07-28 00:21:48 +00008676 std::vector<Constant*> CV(4, CI);
8677 Constant *C = ConstantVector::get(CV);
8678 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8679 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008680 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00008681 false, false, 16);
8682
8683 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008684 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +00008685 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
8686 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
8687 }
8688 if (VT == MVT::v16i8) {
8689 // a = a << 5;
8690 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8691 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
8692 Op.getOperand(1), DAG.getConstant(5, MVT::i32));
8693
8694 ConstantInt *CM1 = ConstantInt::get(*Context, APInt(8, 15));
8695 ConstantInt *CM2 = ConstantInt::get(*Context, APInt(8, 63));
8696
8697 std::vector<Constant*> CVM1(16, CM1);
8698 std::vector<Constant*> CVM2(16, CM2);
8699 Constant *C = ConstantVector::get(CVM1);
8700 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8701 SDValue M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008702 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00008703 false, false, 16);
8704
8705 // r = pblendv(r, psllw(r & (char16)15, 4), a);
8706 M = DAG.getNode(ISD::AND, dl, VT, R, M);
8707 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8708 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
8709 DAG.getConstant(4, MVT::i32));
Nate Begeman672fb622010-12-20 22:04:24 +00008710 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT, R, M, Op);
Nate Begeman51409212010-07-28 00:21:48 +00008711 // a += a
8712 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008713
Nate Begeman51409212010-07-28 00:21:48 +00008714 C = ConstantVector::get(CVM2);
8715 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8716 M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008717 MachinePointerInfo::getConstantPool(),
Chris Lattner51abfe42010-09-21 06:02:19 +00008718 false, false, 16);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008719
Nate Begeman51409212010-07-28 00:21:48 +00008720 // r = pblendv(r, psllw(r & (char16)63, 2), a);
8721 M = DAG.getNode(ISD::AND, dl, VT, R, M);
8722 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8723 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
8724 DAG.getConstant(2, MVT::i32));
Nate Begeman672fb622010-12-20 22:04:24 +00008725 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT, R, M, Op);
Nate Begeman51409212010-07-28 00:21:48 +00008726 // a += a
8727 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008728
Nate Begeman51409212010-07-28 00:21:48 +00008729 // return pblendv(r, r+r, a);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008730 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT,
Nate Begeman51409212010-07-28 00:21:48 +00008731 R, DAG.getNode(ISD::ADD, dl, VT, R, R), Op);
8732 return R;
8733 }
8734 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008735}
Mon P Wangaf9b9522008-12-18 21:42:19 +00008736
Dan Gohmand858e902010-04-17 15:26:15 +00008737SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
Bill Wendling74c37652008-12-09 22:08:41 +00008738 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
8739 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +00008740 // looks for this combo and may remove the "setcc" instruction if the "setcc"
8741 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +00008742 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +00008743 SDValue LHS = N->getOperand(0);
8744 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +00008745 unsigned BaseOp = 0;
8746 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008747 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +00008748 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008749 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +00008750 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +00008751 // A subtract of one will be selected as a INC. Note that INC doesn't
8752 // set CF, so we can't do this for UADDO.
8753 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
8754 if (C->getAPIntValue() == 1) {
8755 BaseOp = X86ISD::INC;
8756 Cond = X86::COND_O;
8757 break;
8758 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008759 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +00008760 Cond = X86::COND_O;
8761 break;
8762 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008763 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +00008764 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00008765 break;
8766 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +00008767 // A subtract of one will be selected as a DEC. Note that DEC doesn't
8768 // set CF, so we can't do this for USUBO.
8769 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
8770 if (C->getAPIntValue() == 1) {
8771 BaseOp = X86ISD::DEC;
8772 Cond = X86::COND_O;
8773 break;
8774 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008775 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +00008776 Cond = X86::COND_O;
8777 break;
8778 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008779 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +00008780 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00008781 break;
8782 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00008783 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +00008784 Cond = X86::COND_O;
8785 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008786 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
8787 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
8788 MVT::i32);
8789 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008790
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008791 SDValue SetCC =
8792 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
8793 DAG.getConstant(X86::COND_O, MVT::i32),
8794 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008795
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008796 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
8797 return Sum;
8798 }
Bill Wendling74c37652008-12-09 22:08:41 +00008799 }
Bill Wendling3fafd932008-11-26 22:37:40 +00008800
Bill Wendling61edeb52008-12-02 01:06:39 +00008801 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008802 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008803 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +00008804
Bill Wendling61edeb52008-12-02 01:06:39 +00008805 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008806 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
8807 DAG.getConstant(Cond, MVT::i32),
8808 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +00008809
Bill Wendling61edeb52008-12-02 01:06:39 +00008810 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
8811 return Sum;
Bill Wendling41ea7e72008-11-24 19:21:46 +00008812}
8813
Eric Christopher9a9d2752010-07-22 02:48:34 +00008814SDValue X86TargetLowering::LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG) const{
8815 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00008816
Eric Christopherb6729dc2010-08-04 23:03:04 +00008817 if (!Subtarget->hasSSE2()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +00008818 SDValue Chain = Op.getOperand(0);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008819 SDValue Zero = DAG.getConstant(0,
Eric Christopherb6729dc2010-08-04 23:03:04 +00008820 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +00008821 SDValue Ops[] = {
8822 DAG.getRegister(X86::ESP, MVT::i32), // Base
8823 DAG.getTargetConstant(1, MVT::i8), // Scale
8824 DAG.getRegister(0, MVT::i32), // Index
8825 DAG.getTargetConstant(0, MVT::i32), // Disp
8826 DAG.getRegister(0, MVT::i32), // Segment.
8827 Zero,
8828 Chain
8829 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00008830 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +00008831 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
8832 array_lengthof(Ops));
8833 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +00008834 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00008835
Eric Christopher9a9d2752010-07-22 02:48:34 +00008836 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +00008837 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +00008838 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008839
Chris Lattner132929a2010-08-14 17:26:09 +00008840 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
8841 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
8842 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
8843 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +00008844
Chris Lattner132929a2010-08-14 17:26:09 +00008845 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
8846 if (!Op1 && !Op2 && !Op3 && Op4)
8847 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008848
Chris Lattner132929a2010-08-14 17:26:09 +00008849 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
8850 if (Op1 && !Op2 && !Op3 && !Op4)
8851 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008852
8853 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +00008854 // (MFENCE)>;
8855 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +00008856}
8857
Dan Gohmand858e902010-04-17 15:26:15 +00008858SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008859 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008860 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +00008861 unsigned Reg = 0;
8862 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00008863 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008864 default:
8865 assert(false && "Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +00008866 case MVT::i8: Reg = X86::AL; size = 1; break;
8867 case MVT::i16: Reg = X86::AX; size = 2; break;
8868 case MVT::i32: Reg = X86::EAX; size = 4; break;
8869 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +00008870 assert(Subtarget->is64Bit() && "Node not type legal!");
8871 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00008872 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00008873 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008874 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +00008875 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +00008876 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +00008877 Op.getOperand(1),
8878 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +00008879 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +00008880 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008881 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008882 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
8883 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
8884 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +00008885 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008886 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +00008887 return cpOut;
8888}
8889
Duncan Sands1607f052008-12-01 11:39:25 +00008890SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008891 SelectionDAG &DAG) const {
Duncan Sands1607f052008-12-01 11:39:25 +00008892 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008893 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +00008894 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008895 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +00008896 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00008897 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
8898 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +00008899 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +00008900 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
8901 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +00008902 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +00008903 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +00008904 rdx.getValue(1)
8905 };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008906 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008907}
8908
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008909SDValue X86TargetLowering::LowerBITCAST(SDValue Op,
Dale Johannesen7d07b482010-05-21 00:52:33 +00008910 SelectionDAG &DAG) const {
8911 EVT SrcVT = Op.getOperand(0).getValueType();
8912 EVT DstVT = Op.getValueType();
Chris Lattner2a786eb2010-12-19 20:19:20 +00008913 assert(Subtarget->is64Bit() && !Subtarget->hasSSE2() &&
8914 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +00008915 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +00008916 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008917 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +00008918 // i64 <=> MMX conversions are Legal.
8919 if (SrcVT==MVT::i64 && DstVT.isVector())
8920 return Op;
8921 if (DstVT==MVT::i64 && SrcVT.isVector())
8922 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +00008923 // MMX <=> MMX conversions are Legal.
8924 if (SrcVT.isVector() && DstVT.isVector())
8925 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +00008926 // All other conversions need to be expanded.
8927 return SDValue();
8928}
Chris Lattner5b856542010-12-20 00:59:46 +00008929
Dan Gohmand858e902010-04-17 15:26:15 +00008930SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen71d1bf52008-09-29 22:25:26 +00008931 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +00008932 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008933 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008934 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +00008935 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008936 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008937 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +00008938 Node->getOperand(0),
8939 Node->getOperand(1), negOp,
8940 cast<AtomicSDNode>(Node)->getSrcValue(),
8941 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang63307c32008-05-05 19:05:59 +00008942}
8943
Chris Lattner5b856542010-12-20 00:59:46 +00008944static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
8945 EVT VT = Op.getNode()->getValueType(0);
8946
8947 // Let legalize expand this if it isn't a legal type yet.
8948 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
8949 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008950
Chris Lattner5b856542010-12-20 00:59:46 +00008951 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008952
Chris Lattner5b856542010-12-20 00:59:46 +00008953 unsigned Opc;
8954 bool ExtraOp = false;
8955 switch (Op.getOpcode()) {
8956 default: assert(0 && "Invalid code");
8957 case ISD::ADDC: Opc = X86ISD::ADD; break;
8958 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
8959 case ISD::SUBC: Opc = X86ISD::SUB; break;
8960 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
8961 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008962
Chris Lattner5b856542010-12-20 00:59:46 +00008963 if (!ExtraOp)
8964 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
8965 Op.getOperand(1));
8966 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
8967 Op.getOperand(1), Op.getOperand(2));
8968}
8969
Evan Cheng0db9fe62006-04-25 20:13:52 +00008970/// LowerOperation - Provide custom lowering hooks for some operations.
8971///
Dan Gohmand858e902010-04-17 15:26:15 +00008972SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00008973 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008974 default: llvm_unreachable("Should not custom lower this!");
Eric Christopher9a9d2752010-07-22 02:48:34 +00008975 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op,DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008976 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
8977 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008978 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00008979 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008980 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
8981 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
8982 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
David Greene91585092011-01-26 15:38:49 +00008983 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
David Greenecfe33c42011-01-26 19:13:22 +00008984 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008985 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
8986 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
8987 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00008988 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +00008989 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +00008990 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008991 case ISD::SHL_PARTS:
8992 case ISD::SRA_PARTS:
8993 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
8994 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008995 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008996 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00008997 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008998 case ISD::FABS: return LowerFABS(Op, DAG);
8999 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00009000 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00009001 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman30a0de92008-07-17 16:51:19 +00009002 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00009003 case ISD::SELECT: return LowerSELECT(Op, DAG);
9004 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009005 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009006 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +00009007 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +00009008 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009009 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00009010 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
9011 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009012 case ISD::FRAME_TO_ARGS_OFFSET:
9013 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009014 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009015 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009016 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00009017 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +00009018 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
9019 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Mon P Wangaf9b9522008-12-18 21:42:19 +00009020 case ISD::MUL: return LowerMUL_V2I64(Op, DAG);
Nate Begemanbdcb5af2010-07-27 22:37:06 +00009021 case ISD::SHL: return LowerSHL(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +00009022 case ISD::SADDO:
9023 case ISD::UADDO:
9024 case ISD::SSUBO:
9025 case ISD::USUBO:
9026 case ISD::SMULO:
9027 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00009028 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009029 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +00009030 case ISD::ADDC:
9031 case ISD::ADDE:
9032 case ISD::SUBC:
9033 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009034 }
Chris Lattner27a6c732007-11-24 07:07:01 +00009035}
9036
Duncan Sands1607f052008-12-01 11:39:25 +00009037void X86TargetLowering::
9038ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00009039 SelectionDAG &DAG, unsigned NewOp) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009040 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009041 DebugLoc dl = Node->getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00009042 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +00009043
9044 SDValue Chain = Node->getOperand(0);
9045 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00009046 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00009047 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00009048 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00009049 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +00009050 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +00009051 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +00009052 SDValue Result =
9053 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
9054 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +00009055 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +00009056 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00009057 Results.push_back(Result.getValue(2));
9058}
9059
Duncan Sands126d9072008-07-04 11:47:58 +00009060/// ReplaceNodeResults - Replace a node with an illegal result type
9061/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00009062void X86TargetLowering::ReplaceNodeResults(SDNode *N,
9063 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00009064 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +00009065 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +00009066 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +00009067 default:
Duncan Sands1607f052008-12-01 11:39:25 +00009068 assert(false && "Do not know how to custom type legalize this operation!");
9069 return;
Chris Lattner5b856542010-12-20 00:59:46 +00009070 case ISD::ADDC:
9071 case ISD::ADDE:
9072 case ISD::SUBC:
9073 case ISD::SUBE:
9074 // We don't want to expand or promote these.
9075 return;
Duncan Sands1607f052008-12-01 11:39:25 +00009076 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +00009077 std::pair<SDValue,SDValue> Vals =
9078 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +00009079 SDValue FIST = Vals.first, StackSlot = Vals.second;
9080 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +00009081 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +00009082 // Return a load from the stack slot.
Chris Lattner51abfe42010-09-21 06:02:19 +00009083 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
9084 MachinePointerInfo(), false, false, 0));
Duncan Sands1607f052008-12-01 11:39:25 +00009085 }
9086 return;
9087 }
9088 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009089 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +00009090 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009091 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00009092 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +00009093 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00009094 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00009095 eax.getValue(2));
9096 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
9097 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +00009098 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00009099 Results.push_back(edx.getValue(1));
9100 return;
9101 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009102 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +00009103 EVT T = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009104 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Duncan Sands1607f052008-12-01 11:39:25 +00009105 SDValue cpInL, cpInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00009106 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
9107 DAG.getConstant(0, MVT::i32));
9108 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
9109 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00009110 cpInL = DAG.getCopyToReg(N->getOperand(0), dl, X86::EAX, cpInL, SDValue());
9111 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl, X86::EDX, cpInH,
Duncan Sands1607f052008-12-01 11:39:25 +00009112 cpInL.getValue(1));
9113 SDValue swapInL, swapInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00009114 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
9115 DAG.getConstant(0, MVT::i32));
9116 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
9117 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00009118 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl, X86::EBX, swapInL,
Duncan Sands1607f052008-12-01 11:39:25 +00009119 cpInH.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00009120 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl, X86::ECX, swapInH,
Duncan Sands1607f052008-12-01 11:39:25 +00009121 swapInL.getValue(1));
9122 SDValue Ops[] = { swapInH.getValue(0),
9123 N->getOperand(1),
9124 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009125 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +00009126 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
9127 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG8_DAG, dl, Tys,
9128 Ops, 3, T, MMO);
Dale Johannesendd64c412009-02-04 00:33:20 +00009129 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl, X86::EAX,
Owen Anderson825b72b2009-08-11 20:47:22 +00009130 MVT::i32, Result.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00009131 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl, X86::EDX,
Owen Anderson825b72b2009-08-11 20:47:22 +00009132 MVT::i32, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +00009133 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Owen Anderson825b72b2009-08-11 20:47:22 +00009134 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00009135 Results.push_back(cpOutH.getValue(1));
9136 return;
9137 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009138 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +00009139 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
9140 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009141 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +00009142 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
9143 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009144 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +00009145 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
9146 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009147 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +00009148 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
9149 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009150 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +00009151 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
9152 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009153 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +00009154 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
9155 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009156 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +00009157 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
9158 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00009159 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00009160}
9161
Evan Cheng72261582005-12-20 06:22:03 +00009162const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
9163 switch (Opcode) {
9164 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +00009165 case X86ISD::BSF: return "X86ISD::BSF";
9166 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +00009167 case X86ISD::SHLD: return "X86ISD::SHLD";
9168 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00009169 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00009170 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00009171 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00009172 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00009173 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00009174 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00009175 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
9176 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
9177 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00009178 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00009179 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +00009180 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +00009181 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +00009182 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +00009183 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00009184 case X86ISD::COMI: return "X86ISD::COMI";
9185 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00009186 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +00009187 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Evan Cheng72261582005-12-20 06:22:03 +00009188 case X86ISD::CMOV: return "X86ISD::CMOV";
9189 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00009190 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00009191 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
9192 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +00009193 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00009194 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +00009195 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +00009196 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +00009197 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +00009198 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
9199 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +00009200 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +00009201 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Nate Begemanb65c1752010-12-17 22:55:37 +00009202 case X86ISD::PANDN: return "X86ISD::PANDN";
9203 case X86ISD::PSIGNB: return "X86ISD::PSIGNB";
9204 case X86ISD::PSIGNW: return "X86ISD::PSIGNW";
9205 case X86ISD::PSIGND: return "X86ISD::PSIGND";
Nate Begeman672fb622010-12-20 22:04:24 +00009206 case X86ISD::PBLENDVB: return "X86ISD::PBLENDVB";
Evan Cheng8ca29322006-11-10 21:43:37 +00009207 case X86ISD::FMAX: return "X86ISD::FMAX";
9208 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +00009209 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
9210 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00009211 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +00009212 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009213 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00009214 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009215 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +00009216 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
9217 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009218 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
9219 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
9220 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
9221 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
9222 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
9223 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +00009224 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
9225 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +00009226 case X86ISD::VSHL: return "X86ISD::VSHL";
9227 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +00009228 case X86ISD::CMPPD: return "X86ISD::CMPPD";
9229 case X86ISD::CMPPS: return "X86ISD::CMPPS";
9230 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
9231 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
9232 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
9233 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
9234 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
9235 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
9236 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
9237 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +00009238 case X86ISD::ADD: return "X86ISD::ADD";
9239 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +00009240 case X86ISD::ADC: return "X86ISD::ADC";
9241 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +00009242 case X86ISD::SMUL: return "X86ISD::SMUL";
9243 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +00009244 case X86ISD::INC: return "X86ISD::INC";
9245 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +00009246 case X86ISD::OR: return "X86ISD::OR";
9247 case X86ISD::XOR: return "X86ISD::XOR";
9248 case X86ISD::AND: return "X86ISD::AND";
Evan Cheng73f24c92009-03-30 21:36:47 +00009249 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +00009250 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009251 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009252 case X86ISD::PALIGN: return "X86ISD::PALIGN";
9253 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
9254 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
9255 case X86ISD::PSHUFHW_LD: return "X86ISD::PSHUFHW_LD";
9256 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
9257 case X86ISD::PSHUFLW_LD: return "X86ISD::PSHUFLW_LD";
9258 case X86ISD::SHUFPS: return "X86ISD::SHUFPS";
9259 case X86ISD::SHUFPD: return "X86ISD::SHUFPD";
9260 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009261 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00009262 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009263 case X86ISD::MOVHLPD: return "X86ISD::MOVHLPD";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00009264 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
9265 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009266 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
9267 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
9268 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
9269 case X86ISD::MOVSHDUP_LD: return "X86ISD::MOVSHDUP_LD";
9270 case X86ISD::MOVSLDUP_LD: return "X86ISD::MOVSLDUP_LD";
9271 case X86ISD::MOVSD: return "X86ISD::MOVSD";
9272 case X86ISD::MOVSS: return "X86ISD::MOVSS";
9273 case X86ISD::UNPCKLPS: return "X86ISD::UNPCKLPS";
9274 case X86ISD::UNPCKLPD: return "X86ISD::UNPCKLPD";
9275 case X86ISD::UNPCKHPS: return "X86ISD::UNPCKHPS";
9276 case X86ISD::UNPCKHPD: return "X86ISD::UNPCKHPD";
9277 case X86ISD::PUNPCKLBW: return "X86ISD::PUNPCKLBW";
9278 case X86ISD::PUNPCKLWD: return "X86ISD::PUNPCKLWD";
9279 case X86ISD::PUNPCKLDQ: return "X86ISD::PUNPCKLDQ";
9280 case X86ISD::PUNPCKLQDQ: return "X86ISD::PUNPCKLQDQ";
9281 case X86ISD::PUNPCKHBW: return "X86ISD::PUNPCKHBW";
9282 case X86ISD::PUNPCKHWD: return "X86ISD::PUNPCKHWD";
9283 case X86ISD::PUNPCKHDQ: return "X86ISD::PUNPCKHDQ";
9284 case X86ISD::PUNPCKHQDQ: return "X86ISD::PUNPCKHQDQ";
Dan Gohmand6708ea2009-08-15 01:38:56 +00009285 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +00009286 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +00009287 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Evan Cheng72261582005-12-20 06:22:03 +00009288 }
9289}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00009290
Chris Lattnerc9addb72007-03-30 23:15:24 +00009291// isLegalAddressingMode - Return true if the addressing mode represented
9292// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +00009293bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerc9addb72007-03-30 23:15:24 +00009294 const Type *Ty) const {
9295 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009296 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +00009297 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +00009298
Chris Lattnerc9addb72007-03-30 23:15:24 +00009299 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009300 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +00009301 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00009302
Chris Lattnerc9addb72007-03-30 23:15:24 +00009303 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +00009304 unsigned GVFlags =
9305 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009306
Chris Lattnerdfed4132009-07-10 07:38:24 +00009307 // If a reference to this global requires an extra load, we can't fold it.
9308 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +00009309 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009310
Chris Lattnerdfed4132009-07-10 07:38:24 +00009311 // If BaseGV requires a register for the PIC base, we cannot also have a
9312 // BaseReg specified.
9313 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +00009314 return false;
Evan Cheng52787842007-08-01 23:46:47 +00009315
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009316 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +00009317 if ((M != CodeModel::Small || R != Reloc::Static) &&
9318 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009319 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +00009320 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009321
Chris Lattnerc9addb72007-03-30 23:15:24 +00009322 switch (AM.Scale) {
9323 case 0:
9324 case 1:
9325 case 2:
9326 case 4:
9327 case 8:
9328 // These scales always work.
9329 break;
9330 case 3:
9331 case 5:
9332 case 9:
9333 // These scales are formed with basereg+scalereg. Only accept if there is
9334 // no basereg yet.
9335 if (AM.HasBaseReg)
9336 return false;
9337 break;
9338 default: // Other stuff never works.
9339 return false;
9340 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009341
Chris Lattnerc9addb72007-03-30 23:15:24 +00009342 return true;
9343}
9344
9345
Evan Cheng2bd122c2007-10-26 01:56:11 +00009346bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009347 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +00009348 return false;
Evan Chenge127a732007-10-29 07:57:50 +00009349 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
9350 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00009351 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +00009352 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00009353 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +00009354}
9355
Owen Andersone50ed302009-08-10 22:56:29 +00009356bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +00009357 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009358 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009359 unsigned NumBits1 = VT1.getSizeInBits();
9360 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00009361 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009362 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00009363 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009364}
Evan Cheng2bd122c2007-10-26 01:56:11 +00009365
Dan Gohman97121ba2009-04-08 00:15:30 +00009366bool X86TargetLowering::isZExtFree(const Type *Ty1, const Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00009367 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009368 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00009369}
9370
Owen Andersone50ed302009-08-10 22:56:29 +00009371bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00009372 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00009373 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00009374}
9375
Owen Andersone50ed302009-08-10 22:56:29 +00009376bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +00009377 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +00009378 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +00009379}
9380
Evan Cheng60c07e12006-07-05 22:17:51 +00009381/// isShuffleMaskLegal - Targets can use this to indicate that they only
9382/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
9383/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
9384/// are assumed to be legal.
9385bool
Eric Christopherfd179292009-08-27 18:07:15 +00009386X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +00009387 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +00009388 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +00009389 if (VT.getSizeInBits() == 64)
Eric Christophercff6f852010-04-15 01:40:20 +00009390 return isPALIGNRMask(M, VT, Subtarget->hasSSSE3());
Nate Begeman9008ca62009-04-27 18:41:29 +00009391
Nate Begemana09008b2009-10-19 02:17:23 +00009392 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +00009393 return (VT.getVectorNumElements() == 2 ||
9394 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
9395 isMOVLMask(M, VT) ||
9396 isSHUFPMask(M, VT) ||
9397 isPSHUFDMask(M, VT) ||
9398 isPSHUFHWMask(M, VT) ||
9399 isPSHUFLWMask(M, VT) ||
Nate Begemana09008b2009-10-19 02:17:23 +00009400 isPALIGNRMask(M, VT, Subtarget->hasSSSE3()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +00009401 isUNPCKLMask(M, VT) ||
9402 isUNPCKHMask(M, VT) ||
9403 isUNPCKL_v_undef_Mask(M, VT) ||
9404 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00009405}
9406
Dan Gohman7d8143f2008-04-09 20:09:42 +00009407bool
Nate Begeman5a5ca152009-04-29 05:20:52 +00009408X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +00009409 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00009410 unsigned NumElts = VT.getVectorNumElements();
9411 // FIXME: This collection of masks seems suspect.
9412 if (NumElts == 2)
9413 return true;
9414 if (NumElts == 4 && VT.getSizeInBits() == 128) {
9415 return (isMOVLMask(Mask, VT) ||
9416 isCommutedMOVLMask(Mask, VT, true) ||
9417 isSHUFPMask(Mask, VT) ||
9418 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00009419 }
9420 return false;
9421}
9422
9423//===----------------------------------------------------------------------===//
9424// X86 Scheduler Hooks
9425//===----------------------------------------------------------------------===//
9426
Mon P Wang63307c32008-05-05 19:05:59 +00009427// private utility function
9428MachineBasicBlock *
9429X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
9430 MachineBasicBlock *MBB,
9431 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009432 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009433 unsigned LoadOpc,
9434 unsigned CXchgOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009435 unsigned notOpc,
9436 unsigned EAXreg,
9437 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009438 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00009439 // For the atomic bitwise operator, we generate
9440 // thisMBB:
9441 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00009442 // ld t1 = [bitinstr.addr]
9443 // op t2 = t1, [bitinstr.val]
9444 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00009445 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
9446 // bz newMBB
9447 // fallthrough -->nextMBB
9448 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9449 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009450 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00009451 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009452
Mon P Wang63307c32008-05-05 19:05:59 +00009453 /// First build the CFG
9454 MachineFunction *F = MBB->getParent();
9455 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009456 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9457 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9458 F->insert(MBBIter, newMBB);
9459 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009460
Dan Gohman14152b42010-07-06 20:24:04 +00009461 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9462 nextMBB->splice(nextMBB->begin(), thisMBB,
9463 llvm::next(MachineBasicBlock::iterator(bInstr)),
9464 thisMBB->end());
9465 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009466
Mon P Wang63307c32008-05-05 19:05:59 +00009467 // Update thisMBB to fall through to newMBB
9468 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009469
Mon P Wang63307c32008-05-05 19:05:59 +00009470 // newMBB jumps to itself and fall through to nextMBB
9471 newMBB->addSuccessor(nextMBB);
9472 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009473
Mon P Wang63307c32008-05-05 19:05:59 +00009474 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009475 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009476 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +00009477 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00009478 MachineOperand& destOper = bInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009479 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00009480 int numArgs = bInstr->getNumOperands() - 1;
9481 for (int i=0; i < numArgs; ++i)
9482 argOpers[i] = &bInstr->getOperand(i+1);
9483
9484 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009485 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009486 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00009487
Dale Johannesen140be2d2008-08-19 18:47:28 +00009488 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009489 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00009490 for (int i=0; i <= lastAddrIndx; ++i)
9491 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009492
Dale Johannesen140be2d2008-08-19 18:47:28 +00009493 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009494 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00009495 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009496 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009497 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009498 tt = t1;
9499
Dale Johannesen140be2d2008-08-19 18:47:28 +00009500 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +00009501 assert((argOpers[valArgIndx]->isReg() ||
9502 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00009503 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +00009504 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009505 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00009506 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009507 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009508 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +00009509 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009510
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009511 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +00009512 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009513
Dale Johannesene4d209d2009-02-03 20:21:25 +00009514 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +00009515 for (int i=0; i <= lastAddrIndx; ++i)
9516 (*MIB).addOperand(*argOpers[i]);
9517 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +00009518 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009519 (*MIB).setMemRefs(bInstr->memoperands_begin(),
9520 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +00009521
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009522 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +00009523 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +00009524
Mon P Wang63307c32008-05-05 19:05:59 +00009525 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009526 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00009527
Dan Gohman14152b42010-07-06 20:24:04 +00009528 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00009529 return nextMBB;
9530}
9531
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00009532// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +00009533MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009534X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
9535 MachineBasicBlock *MBB,
9536 unsigned regOpcL,
9537 unsigned regOpcH,
9538 unsigned immOpcL,
9539 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009540 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009541 // For the atomic bitwise operator, we generate
9542 // thisMBB (instructions are in pairs, except cmpxchg8b)
9543 // ld t1,t2 = [bitinstr.addr]
9544 // newMBB:
9545 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
9546 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +00009547 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009548 // mov ECX, EBX <- t5, t6
9549 // mov EAX, EDX <- t1, t2
9550 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
9551 // mov t3, t4 <- EAX, EDX
9552 // bz newMBB
9553 // result in out1, out2
9554 // fallthrough -->nextMBB
9555
9556 const TargetRegisterClass *RC = X86::GR32RegisterClass;
9557 const unsigned LoadOpc = X86::MOV32rm;
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009558 const unsigned NotOpc = X86::NOT32r;
9559 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9560 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
9561 MachineFunction::iterator MBBIter = MBB;
9562 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009563
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009564 /// First build the CFG
9565 MachineFunction *F = MBB->getParent();
9566 MachineBasicBlock *thisMBB = MBB;
9567 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9568 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9569 F->insert(MBBIter, newMBB);
9570 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009571
Dan Gohman14152b42010-07-06 20:24:04 +00009572 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9573 nextMBB->splice(nextMBB->begin(), thisMBB,
9574 llvm::next(MachineBasicBlock::iterator(bInstr)),
9575 thisMBB->end());
9576 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009577
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009578 // Update thisMBB to fall through to newMBB
9579 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009580
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009581 // newMBB jumps to itself and fall through to nextMBB
9582 newMBB->addSuccessor(nextMBB);
9583 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009584
Dale Johannesene4d209d2009-02-03 20:21:25 +00009585 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009586 // Insert instructions into newMBB based on incoming instruction
9587 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009588 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009589 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009590 MachineOperand& dest1Oper = bInstr->getOperand(0);
9591 MachineOperand& dest2Oper = bInstr->getOperand(1);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009592 MachineOperand* argOpers[2 + X86::AddrNumOperands];
9593 for (int i=0; i < 2 + X86::AddrNumOperands; ++i) {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009594 argOpers[i] = &bInstr->getOperand(i+2);
9595
Dan Gohman71ea4e52010-05-14 21:01:44 +00009596 // We use some of the operands multiple times, so conservatively just
9597 // clear any kill flags that might be present.
9598 if (argOpers[i]->isReg() && argOpers[i]->isUse())
9599 argOpers[i]->setIsKill(false);
9600 }
9601
Evan Chengad5b52f2010-01-08 19:14:57 +00009602 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009603 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +00009604
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009605 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009606 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009607 for (int i=0; i <= lastAddrIndx; ++i)
9608 (*MIB).addOperand(*argOpers[i]);
9609 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009610 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +00009611 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +00009612 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009613 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +00009614 MachineOperand newOp3 = *(argOpers[3]);
9615 if (newOp3.isImm())
9616 newOp3.setImm(newOp3.getImm()+4);
9617 else
9618 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009619 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +00009620 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009621
9622 // t3/4 are defined later, at the bottom of the loop
9623 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
9624 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009625 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009626 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009627 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009628 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
9629
Evan Cheng306b4ca2010-01-08 23:41:50 +00009630 // The subsequent operations should be using the destination registers of
9631 //the PHI instructions.
Scott Michelfdc40a02009-02-17 22:15:04 +00009632 if (invSrc) {
Evan Cheng306b4ca2010-01-08 23:41:50 +00009633 t1 = F->getRegInfo().createVirtualRegister(RC);
9634 t2 = F->getRegInfo().createVirtualRegister(RC);
9635 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
9636 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009637 } else {
Evan Cheng306b4ca2010-01-08 23:41:50 +00009638 t1 = dest1Oper.getReg();
9639 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009640 }
9641
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009642 int valArgIndx = lastAddrIndx + 1;
9643 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +00009644 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009645 "invalid operand");
9646 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
9647 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009648 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009649 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009650 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009651 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +00009652 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00009653 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009654 (*MIB).addOperand(*argOpers[valArgIndx]);
9655 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00009656 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009657 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00009658 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009659 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009660 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009661 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009662 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +00009663 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00009664 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009665 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009666
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009667 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009668 MIB.addReg(t1);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009669 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009670 MIB.addReg(t2);
9671
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009672 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009673 MIB.addReg(t5);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009674 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009675 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +00009676
Dale Johannesene4d209d2009-02-03 20:21:25 +00009677 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009678 for (int i=0; i <= lastAddrIndx; ++i)
9679 (*MIB).addOperand(*argOpers[i]);
9680
9681 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009682 (*MIB).setMemRefs(bInstr->memoperands_begin(),
9683 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009684
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009685 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009686 MIB.addReg(X86::EAX);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009687 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009688 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +00009689
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009690 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009691 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009692
Dan Gohman14152b42010-07-06 20:24:04 +00009693 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009694 return nextMBB;
9695}
9696
9697// private utility function
9698MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +00009699X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
9700 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009701 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00009702 // For the atomic min/max operator, we generate
9703 // thisMBB:
9704 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00009705 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +00009706 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +00009707 // cmp t1, t2
9708 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +00009709 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00009710 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
9711 // bz newMBB
9712 // fallthrough -->nextMBB
9713 //
9714 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9715 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009716 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00009717 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009718
Mon P Wang63307c32008-05-05 19:05:59 +00009719 /// First build the CFG
9720 MachineFunction *F = MBB->getParent();
9721 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009722 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9723 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9724 F->insert(MBBIter, newMBB);
9725 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009726
Dan Gohman14152b42010-07-06 20:24:04 +00009727 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9728 nextMBB->splice(nextMBB->begin(), thisMBB,
9729 llvm::next(MachineBasicBlock::iterator(mInstr)),
9730 thisMBB->end());
9731 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009732
Mon P Wang63307c32008-05-05 19:05:59 +00009733 // Update thisMBB to fall through to newMBB
9734 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009735
Mon P Wang63307c32008-05-05 19:05:59 +00009736 // newMBB jumps to newMBB and fall through to nextMBB
9737 newMBB->addSuccessor(nextMBB);
9738 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009739
Dale Johannesene4d209d2009-02-03 20:21:25 +00009740 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00009741 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009742 assert(mInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009743 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +00009744 MachineOperand& destOper = mInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009745 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00009746 int numArgs = mInstr->getNumOperands() - 1;
9747 for (int i=0; i < numArgs; ++i)
9748 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009749
Mon P Wang63307c32008-05-05 19:05:59 +00009750 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009751 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009752 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00009753
Mon P Wangab3e7472008-05-05 22:56:23 +00009754 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009755 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00009756 for (int i=0; i <= lastAddrIndx; ++i)
9757 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +00009758
Mon P Wang63307c32008-05-05 19:05:59 +00009759 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +00009760 assert((argOpers[valArgIndx]->isReg() ||
9761 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00009762 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +00009763
9764 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +00009765 if (argOpers[valArgIndx]->isReg())
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009766 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +00009767 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009768 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00009769 (*MIB).addOperand(*argOpers[valArgIndx]);
9770
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009771 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +00009772 MIB.addReg(t1);
9773
Dale Johannesene4d209d2009-02-03 20:21:25 +00009774 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +00009775 MIB.addReg(t1);
9776 MIB.addReg(t2);
9777
9778 // Generate movc
9779 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009780 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +00009781 MIB.addReg(t2);
9782 MIB.addReg(t1);
9783
9784 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +00009785 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +00009786 for (int i=0; i <= lastAddrIndx; ++i)
9787 (*MIB).addOperand(*argOpers[i]);
9788 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +00009789 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009790 (*MIB).setMemRefs(mInstr->memoperands_begin(),
9791 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +00009792
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009793 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +00009794 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +00009795
Mon P Wang63307c32008-05-05 19:05:59 +00009796 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009797 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00009798
Dan Gohman14152b42010-07-06 20:24:04 +00009799 mInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00009800 return nextMBB;
9801}
9802
Eric Christopherf83a5de2009-08-27 18:08:16 +00009803// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009804// or XMM0_V32I8 in AVX all of this code can be replaced with that
9805// in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +00009806MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +00009807X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +00009808 unsigned numArgs, bool memArg) const {
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009809 assert((Subtarget->hasSSE42() || Subtarget->hasAVX()) &&
9810 "Target must have SSE4.2 or AVX features enabled");
9811
Eric Christopherb120ab42009-08-18 22:50:32 +00009812 DebugLoc dl = MI->getDebugLoc();
9813 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Eric Christopherb120ab42009-08-18 22:50:32 +00009814 unsigned Opc;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009815 if (!Subtarget->hasAVX()) {
9816 if (memArg)
9817 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
9818 else
9819 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
9820 } else {
9821 if (memArg)
9822 Opc = numArgs == 3 ? X86::VPCMPISTRM128rm : X86::VPCMPESTRM128rm;
9823 else
9824 Opc = numArgs == 3 ? X86::VPCMPISTRM128rr : X86::VPCMPESTRM128rr;
9825 }
Eric Christopherb120ab42009-08-18 22:50:32 +00009826
Eric Christopher41c902f2010-11-30 08:20:21 +00009827 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Eric Christopherb120ab42009-08-18 22:50:32 +00009828 for (unsigned i = 0; i < numArgs; ++i) {
9829 MachineOperand &Op = MI->getOperand(i+1);
Eric Christopherb120ab42009-08-18 22:50:32 +00009830 if (!(Op.isReg() && Op.isImplicit()))
9831 MIB.addOperand(Op);
9832 }
Eric Christopher41c902f2010-11-30 08:20:21 +00009833 BuildMI(*BB, MI, dl, TII->get(X86::MOVAPSrr), MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +00009834 .addReg(X86::XMM0);
9835
Dan Gohman14152b42010-07-06 20:24:04 +00009836 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +00009837 return BB;
9838}
9839
9840MachineBasicBlock *
Eric Christopher228232b2010-11-30 07:20:12 +00009841X86TargetLowering::EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +00009842 DebugLoc dl = MI->getDebugLoc();
9843 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009844
Eric Christopher228232b2010-11-30 07:20:12 +00009845 // Address into RAX/EAX, other two args into ECX, EDX.
9846 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
9847 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
9848 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
9849 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +00009850 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009851
Eric Christopher228232b2010-11-30 07:20:12 +00009852 unsigned ValOps = X86::AddrNumOperands;
9853 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
9854 .addReg(MI->getOperand(ValOps).getReg());
9855 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
9856 .addReg(MI->getOperand(ValOps+1).getReg());
9857
9858 // The instruction doesn't actually take any operands though.
9859 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009860
Eric Christopher228232b2010-11-30 07:20:12 +00009861 MI->eraseFromParent(); // The pseudo is gone now.
9862 return BB;
9863}
9864
9865MachineBasicBlock *
9866X86TargetLowering::EmitMwait(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +00009867 DebugLoc dl = MI->getDebugLoc();
9868 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009869
Eric Christopher228232b2010-11-30 07:20:12 +00009870 // First arg in ECX, the second in EAX.
9871 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
9872 .addReg(MI->getOperand(0).getReg());
9873 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EAX)
9874 .addReg(MI->getOperand(1).getReg());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009875
Eric Christopher228232b2010-11-30 07:20:12 +00009876 // The instruction doesn't actually take any operands though.
9877 BuildMI(*BB, MI, dl, TII->get(X86::MWAITrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009878
Eric Christopher228232b2010-11-30 07:20:12 +00009879 MI->eraseFromParent(); // The pseudo is gone now.
9880 return BB;
9881}
9882
9883MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +00009884X86TargetLowering::EmitVAARG64WithCustomInserter(
9885 MachineInstr *MI,
9886 MachineBasicBlock *MBB) const {
9887 // Emit va_arg instruction on X86-64.
9888
9889 // Operands to this pseudo-instruction:
9890 // 0 ) Output : destination address (reg)
9891 // 1-5) Input : va_list address (addr, i64mem)
9892 // 6 ) ArgSize : Size (in bytes) of vararg type
9893 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
9894 // 8 ) Align : Alignment of type
9895 // 9 ) EFLAGS (implicit-def)
9896
9897 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
9898 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
9899
9900 unsigned DestReg = MI->getOperand(0).getReg();
9901 MachineOperand &Base = MI->getOperand(1);
9902 MachineOperand &Scale = MI->getOperand(2);
9903 MachineOperand &Index = MI->getOperand(3);
9904 MachineOperand &Disp = MI->getOperand(4);
9905 MachineOperand &Segment = MI->getOperand(5);
9906 unsigned ArgSize = MI->getOperand(6).getImm();
9907 unsigned ArgMode = MI->getOperand(7).getImm();
9908 unsigned Align = MI->getOperand(8).getImm();
9909
9910 // Memory Reference
9911 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
9912 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
9913 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
9914
9915 // Machine Information
9916 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9917 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
9918 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
9919 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
9920 DebugLoc DL = MI->getDebugLoc();
9921
9922 // struct va_list {
9923 // i32 gp_offset
9924 // i32 fp_offset
9925 // i64 overflow_area (address)
9926 // i64 reg_save_area (address)
9927 // }
9928 // sizeof(va_list) = 24
9929 // alignment(va_list) = 8
9930
9931 unsigned TotalNumIntRegs = 6;
9932 unsigned TotalNumXMMRegs = 8;
9933 bool UseGPOffset = (ArgMode == 1);
9934 bool UseFPOffset = (ArgMode == 2);
9935 unsigned MaxOffset = TotalNumIntRegs * 8 +
9936 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
9937
9938 /* Align ArgSize to a multiple of 8 */
9939 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
9940 bool NeedsAlign = (Align > 8);
9941
9942 MachineBasicBlock *thisMBB = MBB;
9943 MachineBasicBlock *overflowMBB;
9944 MachineBasicBlock *offsetMBB;
9945 MachineBasicBlock *endMBB;
9946
9947 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
9948 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
9949 unsigned OffsetReg = 0;
9950
9951 if (!UseGPOffset && !UseFPOffset) {
9952 // If we only pull from the overflow region, we don't create a branch.
9953 // We don't need to alter control flow.
9954 OffsetDestReg = 0; // unused
9955 OverflowDestReg = DestReg;
9956
9957 offsetMBB = NULL;
9958 overflowMBB = thisMBB;
9959 endMBB = thisMBB;
9960 } else {
9961 // First emit code to check if gp_offset (or fp_offset) is below the bound.
9962 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
9963 // If not, pull from overflow_area. (branch to overflowMBB)
9964 //
9965 // thisMBB
9966 // | .
9967 // | .
9968 // offsetMBB overflowMBB
9969 // | .
9970 // | .
9971 // endMBB
9972
9973 // Registers for the PHI in endMBB
9974 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
9975 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
9976
9977 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
9978 MachineFunction *MF = MBB->getParent();
9979 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
9980 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
9981 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
9982
9983 MachineFunction::iterator MBBIter = MBB;
9984 ++MBBIter;
9985
9986 // Insert the new basic blocks
9987 MF->insert(MBBIter, offsetMBB);
9988 MF->insert(MBBIter, overflowMBB);
9989 MF->insert(MBBIter, endMBB);
9990
9991 // Transfer the remainder of MBB and its successor edges to endMBB.
9992 endMBB->splice(endMBB->begin(), thisMBB,
9993 llvm::next(MachineBasicBlock::iterator(MI)),
9994 thisMBB->end());
9995 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
9996
9997 // Make offsetMBB and overflowMBB successors of thisMBB
9998 thisMBB->addSuccessor(offsetMBB);
9999 thisMBB->addSuccessor(overflowMBB);
10000
10001 // endMBB is a successor of both offsetMBB and overflowMBB
10002 offsetMBB->addSuccessor(endMBB);
10003 overflowMBB->addSuccessor(endMBB);
10004
10005 // Load the offset value into a register
10006 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
10007 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
10008 .addOperand(Base)
10009 .addOperand(Scale)
10010 .addOperand(Index)
10011 .addDisp(Disp, UseFPOffset ? 4 : 0)
10012 .addOperand(Segment)
10013 .setMemRefs(MMOBegin, MMOEnd);
10014
10015 // Check if there is enough room left to pull this argument.
10016 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
10017 .addReg(OffsetReg)
10018 .addImm(MaxOffset + 8 - ArgSizeA8);
10019
10020 // Branch to "overflowMBB" if offset >= max
10021 // Fall through to "offsetMBB" otherwise
10022 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
10023 .addMBB(overflowMBB);
10024 }
10025
10026 // In offsetMBB, emit code to use the reg_save_area.
10027 if (offsetMBB) {
10028 assert(OffsetReg != 0);
10029
10030 // Read the reg_save_area address.
10031 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
10032 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
10033 .addOperand(Base)
10034 .addOperand(Scale)
10035 .addOperand(Index)
10036 .addDisp(Disp, 16)
10037 .addOperand(Segment)
10038 .setMemRefs(MMOBegin, MMOEnd);
10039
10040 // Zero-extend the offset
10041 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
10042 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
10043 .addImm(0)
10044 .addReg(OffsetReg)
10045 .addImm(X86::sub_32bit);
10046
10047 // Add the offset to the reg_save_area to get the final address.
10048 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
10049 .addReg(OffsetReg64)
10050 .addReg(RegSaveReg);
10051
10052 // Compute the offset for the next argument
10053 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
10054 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
10055 .addReg(OffsetReg)
10056 .addImm(UseFPOffset ? 16 : 8);
10057
10058 // Store it back into the va_list.
10059 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
10060 .addOperand(Base)
10061 .addOperand(Scale)
10062 .addOperand(Index)
10063 .addDisp(Disp, UseFPOffset ? 4 : 0)
10064 .addOperand(Segment)
10065 .addReg(NextOffsetReg)
10066 .setMemRefs(MMOBegin, MMOEnd);
10067
10068 // Jump to endMBB
10069 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
10070 .addMBB(endMBB);
10071 }
10072
10073 //
10074 // Emit code to use overflow area
10075 //
10076
10077 // Load the overflow_area address into a register.
10078 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
10079 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
10080 .addOperand(Base)
10081 .addOperand(Scale)
10082 .addOperand(Index)
10083 .addDisp(Disp, 8)
10084 .addOperand(Segment)
10085 .setMemRefs(MMOBegin, MMOEnd);
10086
10087 // If we need to align it, do so. Otherwise, just copy the address
10088 // to OverflowDestReg.
10089 if (NeedsAlign) {
10090 // Align the overflow address
10091 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
10092 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
10093
10094 // aligned_addr = (addr + (align-1)) & ~(align-1)
10095 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
10096 .addReg(OverflowAddrReg)
10097 .addImm(Align-1);
10098
10099 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
10100 .addReg(TmpReg)
10101 .addImm(~(uint64_t)(Align-1));
10102 } else {
10103 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
10104 .addReg(OverflowAddrReg);
10105 }
10106
10107 // Compute the next overflow address after this argument.
10108 // (the overflow address should be kept 8-byte aligned)
10109 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
10110 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
10111 .addReg(OverflowDestReg)
10112 .addImm(ArgSizeA8);
10113
10114 // Store the new overflow address.
10115 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
10116 .addOperand(Base)
10117 .addOperand(Scale)
10118 .addOperand(Index)
10119 .addDisp(Disp, 8)
10120 .addOperand(Segment)
10121 .addReg(NextAddrReg)
10122 .setMemRefs(MMOBegin, MMOEnd);
10123
10124 // If we branched, emit the PHI to the front of endMBB.
10125 if (offsetMBB) {
10126 BuildMI(*endMBB, endMBB->begin(), DL,
10127 TII->get(X86::PHI), DestReg)
10128 .addReg(OffsetDestReg).addMBB(offsetMBB)
10129 .addReg(OverflowDestReg).addMBB(overflowMBB);
10130 }
10131
10132 // Erase the pseudo instruction
10133 MI->eraseFromParent();
10134
10135 return endMBB;
10136}
10137
10138MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +000010139X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
10140 MachineInstr *MI,
10141 MachineBasicBlock *MBB) const {
10142 // Emit code to save XMM registers to the stack. The ABI says that the
10143 // number of registers to save is given in %al, so it's theoretically
10144 // possible to do an indirect jump trick to avoid saving all of them,
10145 // however this code takes a simpler approach and just executes all
10146 // of the stores if %al is non-zero. It's less code, and it's probably
10147 // easier on the hardware branch predictor, and stores aren't all that
10148 // expensive anyway.
10149
10150 // Create the new basic blocks. One block contains all the XMM stores,
10151 // and one block is the final destination regardless of whether any
10152 // stores were performed.
10153 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
10154 MachineFunction *F = MBB->getParent();
10155 MachineFunction::iterator MBBIter = MBB;
10156 ++MBBIter;
10157 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
10158 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
10159 F->insert(MBBIter, XMMSaveMBB);
10160 F->insert(MBBIter, EndMBB);
10161
Dan Gohman14152b42010-07-06 20:24:04 +000010162 // Transfer the remainder of MBB and its successor edges to EndMBB.
10163 EndMBB->splice(EndMBB->begin(), MBB,
10164 llvm::next(MachineBasicBlock::iterator(MI)),
10165 MBB->end());
10166 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
10167
Dan Gohmand6708ea2009-08-15 01:38:56 +000010168 // The original block will now fall through to the XMM save block.
10169 MBB->addSuccessor(XMMSaveMBB);
10170 // The XMMSaveMBB will fall through to the end block.
10171 XMMSaveMBB->addSuccessor(EndMBB);
10172
10173 // Now add the instructions.
10174 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10175 DebugLoc DL = MI->getDebugLoc();
10176
10177 unsigned CountReg = MI->getOperand(0).getReg();
10178 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
10179 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
10180
10181 if (!Subtarget->isTargetWin64()) {
10182 // If %al is 0, branch around the XMM save block.
10183 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +000010184 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010185 MBB->addSuccessor(EndMBB);
10186 }
10187
10188 // In the XMM save block, save all the XMM argument registers.
10189 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
10190 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +000010191 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +000010192 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +000010193 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +000010194 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +000010195 /*Size=*/16, /*Align=*/16);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010196 BuildMI(XMMSaveMBB, DL, TII->get(X86::MOVAPSmr))
10197 .addFrameIndex(RegSaveFrameIndex)
10198 .addImm(/*Scale=*/1)
10199 .addReg(/*IndexReg=*/0)
10200 .addImm(/*Disp=*/Offset)
10201 .addReg(/*Segment=*/0)
10202 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +000010203 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010204 }
10205
Dan Gohman14152b42010-07-06 20:24:04 +000010206 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +000010207
10208 return EndMBB;
10209}
Mon P Wang63307c32008-05-05 19:05:59 +000010210
Evan Cheng60c07e12006-07-05 22:17:51 +000010211MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +000010212X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010213 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +000010214 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10215 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +000010216
Chris Lattner52600972009-09-02 05:57:00 +000010217 // To "insert" a SELECT_CC instruction, we actually have to insert the
10218 // diamond control-flow pattern. The incoming instruction knows the
10219 // destination vreg to set, the condition code register to branch on, the
10220 // true/false values to select between, and a branch opcode to use.
10221 const BasicBlock *LLVM_BB = BB->getBasicBlock();
10222 MachineFunction::iterator It = BB;
10223 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +000010224
Chris Lattner52600972009-09-02 05:57:00 +000010225 // thisMBB:
10226 // ...
10227 // TrueVal = ...
10228 // cmpTY ccX, r1, r2
10229 // bCC copy1MBB
10230 // fallthrough --> copy0MBB
10231 MachineBasicBlock *thisMBB = BB;
10232 MachineFunction *F = BB->getParent();
10233 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
10234 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +000010235 F->insert(It, copy0MBB);
10236 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +000010237
Bill Wendling730c07e2010-06-25 20:48:10 +000010238 // If the EFLAGS register isn't dead in the terminator, then claim that it's
10239 // live into the sink and copy blocks.
10240 const MachineFunction *MF = BB->getParent();
10241 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
10242 BitVector ReservedRegs = TRI->getReservedRegs(*MF);
Bill Wendling730c07e2010-06-25 20:48:10 +000010243
Dan Gohman14152b42010-07-06 20:24:04 +000010244 for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) {
10245 const MachineOperand &MO = MI->getOperand(I);
10246 if (!MO.isReg() || !MO.isUse() || MO.isKill()) continue;
Bill Wendling730c07e2010-06-25 20:48:10 +000010247 unsigned Reg = MO.getReg();
10248 if (Reg != X86::EFLAGS) continue;
10249 copy0MBB->addLiveIn(Reg);
10250 sinkMBB->addLiveIn(Reg);
10251 }
10252
Dan Gohman14152b42010-07-06 20:24:04 +000010253 // Transfer the remainder of BB and its successor edges to sinkMBB.
10254 sinkMBB->splice(sinkMBB->begin(), BB,
10255 llvm::next(MachineBasicBlock::iterator(MI)),
10256 BB->end());
10257 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
10258
10259 // Add the true and fallthrough blocks as its successors.
10260 BB->addSuccessor(copy0MBB);
10261 BB->addSuccessor(sinkMBB);
10262
10263 // Create the conditional branch instruction.
10264 unsigned Opc =
10265 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
10266 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
10267
Chris Lattner52600972009-09-02 05:57:00 +000010268 // copy0MBB:
10269 // %FalseValue = ...
10270 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000010271 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000010272
Chris Lattner52600972009-09-02 05:57:00 +000010273 // sinkMBB:
10274 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
10275 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000010276 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
10277 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000010278 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
10279 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
10280
Dan Gohman14152b42010-07-06 20:24:04 +000010281 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000010282 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000010283}
10284
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010285MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010286X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010287 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010288 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10289 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010290
10291 // The lowering is pretty easy: we're just emitting the call to _alloca. The
10292 // non-trivial part is impdef of ESP.
10293 // FIXME: The code should be tweaked as soon as we'll try to do codegen for
10294 // mingw-w64.
10295
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010296 const char *StackProbeSymbol =
10297 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
10298
Dan Gohman14152b42010-07-06 20:24:04 +000010299 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010300 .addExternalSymbol(StackProbeSymbol)
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010301 .addReg(X86::EAX, RegState::Implicit)
10302 .addReg(X86::ESP, RegState::Implicit)
10303 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
Anton Korobeynikov9f7f83b2010-08-25 07:50:11 +000010304 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
10305 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010306
Dan Gohman14152b42010-07-06 20:24:04 +000010307 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010308 return BB;
10309}
Chris Lattner52600972009-09-02 05:57:00 +000010310
10311MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000010312X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
10313 MachineBasicBlock *BB) const {
10314 // This is pretty easy. We're taking the value that we received from
10315 // our load from the relocation, sticking it in either RDI (x86-64)
10316 // or EAX and doing an indirect call. The return value will then
10317 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000010318 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000010319 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000010320 DebugLoc DL = MI->getDebugLoc();
10321 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000010322
10323 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000010324 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000010325
Eric Christopher30ef0e52010-06-03 04:07:48 +000010326 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000010327 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10328 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000010329 .addReg(X86::RIP)
10330 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010331 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000010332 MI->getOperand(3).getTargetFlags())
10333 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000010334 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000010335 addDirectMem(MIB, X86::RDI);
Eric Christopher61025492010-06-15 23:08:42 +000010336 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000010337 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10338 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000010339 .addReg(0)
10340 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010341 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000010342 MI->getOperand(3).getTargetFlags())
10343 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000010344 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000010345 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010346 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000010347 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10348 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000010349 .addReg(TII->getGlobalBaseReg(F))
10350 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010351 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000010352 MI->getOperand(3).getTargetFlags())
10353 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000010354 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000010355 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010356 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000010357
Dan Gohman14152b42010-07-06 20:24:04 +000010358 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000010359 return BB;
10360}
10361
10362MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000010363X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010364 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000010365 switch (MI->getOpcode()) {
10366 default: assert(false && "Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000010367 case X86::TAILJMPd64:
10368 case X86::TAILJMPr64:
10369 case X86::TAILJMPm64:
10370 assert(!"TAILJMP64 would not be touched here.");
10371 case X86::TCRETURNdi64:
10372 case X86::TCRETURNri64:
10373 case X86::TCRETURNmi64:
10374 // Defs of TCRETURNxx64 has Win64's callee-saved registers, as subset.
10375 // On AMD64, additional defs should be added before register allocation.
10376 if (!Subtarget->isTargetWin64()) {
10377 MI->addRegisterDefined(X86::RSI);
10378 MI->addRegisterDefined(X86::RDI);
10379 MI->addRegisterDefined(X86::XMM6);
10380 MI->addRegisterDefined(X86::XMM7);
10381 MI->addRegisterDefined(X86::XMM8);
10382 MI->addRegisterDefined(X86::XMM9);
10383 MI->addRegisterDefined(X86::XMM10);
10384 MI->addRegisterDefined(X86::XMM11);
10385 MI->addRegisterDefined(X86::XMM12);
10386 MI->addRegisterDefined(X86::XMM13);
10387 MI->addRegisterDefined(X86::XMM14);
10388 MI->addRegisterDefined(X86::XMM15);
10389 }
10390 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010391 case X86::WIN_ALLOCA:
10392 return EmitLoweredWinAlloca(MI, BB);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010393 case X86::TLSCall_32:
10394 case X86::TLSCall_64:
10395 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000010396 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000010397 case X86::CMOV_FR32:
10398 case X86::CMOV_FR64:
10399 case X86::CMOV_V4F32:
10400 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000010401 case X86::CMOV_V2I64:
Chris Lattner314a1132010-03-14 18:31:44 +000010402 case X86::CMOV_GR16:
10403 case X86::CMOV_GR32:
10404 case X86::CMOV_RFP32:
10405 case X86::CMOV_RFP64:
10406 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010407 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000010408
Dale Johannesen849f2142007-07-03 00:53:03 +000010409 case X86::FP32_TO_INT16_IN_MEM:
10410 case X86::FP32_TO_INT32_IN_MEM:
10411 case X86::FP32_TO_INT64_IN_MEM:
10412 case X86::FP64_TO_INT16_IN_MEM:
10413 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000010414 case X86::FP64_TO_INT64_IN_MEM:
10415 case X86::FP80_TO_INT16_IN_MEM:
10416 case X86::FP80_TO_INT32_IN_MEM:
10417 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000010418 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10419 DebugLoc DL = MI->getDebugLoc();
10420
Evan Cheng60c07e12006-07-05 22:17:51 +000010421 // Change the floating point control register to use "round towards zero"
10422 // mode when truncating to an integer value.
10423 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000010424 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000010425 addFrameReference(BuildMI(*BB, MI, DL,
10426 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010427
10428 // Load the old value of the high byte of the control word...
10429 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +000010430 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohman14152b42010-07-06 20:24:04 +000010431 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000010432 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010433
10434 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000010435 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000010436 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000010437
10438 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000010439 addFrameReference(BuildMI(*BB, MI, DL,
10440 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010441
10442 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000010443 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000010444 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000010445
10446 // Get the X86 opcode to use.
10447 unsigned Opc;
10448 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010449 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000010450 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
10451 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
10452 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
10453 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
10454 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
10455 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000010456 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
10457 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
10458 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000010459 }
10460
10461 X86AddressMode AM;
10462 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000010463 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000010464 AM.BaseType = X86AddressMode::RegBase;
10465 AM.Base.Reg = Op.getReg();
10466 } else {
10467 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000010468 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000010469 }
10470 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000010471 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000010472 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010473 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000010474 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000010475 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010476 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000010477 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000010478 AM.GV = Op.getGlobal();
10479 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000010480 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010481 }
Dan Gohman14152b42010-07-06 20:24:04 +000010482 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010483 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000010484
10485 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000010486 addFrameReference(BuildMI(*BB, MI, DL,
10487 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010488
Dan Gohman14152b42010-07-06 20:24:04 +000010489 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000010490 return BB;
10491 }
Eric Christopherb120ab42009-08-18 22:50:32 +000010492 // String/text processing lowering.
10493 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010494 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000010495 return EmitPCMP(MI, BB, 3, false /* in-mem */);
10496 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010497 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000010498 return EmitPCMP(MI, BB, 3, true /* in-mem */);
10499 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010500 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000010501 return EmitPCMP(MI, BB, 5, false /* in mem */);
10502 case X86::PCMPESTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010503 case X86::VPCMPESTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000010504 return EmitPCMP(MI, BB, 5, true /* in mem */);
10505
Eric Christopher228232b2010-11-30 07:20:12 +000010506 // Thread synchronization.
10507 case X86::MONITOR:
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010508 return EmitMonitor(MI, BB);
Eric Christopher228232b2010-11-30 07:20:12 +000010509 case X86::MWAIT:
10510 return EmitMwait(MI, BB);
10511
Eric Christopherb120ab42009-08-18 22:50:32 +000010512 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +000010513 case X86::ATOMAND32:
10514 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010515 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010516 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010517 X86::NOT32r, X86::EAX,
10518 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000010519 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +000010520 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
10521 X86::OR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010522 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010523 X86::NOT32r, X86::EAX,
10524 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000010525 case X86::ATOMXOR32:
10526 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010527 X86::XOR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010528 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010529 X86::NOT32r, X86::EAX,
10530 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010531 case X86::ATOMNAND32:
10532 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010533 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010534 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010535 X86::NOT32r, X86::EAX,
10536 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +000010537 case X86::ATOMMIN32:
10538 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
10539 case X86::ATOMMAX32:
10540 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
10541 case X86::ATOMUMIN32:
10542 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
10543 case X86::ATOMUMAX32:
10544 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +000010545
10546 case X86::ATOMAND16:
10547 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
10548 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010549 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010550 X86::NOT16r, X86::AX,
10551 X86::GR16RegisterClass);
10552 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +000010553 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010554 X86::OR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010555 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010556 X86::NOT16r, X86::AX,
10557 X86::GR16RegisterClass);
10558 case X86::ATOMXOR16:
10559 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
10560 X86::XOR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010561 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010562 X86::NOT16r, X86::AX,
10563 X86::GR16RegisterClass);
10564 case X86::ATOMNAND16:
10565 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
10566 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010567 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010568 X86::NOT16r, X86::AX,
10569 X86::GR16RegisterClass, true);
10570 case X86::ATOMMIN16:
10571 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
10572 case X86::ATOMMAX16:
10573 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
10574 case X86::ATOMUMIN16:
10575 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
10576 case X86::ATOMUMAX16:
10577 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
10578
10579 case X86::ATOMAND8:
10580 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
10581 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010582 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010583 X86::NOT8r, X86::AL,
10584 X86::GR8RegisterClass);
10585 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +000010586 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010587 X86::OR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010588 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010589 X86::NOT8r, X86::AL,
10590 X86::GR8RegisterClass);
10591 case X86::ATOMXOR8:
10592 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
10593 X86::XOR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010594 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010595 X86::NOT8r, X86::AL,
10596 X86::GR8RegisterClass);
10597 case X86::ATOMNAND8:
10598 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
10599 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010600 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010601 X86::NOT8r, X86::AL,
10602 X86::GR8RegisterClass, true);
10603 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010604 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +000010605 case X86::ATOMAND64:
10606 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010607 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010608 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010609 X86::NOT64r, X86::RAX,
10610 X86::GR64RegisterClass);
10611 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +000010612 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
10613 X86::OR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010614 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010615 X86::NOT64r, X86::RAX,
10616 X86::GR64RegisterClass);
10617 case X86::ATOMXOR64:
10618 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010619 X86::XOR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010620 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010621 X86::NOT64r, X86::RAX,
10622 X86::GR64RegisterClass);
10623 case X86::ATOMNAND64:
10624 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
10625 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010626 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010627 X86::NOT64r, X86::RAX,
10628 X86::GR64RegisterClass, true);
10629 case X86::ATOMMIN64:
10630 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
10631 case X86::ATOMMAX64:
10632 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
10633 case X86::ATOMUMIN64:
10634 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
10635 case X86::ATOMUMAX64:
10636 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010637
10638 // This group does 64-bit operations on a 32-bit host.
10639 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010640 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010641 X86::AND32rr, X86::AND32rr,
10642 X86::AND32ri, X86::AND32ri,
10643 false);
10644 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010645 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010646 X86::OR32rr, X86::OR32rr,
10647 X86::OR32ri, X86::OR32ri,
10648 false);
10649 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010650 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010651 X86::XOR32rr, X86::XOR32rr,
10652 X86::XOR32ri, X86::XOR32ri,
10653 false);
10654 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010655 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010656 X86::AND32rr, X86::AND32rr,
10657 X86::AND32ri, X86::AND32ri,
10658 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010659 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010660 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010661 X86::ADD32rr, X86::ADC32rr,
10662 X86::ADD32ri, X86::ADC32ri,
10663 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010664 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010665 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010666 X86::SUB32rr, X86::SBB32rr,
10667 X86::SUB32ri, X86::SBB32ri,
10668 false);
Dale Johannesen880ae362008-10-03 22:25:52 +000010669 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010670 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +000010671 X86::MOV32rr, X86::MOV32rr,
10672 X86::MOV32ri, X86::MOV32ri,
10673 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010674 case X86::VASTART_SAVE_XMM_REGS:
10675 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000010676
10677 case X86::VAARG_64:
10678 return EmitVAARG64WithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000010679 }
10680}
10681
10682//===----------------------------------------------------------------------===//
10683// X86 Optimization Hooks
10684//===----------------------------------------------------------------------===//
10685
Dan Gohman475871a2008-07-27 21:46:04 +000010686void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +000010687 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000010688 APInt &KnownZero,
10689 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000010690 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000010691 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010692 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000010693 assert((Opc >= ISD::BUILTIN_OP_END ||
10694 Opc == ISD::INTRINSIC_WO_CHAIN ||
10695 Opc == ISD::INTRINSIC_W_CHAIN ||
10696 Opc == ISD::INTRINSIC_VOID) &&
10697 "Should use MaskedValueIsZero if you don't know whether Op"
10698 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010699
Dan Gohmanf4f92f52008-02-13 23:07:24 +000010700 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010701 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000010702 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000010703 case X86ISD::ADD:
10704 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000010705 case X86ISD::ADC:
10706 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000010707 case X86ISD::SMUL:
10708 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000010709 case X86ISD::INC:
10710 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000010711 case X86ISD::OR:
10712 case X86ISD::XOR:
10713 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000010714 // These nodes' second result is a boolean.
10715 if (Op.getResNo() == 0)
10716 break;
10717 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010718 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000010719 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
10720 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000010721 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010722 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010723}
Chris Lattner259e97c2006-01-31 19:43:35 +000010724
Owen Andersonbc146b02010-09-21 20:42:50 +000010725unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
10726 unsigned Depth) const {
10727 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
10728 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
10729 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000010730
Owen Andersonbc146b02010-09-21 20:42:50 +000010731 // Fallback case.
10732 return 1;
10733}
10734
Evan Cheng206ee9d2006-07-07 08:33:52 +000010735/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000010736/// node is a GlobalAddress + offset.
10737bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000010738 const GlobalValue* &GA,
10739 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000010740 if (N->getOpcode() == X86ISD::Wrapper) {
10741 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000010742 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000010743 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000010744 return true;
10745 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000010746 }
Evan Chengad4196b2008-05-12 19:56:52 +000010747 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000010748}
10749
Evan Cheng206ee9d2006-07-07 08:33:52 +000010750/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
10751/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
10752/// if the load addresses are consecutive, non-overlapping, and in the right
Nate Begemanfdea31a2010-03-24 20:49:50 +000010753/// order.
Dan Gohman475871a2008-07-27 21:46:04 +000010754static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Mon P Wanga0fd0d52010-12-19 23:55:53 +000010755 TargetLowering::DAGCombinerInfo &DCI) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000010756 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000010757 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000010758
Eli Friedman7a5e5552009-06-07 06:52:44 +000010759 if (VT.getSizeInBits() != 128)
10760 return SDValue();
10761
Mon P Wanga0fd0d52010-12-19 23:55:53 +000010762 // Don't create instructions with illegal types after legalize types has run.
10763 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10764 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
10765 return SDValue();
10766
Nate Begemanfdea31a2010-03-24 20:49:50 +000010767 SmallVector<SDValue, 16> Elts;
10768 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000010769 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000010770
Nate Begemanfdea31a2010-03-24 20:49:50 +000010771 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000010772}
Evan Chengd880b972008-05-09 21:53:03 +000010773
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000010774/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
10775/// generation and convert it from being a bunch of shuffles and extracts
10776/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010777static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
10778 const TargetLowering &TLI) {
10779 SDValue InputVector = N->getOperand(0);
10780
10781 // Only operate on vectors of 4 elements, where the alternative shuffling
10782 // gets to be more expensive.
10783 if (InputVector.getValueType() != MVT::v4i32)
10784 return SDValue();
10785
10786 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
10787 // single use which is a sign-extend or zero-extend, and all elements are
10788 // used.
10789 SmallVector<SDNode *, 4> Uses;
10790 unsigned ExtractedElements = 0;
10791 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
10792 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
10793 if (UI.getUse().getResNo() != InputVector.getResNo())
10794 return SDValue();
10795
10796 SDNode *Extract = *UI;
10797 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
10798 return SDValue();
10799
10800 if (Extract->getValueType(0) != MVT::i32)
10801 return SDValue();
10802 if (!Extract->hasOneUse())
10803 return SDValue();
10804 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
10805 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
10806 return SDValue();
10807 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
10808 return SDValue();
10809
10810 // Record which element was extracted.
10811 ExtractedElements |=
10812 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
10813
10814 Uses.push_back(Extract);
10815 }
10816
10817 // If not all the elements were used, this may not be worthwhile.
10818 if (ExtractedElements != 15)
10819 return SDValue();
10820
10821 // Ok, we've now decided to do the transformation.
10822 DebugLoc dl = InputVector.getDebugLoc();
10823
10824 // Store the value to a temporary stack slot.
10825 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000010826 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
10827 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010828
10829 // Replace each use (extract) with a load of the appropriate element.
10830 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
10831 UE = Uses.end(); UI != UE; ++UI) {
10832 SDNode *Extract = *UI;
10833
10834 // Compute the element's address.
10835 SDValue Idx = Extract->getOperand(1);
10836 unsigned EltSize =
10837 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
10838 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
10839 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
10840
Eric Christopher90eb4022010-07-22 00:26:08 +000010841 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(),
Chris Lattner51abfe42010-09-21 06:02:19 +000010842 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010843
10844 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000010845 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000010846 ScalarAddr, MachinePointerInfo(),
10847 false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010848
10849 // Replace the exact with the load.
10850 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
10851 }
10852
10853 // The replacement was made in place; don't return anything.
10854 return SDValue();
10855}
10856
Chris Lattner83e6c992006-10-04 06:57:07 +000010857/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000010858static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +000010859 const X86Subtarget *Subtarget) {
10860 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000010861 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000010862 // Get the LHS/RHS of the select.
10863 SDValue LHS = N->getOperand(1);
10864 SDValue RHS = N->getOperand(2);
Eric Christopherfd179292009-08-27 18:07:15 +000010865
Dan Gohman670e5392009-09-21 18:03:22 +000010866 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000010867 // instructions match the semantics of the common C idiom x<y?x:y but not
10868 // x<=y?x:y, because of how they handle negative zero (which can be
10869 // ignored in unsafe-math mode).
Chris Lattner83e6c992006-10-04 06:57:07 +000010870 if (Subtarget->hasSSE2() &&
Owen Anderson825b72b2009-08-11 20:47:22 +000010871 (LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64) &&
Chris Lattner47b4ce82009-03-11 05:48:52 +000010872 Cond.getOpcode() == ISD::SETCC) {
10873 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010874
Chris Lattner47b4ce82009-03-11 05:48:52 +000010875 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000010876 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000010877 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
10878 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000010879 switch (CC) {
10880 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000010881 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000010882 // Converting this to a min would handle NaNs incorrectly, and swapping
10883 // the operands would cause it to handle comparisons between positive
10884 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000010885 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000010886 if (!UnsafeFPMath &&
10887 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
10888 break;
10889 std::swap(LHS, RHS);
10890 }
Dan Gohman670e5392009-09-21 18:03:22 +000010891 Opcode = X86ISD::FMIN;
10892 break;
10893 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000010894 // Converting this to a min would handle comparisons between positive
10895 // and negative zero incorrectly.
10896 if (!UnsafeFPMath &&
10897 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
10898 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010899 Opcode = X86ISD::FMIN;
10900 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000010901 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000010902 // Converting this to a min would handle both negative zeros and NaNs
10903 // incorrectly, but we can swap the operands to fix both.
10904 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000010905 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010906 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000010907 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010908 Opcode = X86ISD::FMIN;
10909 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010910
Dan Gohman670e5392009-09-21 18:03:22 +000010911 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000010912 // Converting this to a max would handle comparisons between positive
10913 // and negative zero incorrectly.
10914 if (!UnsafeFPMath &&
10915 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(LHS))
10916 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010917 Opcode = X86ISD::FMAX;
10918 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000010919 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000010920 // Converting this to a max would handle NaNs incorrectly, and swapping
10921 // the operands would cause it to handle comparisons between positive
10922 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000010923 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000010924 if (!UnsafeFPMath &&
10925 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
10926 break;
10927 std::swap(LHS, RHS);
10928 }
Dan Gohman670e5392009-09-21 18:03:22 +000010929 Opcode = X86ISD::FMAX;
10930 break;
10931 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000010932 // Converting this to a max would handle both negative zeros and NaNs
10933 // incorrectly, but we can swap the operands to fix both.
10934 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000010935 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010936 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010937 case ISD::SETGE:
10938 Opcode = X86ISD::FMAX;
10939 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000010940 }
Dan Gohman670e5392009-09-21 18:03:22 +000010941 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000010942 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
10943 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000010944 switch (CC) {
10945 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000010946 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000010947 // Converting this to a min would handle comparisons between positive
10948 // and negative zero incorrectly, and swapping the operands would
10949 // cause it to handle NaNs incorrectly.
10950 if (!UnsafeFPMath &&
10951 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000010952 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000010953 break;
10954 std::swap(LHS, RHS);
10955 }
Dan Gohman670e5392009-09-21 18:03:22 +000010956 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000010957 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010958 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000010959 // Converting this to a min would handle NaNs incorrectly.
10960 if (!UnsafeFPMath &&
10961 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
10962 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010963 Opcode = X86ISD::FMIN;
10964 break;
10965 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000010966 // Converting this to a min would handle both negative zeros and NaNs
10967 // incorrectly, but we can swap the operands to fix both.
10968 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000010969 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010970 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010971 case ISD::SETGE:
10972 Opcode = X86ISD::FMIN;
10973 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010974
Dan Gohman670e5392009-09-21 18:03:22 +000010975 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000010976 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000010977 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000010978 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010979 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000010980 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010981 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000010982 // Converting this to a max would handle comparisons between positive
10983 // and negative zero incorrectly, and swapping the operands would
10984 // cause it to handle NaNs incorrectly.
10985 if (!UnsafeFPMath &&
10986 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000010987 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000010988 break;
10989 std::swap(LHS, RHS);
10990 }
Dan Gohman670e5392009-09-21 18:03:22 +000010991 Opcode = X86ISD::FMAX;
10992 break;
10993 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000010994 // Converting this to a max would handle both negative zeros and NaNs
10995 // incorrectly, but we can swap the operands to fix both.
10996 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000010997 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010998 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000010999 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011000 Opcode = X86ISD::FMAX;
11001 break;
11002 }
Chris Lattner83e6c992006-10-04 06:57:07 +000011003 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011004
Chris Lattner47b4ce82009-03-11 05:48:52 +000011005 if (Opcode)
11006 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000011007 }
Eric Christopherfd179292009-08-27 18:07:15 +000011008
Chris Lattnerd1980a52009-03-12 06:52:53 +000011009 // If this is a select between two integer constants, try to do some
11010 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000011011 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
11012 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000011013 // Don't do this for crazy integer types.
11014 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
11015 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000011016 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000011017 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000011018
Chris Lattnercee56e72009-03-13 05:53:31 +000011019 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000011020 // Efficiently invertible.
11021 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
11022 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
11023 isa<ConstantSDNode>(Cond.getOperand(1))))) {
11024 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000011025 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000011026 }
Eric Christopherfd179292009-08-27 18:07:15 +000011027
Chris Lattnerd1980a52009-03-12 06:52:53 +000011028 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000011029 if (FalseC->getAPIntValue() == 0 &&
11030 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000011031 if (NeedsCondInvert) // Invert the condition if needed.
11032 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
11033 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011034
Chris Lattnerd1980a52009-03-12 06:52:53 +000011035 // Zero extend the condition if needed.
11036 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011037
Chris Lattnercee56e72009-03-13 05:53:31 +000011038 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000011039 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000011040 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000011041 }
Eric Christopherfd179292009-08-27 18:07:15 +000011042
Chris Lattner97a29a52009-03-13 05:22:11 +000011043 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000011044 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000011045 if (NeedsCondInvert) // Invert the condition if needed.
11046 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
11047 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011048
Chris Lattner97a29a52009-03-13 05:22:11 +000011049 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000011050 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
11051 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000011052 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000011053 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000011054 }
Eric Christopherfd179292009-08-27 18:07:15 +000011055
Chris Lattnercee56e72009-03-13 05:53:31 +000011056 // Optimize cases that will turn into an LEA instruction. This requires
11057 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000011058 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000011059 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000011060 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000011061
Chris Lattnercee56e72009-03-13 05:53:31 +000011062 bool isFastMultiplier = false;
11063 if (Diff < 10) {
11064 switch ((unsigned char)Diff) {
11065 default: break;
11066 case 1: // result = add base, cond
11067 case 2: // result = lea base( , cond*2)
11068 case 3: // result = lea base(cond, cond*2)
11069 case 4: // result = lea base( , cond*4)
11070 case 5: // result = lea base(cond, cond*4)
11071 case 8: // result = lea base( , cond*8)
11072 case 9: // result = lea base(cond, cond*8)
11073 isFastMultiplier = true;
11074 break;
11075 }
11076 }
Eric Christopherfd179292009-08-27 18:07:15 +000011077
Chris Lattnercee56e72009-03-13 05:53:31 +000011078 if (isFastMultiplier) {
11079 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
11080 if (NeedsCondInvert) // Invert the condition if needed.
11081 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
11082 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011083
Chris Lattnercee56e72009-03-13 05:53:31 +000011084 // Zero extend the condition if needed.
11085 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
11086 Cond);
11087 // Scale the condition by the difference.
11088 if (Diff != 1)
11089 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
11090 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011091
Chris Lattnercee56e72009-03-13 05:53:31 +000011092 // Add the base if non-zero.
11093 if (FalseC->getAPIntValue() != 0)
11094 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
11095 SDValue(FalseC, 0));
11096 return Cond;
11097 }
Eric Christopherfd179292009-08-27 18:07:15 +000011098 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000011099 }
11100 }
Eric Christopherfd179292009-08-27 18:07:15 +000011101
Dan Gohman475871a2008-07-27 21:46:04 +000011102 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000011103}
11104
Chris Lattnerd1980a52009-03-12 06:52:53 +000011105/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
11106static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
11107 TargetLowering::DAGCombinerInfo &DCI) {
11108 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000011109
Chris Lattnerd1980a52009-03-12 06:52:53 +000011110 // If the flag operand isn't dead, don't touch this CMOV.
11111 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
11112 return SDValue();
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. Note that the operands are ordered the opposite of SELECT
11116 // operands.
11117 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
11118 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
11119 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
11120 // larger than FalseC (the false value).
11121 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
Eric Christopherfd179292009-08-27 18:07:15 +000011122
Chris Lattnerd1980a52009-03-12 06:52:53 +000011123 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
11124 CC = X86::GetOppositeBranchCondition(CC);
11125 std::swap(TrueC, FalseC);
11126 }
Eric Christopherfd179292009-08-27 18:07:15 +000011127
Chris Lattnerd1980a52009-03-12 06:52:53 +000011128 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000011129 // This is efficient for any integer data type (including i8/i16) and
11130 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000011131 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
11132 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +000011133 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11134 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011135
Chris Lattnerd1980a52009-03-12 06:52:53 +000011136 // Zero extend the condition if needed.
11137 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011138
Chris Lattnerd1980a52009-03-12 06:52:53 +000011139 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
11140 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000011141 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000011142 if (N->getNumValues() == 2) // Dead flag value?
11143 return DCI.CombineTo(N, Cond, SDValue());
11144 return Cond;
11145 }
Eric Christopherfd179292009-08-27 18:07:15 +000011146
Chris Lattnercee56e72009-03-13 05:53:31 +000011147 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
11148 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000011149 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
11150 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +000011151 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11152 DAG.getConstant(CC, MVT::i8), Cond);
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 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
11158 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000011159
Chris Lattner97a29a52009-03-13 05:22:11 +000011160 if (N->getNumValues() == 2) // Dead flag value?
11161 return DCI.CombineTo(N, Cond, SDValue());
11162 return Cond;
11163 }
Eric Christopherfd179292009-08-27 18:07:15 +000011164
Chris Lattnercee56e72009-03-13 05:53:31 +000011165 // Optimize cases that will turn into an LEA instruction. This requires
11166 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000011167 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000011168 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000011169 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000011170
Chris Lattnercee56e72009-03-13 05:53:31 +000011171 bool isFastMultiplier = false;
11172 if (Diff < 10) {
11173 switch ((unsigned char)Diff) {
11174 default: break;
11175 case 1: // result = add base, cond
11176 case 2: // result = lea base( , cond*2)
11177 case 3: // result = lea base(cond, cond*2)
11178 case 4: // result = lea base( , cond*4)
11179 case 5: // result = lea base(cond, cond*4)
11180 case 8: // result = lea base( , cond*8)
11181 case 9: // result = lea base(cond, cond*8)
11182 isFastMultiplier = true;
11183 break;
11184 }
11185 }
Eric Christopherfd179292009-08-27 18:07:15 +000011186
Chris Lattnercee56e72009-03-13 05:53:31 +000011187 if (isFastMultiplier) {
11188 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
11189 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +000011190 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11191 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000011192 // Zero extend the condition if needed.
11193 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
11194 Cond);
11195 // Scale the condition by the difference.
11196 if (Diff != 1)
11197 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
11198 DAG.getConstant(Diff, Cond.getValueType()));
11199
11200 // Add the base if non-zero.
11201 if (FalseC->getAPIntValue() != 0)
11202 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
11203 SDValue(FalseC, 0));
11204 if (N->getNumValues() == 2) // Dead flag value?
11205 return DCI.CombineTo(N, Cond, SDValue());
11206 return Cond;
11207 }
Eric Christopherfd179292009-08-27 18:07:15 +000011208 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000011209 }
11210 }
11211 return SDValue();
11212}
11213
11214
Evan Cheng0b0cd912009-03-28 05:57:29 +000011215/// PerformMulCombine - Optimize a single multiply with constant into two
11216/// in order to implement it with two cheaper instructions, e.g.
11217/// LEA + SHL, LEA + LEA.
11218static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
11219 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000011220 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
11221 return SDValue();
11222
Owen Andersone50ed302009-08-10 22:56:29 +000011223 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000011224 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000011225 return SDValue();
11226
11227 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
11228 if (!C)
11229 return SDValue();
11230 uint64_t MulAmt = C->getZExtValue();
11231 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
11232 return SDValue();
11233
11234 uint64_t MulAmt1 = 0;
11235 uint64_t MulAmt2 = 0;
11236 if ((MulAmt % 9) == 0) {
11237 MulAmt1 = 9;
11238 MulAmt2 = MulAmt / 9;
11239 } else if ((MulAmt % 5) == 0) {
11240 MulAmt1 = 5;
11241 MulAmt2 = MulAmt / 5;
11242 } else if ((MulAmt % 3) == 0) {
11243 MulAmt1 = 3;
11244 MulAmt2 = MulAmt / 3;
11245 }
11246 if (MulAmt2 &&
11247 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
11248 DebugLoc DL = N->getDebugLoc();
11249
11250 if (isPowerOf2_64(MulAmt2) &&
11251 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
11252 // If second multiplifer is pow2, issue it first. We want the multiply by
11253 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
11254 // is an add.
11255 std::swap(MulAmt1, MulAmt2);
11256
11257 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000011258 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000011259 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000011260 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000011261 else
Evan Cheng73f24c92009-03-30 21:36:47 +000011262 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000011263 DAG.getConstant(MulAmt1, VT));
11264
Eric Christopherfd179292009-08-27 18:07:15 +000011265 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000011266 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000011267 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000011268 else
Evan Cheng73f24c92009-03-30 21:36:47 +000011269 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000011270 DAG.getConstant(MulAmt2, VT));
11271
11272 // Do not add new nodes to DAG combiner worklist.
11273 DCI.CombineTo(N, NewMul, false);
11274 }
11275 return SDValue();
11276}
11277
Evan Chengad9c0a32009-12-15 00:53:42 +000011278static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
11279 SDValue N0 = N->getOperand(0);
11280 SDValue N1 = N->getOperand(1);
11281 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
11282 EVT VT = N0.getValueType();
11283
11284 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
11285 // since the result of setcc_c is all zero's or all ones.
11286 if (N1C && N0.getOpcode() == ISD::AND &&
11287 N0.getOperand(1).getOpcode() == ISD::Constant) {
11288 SDValue N00 = N0.getOperand(0);
11289 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
11290 ((N00.getOpcode() == ISD::ANY_EXTEND ||
11291 N00.getOpcode() == ISD::ZERO_EXTEND) &&
11292 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
11293 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
11294 APInt ShAmt = N1C->getAPIntValue();
11295 Mask = Mask.shl(ShAmt);
11296 if (Mask != 0)
11297 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
11298 N00, DAG.getConstant(Mask, VT));
11299 }
11300 }
11301
11302 return SDValue();
11303}
Evan Cheng0b0cd912009-03-28 05:57:29 +000011304
Nate Begeman740ab032009-01-26 00:52:55 +000011305/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
11306/// when possible.
11307static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
11308 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000011309 EVT VT = N->getValueType(0);
11310 if (!VT.isVector() && VT.isInteger() &&
11311 N->getOpcode() == ISD::SHL)
11312 return PerformSHLCombine(N, DAG);
11313
Nate Begeman740ab032009-01-26 00:52:55 +000011314 // On X86 with SSE2 support, we can transform this to a vector shift if
11315 // all elements are shifted by the same amount. We can't do this in legalize
11316 // because the a constant vector is typically transformed to a constant pool
11317 // so we have no knowledge of the shift amount.
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011318 if (!Subtarget->hasSSE2())
11319 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000011320
Owen Anderson825b72b2009-08-11 20:47:22 +000011321 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011322 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000011323
Mon P Wang3becd092009-01-28 08:12:05 +000011324 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000011325 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000011326 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000011327 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000011328 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
11329 unsigned NumElts = VT.getVectorNumElements();
11330 unsigned i = 0;
11331 for (; i != NumElts; ++i) {
11332 SDValue Arg = ShAmtOp.getOperand(i);
11333 if (Arg.getOpcode() == ISD::UNDEF) continue;
11334 BaseShAmt = Arg;
11335 break;
11336 }
11337 for (; i != NumElts; ++i) {
11338 SDValue Arg = ShAmtOp.getOperand(i);
11339 if (Arg.getOpcode() == ISD::UNDEF) continue;
11340 if (Arg != BaseShAmt) {
11341 return SDValue();
11342 }
11343 }
11344 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000011345 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000011346 SDValue InVec = ShAmtOp.getOperand(0);
11347 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
11348 unsigned NumElts = InVec.getValueType().getVectorNumElements();
11349 unsigned i = 0;
11350 for (; i != NumElts; ++i) {
11351 SDValue Arg = InVec.getOperand(i);
11352 if (Arg.getOpcode() == ISD::UNDEF) continue;
11353 BaseShAmt = Arg;
11354 break;
11355 }
11356 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
11357 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000011358 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000011359 if (C->getZExtValue() == SplatIdx)
11360 BaseShAmt = InVec.getOperand(1);
11361 }
11362 }
11363 if (BaseShAmt.getNode() == 0)
11364 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
11365 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +000011366 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011367 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000011368
Mon P Wangefa42202009-09-03 19:56:25 +000011369 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000011370 if (EltVT.bitsGT(MVT::i32))
11371 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
11372 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000011373 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000011374
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011375 // The shift amount is identical so we can do a vector shift.
11376 SDValue ValOp = N->getOperand(0);
11377 switch (N->getOpcode()) {
11378 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000011379 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011380 break;
11381 case ISD::SHL:
Owen Anderson825b72b2009-08-11 20:47:22 +000011382 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011383 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011384 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011385 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011386 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011387 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011388 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011389 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011390 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011391 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011392 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011393 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011394 break;
11395 case ISD::SRA:
Owen Anderson825b72b2009-08-11 20:47:22 +000011396 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011397 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011398 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011399 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011400 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011401 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011402 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011403 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011404 break;
11405 case ISD::SRL:
Owen Anderson825b72b2009-08-11 20:47:22 +000011406 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011407 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011408 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011409 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011410 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011411 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011412 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011413 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011414 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011415 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011416 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011417 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011418 break;
Nate Begeman740ab032009-01-26 00:52:55 +000011419 }
11420 return SDValue();
11421}
11422
Nate Begemanb65c1752010-12-17 22:55:37 +000011423
11424static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
11425 TargetLowering::DAGCombinerInfo &DCI,
11426 const X86Subtarget *Subtarget) {
11427 if (DCI.isBeforeLegalizeOps())
11428 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011429
Nate Begemanb65c1752010-12-17 22:55:37 +000011430 // Want to form PANDN nodes, in the hopes of then easily combining them with
11431 // OR and AND nodes to form PBLEND/PSIGN.
11432 EVT VT = N->getValueType(0);
11433 if (VT != MVT::v2i64)
11434 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011435
Nate Begemanb65c1752010-12-17 22:55:37 +000011436 SDValue N0 = N->getOperand(0);
11437 SDValue N1 = N->getOperand(1);
11438 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011439
Nate Begemanb65c1752010-12-17 22:55:37 +000011440 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011441 if (N0.getOpcode() == ISD::XOR &&
Nate Begemanb65c1752010-12-17 22:55:37 +000011442 ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
11443 return DAG.getNode(X86ISD::PANDN, DL, VT, N0.getOperand(0), N1);
11444
11445 // Check RHS for vnot
11446 if (N1.getOpcode() == ISD::XOR &&
11447 ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
11448 return DAG.getNode(X86ISD::PANDN, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011449
Nate Begemanb65c1752010-12-17 22:55:37 +000011450 return SDValue();
11451}
11452
Evan Cheng760d1942010-01-04 21:22:48 +000011453static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000011454 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000011455 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +000011456 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000011457 return SDValue();
11458
Evan Cheng760d1942010-01-04 21:22:48 +000011459 EVT VT = N->getValueType(0);
Nate Begemanb65c1752010-12-17 22:55:37 +000011460 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64 && VT != MVT::v2i64)
Evan Cheng760d1942010-01-04 21:22:48 +000011461 return SDValue();
11462
Evan Cheng760d1942010-01-04 21:22:48 +000011463 SDValue N0 = N->getOperand(0);
11464 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011465
Nate Begemanb65c1752010-12-17 22:55:37 +000011466 // look for psign/blend
11467 if (Subtarget->hasSSSE3()) {
11468 if (VT == MVT::v2i64) {
11469 // Canonicalize pandn to RHS
11470 if (N0.getOpcode() == X86ISD::PANDN)
11471 std::swap(N0, N1);
11472 // or (and (m, x), (pandn m, y))
11473 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::PANDN) {
11474 SDValue Mask = N1.getOperand(0);
11475 SDValue X = N1.getOperand(1);
11476 SDValue Y;
11477 if (N0.getOperand(0) == Mask)
11478 Y = N0.getOperand(1);
11479 if (N0.getOperand(1) == Mask)
11480 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011481
Nate Begemanb65c1752010-12-17 22:55:37 +000011482 // Check to see if the mask appeared in both the AND and PANDN and
11483 if (!Y.getNode())
11484 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011485
Nate Begemanb65c1752010-12-17 22:55:37 +000011486 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
11487 if (Mask.getOpcode() != ISD::BITCAST ||
11488 X.getOpcode() != ISD::BITCAST ||
11489 Y.getOpcode() != ISD::BITCAST)
11490 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011491
Nate Begemanb65c1752010-12-17 22:55:37 +000011492 // Look through mask bitcast.
11493 Mask = Mask.getOperand(0);
11494 EVT MaskVT = Mask.getValueType();
11495
11496 // Validate that the Mask operand is a vector sra node. The sra node
11497 // will be an intrinsic.
11498 if (Mask.getOpcode() != ISD::INTRINSIC_WO_CHAIN)
11499 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011500
Nate Begemanb65c1752010-12-17 22:55:37 +000011501 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
11502 // there is no psrai.b
11503 switch (cast<ConstantSDNode>(Mask.getOperand(0))->getZExtValue()) {
11504 case Intrinsic::x86_sse2_psrai_w:
11505 case Intrinsic::x86_sse2_psrai_d:
11506 break;
11507 default: return SDValue();
11508 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011509
Nate Begemanb65c1752010-12-17 22:55:37 +000011510 // Check that the SRA is all signbits.
11511 SDValue SraC = Mask.getOperand(2);
11512 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
11513 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
11514 if ((SraAmt + 1) != EltBits)
11515 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011516
Nate Begemanb65c1752010-12-17 22:55:37 +000011517 DebugLoc DL = N->getDebugLoc();
11518
11519 // Now we know we at least have a plendvb with the mask val. See if
11520 // we can form a psignb/w/d.
11521 // psign = x.type == y.type == mask.type && y = sub(0, x);
11522 X = X.getOperand(0);
11523 Y = Y.getOperand(0);
11524 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
11525 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
11526 X.getValueType() == MaskVT && X.getValueType() == Y.getValueType()){
11527 unsigned Opc = 0;
11528 switch (EltBits) {
11529 case 8: Opc = X86ISD::PSIGNB; break;
11530 case 16: Opc = X86ISD::PSIGNW; break;
11531 case 32: Opc = X86ISD::PSIGND; break;
11532 default: break;
11533 }
11534 if (Opc) {
11535 SDValue Sign = DAG.getNode(Opc, DL, MaskVT, X, Mask.getOperand(1));
11536 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Sign);
11537 }
11538 }
11539 // PBLENDVB only available on SSE 4.1
11540 if (!Subtarget->hasSSE41())
11541 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011542
Nate Begemanb65c1752010-12-17 22:55:37 +000011543 X = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, X);
11544 Y = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Y);
11545 Mask = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Mask);
Nate Begeman672fb622010-12-20 22:04:24 +000011546 Mask = DAG.getNode(X86ISD::PBLENDVB, DL, MVT::v16i8, X, Y, Mask);
Nate Begemanb65c1752010-12-17 22:55:37 +000011547 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Mask);
11548 }
11549 }
11550 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011551
Nate Begemanb65c1752010-12-17 22:55:37 +000011552 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000011553 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
11554 std::swap(N0, N1);
11555 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
11556 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000011557 if (!N0.hasOneUse() || !N1.hasOneUse())
11558 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000011559
11560 SDValue ShAmt0 = N0.getOperand(1);
11561 if (ShAmt0.getValueType() != MVT::i8)
11562 return SDValue();
11563 SDValue ShAmt1 = N1.getOperand(1);
11564 if (ShAmt1.getValueType() != MVT::i8)
11565 return SDValue();
11566 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
11567 ShAmt0 = ShAmt0.getOperand(0);
11568 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
11569 ShAmt1 = ShAmt1.getOperand(0);
11570
11571 DebugLoc DL = N->getDebugLoc();
11572 unsigned Opc = X86ISD::SHLD;
11573 SDValue Op0 = N0.getOperand(0);
11574 SDValue Op1 = N1.getOperand(0);
11575 if (ShAmt0.getOpcode() == ISD::SUB) {
11576 Opc = X86ISD::SHRD;
11577 std::swap(Op0, Op1);
11578 std::swap(ShAmt0, ShAmt1);
11579 }
11580
Evan Cheng8b1190a2010-04-28 01:18:01 +000011581 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000011582 if (ShAmt1.getOpcode() == ISD::SUB) {
11583 SDValue Sum = ShAmt1.getOperand(0);
11584 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000011585 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
11586 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
11587 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
11588 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000011589 return DAG.getNode(Opc, DL, VT,
11590 Op0, Op1,
11591 DAG.getNode(ISD::TRUNCATE, DL,
11592 MVT::i8, ShAmt0));
11593 }
11594 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
11595 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
11596 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000011597 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000011598 return DAG.getNode(Opc, DL, VT,
11599 N0.getOperand(0), N1.getOperand(0),
11600 DAG.getNode(ISD::TRUNCATE, DL,
11601 MVT::i8, ShAmt0));
11602 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011603
Evan Cheng760d1942010-01-04 21:22:48 +000011604 return SDValue();
11605}
11606
Chris Lattner149a4e52008-02-22 02:09:43 +000011607/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000011608static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000011609 const X86Subtarget *Subtarget) {
Chris Lattner149a4e52008-02-22 02:09:43 +000011610 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
11611 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000011612 // A preferable solution to the general problem is to figure out the right
11613 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000011614
11615 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng7e2ff772008-05-08 00:57:18 +000011616 StoreSDNode *St = cast<StoreSDNode>(N);
Owen Andersone50ed302009-08-10 22:56:29 +000011617 EVT VT = St->getValue().getValueType();
Evan Cheng536e6672009-03-12 05:59:15 +000011618 if (VT.getSizeInBits() != 64)
11619 return SDValue();
11620
Devang Patel578efa92009-06-05 21:57:13 +000011621 const Function *F = DAG.getMachineFunction().getFunction();
11622 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Eric Christopherfd179292009-08-27 18:07:15 +000011623 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
Devang Patel578efa92009-06-05 21:57:13 +000011624 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +000011625 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000011626 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000011627 isa<LoadSDNode>(St->getValue()) &&
11628 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
11629 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000011630 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011631 LoadSDNode *Ld = 0;
11632 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000011633 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000011634 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011635 // Must be a store of a load. We currently handle two cases: the load
11636 // is a direct child, and it's under an intervening TokenFactor. It is
11637 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000011638 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000011639 Ld = cast<LoadSDNode>(St->getChain());
11640 else if (St->getValue().hasOneUse() &&
11641 ChainVal->getOpcode() == ISD::TokenFactor) {
11642 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000011643 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000011644 TokenFactorIndex = i;
11645 Ld = cast<LoadSDNode>(St->getValue());
11646 } else
11647 Ops.push_back(ChainVal->getOperand(i));
11648 }
11649 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000011650
Evan Cheng536e6672009-03-12 05:59:15 +000011651 if (!Ld || !ISD::isNormalLoad(Ld))
11652 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011653
Evan Cheng536e6672009-03-12 05:59:15 +000011654 // If this is not the MMX case, i.e. we are just turning i64 load/store
11655 // into f64 load/store, avoid the transformation if there are multiple
11656 // uses of the loaded value.
11657 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
11658 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011659
Evan Cheng536e6672009-03-12 05:59:15 +000011660 DebugLoc LdDL = Ld->getDebugLoc();
11661 DebugLoc StDL = N->getDebugLoc();
11662 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
11663 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
11664 // pair instead.
11665 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000011666 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000011667 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
11668 Ld->getPointerInfo(), Ld->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000011669 Ld->isNonTemporal(), Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000011670 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000011671 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000011672 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000011673 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000011674 Ops.size());
11675 }
Evan Cheng536e6672009-03-12 05:59:15 +000011676 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000011677 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000011678 St->isVolatile(), St->isNonTemporal(),
11679 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000011680 }
Evan Cheng536e6672009-03-12 05:59:15 +000011681
11682 // Otherwise, lower to two pairs of 32-bit loads / stores.
11683 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000011684 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
11685 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000011686
Owen Anderson825b72b2009-08-11 20:47:22 +000011687 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000011688 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000011689 Ld->isVolatile(), Ld->isNonTemporal(),
11690 Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000011691 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000011692 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000011693 Ld->isVolatile(), Ld->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000011694 MinAlign(Ld->getAlignment(), 4));
11695
11696 SDValue NewChain = LoLd.getValue(1);
11697 if (TokenFactorIndex != -1) {
11698 Ops.push_back(LoLd);
11699 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000011700 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000011701 Ops.size());
11702 }
11703
11704 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000011705 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
11706 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000011707
11708 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000011709 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000011710 St->isVolatile(), St->isNonTemporal(),
11711 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000011712 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000011713 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000011714 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000011715 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000011716 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000011717 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000011718 }
Dan Gohman475871a2008-07-27 21:46:04 +000011719 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000011720}
11721
Chris Lattner6cf73262008-01-25 06:14:17 +000011722/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
11723/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000011724static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000011725 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
11726 // F[X]OR(0.0, x) -> x
11727 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000011728 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
11729 if (C->getValueAPF().isPosZero())
11730 return N->getOperand(1);
11731 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
11732 if (C->getValueAPF().isPosZero())
11733 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000011734 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000011735}
11736
11737/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000011738static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000011739 // FAND(0.0, x) -> 0.0
11740 // FAND(x, 0.0) -> 0.0
11741 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
11742 if (C->getValueAPF().isPosZero())
11743 return N->getOperand(0);
11744 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
11745 if (C->getValueAPF().isPosZero())
11746 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000011747 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000011748}
11749
Dan Gohmane5af2d32009-01-29 01:59:02 +000011750static SDValue PerformBTCombine(SDNode *N,
11751 SelectionDAG &DAG,
11752 TargetLowering::DAGCombinerInfo &DCI) {
11753 // BT ignores high bits in the bit index operand.
11754 SDValue Op1 = N->getOperand(1);
11755 if (Op1.hasOneUse()) {
11756 unsigned BitWidth = Op1.getValueSizeInBits();
11757 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
11758 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000011759 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
11760 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000011761 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000011762 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
11763 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
11764 DCI.CommitTargetLoweringOpt(TLO);
11765 }
11766 return SDValue();
11767}
Chris Lattner83e6c992006-10-04 06:57:07 +000011768
Eli Friedman7a5e5552009-06-07 06:52:44 +000011769static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
11770 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011771 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000011772 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000011773 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000011774 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000011775 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000011776 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011777 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000011778 }
11779 return SDValue();
11780}
11781
Evan Cheng2e489c42009-12-16 00:53:11 +000011782static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
11783 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
11784 // (and (i32 x86isd::setcc_carry), 1)
11785 // This eliminates the zext. This transformation is necessary because
11786 // ISD::SETCC is always legalized to i8.
11787 DebugLoc dl = N->getDebugLoc();
11788 SDValue N0 = N->getOperand(0);
11789 EVT VT = N->getValueType(0);
11790 if (N0.getOpcode() == ISD::AND &&
11791 N0.hasOneUse() &&
11792 N0.getOperand(0).hasOneUse()) {
11793 SDValue N00 = N0.getOperand(0);
11794 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
11795 return SDValue();
11796 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
11797 if (!C || C->getZExtValue() != 1)
11798 return SDValue();
11799 return DAG.getNode(ISD::AND, dl, VT,
11800 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
11801 N00.getOperand(0), N00.getOperand(1)),
11802 DAG.getConstant(1, VT));
11803 }
11804
11805 return SDValue();
11806}
11807
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011808// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
11809static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG) {
11810 unsigned X86CC = N->getConstantOperandVal(0);
11811 SDValue EFLAG = N->getOperand(1);
11812 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011813
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011814 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
11815 // a zext and produces an all-ones bit which is more useful than 0/1 in some
11816 // cases.
11817 if (X86CC == X86::COND_B)
11818 return DAG.getNode(ISD::AND, DL, MVT::i8,
11819 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
11820 DAG.getConstant(X86CC, MVT::i8), EFLAG),
11821 DAG.getConstant(1, MVT::i8));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011822
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011823 return SDValue();
11824}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011825
Chris Lattner23a01992010-12-20 01:37:09 +000011826// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
11827static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
11828 X86TargetLowering::DAGCombinerInfo &DCI) {
11829 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
11830 // the result is either zero or one (depending on the input carry bit).
11831 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
11832 if (X86::isZeroNode(N->getOperand(0)) &&
11833 X86::isZeroNode(N->getOperand(1)) &&
11834 // We don't have a good way to replace an EFLAGS use, so only do this when
11835 // dead right now.
11836 SDValue(N, 1).use_empty()) {
11837 DebugLoc DL = N->getDebugLoc();
11838 EVT VT = N->getValueType(0);
11839 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
11840 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
11841 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
11842 DAG.getConstant(X86::COND_B,MVT::i8),
11843 N->getOperand(2)),
11844 DAG.getConstant(1, VT));
11845 return DCI.CombineTo(N, Res1, CarryOut);
11846 }
11847
11848 return SDValue();
11849}
11850
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000011851// fold (add Y, (sete X, 0)) -> adc 0, Y
11852// (add Y, (setne X, 0)) -> sbb -1, Y
11853// (sub (sete X, 0), Y) -> sbb 0, Y
11854// (sub (setne X, 0), Y) -> adc -1, Y
11855static SDValue OptimizeConditonalInDecrement(SDNode *N, SelectionDAG &DAG) {
11856 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011857
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000011858 // Look through ZExts.
11859 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
11860 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
11861 return SDValue();
11862
11863 SDValue SetCC = Ext.getOperand(0);
11864 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
11865 return SDValue();
11866
11867 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
11868 if (CC != X86::COND_E && CC != X86::COND_NE)
11869 return SDValue();
11870
11871 SDValue Cmp = SetCC.getOperand(1);
11872 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000011873 !X86::isZeroNode(Cmp.getOperand(1)) ||
11874 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000011875 return SDValue();
11876
11877 SDValue CmpOp0 = Cmp.getOperand(0);
11878 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
11879 DAG.getConstant(1, CmpOp0.getValueType()));
11880
11881 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
11882 if (CC == X86::COND_NE)
11883 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
11884 DL, OtherVal.getValueType(), OtherVal,
11885 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
11886 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
11887 DL, OtherVal.getValueType(), OtherVal,
11888 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
11889}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011890
Dan Gohman475871a2008-07-27 21:46:04 +000011891SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000011892 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000011893 SelectionDAG &DAG = DCI.DAG;
11894 switch (N->getOpcode()) {
11895 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000011896 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011897 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, *this);
Chris Lattneraf723b92008-01-25 05:46:26 +000011898 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +000011899 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000011900 case ISD::ADD:
11901 case ISD::SUB: return OptimizeConditonalInDecrement(N, DAG);
Chris Lattner23a01992010-12-20 01:37:09 +000011902 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000011903 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000011904 case ISD::SHL:
11905 case ISD::SRA:
11906 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000011907 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000011908 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000011909 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +000011910 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000011911 case X86ISD::FOR: return PerformFORCombine(N, DAG);
11912 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000011913 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000011914 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Evan Cheng2e489c42009-12-16 00:53:11 +000011915 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG);
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011916 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000011917 case X86ISD::SHUFPS: // Handle all target specific shuffles
11918 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000011919 case X86ISD::PALIGN:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000011920 case X86ISD::PUNPCKHBW:
11921 case X86ISD::PUNPCKHWD:
11922 case X86ISD::PUNPCKHDQ:
11923 case X86ISD::PUNPCKHQDQ:
11924 case X86ISD::UNPCKHPS:
11925 case X86ISD::UNPCKHPD:
11926 case X86ISD::PUNPCKLBW:
11927 case X86ISD::PUNPCKLWD:
11928 case X86ISD::PUNPCKLDQ:
11929 case X86ISD::PUNPCKLQDQ:
11930 case X86ISD::UNPCKLPS:
11931 case X86ISD::UNPCKLPD:
11932 case X86ISD::MOVHLPS:
11933 case X86ISD::MOVLHPS:
11934 case X86ISD::PSHUFD:
11935 case X86ISD::PSHUFHW:
11936 case X86ISD::PSHUFLW:
11937 case X86ISD::MOVSS:
11938 case X86ISD::MOVSD:
Mon P Wanga0fd0d52010-12-19 23:55:53 +000011939 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI);
Evan Cheng206ee9d2006-07-07 08:33:52 +000011940 }
11941
Dan Gohman475871a2008-07-27 21:46:04 +000011942 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000011943}
11944
Evan Chenge5b51ac2010-04-17 06:13:15 +000011945/// isTypeDesirableForOp - Return true if the target has native support for
11946/// the specified value type and it is 'desirable' to use the type for the
11947/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
11948/// instruction encodings are longer and some i16 instructions are slow.
11949bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
11950 if (!isTypeLegal(VT))
11951 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000011952 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000011953 return true;
11954
11955 switch (Opc) {
11956 default:
11957 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000011958 case ISD::LOAD:
11959 case ISD::SIGN_EXTEND:
11960 case ISD::ZERO_EXTEND:
11961 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000011962 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000011963 case ISD::SRL:
11964 case ISD::SUB:
11965 case ISD::ADD:
11966 case ISD::MUL:
11967 case ISD::AND:
11968 case ISD::OR:
11969 case ISD::XOR:
11970 return false;
11971 }
11972}
11973
11974/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000011975/// beneficial for dag combiner to promote the specified node. If true, it
11976/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000011977bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000011978 EVT VT = Op.getValueType();
11979 if (VT != MVT::i16)
11980 return false;
11981
Evan Cheng4c26e932010-04-19 19:29:22 +000011982 bool Promote = false;
11983 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000011984 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000011985 default: break;
11986 case ISD::LOAD: {
11987 LoadSDNode *LD = cast<LoadSDNode>(Op);
11988 // If the non-extending load has a single use and it's not live out, then it
11989 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000011990 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
11991 Op.hasOneUse()*/) {
11992 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
11993 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
11994 // The only case where we'd want to promote LOAD (rather then it being
11995 // promoted as an operand is when it's only use is liveout.
11996 if (UI->getOpcode() != ISD::CopyToReg)
11997 return false;
11998 }
11999 }
Evan Cheng4c26e932010-04-19 19:29:22 +000012000 Promote = true;
12001 break;
12002 }
12003 case ISD::SIGN_EXTEND:
12004 case ISD::ZERO_EXTEND:
12005 case ISD::ANY_EXTEND:
12006 Promote = true;
12007 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000012008 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000012009 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000012010 SDValue N0 = Op.getOperand(0);
12011 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000012012 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000012013 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000012014 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000012015 break;
12016 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000012017 case ISD::ADD:
12018 case ISD::MUL:
12019 case ISD::AND:
12020 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000012021 case ISD::XOR:
12022 Commute = true;
12023 // fallthrough
12024 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000012025 SDValue N0 = Op.getOperand(0);
12026 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000012027 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000012028 return false;
12029 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000012030 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000012031 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000012032 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000012033 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000012034 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000012035 }
12036 }
12037
12038 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000012039 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000012040}
12041
Evan Cheng60c07e12006-07-05 22:17:51 +000012042//===----------------------------------------------------------------------===//
12043// X86 Inline Assembly Support
12044//===----------------------------------------------------------------------===//
12045
Chris Lattnerb8105652009-07-20 17:51:36 +000012046bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
12047 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000012048
12049 std::string AsmStr = IA->getAsmString();
12050
12051 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000012052 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000012053 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000012054
12055 switch (AsmPieces.size()) {
12056 default: return false;
12057 case 1:
12058 AsmStr = AsmPieces[0];
12059 AsmPieces.clear();
12060 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
12061
Evan Cheng55d42002011-01-08 01:24:27 +000012062 // FIXME: this should verify that we are targetting a 486 or better. If not,
12063 // we will turn this bswap into something that will be lowered to logical ops
12064 // instead of emitting the bswap asm. For now, we don't support 486 or lower
12065 // so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000012066 // bswap $0
12067 if (AsmPieces.size() == 2 &&
12068 (AsmPieces[0] == "bswap" ||
12069 AsmPieces[0] == "bswapq" ||
12070 AsmPieces[0] == "bswapl") &&
12071 (AsmPieces[1] == "$0" ||
12072 AsmPieces[1] == "${0:q}")) {
12073 // No need to check constraints, nothing other than the equivalent of
12074 // "=r,0" would be valid here.
Evan Cheng55d42002011-01-08 01:24:27 +000012075 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
12076 if (!Ty || Ty->getBitWidth() % 16 != 0)
12077 return false;
12078 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000012079 }
12080 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000012081 if (CI->getType()->isIntegerTy(16) &&
Chris Lattnerb8105652009-07-20 17:51:36 +000012082 AsmPieces.size() == 3 &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000012083 (AsmPieces[0] == "rorw" || AsmPieces[0] == "rolw") &&
Chris Lattnerb8105652009-07-20 17:51:36 +000012084 AsmPieces[1] == "$$8," &&
12085 AsmPieces[2] == "${0:w}" &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000012086 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
12087 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000012088 const std::string &ConstraintsStr = IA->getConstraintString();
12089 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000012090 std::sort(AsmPieces.begin(), AsmPieces.end());
12091 if (AsmPieces.size() == 4 &&
12092 AsmPieces[0] == "~{cc}" &&
12093 AsmPieces[1] == "~{dirflag}" &&
12094 AsmPieces[2] == "~{flags}" &&
12095 AsmPieces[3] == "~{fpsr}") {
Evan Cheng55d42002011-01-08 01:24:27 +000012096 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
12097 if (!Ty || Ty->getBitWidth() % 16 != 0)
12098 return false;
12099 return IntrinsicLowering::LowerToByteSwap(CI);
Dan Gohman0ef701e2010-03-04 19:58:08 +000012100 }
Chris Lattnerb8105652009-07-20 17:51:36 +000012101 }
12102 break;
12103 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000012104 if (CI->getType()->isIntegerTy(32) &&
12105 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
12106 SmallVector<StringRef, 4> Words;
12107 SplitString(AsmPieces[0], Words, " \t,");
12108 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
12109 Words[2] == "${0:w}") {
12110 Words.clear();
12111 SplitString(AsmPieces[1], Words, " \t,");
12112 if (Words.size() == 3 && Words[0] == "rorl" && Words[1] == "$$16" &&
12113 Words[2] == "$0") {
12114 Words.clear();
12115 SplitString(AsmPieces[2], Words, " \t,");
12116 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
12117 Words[2] == "${0:w}") {
12118 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000012119 const std::string &ConstraintsStr = IA->getConstraintString();
12120 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Peter Collingbourne948cf022010-11-13 19:54:30 +000012121 std::sort(AsmPieces.begin(), AsmPieces.end());
12122 if (AsmPieces.size() == 4 &&
12123 AsmPieces[0] == "~{cc}" &&
12124 AsmPieces[1] == "~{dirflag}" &&
12125 AsmPieces[2] == "~{flags}" &&
12126 AsmPieces[3] == "~{fpsr}") {
Evan Cheng55d42002011-01-08 01:24:27 +000012127 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
12128 if (!Ty || Ty->getBitWidth() % 16 != 0)
12129 return false;
12130 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000012131 }
12132 }
12133 }
12134 }
12135 }
Evan Cheng55d42002011-01-08 01:24:27 +000012136
12137 if (CI->getType()->isIntegerTy(64)) {
12138 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
12139 if (Constraints.size() >= 2 &&
12140 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
12141 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
12142 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
12143 SmallVector<StringRef, 4> Words;
12144 SplitString(AsmPieces[0], Words, " \t");
12145 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
Chris Lattnerb8105652009-07-20 17:51:36 +000012146 Words.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000012147 SplitString(AsmPieces[1], Words, " \t");
12148 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
12149 Words.clear();
12150 SplitString(AsmPieces[2], Words, " \t,");
12151 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
12152 Words[2] == "%edx") {
12153 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
12154 if (!Ty || Ty->getBitWidth() % 16 != 0)
12155 return false;
12156 return IntrinsicLowering::LowerToByteSwap(CI);
12157 }
Chris Lattnerb8105652009-07-20 17:51:36 +000012158 }
12159 }
12160 }
12161 }
12162 break;
12163 }
12164 return false;
12165}
12166
12167
12168
Chris Lattnerf4dff842006-07-11 02:54:03 +000012169/// getConstraintType - Given a constraint letter, return the type of
12170/// constraint it is for this target.
12171X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000012172X86TargetLowering::getConstraintType(const std::string &Constraint) const {
12173 if (Constraint.size() == 1) {
12174 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000012175 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000012176 case 'q':
12177 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000012178 case 'f':
12179 case 't':
12180 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000012181 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000012182 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000012183 case 'Y':
12184 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000012185 case 'a':
12186 case 'b':
12187 case 'c':
12188 case 'd':
12189 case 'S':
12190 case 'D':
12191 case 'A':
12192 return C_Register;
12193 case 'I':
12194 case 'J':
12195 case 'K':
12196 case 'L':
12197 case 'M':
12198 case 'N':
12199 case 'G':
12200 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000012201 case 'e':
12202 case 'Z':
12203 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000012204 default:
12205 break;
12206 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000012207 }
Chris Lattner4234f572007-03-25 02:14:49 +000012208 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000012209}
12210
John Thompson44ab89e2010-10-29 17:29:13 +000012211/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000012212/// This object must already have been set up with the operand type
12213/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000012214TargetLowering::ConstraintWeight
12215 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000012216 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000012217 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012218 Value *CallOperandVal = info.CallOperandVal;
12219 // If we don't have a value, we can't do a match,
12220 // but allow it at the lowest weight.
12221 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000012222 return CW_Default;
12223 const Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000012224 // Look at the constraint type.
12225 switch (*constraint) {
12226 default:
John Thompson44ab89e2010-10-29 17:29:13 +000012227 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
12228 case 'R':
12229 case 'q':
12230 case 'Q':
12231 case 'a':
12232 case 'b':
12233 case 'c':
12234 case 'd':
12235 case 'S':
12236 case 'D':
12237 case 'A':
12238 if (CallOperandVal->getType()->isIntegerTy())
12239 weight = CW_SpecificReg;
12240 break;
12241 case 'f':
12242 case 't':
12243 case 'u':
12244 if (type->isFloatingPointTy())
12245 weight = CW_SpecificReg;
12246 break;
12247 case 'y':
Chris Lattner2a786eb2010-12-19 20:19:20 +000012248 if (type->isX86_MMXTy() && Subtarget->hasMMX())
John Thompson44ab89e2010-10-29 17:29:13 +000012249 weight = CW_SpecificReg;
12250 break;
12251 case 'x':
12252 case 'Y':
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012253 if ((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasXMM())
John Thompson44ab89e2010-10-29 17:29:13 +000012254 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012255 break;
12256 case 'I':
12257 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
12258 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000012259 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012260 }
12261 break;
John Thompson44ab89e2010-10-29 17:29:13 +000012262 case 'J':
12263 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12264 if (C->getZExtValue() <= 63)
12265 weight = CW_Constant;
12266 }
12267 break;
12268 case 'K':
12269 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12270 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
12271 weight = CW_Constant;
12272 }
12273 break;
12274 case 'L':
12275 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12276 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
12277 weight = CW_Constant;
12278 }
12279 break;
12280 case 'M':
12281 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12282 if (C->getZExtValue() <= 3)
12283 weight = CW_Constant;
12284 }
12285 break;
12286 case 'N':
12287 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12288 if (C->getZExtValue() <= 0xff)
12289 weight = CW_Constant;
12290 }
12291 break;
12292 case 'G':
12293 case 'C':
12294 if (dyn_cast<ConstantFP>(CallOperandVal)) {
12295 weight = CW_Constant;
12296 }
12297 break;
12298 case 'e':
12299 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12300 if ((C->getSExtValue() >= -0x80000000LL) &&
12301 (C->getSExtValue() <= 0x7fffffffLL))
12302 weight = CW_Constant;
12303 }
12304 break;
12305 case 'Z':
12306 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12307 if (C->getZExtValue() <= 0xffffffff)
12308 weight = CW_Constant;
12309 }
12310 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012311 }
12312 return weight;
12313}
12314
Dale Johannesenba2a0b92008-01-29 02:21:21 +000012315/// LowerXConstraint - try to replace an X constraint, which matches anything,
12316/// with another that has more specific requirements based on the type of the
12317/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000012318const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000012319LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000012320 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
12321 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000012322 if (ConstraintVT.isFloatingPoint()) {
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012323 if (Subtarget->hasXMMInt())
Chris Lattner5e764232008-04-26 23:02:14 +000012324 return "Y";
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012325 if (Subtarget->hasXMM())
Chris Lattner5e764232008-04-26 23:02:14 +000012326 return "x";
12327 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012328
Chris Lattner5e764232008-04-26 23:02:14 +000012329 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000012330}
12331
Chris Lattner48884cd2007-08-25 00:47:38 +000012332/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
12333/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000012334void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattner48884cd2007-08-25 00:47:38 +000012335 char Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000012336 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000012337 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000012338 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000012339
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012340 switch (Constraint) {
12341 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000012342 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000012343 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000012344 if (C->getZExtValue() <= 31) {
12345 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000012346 break;
12347 }
Devang Patel84f7fd22007-03-17 00:13:28 +000012348 }
Chris Lattner48884cd2007-08-25 00:47:38 +000012349 return;
Evan Cheng364091e2008-09-22 23:57:37 +000012350 case 'J':
12351 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000012352 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000012353 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
12354 break;
12355 }
12356 }
12357 return;
12358 case 'K':
12359 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000012360 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000012361 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
12362 break;
12363 }
12364 }
12365 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000012366 case 'N':
12367 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000012368 if (C->getZExtValue() <= 255) {
12369 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000012370 break;
12371 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000012372 }
Chris Lattner48884cd2007-08-25 00:47:38 +000012373 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000012374 case 'e': {
12375 // 32-bit signed value
12376 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000012377 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
12378 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000012379 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000012380 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000012381 break;
12382 }
12383 // FIXME gcc accepts some relocatable values here too, but only in certain
12384 // memory models; it's complicated.
12385 }
12386 return;
12387 }
12388 case 'Z': {
12389 // 32-bit unsigned value
12390 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000012391 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
12392 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000012393 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
12394 break;
12395 }
12396 }
12397 // FIXME gcc accepts some relocatable values here too, but only in certain
12398 // memory models; it's complicated.
12399 return;
12400 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012401 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012402 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000012403 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000012404 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000012405 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000012406 break;
12407 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012408
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000012409 // In any sort of PIC mode addresses need to be computed at runtime by
12410 // adding in a register or some sort of table lookup. These can't
12411 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000012412 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000012413 return;
12414
Chris Lattnerdc43a882007-05-03 16:52:29 +000012415 // If we are in non-pic codegen mode, we allow the address of a global (with
12416 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000012417 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000012418 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000012419
Chris Lattner49921962009-05-08 18:23:14 +000012420 // Match either (GA), (GA+C), (GA+C1+C2), etc.
12421 while (1) {
12422 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
12423 Offset += GA->getOffset();
12424 break;
12425 } else if (Op.getOpcode() == ISD::ADD) {
12426 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
12427 Offset += C->getZExtValue();
12428 Op = Op.getOperand(0);
12429 continue;
12430 }
12431 } else if (Op.getOpcode() == ISD::SUB) {
12432 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
12433 Offset += -C->getZExtValue();
12434 Op = Op.getOperand(0);
12435 continue;
12436 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012437 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012438
Chris Lattner49921962009-05-08 18:23:14 +000012439 // Otherwise, this isn't something we can handle, reject it.
12440 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000012441 }
Eric Christopherfd179292009-08-27 18:07:15 +000012442
Dan Gohman46510a72010-04-15 01:51:59 +000012443 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012444 // If we require an extra load to get this address, as in PIC mode, we
12445 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000012446 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
12447 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012448 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000012449
Devang Patel0d881da2010-07-06 22:08:15 +000012450 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
12451 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000012452 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012453 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012454 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012455
Gabor Greifba36cb52008-08-28 21:40:38 +000012456 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000012457 Ops.push_back(Result);
12458 return;
12459 }
Dale Johannesen1784d162010-06-25 21:55:36 +000012460 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012461}
12462
Chris Lattner259e97c2006-01-31 19:43:35 +000012463std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +000012464getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000012465 EVT VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +000012466 if (Constraint.size() == 1) {
12467 // FIXME: not handling fp-stack yet!
Chris Lattner259e97c2006-01-31 19:43:35 +000012468 switch (Constraint[0]) { // GCC X86 Constraint Letters
Chris Lattnerf4dff842006-07-11 02:54:03 +000012469 default: break; // Unknown constraint letter
Evan Cheng47e9fab2009-07-17 22:13:25 +000012470 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
12471 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000012472 if (VT == MVT::i32)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012473 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
12474 X86::ESI, X86::EDI, X86::R8D, X86::R9D,
12475 X86::R10D,X86::R11D,X86::R12D,
12476 X86::R13D,X86::R14D,X86::R15D,
12477 X86::EBP, X86::ESP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012478 else if (VT == MVT::i16)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012479 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
12480 X86::SI, X86::DI, X86::R8W,X86::R9W,
12481 X86::R10W,X86::R11W,X86::R12W,
12482 X86::R13W,X86::R14W,X86::R15W,
12483 X86::BP, X86::SP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012484 else if (VT == MVT::i8)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012485 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL,
12486 X86::SIL, X86::DIL, X86::R8B,X86::R9B,
12487 X86::R10B,X86::R11B,X86::R12B,
12488 X86::R13B,X86::R14B,X86::R15B,
12489 X86::BPL, X86::SPL, 0);
12490
Owen Anderson825b72b2009-08-11 20:47:22 +000012491 else if (VT == MVT::i64)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012492 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX,
12493 X86::RSI, X86::RDI, X86::R8, X86::R9,
12494 X86::R10, X86::R11, X86::R12,
12495 X86::R13, X86::R14, X86::R15,
12496 X86::RBP, X86::RSP, 0);
12497
12498 break;
12499 }
Eric Christopherfd179292009-08-27 18:07:15 +000012500 // 32-bit fallthrough
Chris Lattner259e97c2006-01-31 19:43:35 +000012501 case 'Q': // Q_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000012502 if (VT == MVT::i32)
Chris Lattner80a7ecc2006-05-06 00:29:37 +000012503 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012504 else if (VT == MVT::i16)
Chris Lattner80a7ecc2006-05-06 00:29:37 +000012505 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012506 else if (VT == MVT::i8)
Evan Cheng12914382007-08-13 23:27:11 +000012507 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012508 else if (VT == MVT::i64)
Chris Lattner03e6c702007-11-04 06:51:12 +000012509 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
12510 break;
Chris Lattner259e97c2006-01-31 19:43:35 +000012511 }
12512 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012513
Chris Lattner1efa40f2006-02-22 00:56:39 +000012514 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +000012515}
Chris Lattnerf76d1802006-07-31 23:26:50 +000012516
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012517std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000012518X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000012519 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000012520 // First, see if this is a constraint that directly corresponds to an LLVM
12521 // register class.
12522 if (Constraint.size() == 1) {
12523 // GCC Constraint Letters
12524 switch (Constraint[0]) {
12525 default: break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000012526 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000012527 case 'l': // INDEX_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000012528 if (VT == MVT::i8)
Chris Lattner0f65cad2007-04-09 05:49:22 +000012529 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012530 if (VT == MVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +000012531 return std::make_pair(0U, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012532 if (VT == MVT::i32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +000012533 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +000012534 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000012535 case 'R': // LEGACY_REGS
12536 if (VT == MVT::i8)
12537 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
12538 if (VT == MVT::i16)
12539 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
12540 if (VT == MVT::i32 || !Subtarget->is64Bit())
12541 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
12542 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000012543 case 'f': // FP Stack registers.
12544 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
12545 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000012546 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000012547 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012548 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000012549 return std::make_pair(0U, X86::RFP64RegisterClass);
12550 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000012551 case 'y': // MMX_REGS if MMX allowed.
12552 if (!Subtarget->hasMMX()) break;
12553 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000012554 case 'Y': // SSE_REGS if SSE2 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012555 if (!Subtarget->hasXMMInt()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000012556 // FALL THROUGH.
12557 case 'x': // SSE_REGS if SSE1 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012558 if (!Subtarget->hasXMM()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000012559
Owen Anderson825b72b2009-08-11 20:47:22 +000012560 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000012561 default: break;
12562 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000012563 case MVT::f32:
12564 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +000012565 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012566 case MVT::f64:
12567 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +000012568 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000012569 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000012570 case MVT::v16i8:
12571 case MVT::v8i16:
12572 case MVT::v4i32:
12573 case MVT::v2i64:
12574 case MVT::v4f32:
12575 case MVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +000012576 return std::make_pair(0U, X86::VR128RegisterClass);
12577 }
Chris Lattnerad043e82007-04-09 05:11:28 +000012578 break;
12579 }
12580 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012581
Chris Lattnerf76d1802006-07-31 23:26:50 +000012582 // Use the default implementation in TargetLowering to convert the register
12583 // constraint into a member of a register class.
12584 std::pair<unsigned, const TargetRegisterClass*> Res;
12585 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000012586
12587 // Not found as a standard register?
12588 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000012589 // Map st(0) -> st(7) -> ST0
12590 if (Constraint.size() == 7 && Constraint[0] == '{' &&
12591 tolower(Constraint[1]) == 's' &&
12592 tolower(Constraint[2]) == 't' &&
12593 Constraint[3] == '(' &&
12594 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
12595 Constraint[5] == ')' &&
12596 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000012597
Chris Lattner56d77c72009-09-13 22:41:48 +000012598 Res.first = X86::ST0+Constraint[4]-'0';
12599 Res.second = X86::RFP80RegisterClass;
12600 return Res;
12601 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000012602
Chris Lattner56d77c72009-09-13 22:41:48 +000012603 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000012604 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000012605 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +000012606 Res.second = X86::RFP80RegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000012607 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000012608 }
Chris Lattner56d77c72009-09-13 22:41:48 +000012609
12610 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000012611 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000012612 Res.first = X86::EFLAGS;
12613 Res.second = X86::CCRRegisterClass;
12614 return Res;
12615 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000012616
Dale Johannesen330169f2008-11-13 21:52:36 +000012617 // 'A' means EAX + EDX.
12618 if (Constraint == "A") {
12619 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +000012620 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000012621 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000012622 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000012623 return Res;
12624 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012625
Chris Lattnerf76d1802006-07-31 23:26:50 +000012626 // Otherwise, check to see if this is a register class of the wrong value
12627 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
12628 // turn into {ax},{dx}.
12629 if (Res.second->hasType(VT))
12630 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012631
Chris Lattnerf76d1802006-07-31 23:26:50 +000012632 // All of the single-register GCC register classes map their values onto
12633 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
12634 // really want an 8-bit or 32-bit register, map to the appropriate register
12635 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +000012636 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000012637 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000012638 unsigned DestReg = 0;
12639 switch (Res.first) {
12640 default: break;
12641 case X86::AX: DestReg = X86::AL; break;
12642 case X86::DX: DestReg = X86::DL; break;
12643 case X86::CX: DestReg = X86::CL; break;
12644 case X86::BX: DestReg = X86::BL; break;
12645 }
12646 if (DestReg) {
12647 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000012648 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000012649 }
Owen Anderson825b72b2009-08-11 20:47:22 +000012650 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000012651 unsigned DestReg = 0;
12652 switch (Res.first) {
12653 default: break;
12654 case X86::AX: DestReg = X86::EAX; break;
12655 case X86::DX: DestReg = X86::EDX; break;
12656 case X86::CX: DestReg = X86::ECX; break;
12657 case X86::BX: DestReg = X86::EBX; break;
12658 case X86::SI: DestReg = X86::ESI; break;
12659 case X86::DI: DestReg = X86::EDI; break;
12660 case X86::BP: DestReg = X86::EBP; break;
12661 case X86::SP: DestReg = X86::ESP; break;
12662 }
12663 if (DestReg) {
12664 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000012665 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000012666 }
Owen Anderson825b72b2009-08-11 20:47:22 +000012667 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000012668 unsigned DestReg = 0;
12669 switch (Res.first) {
12670 default: break;
12671 case X86::AX: DestReg = X86::RAX; break;
12672 case X86::DX: DestReg = X86::RDX; break;
12673 case X86::CX: DestReg = X86::RCX; break;
12674 case X86::BX: DestReg = X86::RBX; break;
12675 case X86::SI: DestReg = X86::RSI; break;
12676 case X86::DI: DestReg = X86::RDI; break;
12677 case X86::BP: DestReg = X86::RBP; break;
12678 case X86::SP: DestReg = X86::RSP; break;
12679 }
12680 if (DestReg) {
12681 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000012682 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000012683 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000012684 }
Chris Lattner6ba50a92008-08-26 06:19:02 +000012685 } else if (Res.second == X86::FR32RegisterClass ||
12686 Res.second == X86::FR64RegisterClass ||
12687 Res.second == X86::VR128RegisterClass) {
12688 // Handle references to XMM physical registers that got mapped into the
12689 // wrong class. This can happen with constraints like {xmm0} where the
12690 // target independent register mapper will just pick the first match it can
12691 // find, ignoring the required type.
Owen Anderson825b72b2009-08-11 20:47:22 +000012692 if (VT == MVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +000012693 Res.second = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +000012694 else if (VT == MVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +000012695 Res.second = X86::FR64RegisterClass;
12696 else if (X86::VR128RegisterClass->hasType(VT))
12697 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000012698 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012699
Chris Lattnerf76d1802006-07-31 23:26:50 +000012700 return Res;
12701}