blob: 6cd03d02a71aa01dbc01a87164cffef40440a59f [file] [log] [blame]
Arnold Schwaighofer92226dd2007-10-12 21:53:12 +00001//===-- X86ISelLowering.cpp - X86 DAG Lowering Implementation -------------===//
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that X86 uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
Evan Chengb1712452010-01-27 06:25:16 +000015#define DEBUG_TYPE "x86-isel"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000016#include "X86.h"
Evan Cheng0cc39452006-01-16 21:21:29 +000017#include "X86InstrBuilder.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000018#include "X86ISelLowering.h"
19#include "X86TargetMachine.h"
Chris Lattner8c6ed052009-09-16 01:46:41 +000020#include "X86TargetObjectFile.h"
David Greene583b68f2011-02-17 19:18:59 +000021#include "Utils/X86ShuffleDecode.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000022#include "llvm/CallingConv.h"
Evan Cheng223547a2006-01-31 22:28:30 +000023#include "llvm/Constants.h"
Evan Cheng347d5f72006-04-28 21:29:37 +000024#include "llvm/DerivedTypes.h"
Chris Lattnerb903bed2009-06-26 21:20:29 +000025#include "llvm/GlobalAlias.h"
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000026#include "llvm/GlobalVariable.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000027#include "llvm/Function.h"
Chris Lattnerb8105652009-07-20 17:51:36 +000028#include "llvm/Instructions.h"
Evan Cheng6be2c582006-04-05 23:38:46 +000029#include "llvm/Intrinsics.h"
Owen Andersona90b3dc2009-07-15 21:51:10 +000030#include "llvm/LLVMContext.h"
Evan Cheng55d42002011-01-08 01:24:27 +000031#include "llvm/CodeGen/IntrinsicLowering.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000032#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng4a460802006-01-11 00:33:36 +000033#include "llvm/CodeGen/MachineFunction.h"
34#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner5e1df8d2010-01-25 23:38:14 +000035#include "llvm/CodeGen/MachineJumpTableInfo.h"
Evan Chenga844bde2008-02-02 04:07:54 +000036#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000037#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman69de1932008-02-06 22:27:42 +000038#include "llvm/CodeGen/PseudoSourceValue.h"
Chris Lattner589c6f62010-01-26 06:28:43 +000039#include "llvm/MC/MCAsmInfo.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000040#include "llvm/MC/MCContext.h"
Daniel Dunbar4e815f82010-03-15 23:51:06 +000041#include "llvm/MC/MCExpr.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000042#include "llvm/MC/MCSymbol.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000043#include "llvm/ADT/BitVector.h"
Evan Cheng14b32e12007-12-11 01:46:18 +000044#include "llvm/ADT/SmallSet.h"
Evan Chengb1712452010-01-27 06:25:16 +000045#include "llvm/ADT/Statistic.h"
Chris Lattner1a60aa72006-10-31 19:42:44 +000046#include "llvm/ADT/StringExtras.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000047#include "llvm/ADT/VectorExtras.h"
Evan Cheng485fafc2011-03-21 01:19:09 +000048#include "llvm/Support/CallSite.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
Evan Cheng10e86422008-04-25 19:11:04 +000059// Forward declarations.
Owen Andersone50ed302009-08-10 22:56:29 +000060static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +000061 SDValue V2);
Evan Cheng10e86422008-04-25 19:11:04 +000062
David Greenea5f26012011-02-07 19:36:54 +000063static SDValue Insert128BitVector(SDValue Result,
64 SDValue Vec,
65 SDValue Idx,
66 SelectionDAG &DAG,
67 DebugLoc dl);
David Greenef125a292011-02-08 19:04:41 +000068
David Greenea5f26012011-02-07 19:36:54 +000069static SDValue Extract128BitVector(SDValue Vec,
70 SDValue Idx,
71 SelectionDAG &DAG,
72 DebugLoc dl);
73
David Greenef125a292011-02-08 19:04:41 +000074static SDValue ConcatVectors(SDValue Lower, SDValue Upper, SelectionDAG &DAG);
75
76
David Greenea5f26012011-02-07 19:36:54 +000077/// Generate a DAG to grab 128-bits from a vector > 128 bits. This
78/// sets things up to match to an AVX VEXTRACTF128 instruction or a
David Greene74a579d2011-02-10 16:57:36 +000079/// simple subregister reference. Idx is an index in the 128 bits we
80/// want. It need not be aligned to a 128-bit bounday. That makes
81/// lowering EXTRACT_VECTOR_ELT operations easier.
David Greenea5f26012011-02-07 19:36:54 +000082static SDValue Extract128BitVector(SDValue Vec,
83 SDValue Idx,
84 SelectionDAG &DAG,
85 DebugLoc dl) {
86 EVT VT = Vec.getValueType();
87 assert(VT.getSizeInBits() == 256 && "Unexpected vector size!");
88
89 EVT ElVT = VT.getVectorElementType();
90
91 int Factor = VT.getSizeInBits() / 128;
92
93 EVT ResultVT = EVT::getVectorVT(*DAG.getContext(),
94 ElVT,
95 VT.getVectorNumElements() / Factor);
96
97 // Extract from UNDEF is UNDEF.
98 if (Vec.getOpcode() == ISD::UNDEF)
99 return DAG.getNode(ISD::UNDEF, dl, ResultVT);
100
101 if (isa<ConstantSDNode>(Idx)) {
102 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
103
104 // Extract the relevant 128 bits. Generate an EXTRACT_SUBVECTOR
105 // we can match to VEXTRACTF128.
106 unsigned ElemsPerChunk = 128 / ElVT.getSizeInBits();
107
108 // This is the index of the first element of the 128-bit chunk
109 // we want.
110 unsigned NormalizedIdxVal = (((IdxVal * ElVT.getSizeInBits()) / 128)
111 * ElemsPerChunk);
112
113 SDValue VecIdx = DAG.getConstant(NormalizedIdxVal, MVT::i32);
114
115 SDValue Result = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, ResultVT, Vec,
116 VecIdx);
117
118 return Result;
119 }
120
121 return SDValue();
122}
123
124/// Generate a DAG to put 128-bits into a vector > 128 bits. This
125/// sets things up to match to an AVX VINSERTF128 instruction or a
David Greene6b381262011-02-09 15:32:06 +0000126/// simple superregister reference. Idx is an index in the 128 bits
127/// we want. It need not be aligned to a 128-bit bounday. That makes
128/// lowering INSERT_VECTOR_ELT operations easier.
David Greenea5f26012011-02-07 19:36:54 +0000129static SDValue Insert128BitVector(SDValue Result,
130 SDValue Vec,
131 SDValue Idx,
132 SelectionDAG &DAG,
133 DebugLoc dl) {
134 if (isa<ConstantSDNode>(Idx)) {
135 EVT VT = Vec.getValueType();
136 assert(VT.getSizeInBits() == 128 && "Unexpected vector size!");
137
138 EVT ElVT = VT.getVectorElementType();
139
140 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
141
142 EVT ResultVT = Result.getValueType();
143
144 // Insert the relevant 128 bits.
145 unsigned ElemsPerChunk = 128 / ElVT.getSizeInBits();
146
147 // This is the index of the first element of the 128-bit chunk
148 // we want.
149 unsigned NormalizedIdxVal = (((IdxVal * ElVT.getSizeInBits()) / 128)
150 * ElemsPerChunk);
151
152 SDValue VecIdx = DAG.getConstant(NormalizedIdxVal, MVT::i32);
153
154 Result = DAG.getNode(ISD::INSERT_SUBVECTOR, dl, ResultVT, Result, Vec,
155 VecIdx);
156 return Result;
157 }
158
159 return SDValue();
160}
161
David Greenef125a292011-02-08 19:04:41 +0000162/// Given two vectors, concat them.
163static SDValue ConcatVectors(SDValue Lower, SDValue Upper, SelectionDAG &DAG) {
164 DebugLoc dl = Lower.getDebugLoc();
165
166 assert(Lower.getValueType() == Upper.getValueType() && "Mismatched vectors!");
167
168 EVT VT = EVT::getVectorVT(*DAG.getContext(),
169 Lower.getValueType().getVectorElementType(),
170 Lower.getValueType().getVectorNumElements() * 2);
171
172 // TODO: Generalize to arbitrary vector length (this assumes 256-bit vectors).
173 assert(VT.getSizeInBits() == 256 && "Unsupported vector concat!");
174
175 // Insert the upper subvector.
176 SDValue Vec = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT), Upper,
177 DAG.getConstant(
178 // This is half the length of the result
179 // vector. Start inserting the upper 128
180 // bits here.
181 Lower.getValueType().getVectorNumElements(),
182 MVT::i32),
183 DAG, dl);
184
185 // Insert the lower subvector.
186 Vec = Insert128BitVector(Vec, Lower, DAG.getConstant(0, MVT::i32), DAG, dl);
187 return Vec;
188}
189
Chris Lattnerf0144122009-07-28 03:13:23 +0000190static TargetLoweringObjectFile *createTLOF(X86TargetMachine &TM) {
Evan Cheng2bffee22011-02-01 01:14:13 +0000191 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
192 bool is64Bit = Subtarget->is64Bit();
NAKAMURA Takumi27635382011-02-05 15:10:54 +0000193
Evan Cheng2bffee22011-02-01 01:14:13 +0000194 if (Subtarget->isTargetEnvMacho()) {
Chris Lattnere019ec12010-12-19 20:07:10 +0000195 if (is64Bit)
196 return new X8664_MachoTargetObjectFile();
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000197 return new TargetLoweringObjectFileMachO();
Michael J. Spencerec38de22010-10-10 22:04:20 +0000198 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000199
Evan Cheng2bffee22011-02-01 01:14:13 +0000200 if (Subtarget->isTargetELF()) {
Chris Lattnere019ec12010-12-19 20:07:10 +0000201 if (is64Bit)
202 return new X8664_ELFTargetObjectFile(TM);
203 return new X8632_ELFTargetObjectFile(TM);
204 }
Evan Cheng2bffee22011-02-01 01:14:13 +0000205 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
Chris Lattnere019ec12010-12-19 20:07:10 +0000206 return new TargetLoweringObjectFileCOFF();
Eric Christopher62f35a22010-07-05 19:26:33 +0000207 llvm_unreachable("unknown subtarget type");
Chris Lattnerf0144122009-07-28 03:13:23 +0000208}
209
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +0000210X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
Chris Lattnerf0144122009-07-28 03:13:23 +0000211 : TargetLowering(TM, createTLOF(TM)) {
Evan Cheng559806f2006-01-27 08:10:46 +0000212 Subtarget = &TM.getSubtarget<X86Subtarget>();
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000213 X86ScalarSSEf64 = Subtarget->hasXMMInt();
214 X86ScalarSSEf32 = Subtarget->hasXMM();
Evan Cheng25ab6902006-09-08 06:48:29 +0000215 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000216
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000217 RegInfo = TM.getRegisterInfo();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000218 TD = getTargetData();
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000219
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000220 // Set up the TargetLowering object.
Chris Lattnera34b3cf2010-12-19 20:03:11 +0000221 static MVT IntVTs[] = { MVT::i8, MVT::i16, MVT::i32, MVT::i64 };
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000222
223 // X86 is weird, it always uses i8 for shift amounts and setcc results.
Duncan Sands03228082008-11-23 15:47:28 +0000224 setBooleanContents(ZeroOrOneBooleanContent);
Eric Christopher471e4222011-06-08 23:55:35 +0000225
Eric Christopherde5e1012011-03-11 01:05:58 +0000226 // For 64-bit since we have so many registers use the ILP scheduler, for
227 // 32-bit code use the register pressure specific scheduling.
228 if (Subtarget->is64Bit())
229 setSchedulingPreference(Sched::ILP);
230 else
231 setSchedulingPreference(Sched::RegPressure);
Evan Cheng25ab6902006-09-08 06:48:29 +0000232 setStackPointerRegisterToSaveRestore(X86StackPtr);
Evan Cheng714554d2006-03-16 21:47:42 +0000233
Michael J. Spencer92bf38c2010-10-10 23:11:06 +0000234 if (Subtarget->isTargetWindows() && !Subtarget->isTargetCygMing()) {
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000235 // Setup Windows compiler runtime calls.
236 setLibcallName(RTLIB::SDIV_I64, "_alldiv");
Michael J. Spencer335b8062010-10-11 05:29:15 +0000237 setLibcallName(RTLIB::UDIV_I64, "_aulldiv");
238 setLibcallName(RTLIB::FPTOUINT_F64_I64, "_ftol2");
Michael J. Spencer94f7eeb2010-10-19 07:32:52 +0000239 setLibcallName(RTLIB::FPTOUINT_F32_I64, "_ftol2");
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000240 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::X86_StdCall);
Michael J. Spencer335b8062010-10-11 05:29:15 +0000241 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::X86_StdCall);
Michael J. Spencer6dad10e2010-10-27 18:52:38 +0000242 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I64, CallingConv::C);
243 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I64, CallingConv::C);
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000244 }
245
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000246 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +0000247 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000248 setUseUnderscoreSetJmp(false);
249 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000250 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000251 // MS runtime is weird: it exports _setjmp, but longjmp!
252 setUseUnderscoreSetJmp(true);
253 setUseUnderscoreLongJmp(false);
254 } else {
255 setUseUnderscoreSetJmp(true);
256 setUseUnderscoreLongJmp(true);
257 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000258
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000259 // Set up the register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000260 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
Dan Gohman71edb242010-04-30 18:30:26 +0000261 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000262 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
Evan Cheng25ab6902006-09-08 06:48:29 +0000263 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000264 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000265
Owen Anderson825b72b2009-08-11 20:47:22 +0000266 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Evan Chengc5484282006-10-04 00:56:09 +0000267
Scott Michelfdc40a02009-02-17 22:15:04 +0000268 // We don't accept any truncstore of integer registers.
Owen Anderson825b72b2009-08-11 20:47:22 +0000269 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000270 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000271 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000272 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000273 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
274 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
Evan Cheng7f042682008-10-15 02:05:31 +0000275
276 // SETOEQ and SETUNE require checking two conditions.
Owen Anderson825b72b2009-08-11 20:47:22 +0000277 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
278 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
279 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
280 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
281 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
282 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
Chris Lattnerddf89562008-01-17 19:59:44 +0000283
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000284 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
285 // operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000286 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
287 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
288 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +0000289
Evan Cheng25ab6902006-09-08 06:48:29 +0000290 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000291 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
292 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
Eli Friedman948e95a2009-05-23 09:59:16 +0000293 } else if (!UseSoftFloat) {
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000294 // We have an algorithm for SSE2->double, and we turn this into a
295 // 64-bit FILD followed by conditional FADD for other targets.
296 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Eli Friedman948e95a2009-05-23 09:59:16 +0000297 // We have an algorithm for SSE2, and we turn this into a 64-bit
298 // FILD for other targets.
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000299 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000300 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000301
302 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
303 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000304 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
305 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000306
Devang Patel6a784892009-06-05 18:48:29 +0000307 if (!UseSoftFloat) {
Bill Wendling105be5a2009-03-13 08:41:47 +0000308 // SSE has no i16 to fp conversion, only i32
309 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000310 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000311 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000312 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000313 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000314 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
315 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000316 }
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000317 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000318 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
319 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000320 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000321
Dale Johannesen73328d12007-09-19 23:55:34 +0000322 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
323 // are Legal, f80 is custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000324 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
325 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000326
Evan Cheng02568ff2006-01-30 22:13:22 +0000327 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
328 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000329 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
330 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
Evan Cheng02568ff2006-01-30 22:13:22 +0000331
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000332 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000333 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000334 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000335 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000336 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000337 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
338 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000339 }
340
341 // Handle FP_TO_UINT by promoting the destination to a larger signed
342 // conversion.
Owen Anderson825b72b2009-08-11 20:47:22 +0000343 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
344 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
345 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000346
Evan Cheng25ab6902006-09-08 06:48:29 +0000347 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000348 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
349 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Eli Friedman948e95a2009-05-23 09:59:16 +0000350 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000351 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000352 // Expand FP_TO_UINT into a select.
353 // FIXME: We would like to use a Custom expander here eventually to do
354 // the optimal thing for SSE vs. the default expansion in the legalizer.
Owen Anderson825b72b2009-08-11 20:47:22 +0000355 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000356 else
Eli Friedman948e95a2009-05-23 09:59:16 +0000357 // With SSE3 we can use fisttpll to convert to a signed i64; without
358 // SSE, we're stuck with a fistpll.
Owen Anderson825b72b2009-08-11 20:47:22 +0000359 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000360 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000361
Chris Lattner399610a2006-12-05 18:22:22 +0000362 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Michael J. Spencerec38de22010-10-10 22:04:20 +0000363 if (!X86ScalarSSEf64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000364 setOperationAction(ISD::BITCAST , MVT::f32 , Expand);
365 setOperationAction(ISD::BITCAST , MVT::i32 , Expand);
Dale Johannesene39859a2010-05-21 18:40:15 +0000366 if (Subtarget->is64Bit()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000367 setOperationAction(ISD::BITCAST , MVT::f64 , Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000368 // Without SSE, i64->f64 goes through memory.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000369 setOperationAction(ISD::BITCAST , MVT::i64 , Expand);
Dale Johannesen7d07b482010-05-21 00:52:33 +0000370 }
Chris Lattnerf3597a12006-12-05 18:45:06 +0000371 }
Chris Lattner21f66852005-12-23 05:15:23 +0000372
Dan Gohmanb00ee212008-02-18 19:34:53 +0000373 // Scalar integer divide and remainder are lowered to use operations that
374 // produce two results, to match the available instructions. This exposes
375 // the two-result form to trivial CSE, which is able to combine x/y and x%y
376 // into a single instruction.
377 //
378 // Scalar integer multiply-high is also lowered to use two-result
379 // operations, to match the available instructions. However, plain multiply
380 // (low) operations are left as Legal, as there are single-result
381 // instructions for this in x86. Using the two-result multiply instructions
382 // when both high and low results are needed must be arranged by dagcombine.
Chris Lattnere019ec12010-12-19 20:07:10 +0000383 for (unsigned i = 0, e = 4; i != e; ++i) {
384 MVT VT = IntVTs[i];
385 setOperationAction(ISD::MULHS, VT, Expand);
386 setOperationAction(ISD::MULHU, VT, Expand);
387 setOperationAction(ISD::SDIV, VT, Expand);
388 setOperationAction(ISD::UDIV, VT, Expand);
389 setOperationAction(ISD::SREM, VT, Expand);
390 setOperationAction(ISD::UREM, VT, Expand);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000391
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000392 // Add/Sub overflow ops with MVT::Glues are lowered to EFLAGS dependences.
Chris Lattnerd8ff7ec2010-12-20 01:03:27 +0000393 setOperationAction(ISD::ADDC, VT, Custom);
394 setOperationAction(ISD::ADDE, VT, Custom);
395 setOperationAction(ISD::SUBC, VT, Custom);
396 setOperationAction(ISD::SUBE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000397 }
Dan Gohmana37c9f72007-09-25 18:23:27 +0000398
Owen Anderson825b72b2009-08-11 20:47:22 +0000399 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
400 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
401 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
402 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000403 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000404 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
405 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
406 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
407 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
408 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
409 setOperationAction(ISD::FREM , MVT::f32 , Expand);
410 setOperationAction(ISD::FREM , MVT::f64 , Expand);
411 setOperationAction(ISD::FREM , MVT::f80 , Expand);
412 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000413
Owen Anderson825b72b2009-08-11 20:47:22 +0000414 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
415 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000416 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
417 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000418 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
419 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000420 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000421 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
422 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000423 }
424
Benjamin Kramer1292c222010-12-04 20:32:23 +0000425 if (Subtarget->hasPOPCNT()) {
426 setOperationAction(ISD::CTPOP , MVT::i8 , Promote);
427 } else {
428 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
429 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
430 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
431 if (Subtarget->is64Bit())
432 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
433 }
434
Owen Anderson825b72b2009-08-11 20:47:22 +0000435 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
436 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000437
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000438 // These should be promoted to a larger select which is supported.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000439 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000440 // X86 wants to expand cmov itself.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000441 setOperationAction(ISD::SELECT , MVT::i8 , Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000442 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000443 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
444 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
445 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
446 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
447 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000448 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000449 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
450 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
451 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
452 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000453 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000454 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
Andrew Trickf6c39412011-03-23 23:11:02 +0000455 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000456 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000457 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000458
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000459 // Darwin ABI issue.
Owen Anderson825b72b2009-08-11 20:47:22 +0000460 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
461 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
462 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
463 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000464 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000465 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
466 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000467 setOperationAction(ISD::BlockAddress , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000468 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000469 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
470 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
471 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
472 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000473 setOperationAction(ISD::BlockAddress , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000474 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000475 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Owen Anderson825b72b2009-08-11 20:47:22 +0000476 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
477 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
478 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000479 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000480 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
481 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
482 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000483 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000484
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000485 if (Subtarget->hasXMM())
Owen Anderson825b72b2009-08-11 20:47:22 +0000486 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000487
Eric Christopher9a9d2752010-07-22 02:48:34 +0000488 // We may not have a libcall for MEMBARRIER so we should lower this.
489 setOperationAction(ISD::MEMBARRIER , MVT::Other, Custom);
Michael J. Spencerec38de22010-10-10 22:04:20 +0000490
Jim Grosbachf1ab49e2010-06-23 16:25:07 +0000491 // On X86 and X86-64, atomic operations are lowered to locked instructions.
492 // Locked instructions, in turn, have implicit fence semantics (all memory
493 // operations are flushed before issuing the locked instruction, and they
494 // are not buffered), so we can fold away the common pattern of
495 // fence-atomic-fence.
496 setShouldFoldAtomicFences(true);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000497
Mon P Wang63307c32008-05-05 19:05:59 +0000498 // Expand certain atomics
Chris Lattnere019ec12010-12-19 20:07:10 +0000499 for (unsigned i = 0, e = 4; i != e; ++i) {
500 MVT VT = IntVTs[i];
501 setOperationAction(ISD::ATOMIC_CMP_SWAP, VT, Custom);
502 setOperationAction(ISD::ATOMIC_LOAD_SUB, VT, Custom);
503 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000504
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000505 if (!Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000506 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
507 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
508 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
509 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
510 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
511 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
512 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000513 }
514
Evan Cheng3c992d22006-03-07 02:02:57 +0000515 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000516 if (!Subtarget->isTargetDarwin() &&
517 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000518 !Subtarget->isTargetCygMing()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000519 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Dan Gohman44066042008-07-01 00:05:16 +0000520 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000521
Owen Anderson825b72b2009-08-11 20:47:22 +0000522 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
523 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
524 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
525 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000526 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000527 setExceptionPointerRegister(X86::RAX);
528 setExceptionSelectorRegister(X86::RDX);
529 } else {
530 setExceptionPointerRegister(X86::EAX);
531 setExceptionSelectorRegister(X86::EDX);
532 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000533 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
534 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000535
Owen Anderson825b72b2009-08-11 20:47:22 +0000536 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000537
Owen Anderson825b72b2009-08-11 20:47:22 +0000538 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000539
Nate Begemanacc398c2006-01-25 18:21:52 +0000540 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000541 setOperationAction(ISD::VASTART , MVT::Other, Custom);
542 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000543 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000544 setOperationAction(ISD::VAARG , MVT::Other, Custom);
545 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000546 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000547 setOperationAction(ISD::VAARG , MVT::Other, Expand);
548 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000549 }
Evan Chengae642192007-03-02 23:16:35 +0000550
Owen Anderson825b72b2009-08-11 20:47:22 +0000551 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
552 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
NAKAMURA Takumia2e07622011-03-24 07:07:00 +0000553 setOperationAction(ISD::DYNAMIC_STACKALLOC,
554 (Subtarget->is64Bit() ? MVT::i64 : MVT::i32),
555 (Subtarget->isTargetCOFF()
556 && !Subtarget->isTargetEnvMacho()
557 ? Custom : Expand));
Chris Lattnerb99329e2006-01-13 02:42:53 +0000558
Evan Chengc7ce29b2009-02-13 22:36:38 +0000559 if (!UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000560 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000561 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000562 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
563 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000564
Evan Cheng223547a2006-01-31 22:28:30 +0000565 // Use ANDPD to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000566 setOperationAction(ISD::FABS , MVT::f64, Custom);
567 setOperationAction(ISD::FABS , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000568
569 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000570 setOperationAction(ISD::FNEG , MVT::f64, Custom);
571 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000572
Evan Cheng68c47cb2007-01-05 07:55:56 +0000573 // Use ANDPD and ORPD to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000574 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
575 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000576
Stuart Hastings4fd0dee2011-06-01 04:39:42 +0000577 // Lower this to FGETSIGNx86 plus an AND.
578 setOperationAction(ISD::FGETSIGN, MVT::i64, Custom);
579 setOperationAction(ISD::FGETSIGN, MVT::i32, Custom);
580
Evan Chengd25e9e82006-02-02 00:28:23 +0000581 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000582 setOperationAction(ISD::FSIN , MVT::f64, Expand);
583 setOperationAction(ISD::FCOS , MVT::f64, Expand);
584 setOperationAction(ISD::FSIN , MVT::f32, Expand);
585 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000586
Chris Lattnera54aa942006-01-29 06:26:08 +0000587 // Expand FP immediates into loads from the stack, except for the special
588 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000589 addLegalFPImmediate(APFloat(+0.0)); // xorpd
590 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Evan Chengc7ce29b2009-02-13 22:36:38 +0000591 } else if (!UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000592 // Use SSE for f32, x87 for f64.
593 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000594 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
595 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000596
597 // Use ANDPS to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000598 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000599
600 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000601 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000602
Owen Anderson825b72b2009-08-11 20:47:22 +0000603 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000604
605 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000606 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
607 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000608
609 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000610 setOperationAction(ISD::FSIN , MVT::f32, Expand);
611 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000612
Nate Begemane1795842008-02-14 08:57:00 +0000613 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000614 addLegalFPImmediate(APFloat(+0.0f)); // xorps
615 addLegalFPImmediate(APFloat(+0.0)); // FLD0
616 addLegalFPImmediate(APFloat(+1.0)); // FLD1
617 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
618 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
619
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000620 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000621 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
622 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000623 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000624 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000625 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000626 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000627 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
628 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000629
Owen Anderson825b72b2009-08-11 20:47:22 +0000630 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
631 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
632 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
633 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000634
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000635 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000636 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
637 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000638 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000639 addLegalFPImmediate(APFloat(+0.0)); // FLD0
640 addLegalFPImmediate(APFloat(+1.0)); // FLD1
641 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
642 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000643 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
644 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
645 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
646 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000647 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000648
Dale Johannesen59a58732007-08-05 18:49:15 +0000649 // Long double always uses X87.
Evan Cheng92722532009-03-26 23:06:32 +0000650 if (!UseSoftFloat) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000651 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
652 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
653 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000654 {
Benjamin Kramer98383962010-12-04 14:22:24 +0000655 APFloat TmpFlt = APFloat::getZero(APFloat::x87DoubleExtended);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000656 addLegalFPImmediate(TmpFlt); // FLD0
657 TmpFlt.changeSign();
658 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
Benjamin Kramer98383962010-12-04 14:22:24 +0000659
660 bool ignored;
Evan Chengc7ce29b2009-02-13 22:36:38 +0000661 APFloat TmpFlt2(+1.0);
662 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
663 &ignored);
664 addLegalFPImmediate(TmpFlt2); // FLD1
665 TmpFlt2.changeSign();
666 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
667 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000668
Evan Chengc7ce29b2009-02-13 22:36:38 +0000669 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000670 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
671 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000672 }
Dale Johannesen2f429012007-09-26 21:10:55 +0000673 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000674
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000675 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000676 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
677 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
678 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000679
Owen Anderson825b72b2009-08-11 20:47:22 +0000680 setOperationAction(ISD::FLOG, MVT::f80, Expand);
681 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
682 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
683 setOperationAction(ISD::FEXP, MVT::f80, Expand);
684 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000685
Mon P Wangf007a8b2008-11-06 05:31:54 +0000686 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000687 // (for widening) or expand (for scalarization). Then we will selectively
688 // turn on ones that can be effectively codegen'd.
Owen Anderson825b72b2009-08-11 20:47:22 +0000689 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
690 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
691 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
692 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
693 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
694 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
695 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
696 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
697 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
698 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
699 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
700 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
701 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
702 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
703 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
704 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
705 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000706 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
David Greenecfe33c42011-01-26 19:13:22 +0000707 setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
708 setOperationAction(ISD::INSERT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000709 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
710 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
711 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
712 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
713 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
714 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
715 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
716 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
717 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
718 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
719 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
720 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
721 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
722 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
723 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
724 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
725 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
726 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
727 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
728 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
729 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
730 setOperationAction(ISD::VSETCC, (MVT::SimpleValueType)VT, Expand);
731 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
732 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
733 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
734 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
735 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
736 setOperationAction(ISD::FP_TO_UINT, (MVT::SimpleValueType)VT, Expand);
737 setOperationAction(ISD::FP_TO_SINT, (MVT::SimpleValueType)VT, Expand);
738 setOperationAction(ISD::UINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
739 setOperationAction(ISD::SINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
Dan Gohman87862e72009-12-11 21:31:27 +0000740 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000741 setOperationAction(ISD::TRUNCATE, (MVT::SimpleValueType)VT, Expand);
742 setOperationAction(ISD::SIGN_EXTEND, (MVT::SimpleValueType)VT, Expand);
743 setOperationAction(ISD::ZERO_EXTEND, (MVT::SimpleValueType)VT, Expand);
744 setOperationAction(ISD::ANY_EXTEND, (MVT::SimpleValueType)VT, Expand);
745 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
746 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
747 setTruncStoreAction((MVT::SimpleValueType)VT,
748 (MVT::SimpleValueType)InnerVT, Expand);
749 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
750 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
751 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000752 }
753
Evan Chengc7ce29b2009-02-13 22:36:38 +0000754 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
755 // with -msoft-float, disable use of MMX as well.
Chris Lattner2a786eb2010-12-19 20:19:20 +0000756 if (!UseSoftFloat && Subtarget->hasMMX()) {
Dale Johannesene93d99c2010-10-20 21:32:10 +0000757 addRegisterClass(MVT::x86mmx, X86::VR64RegisterClass);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000758 // No operations on x86mmx supported, everything uses intrinsics.
Evan Cheng470a6ad2006-02-22 02:26:30 +0000759 }
760
Dale Johannesen0488fb62010-09-30 23:57:10 +0000761 // MMX-sized vectors (other than x86mmx) are expected to be expanded
762 // into smaller operations.
763 setOperationAction(ISD::MULHS, MVT::v8i8, Expand);
764 setOperationAction(ISD::MULHS, MVT::v4i16, Expand);
765 setOperationAction(ISD::MULHS, MVT::v2i32, Expand);
766 setOperationAction(ISD::MULHS, MVT::v1i64, Expand);
767 setOperationAction(ISD::AND, MVT::v8i8, Expand);
768 setOperationAction(ISD::AND, MVT::v4i16, Expand);
769 setOperationAction(ISD::AND, MVT::v2i32, Expand);
770 setOperationAction(ISD::AND, MVT::v1i64, Expand);
771 setOperationAction(ISD::OR, MVT::v8i8, Expand);
772 setOperationAction(ISD::OR, MVT::v4i16, Expand);
773 setOperationAction(ISD::OR, MVT::v2i32, Expand);
774 setOperationAction(ISD::OR, MVT::v1i64, Expand);
775 setOperationAction(ISD::XOR, MVT::v8i8, Expand);
776 setOperationAction(ISD::XOR, MVT::v4i16, Expand);
777 setOperationAction(ISD::XOR, MVT::v2i32, Expand);
778 setOperationAction(ISD::XOR, MVT::v1i64, Expand);
779 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Expand);
780 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Expand);
781 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Expand);
782 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Expand);
783 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v1i64, Expand);
784 setOperationAction(ISD::SELECT, MVT::v8i8, Expand);
785 setOperationAction(ISD::SELECT, MVT::v4i16, Expand);
786 setOperationAction(ISD::SELECT, MVT::v2i32, Expand);
787 setOperationAction(ISD::SELECT, MVT::v1i64, Expand);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000788 setOperationAction(ISD::BITCAST, MVT::v8i8, Expand);
789 setOperationAction(ISD::BITCAST, MVT::v4i16, Expand);
790 setOperationAction(ISD::BITCAST, MVT::v2i32, Expand);
791 setOperationAction(ISD::BITCAST, MVT::v1i64, Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000792
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000793 if (!UseSoftFloat && Subtarget->hasXMM()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000794 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000795
Owen Anderson825b72b2009-08-11 20:47:22 +0000796 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
797 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
798 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
799 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
800 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
801 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
802 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
803 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
804 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
805 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
806 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
807 setOperationAction(ISD::VSETCC, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000808 }
809
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000810 if (!UseSoftFloat && Subtarget->hasXMMInt()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000811 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000812
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000813 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
814 // registers cannot be used even for integer operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000815 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
816 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
817 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
818 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000819
Owen Anderson825b72b2009-08-11 20:47:22 +0000820 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
821 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
822 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
823 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
824 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
825 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
826 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
827 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
828 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
829 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
830 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
831 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
832 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
833 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
834 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
835 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000836
Owen Anderson825b72b2009-08-11 20:47:22 +0000837 setOperationAction(ISD::VSETCC, MVT::v2f64, Custom);
838 setOperationAction(ISD::VSETCC, MVT::v16i8, Custom);
839 setOperationAction(ISD::VSETCC, MVT::v8i16, Custom);
840 setOperationAction(ISD::VSETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000841
Owen Anderson825b72b2009-08-11 20:47:22 +0000842 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
843 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
844 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
845 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
846 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000847
Mon P Wangeb38ebf2010-01-24 00:05:03 +0000848 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2f64, Custom);
849 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2i64, Custom);
850 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i8, Custom);
851 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i16, Custom);
852 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Custom);
853
Evan Cheng2c3ae372006-04-12 21:21:57 +0000854 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Owen Anderson825b72b2009-08-11 20:47:22 +0000855 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
856 EVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000857 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000858 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000859 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000860 // Do not attempt to custom lower non-128-bit vectors
861 if (!VT.is128BitVector())
862 continue;
Owen Anderson825b72b2009-08-11 20:47:22 +0000863 setOperationAction(ISD::BUILD_VECTOR,
864 VT.getSimpleVT().SimpleTy, Custom);
865 setOperationAction(ISD::VECTOR_SHUFFLE,
866 VT.getSimpleVT().SimpleTy, Custom);
867 setOperationAction(ISD::EXTRACT_VECTOR_ELT,
868 VT.getSimpleVT().SimpleTy, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000869 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000870
Owen Anderson825b72b2009-08-11 20:47:22 +0000871 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
872 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
873 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
874 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
875 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
876 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000877
Nate Begemancdd1eec2008-02-12 22:51:28 +0000878 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000879 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
880 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000881 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000882
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000883 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000884 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; i++) {
885 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
Owen Andersone50ed302009-08-10 22:56:29 +0000886 EVT VT = SVT;
David Greene9b9838d2009-06-29 16:47:10 +0000887
888 // Do not attempt to promote non-128-bit vectors
Chris Lattner32b4b5a2010-07-05 05:53:14 +0000889 if (!VT.is128BitVector())
David Greene9b9838d2009-06-29 16:47:10 +0000890 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +0000891
Owen Andersond6662ad2009-08-10 20:46:15 +0000892 setOperationAction(ISD::AND, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000893 AddPromotedToType (ISD::AND, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000894 setOperationAction(ISD::OR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000895 AddPromotedToType (ISD::OR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000896 setOperationAction(ISD::XOR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000897 AddPromotedToType (ISD::XOR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000898 setOperationAction(ISD::LOAD, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000899 AddPromotedToType (ISD::LOAD, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000900 setOperationAction(ISD::SELECT, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000901 AddPromotedToType (ISD::SELECT, SVT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000902 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000903
Owen Anderson825b72b2009-08-11 20:47:22 +0000904 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000905
Evan Cheng2c3ae372006-04-12 21:21:57 +0000906 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000907 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
908 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
909 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
910 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000911
Owen Anderson825b72b2009-08-11 20:47:22 +0000912 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
913 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000914 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000915
Nate Begeman14d12ca2008-02-11 04:19:36 +0000916 if (Subtarget->hasSSE41()) {
Dale Johannesen54feef22010-05-27 20:12:41 +0000917 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
918 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
919 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
920 setOperationAction(ISD::FRINT, MVT::f32, Legal);
921 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
922 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
923 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
924 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
925 setOperationAction(ISD::FRINT, MVT::f64, Legal);
926 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
927
Nate Begeman14d12ca2008-02-11 04:19:36 +0000928 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +0000929 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000930
Nate Begemanbdcb5af2010-07-27 22:37:06 +0000931 // Can turn SHL into an integer multiply.
932 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
Nate Begeman51409212010-07-28 00:21:48 +0000933 setOperationAction(ISD::SHL, MVT::v16i8, Custom);
Nate Begemanbdcb5af2010-07-27 22:37:06 +0000934
Nate Begeman14d12ca2008-02-11 04:19:36 +0000935 // i8 and i16 vectors are custom , because the source register and source
936 // source memory operand types are not the same width. f32 vectors are
937 // custom since the immediate controlling the insert encodes additional
938 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +0000939 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
940 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
941 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
942 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000943
Owen Anderson825b72b2009-08-11 20:47:22 +0000944 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
945 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
946 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
947 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000948
949 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000950 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
951 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000952 }
953 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000954
Nadav Rotem43012222011-05-11 08:12:09 +0000955 if (Subtarget->hasSSE2()) {
956 setOperationAction(ISD::SRL, MVT::v2i64, Custom);
957 setOperationAction(ISD::SRL, MVT::v4i32, Custom);
958 setOperationAction(ISD::SRL, MVT::v16i8, Custom);
959
960 setOperationAction(ISD::SHL, MVT::v2i64, Custom);
961 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
962 setOperationAction(ISD::SHL, MVT::v8i16, Custom);
963
964 setOperationAction(ISD::SRA, MVT::v4i32, Custom);
965 setOperationAction(ISD::SRA, MVT::v8i16, Custom);
966 }
967
Chris Lattnera34b3cf2010-12-19 20:03:11 +0000968 if (Subtarget->hasSSE42())
Owen Anderson825b72b2009-08-11 20:47:22 +0000969 setOperationAction(ISD::VSETCC, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000970
David Greene9b9838d2009-06-29 16:47:10 +0000971 if (!UseSoftFloat && Subtarget->hasAVX()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000972 addRegisterClass(MVT::v8f32, X86::VR256RegisterClass);
973 addRegisterClass(MVT::v4f64, X86::VR256RegisterClass);
974 addRegisterClass(MVT::v8i32, X86::VR256RegisterClass);
975 addRegisterClass(MVT::v4i64, X86::VR256RegisterClass);
Bruno Cardoso Lopes405f11b2010-08-10 01:43:16 +0000976 addRegisterClass(MVT::v32i8, X86::VR256RegisterClass);
David Greened94c1012009-06-29 22:50:51 +0000977
Owen Anderson825b72b2009-08-11 20:47:22 +0000978 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
979 setOperationAction(ISD::LOAD, MVT::v8i32, Legal);
980 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
981 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
David Greene54d8eba2011-01-27 22:38:56 +0000982
Owen Anderson825b72b2009-08-11 20:47:22 +0000983 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
984 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
985 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
986 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
987 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
988 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000989
Owen Anderson825b72b2009-08-11 20:47:22 +0000990 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
991 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
992 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
993 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
994 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
995 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000996
David Greene54d8eba2011-01-27 22:38:56 +0000997 // Custom lower build_vector, vector_shuffle, scalar_to_vector,
998 // insert_vector_elt extract_subvector and extract_vector_elt for
999 // 256-bit types.
1000 for (unsigned i = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
1001 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE;
1002 ++i) {
1003 MVT::SimpleValueType VT = (MVT::SimpleValueType)i;
1004 // Do not attempt to custom lower non-256-bit vectors
1005 if (!isPowerOf2_32(MVT(VT).getVectorNumElements())
1006 || (MVT(VT).getSizeInBits() < 256))
David Greene9b9838d2009-06-29 16:47:10 +00001007 continue;
David Greene9b9838d2009-06-29 16:47:10 +00001008 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
1009 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
David Greene54d8eba2011-01-27 22:38:56 +00001010 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001011 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
David Greene54d8eba2011-01-27 22:38:56 +00001012 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001013 }
David Greene54d8eba2011-01-27 22:38:56 +00001014 // Custom-lower insert_subvector and extract_subvector based on
1015 // the result type.
1016 for (unsigned i = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
1017 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE;
1018 ++i) {
1019 MVT::SimpleValueType VT = (MVT::SimpleValueType)i;
1020 // Do not attempt to custom lower non-256-bit vectors
1021 if (!isPowerOf2_32(MVT(VT).getVectorNumElements()))
David Greene9b9838d2009-06-29 16:47:10 +00001022 continue;
David Greene54d8eba2011-01-27 22:38:56 +00001023
1024 if (MVT(VT).getSizeInBits() == 128) {
1025 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001026 }
David Greene54d8eba2011-01-27 22:38:56 +00001027 else if (MVT(VT).getSizeInBits() == 256) {
1028 setOperationAction(ISD::INSERT_SUBVECTOR, VT, Custom);
1029 }
David Greene9b9838d2009-06-29 16:47:10 +00001030 }
1031
David Greene54d8eba2011-01-27 22:38:56 +00001032 // Promote v32i8, v16i16, v8i32 select, and, or, xor to v4i64.
1033 // Don't promote loads because we need them for VPERM vector index versions.
1034
1035 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
1036 VT != (unsigned)MVT::LAST_VECTOR_VALUETYPE;
1037 VT++) {
1038 if (!isPowerOf2_32(MVT((MVT::SimpleValueType)VT).getVectorNumElements())
1039 || (MVT((MVT::SimpleValueType)VT).getSizeInBits() < 256))
1040 continue;
1041 setOperationAction(ISD::AND, (MVT::SimpleValueType)VT, Promote);
1042 AddPromotedToType (ISD::AND, (MVT::SimpleValueType)VT, MVT::v4i64);
1043 setOperationAction(ISD::OR, (MVT::SimpleValueType)VT, Promote);
1044 AddPromotedToType (ISD::OR, (MVT::SimpleValueType)VT, MVT::v4i64);
1045 setOperationAction(ISD::XOR, (MVT::SimpleValueType)VT, Promote);
1046 AddPromotedToType (ISD::XOR, (MVT::SimpleValueType)VT, MVT::v4i64);
1047 //setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Promote);
1048 //AddPromotedToType (ISD::LOAD, (MVT::SimpleValueType)VT, MVT::v4i64);
1049 setOperationAction(ISD::SELECT, (MVT::SimpleValueType)VT, Promote);
1050 AddPromotedToType (ISD::SELECT, (MVT::SimpleValueType)VT, MVT::v4i64);
1051 }
David Greene9b9838d2009-06-29 16:47:10 +00001052 }
1053
Evan Cheng6be2c582006-04-05 23:38:46 +00001054 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +00001055 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +00001056
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001057
Eli Friedman962f5492010-06-02 19:35:46 +00001058 // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
1059 // handle type legalization for these operations here.
Dan Gohman71c62a22010-06-02 19:13:40 +00001060 //
Eli Friedman962f5492010-06-02 19:35:46 +00001061 // FIXME: We really should do custom legalization for addition and
1062 // subtraction on x86-32 once PR3203 is fixed. We really can't do much better
1063 // than generic legalization for 64-bit multiplication-with-overflow, though.
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001064 for (unsigned i = 0, e = 3+Subtarget->is64Bit(); i != e; ++i) {
1065 // Add/Sub/Mul with overflow operations are custom lowered.
1066 MVT VT = IntVTs[i];
1067 setOperationAction(ISD::SADDO, VT, Custom);
1068 setOperationAction(ISD::UADDO, VT, Custom);
1069 setOperationAction(ISD::SSUBO, VT, Custom);
1070 setOperationAction(ISD::USUBO, VT, Custom);
1071 setOperationAction(ISD::SMULO, VT, Custom);
1072 setOperationAction(ISD::UMULO, VT, Custom);
Eli Friedmana993f0a2010-06-02 00:27:18 +00001073 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001074
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001075 // There are no 8-bit 3-address imul/mul instructions
1076 setOperationAction(ISD::SMULO, MVT::i8, Expand);
1077 setOperationAction(ISD::UMULO, MVT::i8, Expand);
Bill Wendling41ea7e72008-11-24 19:21:46 +00001078
Evan Chengd54f2d52009-03-31 19:38:51 +00001079 if (!Subtarget->is64Bit()) {
1080 // These libcalls are not available in 32-bit.
1081 setLibcallName(RTLIB::SHL_I128, 0);
1082 setLibcallName(RTLIB::SRL_I128, 0);
1083 setLibcallName(RTLIB::SRA_I128, 0);
1084 }
1085
Evan Cheng206ee9d2006-07-07 08:33:52 +00001086 // We have target-specific dag combine patterns for the following nodes:
1087 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00001088 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Evan Chengd880b972008-05-09 21:53:03 +00001089 setTargetDAGCombine(ISD::BUILD_VECTOR);
Chris Lattner83e6c992006-10-04 06:57:07 +00001090 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +00001091 setTargetDAGCombine(ISD::SHL);
1092 setTargetDAGCombine(ISD::SRA);
1093 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +00001094 setTargetDAGCombine(ISD::OR);
Nate Begemanb65c1752010-12-17 22:55:37 +00001095 setTargetDAGCombine(ISD::AND);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001096 setTargetDAGCombine(ISD::ADD);
1097 setTargetDAGCombine(ISD::SUB);
Chris Lattner149a4e52008-02-22 02:09:43 +00001098 setTargetDAGCombine(ISD::STORE);
Evan Cheng2e489c42009-12-16 00:53:11 +00001099 setTargetDAGCombine(ISD::ZERO_EXTEND);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +00001100 setTargetDAGCombine(ISD::SINT_TO_FP);
Evan Cheng0b0cd912009-03-28 05:57:29 +00001101 if (Subtarget->is64Bit())
1102 setTargetDAGCombine(ISD::MUL);
Evan Cheng206ee9d2006-07-07 08:33:52 +00001103
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001104 computeRegisterProperties();
1105
Evan Cheng05219282011-01-06 06:52:41 +00001106 // On Darwin, -Os means optimize for size without hurting performance,
1107 // do not reduce the limit.
Dan Gohman87060f52008-06-30 21:00:56 +00001108 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001109 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 16 : 8;
Evan Cheng255f20f2010-04-01 06:04:33 +00001110 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001111 maxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
1112 maxStoresPerMemmove = 8; // For @llvm.memmove -> sequence of stores
1113 maxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Evan Chengfb8075d2008-02-28 00:43:03 +00001114 setPrefLoopAlignment(16);
Evan Cheng6ebf7bc2009-05-13 21:42:09 +00001115 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +00001116
1117 setPrefFunctionAlignment(4);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001118}
1119
Scott Michel5b8f82e2008-03-10 15:42:14 +00001120
Owen Anderson825b72b2009-08-11 20:47:22 +00001121MVT::SimpleValueType X86TargetLowering::getSetCCResultType(EVT VT) const {
1122 return MVT::i8;
Scott Michel5b8f82e2008-03-10 15:42:14 +00001123}
1124
1125
Evan Cheng29286502008-01-23 23:17:41 +00001126/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1127/// the desired ByVal argument alignment.
1128static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
1129 if (MaxAlign == 16)
1130 return;
1131 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
1132 if (VTy->getBitWidth() == 128)
1133 MaxAlign = 16;
Evan Cheng29286502008-01-23 23:17:41 +00001134 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
1135 unsigned EltAlign = 0;
1136 getMaxByValAlign(ATy->getElementType(), EltAlign);
1137 if (EltAlign > MaxAlign)
1138 MaxAlign = EltAlign;
1139 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
1140 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1141 unsigned EltAlign = 0;
1142 getMaxByValAlign(STy->getElementType(i), EltAlign);
1143 if (EltAlign > MaxAlign)
1144 MaxAlign = EltAlign;
1145 if (MaxAlign == 16)
1146 break;
1147 }
1148 }
1149 return;
1150}
1151
1152/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1153/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001154/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1155/// are at 4-byte boundaries.
Evan Cheng29286502008-01-23 23:17:41 +00001156unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001157 if (Subtarget->is64Bit()) {
1158 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001159 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001160 if (TyAlign > 8)
1161 return TyAlign;
1162 return 8;
1163 }
1164
Evan Cheng29286502008-01-23 23:17:41 +00001165 unsigned Align = 4;
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001166 if (Subtarget->hasXMM())
Dale Johannesen0c191872008-02-08 19:48:20 +00001167 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001168 return Align;
1169}
Chris Lattner2b02a442007-02-25 08:29:00 +00001170
Evan Chengf0df0312008-05-15 08:39:06 +00001171/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001172/// and store operations as a result of memset, memcpy, and memmove
1173/// lowering. If DstAlign is zero that means it's safe to destination
1174/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1175/// means there isn't a need to check it against alignment requirement,
1176/// probably because the source does not need to be loaded. If
1177/// 'NonScalarIntSafe' is true, that means it's safe to return a
1178/// non-scalar-integer type, e.g. empty string source, constant, or loaded
1179/// from memory. 'MemcpyStrSrc' indicates whether the memcpy source is
1180/// constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001181/// It returns EVT::Other if the type should be determined using generic
1182/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001183EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001184X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1185 unsigned DstAlign, unsigned SrcAlign,
Evan Chengf28f8bc2010-04-02 19:36:14 +00001186 bool NonScalarIntSafe,
Evan Chengc3b0c342010-04-08 07:37:57 +00001187 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001188 MachineFunction &MF) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001189 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1190 // linux. This is because the stack realignment code can't handle certain
1191 // cases like PR2962. This should be removed when PR2962 is fixed.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001192 const Function *F = MF.getFunction();
Evan Chenga5e13622011-01-07 19:35:30 +00001193 if (NonScalarIntSafe &&
1194 !F->hasFnAttr(Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001195 if (Size >= 16 &&
Evan Chenga5e13622011-01-07 19:35:30 +00001196 (Subtarget->isUnalignedMemAccessFast() ||
1197 ((DstAlign == 0 || DstAlign >= 16) &&
1198 (SrcAlign == 0 || SrcAlign >= 16))) &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001199 Subtarget->getStackAlignment() >= 16) {
1200 if (Subtarget->hasSSE2())
1201 return MVT::v4i32;
Evan Chengf28f8bc2010-04-02 19:36:14 +00001202 if (Subtarget->hasSSE1())
Evan Cheng255f20f2010-04-01 06:04:33 +00001203 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001204 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001205 !Subtarget->is64Bit() &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001206 Subtarget->getStackAlignment() >= 8 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001207 Subtarget->hasXMMInt()) {
Evan Chengc3b0c342010-04-08 07:37:57 +00001208 // Do not use f64 to lower memcpy if source is string constant. It's
1209 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001210 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001211 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001212 }
Evan Chengf0df0312008-05-15 08:39:06 +00001213 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001214 return MVT::i64;
1215 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001216}
1217
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001218/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1219/// current function. The returned value is a member of the
1220/// MachineJumpTableInfo::JTEntryKind enum.
1221unsigned X86TargetLowering::getJumpTableEncoding() const {
1222 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1223 // symbol.
1224 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1225 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001226 return MachineJumpTableInfo::EK_Custom32;
Michael J. Spencerec38de22010-10-10 22:04:20 +00001227
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001228 // Otherwise, use the normal jump table encoding heuristics.
1229 return TargetLowering::getJumpTableEncoding();
1230}
1231
Chris Lattnerc64daab2010-01-26 05:02:42 +00001232const MCExpr *
1233X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1234 const MachineBasicBlock *MBB,
1235 unsigned uid,MCContext &Ctx) const{
1236 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1237 Subtarget->isPICStyleGOT());
1238 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1239 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001240 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1241 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001242}
1243
Evan Chengcc415862007-11-09 01:32:10 +00001244/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1245/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001246SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001247 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001248 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001249 // This doesn't have DebugLoc associated with it, but is not really the
1250 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001251 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001252 return Table;
1253}
1254
Chris Lattner589c6f62010-01-26 06:28:43 +00001255/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1256/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1257/// MCExpr.
1258const MCExpr *X86TargetLowering::
1259getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1260 MCContext &Ctx) const {
1261 // X86-64 uses RIP relative addressing based on the jump table label.
1262 if (Subtarget->isPICStyleRIPRel())
1263 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1264
1265 // Otherwise, the reference is relative to the PIC base.
Chris Lattner142b5312010-11-14 22:48:15 +00001266 return MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
Chris Lattner589c6f62010-01-26 06:28:43 +00001267}
1268
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001269// FIXME: Why this routine is here? Move to RegInfo!
Evan Chengdee81012010-07-26 21:50:05 +00001270std::pair<const TargetRegisterClass*, uint8_t>
1271X86TargetLowering::findRepresentativeClass(EVT VT) const{
1272 const TargetRegisterClass *RRC = 0;
1273 uint8_t Cost = 1;
1274 switch (VT.getSimpleVT().SimpleTy) {
1275 default:
1276 return TargetLowering::findRepresentativeClass(VT);
1277 case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
1278 RRC = (Subtarget->is64Bit()
1279 ? X86::GR64RegisterClass : X86::GR32RegisterClass);
1280 break;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001281 case MVT::x86mmx:
Evan Chengdee81012010-07-26 21:50:05 +00001282 RRC = X86::VR64RegisterClass;
1283 break;
1284 case MVT::f32: case MVT::f64:
1285 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1286 case MVT::v4f32: case MVT::v2f64:
1287 case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1288 case MVT::v4f64:
1289 RRC = X86::VR128RegisterClass;
1290 break;
1291 }
1292 return std::make_pair(RRC, Cost);
1293}
1294
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001295bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1296 unsigned &Offset) const {
1297 if (!Subtarget->isTargetLinux())
1298 return false;
1299
1300 if (Subtarget->is64Bit()) {
1301 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1302 Offset = 0x28;
1303 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1304 AddressSpace = 256;
1305 else
1306 AddressSpace = 257;
1307 } else {
1308 // %gs:0x14 on i386
1309 Offset = 0x14;
1310 AddressSpace = 256;
1311 }
1312 return true;
1313}
1314
1315
Chris Lattner2b02a442007-02-25 08:29:00 +00001316//===----------------------------------------------------------------------===//
1317// Return Value Calling Convention Implementation
1318//===----------------------------------------------------------------------===//
1319
Chris Lattner59ed56b2007-02-28 04:55:35 +00001320#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001321
Michael J. Spencerec38de22010-10-10 22:04:20 +00001322bool
Eric Christopher471e4222011-06-08 23:55:35 +00001323X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv,
1324 MachineFunction &MF, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001325 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001326 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001327 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001328 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001329 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001330 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001331}
1332
Dan Gohman98ca4f22009-08-05 01:29:28 +00001333SDValue
1334X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001335 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001336 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001337 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001338 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001339 MachineFunction &MF = DAG.getMachineFunction();
1340 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001341
Chris Lattner9774c912007-02-27 05:28:59 +00001342 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001343 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001344 RVLocs, *DAG.getContext());
1345 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001346
Evan Chengdcea1632010-02-04 02:40:39 +00001347 // Add the regs to the liveout set for the function.
1348 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1349 for (unsigned i = 0; i != RVLocs.size(); ++i)
1350 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1351 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001352
Dan Gohman475871a2008-07-27 21:46:04 +00001353 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001354
Dan Gohman475871a2008-07-27 21:46:04 +00001355 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001356 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1357 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001358 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1359 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001360
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001361 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001362 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1363 CCValAssign &VA = RVLocs[i];
1364 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001365 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001366 EVT ValVT = ValToCopy.getValueType();
1367
Dale Johannesenc4510512010-09-24 19:05:48 +00001368 // If this is x86-64, and we disabled SSE, we can't return FP values,
1369 // or SSE or MMX vectors.
1370 if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
1371 VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001372 (Subtarget->is64Bit() && !Subtarget->hasXMM())) {
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001373 report_fatal_error("SSE register return with SSE disabled");
1374 }
1375 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1376 // llvm-gcc has never done it right and no one has noticed, so this
1377 // should be OK for now.
1378 if (ValVT == MVT::f64 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001379 (Subtarget->is64Bit() && !Subtarget->hasXMMInt()))
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001380 report_fatal_error("SSE2 register return with SSE2 disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001381
Chris Lattner447ff682008-03-11 03:23:40 +00001382 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1383 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001384 if (VA.getLocReg() == X86::ST0 ||
1385 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001386 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1387 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001388 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001389 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001390 RetOps.push_back(ValToCopy);
1391 // Don't emit a copytoreg.
1392 continue;
1393 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001394
Evan Cheng242b38b2009-02-23 09:03:22 +00001395 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1396 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001397 if (Subtarget->is64Bit()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001398 if (ValVT == MVT::x86mmx) {
Chris Lattner97a2a562010-08-26 05:24:29 +00001399 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001400 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ValToCopy);
Eric Christopher90eb4022010-07-22 00:26:08 +00001401 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1402 ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001403 // If we don't have SSE2 available, convert to v4f32 so the generated
1404 // register is legal.
1405 if (!Subtarget->hasSSE2())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001406 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32,ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001407 }
Evan Cheng242b38b2009-02-23 09:03:22 +00001408 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001409 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00001410
Dale Johannesendd64c412009-02-04 00:33:20 +00001411 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001412 Flag = Chain.getValue(1);
1413 }
Dan Gohman61a92132008-04-21 23:59:07 +00001414
1415 // The x86-64 ABI for returning structs by value requires that we copy
1416 // the sret argument into %rax for the return. We saved the argument into
1417 // a virtual register in the entry block, so now we copy the value out
1418 // and into %rax.
1419 if (Subtarget->is64Bit() &&
1420 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1421 MachineFunction &MF = DAG.getMachineFunction();
1422 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1423 unsigned Reg = FuncInfo->getSRetReturnReg();
Michael J. Spencerec38de22010-10-10 22:04:20 +00001424 assert(Reg &&
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001425 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001426 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001427
Dale Johannesendd64c412009-02-04 00:33:20 +00001428 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001429 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001430
1431 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001432 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001433 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001434
Chris Lattner447ff682008-03-11 03:23:40 +00001435 RetOps[0] = Chain; // Update chain.
1436
1437 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001438 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001439 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001440
1441 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001442 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001443}
1444
Evan Cheng3d2125c2010-11-30 23:55:39 +00001445bool X86TargetLowering::isUsedByReturnOnly(SDNode *N) const {
1446 if (N->getNumValues() != 1)
1447 return false;
1448 if (!N->hasNUsesOfValue(1, 0))
1449 return false;
1450
1451 SDNode *Copy = *N->use_begin();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001452 if (Copy->getOpcode() != ISD::CopyToReg &&
1453 Copy->getOpcode() != ISD::FP_EXTEND)
Evan Cheng3d2125c2010-11-30 23:55:39 +00001454 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001455
1456 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001457 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001458 UI != UE; ++UI) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001459 if (UI->getOpcode() != X86ISD::RET_FLAG)
1460 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001461 HasRet = true;
1462 }
Evan Cheng3d2125c2010-11-30 23:55:39 +00001463
Evan Cheng1bf891a2010-12-01 22:59:46 +00001464 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001465}
1466
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001467EVT
1468X86TargetLowering::getTypeForExtArgOrReturn(LLVMContext &Context, EVT VT,
Cameron Zwarich44579682011-03-17 14:21:56 +00001469 ISD::NodeType ExtendKind) const {
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001470 MVT ReturnMVT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001471 // TODO: Is this also valid on 32-bit?
1472 if (Subtarget->is64Bit() && VT == MVT::i1 && ExtendKind == ISD::ZERO_EXTEND)
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001473 ReturnMVT = MVT::i8;
1474 else
1475 ReturnMVT = MVT::i32;
1476
1477 EVT MinVT = getRegisterType(Context, ReturnMVT);
1478 return VT.bitsLT(MinVT) ? MinVT : VT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001479}
1480
Dan Gohman98ca4f22009-08-05 01:29:28 +00001481/// LowerCallResult - Lower the result values of a call into the
1482/// appropriate copies out of appropriate physical registers.
1483///
1484SDValue
1485X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001486 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001487 const SmallVectorImpl<ISD::InputArg> &Ins,
1488 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001489 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001490
Chris Lattnere32bbf62007-02-28 07:09:55 +00001491 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001492 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001493 bool Is64Bit = Subtarget->is64Bit();
Eric Christopher471e4222011-06-08 23:55:35 +00001494 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1495 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001496 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001497
Chris Lattner3085e152007-02-25 08:59:22 +00001498 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001499 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001500 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001501 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001502
Torok Edwin3f142c32009-02-01 18:15:56 +00001503 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001504 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001505 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasXMM())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001506 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001507 }
1508
Evan Cheng79fb3b42009-02-20 20:43:02 +00001509 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001510
1511 // If this is a call to a function that returns an fp value on the floating
1512 // point stack, we must guarantee the the value is popped from the stack, so
1513 // a CopyFromReg is not good enough - the copy instruction may be eliminated
1514 // if the return value is not used. We use the FpGET_ST0 instructions
1515 // instead.
1516 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1517 // If we prefer to use the value in xmm registers, copy it out as f80 and
1518 // use a truncate to move it from fp stack reg to xmm reg.
1519 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
1520 bool isST0 = VA.getLocReg() == X86::ST0;
1521 unsigned Opc = 0;
1522 if (CopyVT == MVT::f32) Opc = isST0 ? X86::FpGET_ST0_32:X86::FpGET_ST1_32;
1523 if (CopyVT == MVT::f64) Opc = isST0 ? X86::FpGET_ST0_64:X86::FpGET_ST1_64;
1524 if (CopyVT == MVT::f80) Opc = isST0 ? X86::FpGET_ST0_80:X86::FpGET_ST1_80;
1525 SDValue Ops[] = { Chain, InFlag };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001526 Chain = SDValue(DAG.getMachineNode(Opc, dl, CopyVT, MVT::Other, MVT::Glue,
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001527 Ops, 2), 1);
1528 Val = Chain.getValue(0);
1529
1530 // Round the f80 to the right size, which also moves it to the appropriate
1531 // xmm register.
1532 if (CopyVT != VA.getValVT())
1533 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1534 // This truncation won't change the value.
1535 DAG.getIntPtrConstant(1));
Evan Cheng79fb3b42009-02-20 20:43:02 +00001536 } else {
1537 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1538 CopyVT, InFlag).getValue(1);
1539 Val = Chain.getValue(0);
1540 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001541 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001542 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001543 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001544
Dan Gohman98ca4f22009-08-05 01:29:28 +00001545 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001546}
1547
1548
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001549//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001550// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001551//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001552// StdCall calling convention seems to be standard for many Windows' API
1553// routines and around. It differs from C calling convention just a little:
1554// callee should clean up the stack, not caller. Symbols should be also
1555// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001556// For info on fast calling convention see Fast Calling Convention (tail call)
1557// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001558
Dan Gohman98ca4f22009-08-05 01:29:28 +00001559/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001560/// semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001561static bool CallIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
1562 if (Outs.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001563 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001564
Dan Gohman98ca4f22009-08-05 01:29:28 +00001565 return Outs[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001566}
1567
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001568/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001569/// return semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001570static bool
1571ArgsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
1572 if (Ins.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001573 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001574
Dan Gohman98ca4f22009-08-05 01:29:28 +00001575 return Ins[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001576}
1577
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001578/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1579/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001580/// the specific parameter attribute. The copy will be passed as a byval
1581/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001582static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001583CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001584 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1585 DebugLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00001586 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Michael J. Spencerec38de22010-10-10 22:04:20 +00001587
Dale Johannesendd64c412009-02-04 00:33:20 +00001588 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Stuart Hastings03d58262011-03-10 00:25:53 +00001589 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001590 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001591}
1592
Chris Lattner29689432010-03-11 00:22:57 +00001593/// IsTailCallConvention - Return true if the calling convention is one that
1594/// supports tail call optimization.
1595static bool IsTailCallConvention(CallingConv::ID CC) {
1596 return (CC == CallingConv::Fast || CC == CallingConv::GHC);
1597}
1598
Evan Cheng485fafc2011-03-21 01:19:09 +00001599bool X86TargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
1600 if (!CI->isTailCall())
1601 return false;
1602
1603 CallSite CS(CI);
1604 CallingConv::ID CalleeCC = CS.getCallingConv();
1605 if (!IsTailCallConvention(CalleeCC) && CalleeCC != CallingConv::C)
1606 return false;
1607
1608 return true;
1609}
1610
Evan Cheng0c439eb2010-01-27 00:07:07 +00001611/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1612/// a tailcall target by changing its ABI.
1613static bool FuncIsMadeTailCallSafe(CallingConv::ID CC) {
Chris Lattner29689432010-03-11 00:22:57 +00001614 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001615}
1616
Dan Gohman98ca4f22009-08-05 01:29:28 +00001617SDValue
1618X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001619 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001620 const SmallVectorImpl<ISD::InputArg> &Ins,
1621 DebugLoc dl, SelectionDAG &DAG,
1622 const CCValAssign &VA,
1623 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001624 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001625 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001626 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Evan Cheng0c439eb2010-01-27 00:07:07 +00001627 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001628 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001629 EVT ValVT;
1630
1631 // If value is passed by pointer we have address passed instead of the value
1632 // itself.
1633 if (VA.getLocInfo() == CCValAssign::Indirect)
1634 ValVT = VA.getLocVT();
1635 else
1636 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001637
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001638 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001639 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001640 // In case of tail call optimization mark all arguments mutable. Since they
1641 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001642 if (Flags.isByVal()) {
Evan Chengee2e0e32011-03-30 23:44:13 +00001643 unsigned Bytes = Flags.getByValSize();
1644 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
1645 int FI = MFI->CreateFixedObject(Bytes, VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001646 return DAG.getFrameIndex(FI, getPointerTy());
1647 } else {
1648 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001649 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001650 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1651 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001652 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00001653 false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001654 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001655}
1656
Dan Gohman475871a2008-07-27 21:46:04 +00001657SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001658X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001659 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001660 bool isVarArg,
1661 const SmallVectorImpl<ISD::InputArg> &Ins,
1662 DebugLoc dl,
1663 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001664 SmallVectorImpl<SDValue> &InVals)
1665 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001666 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001667 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001668
Gordon Henriksen86737662008-01-05 16:56:59 +00001669 const Function* Fn = MF.getFunction();
1670 if (Fn->hasExternalLinkage() &&
1671 Subtarget->isTargetCygMing() &&
1672 Fn->getName() == "main")
1673 FuncInfo->setForceFramePointer(true);
1674
Evan Cheng1bc78042006-04-26 01:20:17 +00001675 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001676 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001677 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001678
Chris Lattner29689432010-03-11 00:22:57 +00001679 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1680 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001681
Chris Lattner638402b2007-02-28 07:00:42 +00001682 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001683 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001684 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001685 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001686
1687 // Allocate shadow area for Win64
1688 if (IsWin64) {
1689 CCInfo.AllocateStack(32, 8);
1690 }
1691
Duncan Sands45907662010-10-31 13:21:44 +00001692 CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001693
Chris Lattnerf39f7712007-02-28 05:46:49 +00001694 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001695 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001696 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1697 CCValAssign &VA = ArgLocs[i];
1698 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1699 // places.
1700 assert(VA.getValNo() != LastVal &&
1701 "Don't support value assigned to multiple locs yet");
1702 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001703
Chris Lattnerf39f7712007-02-28 05:46:49 +00001704 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001705 EVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001706 TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001707 if (RegVT == MVT::i32)
Chris Lattnerf39f7712007-02-28 05:46:49 +00001708 RC = X86::GR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001709 else if (Is64Bit && RegVT == MVT::i64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001710 RC = X86::GR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001711 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001712 RC = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001713 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001714 RC = X86::FR64RegisterClass;
Bruno Cardoso Lopesac098352010-08-05 23:35:51 +00001715 else if (RegVT.isVector() && RegVT.getSizeInBits() == 256)
1716 RC = X86::VR256RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001717 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001718 RC = X86::VR128RegisterClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001719 else if (RegVT == MVT::x86mmx)
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001720 RC = X86::VR64RegisterClass;
1721 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001722 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001723
Devang Patel68e6bee2011-02-21 23:21:26 +00001724 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001725 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001726
Chris Lattnerf39f7712007-02-28 05:46:49 +00001727 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1728 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1729 // right size.
1730 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001731 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001732 DAG.getValueType(VA.getValVT()));
1733 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001734 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001735 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001736 else if (VA.getLocInfo() == CCValAssign::BCvt)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001737 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001738
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001739 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001740 // Handle MMX values passed in XMM regs.
1741 if (RegVT.isVector()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001742 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(),
1743 ArgValue);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001744 } else
1745 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001746 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001747 } else {
1748 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001749 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001750 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001751
1752 // If value is passed via pointer - do a load.
1753 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00001754 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
1755 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001756
Dan Gohman98ca4f22009-08-05 01:29:28 +00001757 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001758 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001759
Dan Gohman61a92132008-04-21 23:59:07 +00001760 // The x86-64 ABI for returning structs by value requires that we copy
1761 // the sret argument into %rax for the return. Save the argument into
1762 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001763 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001764 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1765 unsigned Reg = FuncInfo->getSRetReturnReg();
1766 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001767 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001768 FuncInfo->setSRetReturnReg(Reg);
1769 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001770 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001771 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001772 }
1773
Chris Lattnerf39f7712007-02-28 05:46:49 +00001774 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001775 // Align stack specially for tail calls.
1776 if (FuncIsMadeTailCallSafe(CallConv))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001777 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001778
Evan Cheng1bc78042006-04-26 01:20:17 +00001779 // If the function takes variable number of arguments, make a frame index for
1780 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001781 if (isVarArg) {
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001782 if (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
1783 CallConv != CallingConv::X86_ThisCall)) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00001784 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00001785 }
1786 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001787 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1788
1789 // FIXME: We should really autogenerate these arrays
1790 static const unsigned GPR64ArgRegsWin64[] = {
1791 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001792 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001793 static const unsigned GPR64ArgRegs64Bit[] = {
1794 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1795 };
1796 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001797 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1798 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1799 };
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001800 const unsigned *GPR64ArgRegs;
1801 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001802
1803 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001804 // The XMM registers which might contain var arg parameters are shadowed
1805 // in their paired GPR. So we only need to save the GPR to their home
1806 // slots.
1807 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001808 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001809 } else {
1810 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1811 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001812
1813 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit, TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001814 }
1815 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1816 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001817
Devang Patel578efa92009-06-05 21:57:13 +00001818 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001819 assert(!(NumXMMRegs && !Subtarget->hasXMM()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001820 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001821 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001822 "SSE register cannot be used when SSE is disabled!");
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001823 if (UseSoftFloat || NoImplicitFloatOps || !Subtarget->hasXMM())
Torok Edwin3f142c32009-02-01 18:15:56 +00001824 // Kernel mode asks for SSE to be disabled, so don't push them
1825 // on the stack.
1826 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001827
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001828 if (IsWin64) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001829 const TargetFrameLowering &TFI = *getTargetMachine().getFrameLowering();
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001830 // Get to the caller-allocated home save location. Add 8 to account
1831 // for the return address.
1832 int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001833 FuncInfo->setRegSaveFrameIndex(
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001834 MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001835 // Fixup to set vararg frame on shadow area (4 x i64).
1836 if (NumIntRegs < 4)
1837 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001838 } else {
1839 // For X86-64, if there are vararg parameters that are passed via
1840 // registers, then we must store them to their spots on the stack so they
1841 // may be loaded by deferencing the result of va_next.
1842 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
1843 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
1844 FuncInfo->setRegSaveFrameIndex(
1845 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00001846 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001847 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001848
Gordon Henriksen86737662008-01-05 16:56:59 +00001849 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001850 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00001851 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
1852 getPointerTy());
1853 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001854 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00001855 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
1856 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00001857 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00001858 X86::GR64RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00001859 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001860 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001861 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001862 MachinePointerInfo::getFixedStack(
1863 FuncInfo->getRegSaveFrameIndex(), Offset),
1864 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001865 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001866 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001867 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001868
Dan Gohmanface41a2009-08-16 21:24:25 +00001869 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
1870 // Now store the XMM (fp + vector) parameter registers.
1871 SmallVector<SDValue, 11> SaveXMMOps;
1872 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001873
Devang Patel68e6bee2011-02-21 23:21:26 +00001874 unsigned AL = MF.addLiveIn(X86::AL, X86::GR8RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00001875 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
1876 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001877
Dan Gohman1e93df62010-04-17 14:41:14 +00001878 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1879 FuncInfo->getRegSaveFrameIndex()));
1880 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1881 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00001882
Dan Gohmanface41a2009-08-16 21:24:25 +00001883 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001884 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00001885 X86::VR128RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00001886 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
1887 SaveXMMOps.push_back(Val);
1888 }
1889 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
1890 MVT::Other,
1891 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00001892 }
Dan Gohmanface41a2009-08-16 21:24:25 +00001893
1894 if (!MemOps.empty())
1895 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1896 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001897 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001898 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001899
Gordon Henriksen86737662008-01-05 16:56:59 +00001900 // Some CCs need callee pop.
Dan Gohman4d3d6e12010-05-27 18:43:40 +00001901 if (Subtarget->IsCalleePop(isVarArg, CallConv)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001902 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001903 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00001904 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001905 // If this is an sret function, the return should pop the hidden pointer.
Chris Lattner29689432010-03-11 00:22:57 +00001906 if (!Is64Bit && !IsTailCallConvention(CallConv) && ArgsAreStructReturn(Ins))
Dan Gohman1e93df62010-04-17 14:41:14 +00001907 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001908 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001909
Gordon Henriksen86737662008-01-05 16:56:59 +00001910 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001911 // RegSaveFrameIndex is X86-64 only.
1912 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001913 if (CallConv == CallingConv::X86_FastCall ||
1914 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00001915 // fastcc functions can't have varargs.
1916 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00001917 }
Evan Cheng25caf632006-05-23 21:06:34 +00001918
Dan Gohman98ca4f22009-08-05 01:29:28 +00001919 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001920}
1921
Dan Gohman475871a2008-07-27 21:46:04 +00001922SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001923X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
1924 SDValue StackPtr, SDValue Arg,
1925 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00001926 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001927 ISD::ArgFlagsTy Flags) const {
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001928 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00001929 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00001930 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001931 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00001932 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001933
1934 return DAG.getStore(Chain, dl, Arg, PtrOff,
1935 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00001936 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00001937}
1938
Bill Wendling64e87322009-01-16 19:25:27 +00001939/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001940/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00001941SDValue
1942X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00001943 SDValue &OutRetAddr, SDValue Chain,
1944 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00001945 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001946 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00001947 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001948 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00001949
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001950 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00001951 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
1952 false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001953 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001954}
1955
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001956/// EmitTailCallStoreRetAddr - Emit a store of the return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001957/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00001958static SDValue
1959EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00001960 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00001961 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001962 // Store the return address to the appropriate stack slot.
1963 if (!FPDiff) return Chain;
1964 // Calculate the new stack slot for the return address.
1965 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00001966 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00001967 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00001968 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00001969 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001970 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00001971 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00001972 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001973 return Chain;
1974}
1975
Dan Gohman98ca4f22009-08-05 01:29:28 +00001976SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001977X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001978 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001979 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001980 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001981 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001982 const SmallVectorImpl<ISD::InputArg> &Ins,
1983 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001984 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001985 MachineFunction &MF = DAG.getMachineFunction();
1986 bool Is64Bit = Subtarget->is64Bit();
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00001987 bool IsWin64 = Subtarget->isTargetWin64();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001988 bool IsStructRet = CallIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00001989 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001990
Evan Cheng5f941932010-02-05 02:21:12 +00001991 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00001992 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00001993 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1994 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001995 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00001996
1997 // Sibcalls are automatically detected tailcalls which do not require
1998 // ABI changes.
Dan Gohman1797ed52010-02-08 20:27:50 +00001999 if (!GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00002000 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00002001
2002 if (isTailCall)
2003 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00002004 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00002005
Chris Lattner29689432010-03-11 00:22:57 +00002006 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
2007 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00002008
Chris Lattner638402b2007-02-28 07:00:42 +00002009 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00002010 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002011 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00002012 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002013
2014 // Allocate shadow area for Win64
2015 if (IsWin64) {
2016 CCInfo.AllocateStack(32, 8);
2017 }
2018
Duncan Sands45907662010-10-31 13:21:44 +00002019 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00002020
Chris Lattner423c5f42007-02-28 05:31:48 +00002021 // Get a count of how many bytes are to be pushed on the stack.
2022 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00002023 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00002024 // This is a sibcall. The memory operands are available in caller's
2025 // own caller's stack.
2026 NumBytes = 0;
Chris Lattner29689432010-03-11 00:22:57 +00002027 else if (GuaranteedTailCallOpt && IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00002028 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002029
Gordon Henriksen86737662008-01-05 16:56:59 +00002030 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00002031 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002032 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00002033 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00002034 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
2035 FPDiff = NumBytesCallerPushed - NumBytes;
2036
2037 // Set the delta of movement of the returnaddr stackslot.
2038 // But only set if delta is greater than previous delta.
2039 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
2040 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
2041 }
2042
Evan Chengf22f9b32010-02-06 03:28:46 +00002043 if (!IsSibcall)
2044 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002045
Dan Gohman475871a2008-07-27 21:46:04 +00002046 SDValue RetAddrFrIdx;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002047 // Load return address for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00002048 if (isTailCall && FPDiff)
2049 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
2050 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002051
Dan Gohman475871a2008-07-27 21:46:04 +00002052 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
2053 SmallVector<SDValue, 8> MemOpChains;
2054 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00002055
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002056 // Walk the register/memloc assignments, inserting copies/loads. In the case
2057 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00002058 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2059 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00002060 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00002061 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002062 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00002063 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00002064
Chris Lattner423c5f42007-02-28 05:31:48 +00002065 // Promote the value if needed.
2066 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002067 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00002068 case CCValAssign::Full: break;
2069 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002070 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002071 break;
2072 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002073 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002074 break;
2075 case CCValAssign::AExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002076 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
2077 // Special case: passing MMX values in XMM registers.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002078 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00002079 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
2080 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002081 } else
2082 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
2083 break;
2084 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002085 Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002086 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002087 case CCValAssign::Indirect: {
2088 // Store the argument.
2089 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00002090 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002091 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00002092 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002093 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002094 Arg = SpillSlot;
2095 break;
2096 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00002097 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002098
Chris Lattner423c5f42007-02-28 05:31:48 +00002099 if (VA.isRegLoc()) {
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002100 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
2101 if (isVarArg && IsWin64) {
2102 // Win64 ABI requires argument XMM reg to be copied to the corresponding
2103 // shadow reg if callee is a varargs function.
2104 unsigned ShadowReg = 0;
2105 switch (VA.getLocReg()) {
2106 case X86::XMM0: ShadowReg = X86::RCX; break;
2107 case X86::XMM1: ShadowReg = X86::RDX; break;
2108 case X86::XMM2: ShadowReg = X86::R8; break;
2109 case X86::XMM3: ShadowReg = X86::R9; break;
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002110 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002111 if (ShadowReg)
2112 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002113 }
Evan Chengf22f9b32010-02-06 03:28:46 +00002114 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00002115 assert(VA.isMemLoc());
2116 if (StackPtr.getNode() == 0)
2117 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
2118 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
2119 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002120 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002121 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002122
Evan Cheng32fe1032006-05-25 00:59:30 +00002123 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002124 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002125 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002126
Evan Cheng347d5f72006-04-28 21:29:37 +00002127 // Build a sequence of copy-to-reg nodes chained together with token chain
2128 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002129 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002130 // Tail call byval lowering might overwrite argument registers so in case of
2131 // tail call optimization the copies to registers are lowered later.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002132 if (!isTailCall)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002133 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002134 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002135 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002136 InFlag = Chain.getValue(1);
2137 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002138
Chris Lattner88e1fd52009-07-09 04:24:46 +00002139 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002140 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2141 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002142 if (!isTailCall) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002143 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
2144 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00002145 DebugLoc(), getPointerTy()),
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002146 InFlag);
2147 InFlag = Chain.getValue(1);
2148 } else {
2149 // If we are tail calling and generating PIC/GOT style code load the
2150 // address of the callee into ECX. The value in ecx is used as target of
2151 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2152 // for tail calls on PIC/GOT architectures. Normally we would just put the
2153 // address of GOT into ebx and then call target@PLT. But for tail calls
2154 // ebx would be restored (since ebx is callee saved) before jumping to the
2155 // target@PLT.
2156
2157 // Note: The actual moving to ECX is done further down.
2158 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2159 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2160 !G->getGlobal()->hasProtectedVisibility())
2161 Callee = LowerGlobalAddress(Callee, DAG);
2162 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002163 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002164 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002165 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002166
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002167 if (Is64Bit && isVarArg && !IsWin64) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002168 // From AMD64 ABI document:
2169 // For calls that may call functions that use varargs or stdargs
2170 // (prototype-less calls or calls to functions containing ellipsis (...) in
2171 // the declaration) %al is used as hidden argument to specify the number
2172 // of SSE registers used. The contents of %al do not need to match exactly
2173 // the number of registers, but must be an ubound on the number of SSE
2174 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002175
Gordon Henriksen86737662008-01-05 16:56:59 +00002176 // Count the number of XMM registers allocated.
2177 static const unsigned XMMArgRegs[] = {
2178 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2179 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2180 };
2181 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00002182 assert((Subtarget->hasXMM() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002183 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002184
Dale Johannesendd64c412009-02-04 00:33:20 +00002185 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Owen Anderson825b72b2009-08-11 20:47:22 +00002186 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002187 InFlag = Chain.getValue(1);
2188 }
2189
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002190
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002191 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002192 if (isTailCall) {
2193 // Force all the incoming stack arguments to be loaded from the stack
2194 // before any new outgoing arguments are stored to the stack, because the
2195 // outgoing stack slots may alias the incoming argument stack slots, and
2196 // the alias isn't otherwise explicit. This is slightly more conservative
2197 // than necessary, because it means that each store effectively depends
2198 // on every argument instead of just those arguments it would clobber.
2199 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2200
Dan Gohman475871a2008-07-27 21:46:04 +00002201 SmallVector<SDValue, 8> MemOpChains2;
2202 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002203 int FI = 0;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002204 // Do not flag preceding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00002205 InFlag = SDValue();
Dan Gohman1797ed52010-02-08 20:27:50 +00002206 if (GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002207 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2208 CCValAssign &VA = ArgLocs[i];
2209 if (VA.isRegLoc())
2210 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002211 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002212 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002213 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002214 // Create frame index.
2215 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002216 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002217 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002218 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002219
Duncan Sands276dcbd2008-03-21 09:14:45 +00002220 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002221 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002222 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002223 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002224 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00002225 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002226 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002227
Dan Gohman98ca4f22009-08-05 01:29:28 +00002228 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2229 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002230 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002231 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002232 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002233 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002234 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002235 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002236 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002237 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002238 }
2239 }
2240
2241 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002242 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002243 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002244
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002245 // Copy arguments to their registers.
2246 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002247 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002248 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002249 InFlag = Chain.getValue(1);
2250 }
Dan Gohman475871a2008-07-27 21:46:04 +00002251 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002252
Gordon Henriksen86737662008-01-05 16:56:59 +00002253 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002254 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002255 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002256 }
2257
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002258 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2259 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2260 // In the 64-bit large code model, we have to make all calls
2261 // through a register, since the call instruction's 32-bit
2262 // pc-relative offset may not be large enough to hold the whole
2263 // address.
2264 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002265 // If the callee is a GlobalAddress node (quite common, every direct call
2266 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2267 // it.
2268
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002269 // We should use extra load for direct calls to dllimported functions in
2270 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002271 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002272 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002273 unsigned char OpFlags = 0;
John McCall3a3465b2011-06-15 20:36:13 +00002274 bool ExtraLoad = false;
2275 unsigned WrapperKind = ISD::DELETED_NODE;
Eric Christopherfd179292009-08-27 18:07:15 +00002276
Chris Lattner48a7d022009-07-09 05:02:21 +00002277 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2278 // external symbols most go through the PLT in PIC mode. If the symbol
2279 // has hidden or protected visibility, or if it is static or local, then
2280 // we don't need to use the PLT - we can directly call it.
2281 if (Subtarget->isTargetELF() &&
2282 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002283 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002284 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002285 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002286 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002287 (!Subtarget->getTargetTriple().isMacOSX() ||
2288 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002289 // PC-relative references to external symbols should go through $stub,
2290 // unless we're building with the leopard linker or later, which
2291 // automatically synthesizes these stubs.
2292 OpFlags = X86II::MO_DARWIN_STUB;
John McCall3a3465b2011-06-15 20:36:13 +00002293 } else if (Subtarget->isPICStyleRIPRel() &&
2294 isa<Function>(GV) &&
2295 cast<Function>(GV)->hasFnAttr(Attribute::NonLazyBind)) {
2296 // If the function is marked as non-lazy, generate an indirect call
2297 // which loads from the GOT directly. This avoids runtime overhead
2298 // at the cost of eager binding (and one extra byte of encoding).
2299 OpFlags = X86II::MO_GOTPCREL;
2300 WrapperKind = X86ISD::WrapperRIP;
2301 ExtraLoad = true;
Chris Lattner74e726e2009-07-09 05:27:35 +00002302 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002303
Devang Patel0d881da2010-07-06 22:08:15 +00002304 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002305 G->getOffset(), OpFlags);
John McCall3a3465b2011-06-15 20:36:13 +00002306
2307 // Add a wrapper if needed.
2308 if (WrapperKind != ISD::DELETED_NODE)
2309 Callee = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Callee);
2310 // Add extra indirection if needed.
2311 if (ExtraLoad)
2312 Callee = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Callee,
2313 MachinePointerInfo::getGOT(),
2314 false, false, 0);
Chris Lattner48a7d022009-07-09 05:02:21 +00002315 }
Bill Wendling056292f2008-09-16 21:48:12 +00002316 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002317 unsigned char OpFlags = 0;
2318
Evan Cheng1bf891a2010-12-01 22:59:46 +00002319 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2320 // external symbols should go through the PLT.
2321 if (Subtarget->isTargetELF() &&
2322 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2323 OpFlags = X86II::MO_PLT;
2324 } else if (Subtarget->isPICStyleStubAny() &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002325 (!Subtarget->getTargetTriple().isMacOSX() ||
2326 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Evan Cheng1bf891a2010-12-01 22:59:46 +00002327 // PC-relative references to external symbols should go through $stub,
2328 // unless we're building with the leopard linker or later, which
2329 // automatically synthesizes these stubs.
2330 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002331 }
Eric Christopherfd179292009-08-27 18:07:15 +00002332
Chris Lattner48a7d022009-07-09 05:02:21 +00002333 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2334 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002335 }
2336
Chris Lattnerd96d0722007-02-25 06:40:16 +00002337 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002338 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002339 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002340
Evan Chengf22f9b32010-02-06 03:28:46 +00002341 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002342 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2343 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002344 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002345 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002346
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002347 Ops.push_back(Chain);
2348 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002349
Dan Gohman98ca4f22009-08-05 01:29:28 +00002350 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002351 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002352
Gordon Henriksen86737662008-01-05 16:56:59 +00002353 // Add argument registers to the end of the list so that they are known live
2354 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002355 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2356 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2357 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002358
Evan Cheng586ccac2008-03-18 23:36:35 +00002359 // Add an implicit use GOT pointer in EBX.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002360 if (!isTailCall && Subtarget->isPICStyleGOT())
Evan Cheng586ccac2008-03-18 23:36:35 +00002361 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
2362
Anton Korobeynikov3a1e54a2010-08-17 21:06:07 +00002363 // Add an implicit use of AL for non-Windows x86 64-bit vararg functions.
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002364 if (Is64Bit && isVarArg && !IsWin64)
Owen Anderson825b72b2009-08-11 20:47:22 +00002365 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
Evan Cheng586ccac2008-03-18 23:36:35 +00002366
Gabor Greifba36cb52008-08-28 21:40:38 +00002367 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002368 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002369
Dan Gohman98ca4f22009-08-05 01:29:28 +00002370 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002371 // We used to do:
2372 //// If this is the first return lowered for this function, add the regs
2373 //// to the liveout set for the function.
2374 // This isn't right, although it's probably harmless on x86; liveouts
2375 // should be computed from returns not tail calls. Consider a void
2376 // function making a tail call to a function returning int.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002377 return DAG.getNode(X86ISD::TC_RETURN, dl,
2378 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002379 }
2380
Dale Johannesenace16102009-02-03 19:33:06 +00002381 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002382 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002383
Chris Lattner2d297092006-05-23 18:50:38 +00002384 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002385 unsigned NumBytesForCalleeToPush;
Dan Gohman4d3d6e12010-05-27 18:43:40 +00002386 if (Subtarget->IsCalleePop(isVarArg, CallConv))
Gordon Henriksen86737662008-01-05 16:56:59 +00002387 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Chris Lattner29689432010-03-11 00:22:57 +00002388 else if (!Is64Bit && !IsTailCallConvention(CallConv) && IsStructRet)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002389 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002390 // pops the hidden struct pointer, so we have to push it back.
2391 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002392 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002393 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002394 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002395
Gordon Henriksenae636f82008-01-03 16:47:34 +00002396 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002397 if (!IsSibcall) {
2398 Chain = DAG.getCALLSEQ_END(Chain,
2399 DAG.getIntPtrConstant(NumBytes, true),
2400 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2401 true),
2402 InFlag);
2403 InFlag = Chain.getValue(1);
2404 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002405
Chris Lattner3085e152007-02-25 08:59:22 +00002406 // Handle result values, copying them out of physregs into vregs that we
2407 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002408 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2409 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002410}
2411
Evan Cheng25ab6902006-09-08 06:48:29 +00002412
2413//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002414// Fast Calling Convention (tail call) implementation
2415//===----------------------------------------------------------------------===//
2416
2417// Like std call, callee cleans arguments, convention except that ECX is
2418// reserved for storing the tail called function address. Only 2 registers are
2419// free for argument passing (inreg). Tail call optimization is performed
2420// provided:
2421// * tailcallopt is enabled
2422// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002423// On X86_64 architecture with GOT-style position independent code only local
2424// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002425// To keep the stack aligned according to platform abi the function
2426// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2427// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002428// If a tail called function callee has more arguments than the caller the
2429// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002430// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002431// original REtADDR, but before the saved framepointer or the spilled registers
2432// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2433// stack layout:
2434// arg1
2435// arg2
2436// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002437// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002438// move area ]
2439// (possible EBP)
2440// ESI
2441// EDI
2442// local1 ..
2443
2444/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2445/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002446unsigned
2447X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2448 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002449 MachineFunction &MF = DAG.getMachineFunction();
2450 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002451 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002452 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002453 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002454 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002455 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002456 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2457 // Number smaller than 12 so just add the difference.
2458 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2459 } else {
2460 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002461 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002462 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002463 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002464 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002465}
2466
Evan Cheng5f941932010-02-05 02:21:12 +00002467/// MatchingStackOffset - Return true if the given stack call argument is
2468/// already available in the same position (relatively) of the caller's
2469/// incoming argument stack.
2470static
2471bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2472 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2473 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002474 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2475 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002476 if (Arg.getOpcode() == ISD::CopyFromReg) {
2477 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00002478 if (!TargetRegisterInfo::isVirtualRegister(VR))
Evan Cheng5f941932010-02-05 02:21:12 +00002479 return false;
2480 MachineInstr *Def = MRI->getVRegDef(VR);
2481 if (!Def)
2482 return false;
2483 if (!Flags.isByVal()) {
2484 if (!TII->isLoadFromStackSlot(Def, FI))
2485 return false;
2486 } else {
2487 unsigned Opcode = Def->getOpcode();
2488 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2489 Def->getOperand(1).isFI()) {
2490 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002491 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002492 } else
2493 return false;
2494 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002495 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2496 if (Flags.isByVal())
2497 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002498 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002499 // define @foo(%struct.X* %A) {
2500 // tail call @bar(%struct.X* byval %A)
2501 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002502 return false;
2503 SDValue Ptr = Ld->getBasePtr();
2504 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2505 if (!FINode)
2506 return false;
2507 FI = FINode->getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002508 } else
2509 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002510
Evan Cheng4cae1332010-03-05 08:38:04 +00002511 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002512 if (!MFI->isFixedObjectIndex(FI))
2513 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002514 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002515}
2516
Dan Gohman98ca4f22009-08-05 01:29:28 +00002517/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2518/// for tail call optimization. Targets which want to do tail call
2519/// optimization should implement this function.
2520bool
2521X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002522 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002523 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002524 bool isCalleeStructRet,
2525 bool isCallerStructRet,
Evan Chengb1712452010-01-27 06:25:16 +00002526 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002527 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002528 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002529 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002530 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002531 CalleeCC != CallingConv::C)
2532 return false;
2533
Evan Cheng7096ae42010-01-29 06:45:59 +00002534 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002535 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002536 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Cheng13617962010-04-30 01:12:32 +00002537 CallingConv::ID CallerCC = CallerF->getCallingConv();
2538 bool CCMatch = CallerCC == CalleeCC;
2539
Dan Gohman1797ed52010-02-08 20:27:50 +00002540 if (GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002541 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002542 return true;
2543 return false;
2544 }
2545
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002546 // Look for obvious safe cases to perform tail call optimization that do not
2547 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002548
Evan Cheng2c12cb42010-03-26 16:26:03 +00002549 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2550 // emit a special epilogue.
2551 if (RegInfo->needsStackRealignment(MF))
2552 return false;
2553
Evan Chenga375d472010-03-15 18:54:48 +00002554 // Also avoid sibcall optimization if either caller or callee uses struct
2555 // return semantics.
2556 if (isCalleeStructRet || isCallerStructRet)
2557 return false;
2558
Chad Rosier871f6642011-05-18 19:59:50 +00002559 // Do not sibcall optimize vararg calls unless all arguments are passed via
Chad Rosiera1660892011-05-20 00:59:28 +00002560 // registers.
Chad Rosier871f6642011-05-18 19:59:50 +00002561 if (isVarArg && !Outs.empty()) {
Chad Rosiera1660892011-05-20 00:59:28 +00002562
2563 // Optimizing for varargs on Win64 is unlikely to be safe without
2564 // additional testing.
2565 if (Subtarget->isTargetWin64())
2566 return false;
2567
Chad Rosier871f6642011-05-18 19:59:50 +00002568 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002569 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
2570 getTargetMachine(), ArgLocs, *DAG.getContext());
Chad Rosier871f6642011-05-18 19:59:50 +00002571
Chad Rosier871f6642011-05-18 19:59:50 +00002572 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
2573 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i)
2574 if (!ArgLocs[i].isRegLoc())
2575 return false;
2576 }
2577
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002578 // If the call result is in ST0 / ST1, it needs to be popped off the x87 stack.
2579 // Therefore if it's not used by the call it is not safe to optimize this into
2580 // a sibcall.
2581 bool Unused = false;
2582 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2583 if (!Ins[i].Used) {
2584 Unused = true;
2585 break;
2586 }
2587 }
2588 if (Unused) {
2589 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002590 CCState CCInfo(CalleeCC, false, DAG.getMachineFunction(),
2591 getTargetMachine(), RVLocs, *DAG.getContext());
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002592 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002593 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002594 CCValAssign &VA = RVLocs[i];
2595 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2596 return false;
2597 }
2598 }
2599
Evan Cheng13617962010-04-30 01:12:32 +00002600 // If the calling conventions do not match, then we'd better make sure the
2601 // results are returned in the same way as what the caller expects.
2602 if (!CCMatch) {
2603 SmallVector<CCValAssign, 16> RVLocs1;
Eric Christopher471e4222011-06-08 23:55:35 +00002604 CCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
2605 getTargetMachine(), RVLocs1, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002606 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2607
2608 SmallVector<CCValAssign, 16> RVLocs2;
Eric Christopher471e4222011-06-08 23:55:35 +00002609 CCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
2610 getTargetMachine(), RVLocs2, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002611 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2612
2613 if (RVLocs1.size() != RVLocs2.size())
2614 return false;
2615 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2616 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2617 return false;
2618 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2619 return false;
2620 if (RVLocs1[i].isRegLoc()) {
2621 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2622 return false;
2623 } else {
2624 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2625 return false;
2626 }
2627 }
2628 }
2629
Evan Chenga6bff982010-01-30 01:22:00 +00002630 // If the callee takes no arguments then go on to check the results of the
2631 // call.
2632 if (!Outs.empty()) {
2633 // Check if stack adjustment is needed. For now, do not do this if any
2634 // argument is passed on the stack.
2635 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002636 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
2637 getTargetMachine(), ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002638
2639 // Allocate shadow area for Win64
2640 if (Subtarget->isTargetWin64()) {
2641 CCInfo.AllocateStack(32, 8);
2642 }
2643
Duncan Sands45907662010-10-31 13:21:44 +00002644 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Stuart Hastings6db2c2f2011-05-17 16:59:46 +00002645 if (CCInfo.getNextStackOffset()) {
Evan Chengb2c92902010-02-02 02:22:50 +00002646 MachineFunction &MF = DAG.getMachineFunction();
2647 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2648 return false;
Evan Chengb2c92902010-02-02 02:22:50 +00002649
2650 // Check if the arguments are already laid out in the right way as
2651 // the caller's fixed stack objects.
2652 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002653 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2654 const X86InstrInfo *TII =
2655 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002656 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2657 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002658 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002659 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002660 if (VA.getLocInfo() == CCValAssign::Indirect)
2661 return false;
2662 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002663 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2664 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002665 return false;
2666 }
2667 }
2668 }
Evan Cheng9c044672010-05-29 01:35:22 +00002669
2670 // If the tailcall address may be in a register, then make sure it's
2671 // possible to register allocate for it. In 32-bit, the call address can
2672 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002673 // callee-saved registers are restored. These happen to be the same
2674 // registers used to pass 'inreg' arguments so watch out for those.
2675 if (!Subtarget->is64Bit() &&
2676 !isa<GlobalAddressSDNode>(Callee) &&
Evan Cheng9c044672010-05-29 01:35:22 +00002677 !isa<ExternalSymbolSDNode>(Callee)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002678 unsigned NumInRegs = 0;
2679 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2680 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002681 if (!VA.isRegLoc())
2682 continue;
2683 unsigned Reg = VA.getLocReg();
2684 switch (Reg) {
2685 default: break;
2686 case X86::EAX: case X86::EDX: case X86::ECX:
2687 if (++NumInRegs == 3)
Evan Cheng9c044672010-05-29 01:35:22 +00002688 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002689 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002690 }
2691 }
2692 }
Evan Chenga6bff982010-01-30 01:22:00 +00002693 }
Evan Chengb1712452010-01-27 06:25:16 +00002694
Dale Johannesend155d7e2010-10-25 22:17:05 +00002695 // An stdcall caller is expected to clean up its arguments; the callee
Dale Johannesen0e034562010-11-12 00:43:18 +00002696 // isn't going to do that.
Dale Johannesend155d7e2010-10-25 22:17:05 +00002697 if (!CCMatch && CallerCC==CallingConv::X86_StdCall)
2698 return false;
2699
Evan Cheng86809cc2010-02-03 03:28:02 +00002700 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002701}
2702
Dan Gohman3df24e62008-09-03 23:12:08 +00002703FastISel *
Dan Gohmana4160c32010-07-07 16:29:44 +00002704X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
2705 return X86::createFastISel(funcInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00002706}
2707
2708
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002709//===----------------------------------------------------------------------===//
2710// Other Lowering Hooks
2711//===----------------------------------------------------------------------===//
2712
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00002713static bool MayFoldLoad(SDValue Op) {
2714 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
2715}
2716
2717static bool MayFoldIntoStore(SDValue Op) {
2718 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
2719}
2720
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002721static bool isTargetShuffle(unsigned Opcode) {
2722 switch(Opcode) {
2723 default: return false;
2724 case X86ISD::PSHUFD:
2725 case X86ISD::PSHUFHW:
2726 case X86ISD::PSHUFLW:
2727 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002728 case X86ISD::PALIGN:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002729 case X86ISD::SHUFPS:
2730 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002731 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002732 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002733 case X86ISD::MOVLPS:
2734 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002735 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002736 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002737 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002738 case X86ISD::MOVSS:
2739 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002740 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002741 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +00002742 case X86ISD::VUNPCKLPS:
2743 case X86ISD::VUNPCKLPD:
2744 case X86ISD::VUNPCKLPSY:
2745 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002746 case X86ISD::PUNPCKLWD:
2747 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002748 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002749 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002750 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002751 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002752 case X86ISD::PUNPCKHWD:
2753 case X86ISD::PUNPCKHBW:
2754 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002755 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002756 return true;
2757 }
2758 return false;
2759}
2760
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002761static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002762 SDValue V1, SelectionDAG &DAG) {
2763 switch(Opc) {
2764 default: llvm_unreachable("Unknown x86 shuffle node");
2765 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002766 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002767 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002768 return DAG.getNode(Opc, dl, VT, V1);
2769 }
2770
2771 return SDValue();
2772}
2773
2774static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00002775 SDValue V1, unsigned TargetMask, SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002776 switch(Opc) {
2777 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002778 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002779 case X86ISD::PSHUFHW:
2780 case X86ISD::PSHUFLW:
2781 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
2782 }
2783
2784 return SDValue();
2785}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002786
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002787static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2788 SDValue V1, SDValue V2, unsigned TargetMask, SelectionDAG &DAG) {
2789 switch(Opc) {
2790 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002791 case X86ISD::PALIGN:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002792 case X86ISD::SHUFPD:
2793 case X86ISD::SHUFPS:
2794 return DAG.getNode(Opc, dl, VT, V1, V2,
2795 DAG.getConstant(TargetMask, MVT::i8));
2796 }
2797 return SDValue();
2798}
2799
2800static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2801 SDValue V1, SDValue V2, SelectionDAG &DAG) {
2802 switch(Opc) {
2803 default: llvm_unreachable("Unknown x86 shuffle node");
2804 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00002805 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002806 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002807 case X86ISD::MOVLPS:
2808 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002809 case X86ISD::MOVSS:
2810 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002811 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002812 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +00002813 case X86ISD::VUNPCKLPS:
2814 case X86ISD::VUNPCKLPD:
2815 case X86ISD::VUNPCKLPSY:
2816 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002817 case X86ISD::PUNPCKLWD:
2818 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002819 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002820 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002821 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002822 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002823 case X86ISD::PUNPCKHWD:
2824 case X86ISD::PUNPCKHBW:
2825 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002826 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002827 return DAG.getNode(Opc, dl, VT, V1, V2);
2828 }
2829 return SDValue();
2830}
2831
Dan Gohmand858e902010-04-17 15:26:15 +00002832SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002833 MachineFunction &MF = DAG.getMachineFunction();
2834 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2835 int ReturnAddrIndex = FuncInfo->getRAIndex();
2836
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002837 if (ReturnAddrIndex == 0) {
2838 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002839 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002840 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00002841 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002842 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002843 }
2844
Evan Cheng25ab6902006-09-08 06:48:29 +00002845 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002846}
2847
2848
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002849bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2850 bool hasSymbolicDisplacement) {
2851 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00002852 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002853 return false;
2854
2855 // If we don't have a symbolic displacement - we don't have any extra
2856 // restrictions.
2857 if (!hasSymbolicDisplacement)
2858 return true;
2859
2860 // FIXME: Some tweaks might be needed for medium code model.
2861 if (M != CodeModel::Small && M != CodeModel::Kernel)
2862 return false;
2863
2864 // For small code model we assume that latest object is 16MB before end of 31
2865 // bits boundary. We may also accept pretty large negative constants knowing
2866 // that all objects are in the positive half of address space.
2867 if (M == CodeModel::Small && Offset < 16*1024*1024)
2868 return true;
2869
2870 // For kernel code model we know that all object resist in the negative half
2871 // of 32bits address space. We may not accept negative offsets, since they may
2872 // be just off and we may accept pretty large positive ones.
2873 if (M == CodeModel::Kernel && Offset > 0)
2874 return true;
2875
2876 return false;
2877}
2878
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002879/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2880/// specific condition code, returning the condition code and the LHS/RHS of the
2881/// comparison to make.
2882static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2883 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00002884 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002885 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2886 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2887 // X > -1 -> X == 0, jump !sign.
2888 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002889 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002890 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2891 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002892 return X86::COND_S;
Andrew Trickf6c39412011-03-23 23:11:02 +00002893 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00002894 // X < 1 -> X <= 0
2895 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002896 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002897 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002898 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002899
Evan Chengd9558e02006-01-06 00:43:03 +00002900 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002901 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002902 case ISD::SETEQ: return X86::COND_E;
2903 case ISD::SETGT: return X86::COND_G;
2904 case ISD::SETGE: return X86::COND_GE;
2905 case ISD::SETLT: return X86::COND_L;
2906 case ISD::SETLE: return X86::COND_LE;
2907 case ISD::SETNE: return X86::COND_NE;
2908 case ISD::SETULT: return X86::COND_B;
2909 case ISD::SETUGT: return X86::COND_A;
2910 case ISD::SETULE: return X86::COND_BE;
2911 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00002912 }
Chris Lattner4c78e022008-12-23 23:42:27 +00002913 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002914
Chris Lattner4c78e022008-12-23 23:42:27 +00002915 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00002916
Chris Lattner4c78e022008-12-23 23:42:27 +00002917 // If LHS is a foldable load, but RHS is not, flip the condition.
Rafael Espindolaf297c932011-02-03 03:58:05 +00002918 if (ISD::isNON_EXTLoad(LHS.getNode()) &&
2919 !ISD::isNON_EXTLoad(RHS.getNode())) {
Chris Lattner4c78e022008-12-23 23:42:27 +00002920 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
2921 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00002922 }
2923
Chris Lattner4c78e022008-12-23 23:42:27 +00002924 switch (SetCCOpcode) {
2925 default: break;
2926 case ISD::SETOLT:
2927 case ISD::SETOLE:
2928 case ISD::SETUGT:
2929 case ISD::SETUGE:
2930 std::swap(LHS, RHS);
2931 break;
2932 }
2933
2934 // On a floating point condition, the flags are set as follows:
2935 // ZF PF CF op
2936 // 0 | 0 | 0 | X > Y
2937 // 0 | 0 | 1 | X < Y
2938 // 1 | 0 | 0 | X == Y
2939 // 1 | 1 | 1 | unordered
2940 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002941 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00002942 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002943 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00002944 case ISD::SETOLT: // flipped
2945 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002946 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00002947 case ISD::SETOLE: // flipped
2948 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002949 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002950 case ISD::SETUGT: // flipped
2951 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002952 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00002953 case ISD::SETUGE: // flipped
2954 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002955 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002956 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002957 case ISD::SETNE: return X86::COND_NE;
2958 case ISD::SETUO: return X86::COND_P;
2959 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00002960 case ISD::SETOEQ:
2961 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00002962 }
Evan Chengd9558e02006-01-06 00:43:03 +00002963}
2964
Evan Cheng4a460802006-01-11 00:33:36 +00002965/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2966/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00002967/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00002968static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00002969 switch (X86CC) {
2970 default:
2971 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00002972 case X86::COND_B:
2973 case X86::COND_BE:
2974 case X86::COND_E:
2975 case X86::COND_P:
2976 case X86::COND_A:
2977 case X86::COND_AE:
2978 case X86::COND_NE:
2979 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00002980 return true;
2981 }
2982}
2983
Evan Chengeb2f9692009-10-27 19:56:55 +00002984/// isFPImmLegal - Returns true if the target can instruction select the
2985/// specified FP immediate natively. If false, the legalizer will
2986/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00002987bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00002988 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
2989 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
2990 return true;
2991 }
2992 return false;
2993}
2994
Nate Begeman9008ca62009-04-27 18:41:29 +00002995/// isUndefOrInRange - Return true if Val is undef or if its value falls within
2996/// the specified range (L, H].
2997static bool isUndefOrInRange(int Val, int Low, int Hi) {
2998 return (Val < 0) || (Val >= Low && Val < Hi);
2999}
3000
3001/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
3002/// specified value.
3003static bool isUndefOrEqual(int Val, int CmpVal) {
3004 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003005 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00003006 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00003007}
3008
Nate Begeman9008ca62009-04-27 18:41:29 +00003009/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
3010/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
3011/// the second operand.
Owen Andersone50ed302009-08-10 22:56:29 +00003012static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00003013 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00003014 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00003015 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00003016 return (Mask[0] < 2 && Mask[1] < 2);
3017 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003018}
3019
Nate Begeman9008ca62009-04-27 18:41:29 +00003020bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003021 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003022 N->getMask(M);
3023 return ::isPSHUFDMask(M, N->getValueType(0));
3024}
Evan Cheng0188ecb2006-03-22 18:59:22 +00003025
Nate Begeman9008ca62009-04-27 18:41:29 +00003026/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
3027/// is suitable for input to PSHUFHW.
Owen Andersone50ed302009-08-10 22:56:29 +00003028static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003029 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00003030 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003031
Nate Begeman9008ca62009-04-27 18:41:29 +00003032 // Lower quadword copied in order or undef.
3033 for (int i = 0; i != 4; ++i)
3034 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00003035 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003036
Evan Cheng506d3df2006-03-29 23:07:14 +00003037 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00003038 for (int i = 4; i != 8; ++i)
3039 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00003040 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003041
Evan Cheng506d3df2006-03-29 23:07:14 +00003042 return true;
3043}
3044
Nate Begeman9008ca62009-04-27 18:41:29 +00003045bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003046 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003047 N->getMask(M);
3048 return ::isPSHUFHWMask(M, N->getValueType(0));
3049}
Evan Cheng506d3df2006-03-29 23:07:14 +00003050
Nate Begeman9008ca62009-04-27 18:41:29 +00003051/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
3052/// is suitable for input to PSHUFLW.
Owen Andersone50ed302009-08-10 22:56:29 +00003053static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003054 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00003055 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003056
Rafael Espindola15684b22009-04-24 12:40:33 +00003057 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00003058 for (int i = 4; i != 8; ++i)
3059 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00003060 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003061
Rafael Espindola15684b22009-04-24 12:40:33 +00003062 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00003063 for (int i = 0; i != 4; ++i)
3064 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00003065 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003066
Rafael Espindola15684b22009-04-24 12:40:33 +00003067 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003068}
3069
Nate Begeman9008ca62009-04-27 18:41:29 +00003070bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003071 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003072 N->getMask(M);
3073 return ::isPSHUFLWMask(M, N->getValueType(0));
3074}
3075
Nate Begemana09008b2009-10-19 02:17:23 +00003076/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
3077/// is suitable for input to PALIGNR.
3078static bool isPALIGNRMask(const SmallVectorImpl<int> &Mask, EVT VT,
3079 bool hasSSSE3) {
3080 int i, e = VT.getVectorNumElements();
Michael J. Spencerec38de22010-10-10 22:04:20 +00003081
Nate Begemana09008b2009-10-19 02:17:23 +00003082 // Do not handle v2i64 / v2f64 shuffles with palignr.
3083 if (e < 4 || !hasSSSE3)
3084 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003085
Nate Begemana09008b2009-10-19 02:17:23 +00003086 for (i = 0; i != e; ++i)
3087 if (Mask[i] >= 0)
3088 break;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003089
Nate Begemana09008b2009-10-19 02:17:23 +00003090 // All undef, not a palignr.
3091 if (i == e)
3092 return false;
3093
3094 // Determine if it's ok to perform a palignr with only the LHS, since we
3095 // don't have access to the actual shuffle elements to see if RHS is undef.
3096 bool Unary = Mask[i] < (int)e;
3097 bool NeedsUnary = false;
3098
3099 int s = Mask[i] - i;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003100
Nate Begemana09008b2009-10-19 02:17:23 +00003101 // Check the rest of the elements to see if they are consecutive.
3102 for (++i; i != e; ++i) {
3103 int m = Mask[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00003104 if (m < 0)
Nate Begemana09008b2009-10-19 02:17:23 +00003105 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003106
Nate Begemana09008b2009-10-19 02:17:23 +00003107 Unary = Unary && (m < (int)e);
3108 NeedsUnary = NeedsUnary || (m < s);
3109
3110 if (NeedsUnary && !Unary)
3111 return false;
3112 if (Unary && m != ((s+i) & (e-1)))
3113 return false;
3114 if (!Unary && m != (s+i))
3115 return false;
3116 }
3117 return true;
3118}
3119
3120bool X86::isPALIGNRMask(ShuffleVectorSDNode *N) {
3121 SmallVector<int, 8> M;
3122 N->getMask(M);
3123 return ::isPALIGNRMask(M, N->getValueType(0), true);
3124}
3125
Evan Cheng14aed5e2006-03-24 01:18:28 +00003126/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
3127/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Owen Andersone50ed302009-08-10 22:56:29 +00003128static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003129 int NumElems = VT.getVectorNumElements();
3130 if (NumElems != 2 && NumElems != 4)
3131 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003132
Nate Begeman9008ca62009-04-27 18:41:29 +00003133 int Half = NumElems / 2;
3134 for (int i = 0; i < Half; ++i)
3135 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003136 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003137 for (int i = Half; i < NumElems; ++i)
3138 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00003139 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003140
Evan Cheng14aed5e2006-03-24 01:18:28 +00003141 return true;
3142}
3143
Nate Begeman9008ca62009-04-27 18:41:29 +00003144bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
3145 SmallVector<int, 8> M;
3146 N->getMask(M);
3147 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003148}
3149
Evan Cheng213d2cf2007-05-17 18:45:50 +00003150/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00003151/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
3152/// half elements to come from vector 1 (which would equal the dest.) and
3153/// the upper half to come from vector 2.
Owen Andersone50ed302009-08-10 22:56:29 +00003154static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003155 int NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003156
3157 if (NumElems != 2 && NumElems != 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00003158 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003159
Nate Begeman9008ca62009-04-27 18:41:29 +00003160 int Half = NumElems / 2;
3161 for (int i = 0; i < Half; ++i)
3162 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00003163 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003164 for (int i = Half; i < NumElems; ++i)
3165 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003166 return false;
3167 return true;
3168}
3169
Nate Begeman9008ca62009-04-27 18:41:29 +00003170static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
3171 SmallVector<int, 8> M;
3172 N->getMask(M);
3173 return isCommutedSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003174}
3175
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003176/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
3177/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00003178bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
3179 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003180 return false;
3181
Evan Cheng2064a2b2006-03-28 06:50:32 +00003182 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00003183 return isUndefOrEqual(N->getMaskElt(0), 6) &&
3184 isUndefOrEqual(N->getMaskElt(1), 7) &&
3185 isUndefOrEqual(N->getMaskElt(2), 2) &&
3186 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003187}
3188
Nate Begeman0b10b912009-11-07 23:17:15 +00003189/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3190/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3191/// <2, 3, 2, 3>
3192bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
3193 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Michael J. Spencerec38de22010-10-10 22:04:20 +00003194
Nate Begeman0b10b912009-11-07 23:17:15 +00003195 if (NumElems != 4)
3196 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003197
Nate Begeman0b10b912009-11-07 23:17:15 +00003198 return isUndefOrEqual(N->getMaskElt(0), 2) &&
3199 isUndefOrEqual(N->getMaskElt(1), 3) &&
3200 isUndefOrEqual(N->getMaskElt(2), 2) &&
3201 isUndefOrEqual(N->getMaskElt(3), 3);
3202}
3203
Evan Cheng5ced1d82006-04-06 23:23:56 +00003204/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3205/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00003206bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
3207 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003208
Evan Cheng5ced1d82006-04-06 23:23:56 +00003209 if (NumElems != 2 && NumElems != 4)
3210 return false;
3211
Evan Chengc5cdff22006-04-07 21:53:05 +00003212 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003213 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003214 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003215
Evan Chengc5cdff22006-04-07 21:53:05 +00003216 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003217 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003218 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003219
3220 return true;
3221}
3222
Nate Begeman0b10b912009-11-07 23:17:15 +00003223/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3224/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
3225bool X86::isMOVLHPSMask(ShuffleVectorSDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003226 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003227
David Greenea20244d2011-03-02 17:23:43 +00003228 if ((NumElems != 2 && NumElems != 4)
3229 || N->getValueType(0).getSizeInBits() > 128)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003230 return false;
3231
Evan Chengc5cdff22006-04-07 21:53:05 +00003232 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003233 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003234 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003235
Nate Begeman9008ca62009-04-27 18:41:29 +00003236 for (unsigned i = 0; i < NumElems/2; ++i)
3237 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003238 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003239
3240 return true;
3241}
3242
Evan Cheng0038e592006-03-28 00:39:58 +00003243/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3244/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Owen Andersone50ed302009-08-10 22:56:29 +00003245static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003246 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003247 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003248 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng0038e592006-03-28 00:39:58 +00003249 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003250
David Greenea20244d2011-03-02 17:23:43 +00003251 // Handle vector lengths > 128 bits. Define a "section" as a set of
3252 // 128 bits. AVX defines UNPCK* to operate independently on 128-bit
3253 // sections.
3254 unsigned NumSections = VT.getSizeInBits() / 128;
3255 if (NumSections == 0 ) NumSections = 1; // Handle MMX
3256 unsigned NumSectionElts = NumElts / NumSections;
3257
3258 unsigned Start = 0;
3259 unsigned End = NumSectionElts;
3260 for (unsigned s = 0; s < NumSections; ++s) {
3261 for (unsigned i = Start, j = s * NumSectionElts;
3262 i != End;
3263 i += 2, ++j) {
3264 int BitI = Mask[i];
3265 int BitI1 = Mask[i+1];
3266 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003267 return false;
David Greenea20244d2011-03-02 17:23:43 +00003268 if (V2IsSplat) {
3269 if (!isUndefOrEqual(BitI1, NumElts))
3270 return false;
3271 } else {
3272 if (!isUndefOrEqual(BitI1, j + NumElts))
3273 return false;
3274 }
Evan Cheng39623da2006-04-20 08:58:49 +00003275 }
David Greenea20244d2011-03-02 17:23:43 +00003276 // Process the next 128 bits.
3277 Start += NumSectionElts;
3278 End += NumSectionElts;
Evan Cheng0038e592006-03-28 00:39:58 +00003279 }
David Greenea20244d2011-03-02 17:23:43 +00003280
Evan Cheng0038e592006-03-28 00:39:58 +00003281 return true;
3282}
3283
Nate Begeman9008ca62009-04-27 18:41:29 +00003284bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3285 SmallVector<int, 8> M;
3286 N->getMask(M);
3287 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003288}
3289
Evan Cheng4fcb9222006-03-28 02:43:26 +00003290/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3291/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Eric Christopherfd179292009-08-27 18:07:15 +00003292static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003293 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003294 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003295 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng4fcb9222006-03-28 02:43:26 +00003296 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003297
Nate Begeman9008ca62009-04-27 18:41:29 +00003298 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
3299 int BitI = Mask[i];
3300 int BitI1 = Mask[i+1];
Chris Lattner5a88b832007-02-25 07:10:00 +00003301 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengc5cdff22006-04-07 21:53:05 +00003302 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00003303 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00003304 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003305 return false;
3306 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00003307 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003308 return false;
3309 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003310 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003311 return true;
3312}
3313
Nate Begeman9008ca62009-04-27 18:41:29 +00003314bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3315 SmallVector<int, 8> M;
3316 N->getMask(M);
3317 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003318}
3319
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003320/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3321/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3322/// <0, 0, 1, 1>
Owen Andersone50ed302009-08-10 22:56:29 +00003323static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003324 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003325 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003326 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003327
David Greenea20244d2011-03-02 17:23:43 +00003328 // Handle vector lengths > 128 bits. Define a "section" as a set of
3329 // 128 bits. AVX defines UNPCK* to operate independently on 128-bit
3330 // sections.
3331 unsigned NumSections = VT.getSizeInBits() / 128;
3332 if (NumSections == 0 ) NumSections = 1; // Handle MMX
3333 unsigned NumSectionElts = NumElems / NumSections;
3334
3335 for (unsigned s = 0; s < NumSections; ++s) {
3336 for (unsigned i = s * NumSectionElts, j = s * NumSectionElts;
3337 i != NumSectionElts * (s + 1);
3338 i += 2, ++j) {
3339 int BitI = Mask[i];
3340 int BitI1 = Mask[i+1];
3341
3342 if (!isUndefOrEqual(BitI, j))
3343 return false;
3344 if (!isUndefOrEqual(BitI1, j))
3345 return false;
3346 }
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003347 }
David Greenea20244d2011-03-02 17:23:43 +00003348
Rafael Espindola15684b22009-04-24 12:40:33 +00003349 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003350}
3351
Nate Begeman9008ca62009-04-27 18:41:29 +00003352bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
3353 SmallVector<int, 8> M;
3354 N->getMask(M);
3355 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
3356}
3357
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003358/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3359/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3360/// <2, 2, 3, 3>
Owen Andersone50ed302009-08-10 22:56:29 +00003361static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003362 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003363 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
3364 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003365
Nate Begeman9008ca62009-04-27 18:41:29 +00003366 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
3367 int BitI = Mask[i];
3368 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003369 if (!isUndefOrEqual(BitI, j))
3370 return false;
3371 if (!isUndefOrEqual(BitI1, j))
3372 return false;
3373 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003374 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003375}
3376
Nate Begeman9008ca62009-04-27 18:41:29 +00003377bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
3378 SmallVector<int, 8> M;
3379 N->getMask(M);
3380 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
3381}
3382
Evan Cheng017dcc62006-04-21 01:05:10 +00003383/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3384/// specifies a shuffle of elements that is suitable for input to MOVSS,
3385/// MOVSD, and MOVD, i.e. setting the lowest element.
Owen Andersone50ed302009-08-10 22:56:29 +00003386static bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003387 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003388 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003389
3390 int NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003391
Nate Begeman9008ca62009-04-27 18:41:29 +00003392 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003393 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003394
Nate Begeman9008ca62009-04-27 18:41:29 +00003395 for (int i = 1; i < NumElts; ++i)
3396 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003397 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003398
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003399 return true;
3400}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003401
Nate Begeman9008ca62009-04-27 18:41:29 +00003402bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
3403 SmallVector<int, 8> M;
3404 N->getMask(M);
3405 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003406}
3407
Evan Cheng017dcc62006-04-21 01:05:10 +00003408/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
3409/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003410/// element of vector 2 and the other elements to come from vector 1 in order.
Owen Andersone50ed302009-08-10 22:56:29 +00003411static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003412 bool V2IsSplat = false, bool V2IsUndef = false) {
3413 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003414 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003415 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003416
Nate Begeman9008ca62009-04-27 18:41:29 +00003417 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003418 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003419
Nate Begeman9008ca62009-04-27 18:41:29 +00003420 for (int i = 1; i < NumOps; ++i)
3421 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3422 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3423 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003424 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003425
Evan Cheng39623da2006-04-20 08:58:49 +00003426 return true;
3427}
3428
Nate Begeman9008ca62009-04-27 18:41:29 +00003429static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00003430 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003431 SmallVector<int, 8> M;
3432 N->getMask(M);
3433 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00003434}
3435
Evan Chengd9539472006-04-14 21:59:03 +00003436/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3437/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003438bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N) {
3439 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003440 return false;
3441
3442 // Expect 1, 1, 3, 3
Rafael Espindola15684b22009-04-24 12:40:33 +00003443 for (unsigned i = 0; i < 2; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003444 int Elt = N->getMaskElt(i);
3445 if (Elt >= 0 && Elt != 1)
3446 return false;
Rafael Espindola15684b22009-04-24 12:40:33 +00003447 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003448
3449 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003450 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003451 int Elt = N->getMaskElt(i);
3452 if (Elt >= 0 && Elt != 3)
3453 return false;
3454 if (Elt == 3)
3455 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003456 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003457 // Don't use movshdup if it can be done with a shufps.
Nate Begeman9008ca62009-04-27 18:41:29 +00003458 // FIXME: verify that matching u, u, 3, 3 is what we want.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003459 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003460}
3461
3462/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3463/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003464bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N) {
3465 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003466 return false;
3467
3468 // Expect 0, 0, 2, 2
Nate Begeman9008ca62009-04-27 18:41:29 +00003469 for (unsigned i = 0; i < 2; ++i)
3470 if (N->getMaskElt(i) > 0)
3471 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003472
3473 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003474 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003475 int Elt = N->getMaskElt(i);
3476 if (Elt >= 0 && Elt != 2)
3477 return false;
3478 if (Elt == 2)
3479 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003480 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003481 // Don't use movsldup if it can be done with a shufps.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003482 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003483}
3484
Evan Cheng0b457f02008-09-25 20:50:48 +00003485/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3486/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003487bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
3488 int e = N->getValueType(0).getVectorNumElements() / 2;
Eric Christopherfd179292009-08-27 18:07:15 +00003489
Nate Begeman9008ca62009-04-27 18:41:29 +00003490 for (int i = 0; i < e; ++i)
3491 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003492 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003493 for (int i = 0; i < e; ++i)
3494 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003495 return false;
3496 return true;
3497}
3498
David Greenec38a03e2011-02-03 15:50:00 +00003499/// isVEXTRACTF128Index - Return true if the specified
3500/// EXTRACT_SUBVECTOR operand specifies a vector extract that is
3501/// suitable for input to VEXTRACTF128.
3502bool X86::isVEXTRACTF128Index(SDNode *N) {
3503 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3504 return false;
3505
3506 // The index should be aligned on a 128-bit boundary.
3507 uint64_t Index =
3508 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
3509
3510 unsigned VL = N->getValueType(0).getVectorNumElements();
3511 unsigned VBits = N->getValueType(0).getSizeInBits();
3512 unsigned ElSize = VBits / VL;
3513 bool Result = (Index * ElSize) % 128 == 0;
3514
3515 return Result;
3516}
3517
David Greeneccacdc12011-02-04 16:08:29 +00003518/// isVINSERTF128Index - Return true if the specified INSERT_SUBVECTOR
3519/// operand specifies a subvector insert that is suitable for input to
3520/// VINSERTF128.
3521bool X86::isVINSERTF128Index(SDNode *N) {
3522 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
3523 return false;
3524
3525 // The index should be aligned on a 128-bit boundary.
3526 uint64_t Index =
3527 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
3528
3529 unsigned VL = N->getValueType(0).getVectorNumElements();
3530 unsigned VBits = N->getValueType(0).getSizeInBits();
3531 unsigned ElSize = VBits / VL;
3532 bool Result = (Index * ElSize) % 128 == 0;
3533
3534 return Result;
3535}
3536
Evan Cheng63d33002006-03-22 08:01:21 +00003537/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003538/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Evan Cheng63d33002006-03-22 08:01:21 +00003539unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003540 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3541 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
3542
Evan Chengb9df0ca2006-03-22 02:53:00 +00003543 unsigned Shift = (NumOperands == 4) ? 2 : 1;
3544 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00003545 for (int i = 0; i < NumOperands; ++i) {
3546 int Val = SVOp->getMaskElt(NumOperands-i-1);
3547 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00003548 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00003549 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00003550 if (i != NumOperands - 1)
3551 Mask <<= Shift;
3552 }
Evan Cheng63d33002006-03-22 08:01:21 +00003553 return Mask;
3554}
3555
Evan Cheng506d3df2006-03-29 23:07:14 +00003556/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003557/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003558unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003559 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003560 unsigned Mask = 0;
3561 // 8 nodes, but we only care about the last 4.
3562 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003563 int Val = SVOp->getMaskElt(i);
3564 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00003565 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00003566 if (i != 4)
3567 Mask <<= 2;
3568 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003569 return Mask;
3570}
3571
3572/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003573/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003574unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003575 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003576 unsigned Mask = 0;
3577 // 8 nodes, but we only care about the first 4.
3578 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003579 int Val = SVOp->getMaskElt(i);
3580 if (Val >= 0)
3581 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00003582 if (i != 0)
3583 Mask <<= 2;
3584 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003585 return Mask;
3586}
3587
Nate Begemana09008b2009-10-19 02:17:23 +00003588/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
3589/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
3590unsigned X86::getShufflePALIGNRImmediate(SDNode *N) {
3591 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3592 EVT VVT = N->getValueType(0);
3593 unsigned EltSize = VVT.getVectorElementType().getSizeInBits() >> 3;
3594 int Val = 0;
3595
3596 unsigned i, e;
3597 for (i = 0, e = VVT.getVectorNumElements(); i != e; ++i) {
3598 Val = SVOp->getMaskElt(i);
3599 if (Val >= 0)
3600 break;
3601 }
3602 return (Val - i) * EltSize;
3603}
3604
David Greenec38a03e2011-02-03 15:50:00 +00003605/// getExtractVEXTRACTF128Immediate - Return the appropriate immediate
3606/// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF128
3607/// instructions.
3608unsigned X86::getExtractVEXTRACTF128Immediate(SDNode *N) {
3609 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3610 llvm_unreachable("Illegal extract subvector for VEXTRACTF128");
3611
3612 uint64_t Index =
3613 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
3614
3615 EVT VecVT = N->getOperand(0).getValueType();
3616 EVT ElVT = VecVT.getVectorElementType();
3617
3618 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
3619
3620 return Index / NumElemsPerChunk;
3621}
3622
David Greeneccacdc12011-02-04 16:08:29 +00003623/// getInsertVINSERTF128Immediate - Return the appropriate immediate
3624/// to insert at the specified INSERT_SUBVECTOR index with VINSERTF128
3625/// instructions.
3626unsigned X86::getInsertVINSERTF128Immediate(SDNode *N) {
3627 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
3628 llvm_unreachable("Illegal insert subvector for VINSERTF128");
3629
3630 uint64_t Index =
NAKAMURA Takumi27635382011-02-05 15:10:54 +00003631 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
David Greeneccacdc12011-02-04 16:08:29 +00003632
3633 EVT VecVT = N->getValueType(0);
3634 EVT ElVT = VecVT.getVectorElementType();
3635
3636 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
3637
3638 return Index / NumElemsPerChunk;
3639}
3640
Evan Cheng37b73872009-07-30 08:33:02 +00003641/// isZeroNode - Returns true if Elt is a constant zero or a floating point
3642/// constant +0.0.
3643bool X86::isZeroNode(SDValue Elt) {
3644 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00003645 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00003646 (isa<ConstantFPSDNode>(Elt) &&
3647 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
3648}
3649
Nate Begeman9008ca62009-04-27 18:41:29 +00003650/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
3651/// their permute mask.
3652static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
3653 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003654 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003655 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00003656 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00003657
Nate Begeman5a5ca152009-04-29 05:20:52 +00003658 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003659 int idx = SVOp->getMaskElt(i);
3660 if (idx < 0)
3661 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003662 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003663 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003664 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003665 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003666 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003667 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
3668 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003669}
3670
Evan Cheng779ccea2007-12-07 21:30:01 +00003671/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3672/// the two vector operands have swapped position.
Owen Andersone50ed302009-08-10 22:56:29 +00003673static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00003674 unsigned NumElems = VT.getVectorNumElements();
3675 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003676 int idx = Mask[i];
3677 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003678 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003679 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003680 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003681 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003682 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003683 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003684}
3685
Evan Cheng533a0aa2006-04-19 20:35:22 +00003686/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
3687/// match movhlps. The lower half elements should come from upper half of
3688/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003689/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00003690static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
3691 if (Op->getValueType(0).getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00003692 return false;
3693 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003694 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003695 return false;
3696 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003697 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003698 return false;
3699 return true;
3700}
3701
Evan Cheng5ced1d82006-04-06 23:23:56 +00003702/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00003703/// is promoted to a vector. It also returns the LoadSDNode by reference if
3704/// required.
3705static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00003706 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
3707 return false;
3708 N = N->getOperand(0).getNode();
3709 if (!ISD::isNON_EXTLoad(N))
3710 return false;
3711 if (LD)
3712 *LD = cast<LoadSDNode>(N);
3713 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003714}
3715
Evan Cheng533a0aa2006-04-19 20:35:22 +00003716/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
3717/// match movlp{s|d}. The lower half elements should come from lower half of
3718/// V1 (and in order), and the upper half elements should come from the upper
3719/// half of V2 (and in order). And since V1 will become the source of the
3720/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003721static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
3722 ShuffleVectorSDNode *Op) {
Evan Cheng466685d2006-10-09 20:57:25 +00003723 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003724 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00003725 // Is V2 is a vector load, don't do this transformation. We will try to use
3726 // load folding shufps op.
3727 if (ISD::isNON_EXTLoad(V2))
3728 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003729
Nate Begeman5a5ca152009-04-29 05:20:52 +00003730 unsigned NumElems = Op->getValueType(0).getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003731
Evan Cheng533a0aa2006-04-19 20:35:22 +00003732 if (NumElems != 2 && NumElems != 4)
3733 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003734 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003735 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003736 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003737 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003738 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003739 return false;
3740 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003741}
3742
Evan Cheng39623da2006-04-20 08:58:49 +00003743/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
3744/// all the same.
3745static bool isSplatVector(SDNode *N) {
3746 if (N->getOpcode() != ISD::BUILD_VECTOR)
3747 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003748
Dan Gohman475871a2008-07-27 21:46:04 +00003749 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00003750 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
3751 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003752 return false;
3753 return true;
3754}
3755
Evan Cheng213d2cf2007-05-17 18:45:50 +00003756/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00003757/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00003758/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00003759static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00003760 SDValue V1 = N->getOperand(0);
3761 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003762 unsigned NumElems = N->getValueType(0).getVectorNumElements();
3763 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003764 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003765 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003766 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00003767 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
3768 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003769 if (Opc != ISD::BUILD_VECTOR ||
3770 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00003771 return false;
3772 } else if (Idx >= 0) {
3773 unsigned Opc = V1.getOpcode();
3774 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
3775 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003776 if (Opc != ISD::BUILD_VECTOR ||
3777 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00003778 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00003779 }
3780 }
3781 return true;
3782}
3783
3784/// getZeroVector - Returns a vector of specified type with all zero elements.
3785///
Owen Andersone50ed302009-08-10 22:56:29 +00003786static SDValue getZeroVector(EVT VT, bool HasSSE2, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00003787 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003788 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003789
Dale Johannesen0488fb62010-09-30 23:57:10 +00003790 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003791 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00003792 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00003793 if (VT.getSizeInBits() == 128) { // SSE
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003794 if (HasSSE2) { // SSE2
3795 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3796 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
3797 } else { // SSE1
3798 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
3799 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
3800 }
3801 } else if (VT.getSizeInBits() == 256) { // AVX
3802 // 256-bit logic and arithmetic instructions in AVX are
3803 // all floating-point, no support for integer ops. Default
3804 // to emitting fp zeroed vectors then.
Owen Anderson825b72b2009-08-11 20:47:22 +00003805 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003806 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
3807 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
Evan Chengf0df0312008-05-15 08:39:06 +00003808 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003809 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00003810}
3811
Chris Lattner8a594482007-11-25 00:24:49 +00003812/// getOnesVector - Returns a vector of specified type with all bits set.
3813///
Owen Andersone50ed302009-08-10 22:56:29 +00003814static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003815 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003816
Chris Lattner8a594482007-11-25 00:24:49 +00003817 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
3818 // type. This ensures they get CSE'd.
Owen Anderson825b72b2009-08-11 20:47:22 +00003819 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00003820 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00003821 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003822 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00003823}
3824
3825
Evan Cheng39623da2006-04-20 08:58:49 +00003826/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
3827/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00003828static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003829 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003830 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003831
Evan Cheng39623da2006-04-20 08:58:49 +00003832 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003833 SmallVector<int, 8> MaskVec;
3834 SVOp->getMask(MaskVec);
Eric Christopherfd179292009-08-27 18:07:15 +00003835
Nate Begeman5a5ca152009-04-29 05:20:52 +00003836 for (unsigned i = 0; i != NumElems; ++i) {
3837 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003838 MaskVec[i] = NumElems;
3839 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00003840 }
Evan Cheng39623da2006-04-20 08:58:49 +00003841 }
Evan Cheng39623da2006-04-20 08:58:49 +00003842 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00003843 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
3844 SVOp->getOperand(1), &MaskVec[0]);
3845 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00003846}
3847
Evan Cheng017dcc62006-04-21 01:05:10 +00003848/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
3849/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00003850static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003851 SDValue V2) {
3852 unsigned NumElems = VT.getVectorNumElements();
3853 SmallVector<int, 8> Mask;
3854 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00003855 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003856 Mask.push_back(i);
3857 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00003858}
3859
Nate Begeman9008ca62009-04-27 18:41:29 +00003860/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003861static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003862 SDValue V2) {
3863 unsigned NumElems = VT.getVectorNumElements();
3864 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00003865 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003866 Mask.push_back(i);
3867 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00003868 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003869 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00003870}
3871
Nate Begeman9008ca62009-04-27 18:41:29 +00003872/// getUnpackhMask - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003873static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003874 SDValue V2) {
3875 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00003876 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00003877 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00003878 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003879 Mask.push_back(i + Half);
3880 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00003881 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003882 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003883}
3884
Bruno Cardoso Lopesbb0a9482010-08-13 17:50:47 +00003885/// PromoteSplat - Promote a splat of v4i32, v8i16 or v16i8 to v4f32.
3886static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003887 EVT PVT = MVT::v4f32;
Owen Andersone50ed302009-08-10 22:56:29 +00003888 EVT VT = SV->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00003889 DebugLoc dl = SV->getDebugLoc();
3890 SDValue V1 = SV->getOperand(0);
3891 int NumElems = VT.getVectorNumElements();
3892 int EltNo = SV->getSplatIndex();
Rafael Espindola15684b22009-04-24 12:40:33 +00003893
Nate Begeman9008ca62009-04-27 18:41:29 +00003894 // unpack elements to the correct location
3895 while (NumElems > 4) {
3896 if (EltNo < NumElems/2) {
3897 V1 = getUnpackl(DAG, dl, VT, V1, V1);
3898 } else {
3899 V1 = getUnpackh(DAG, dl, VT, V1, V1);
3900 EltNo -= NumElems/2;
3901 }
3902 NumElems >>= 1;
3903 }
Eric Christopherfd179292009-08-27 18:07:15 +00003904
Nate Begeman9008ca62009-04-27 18:41:29 +00003905 // Perform the splat.
3906 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003907 V1 = DAG.getNode(ISD::BITCAST, dl, PVT, V1);
Nate Begeman9008ca62009-04-27 18:41:29 +00003908 V1 = DAG.getVectorShuffle(PVT, dl, V1, DAG.getUNDEF(PVT), &SplatMask[0]);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003909 return DAG.getNode(ISD::BITCAST, dl, VT, V1);
Evan Chengc575ca22006-04-17 20:43:08 +00003910}
3911
Evan Chengba05f722006-04-21 23:03:30 +00003912/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00003913/// vector of zero or undef vector. This produces a shuffle where the low
3914/// element of V2 is swizzled into the zero/undef vector, landing at element
3915/// 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 +00003916static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Chengf0df0312008-05-15 08:39:06 +00003917 bool isZero, bool HasSSE2,
3918 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003919 EVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00003920 SDValue V1 = isZero
Nate Begeman9008ca62009-04-27 18:41:29 +00003921 ? getZeroVector(VT, HasSSE2, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
3922 unsigned NumElems = VT.getVectorNumElements();
3923 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00003924 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003925 // If this is the insertion idx, put the low elt of V2 here.
3926 MaskVec.push_back(i == Idx ? NumElems : i);
3927 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00003928}
3929
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003930/// getShuffleScalarElt - Returns the scalar element that will make up the ith
3931/// element of the result of the vector shuffle.
Benjamin Kramer050db522011-03-26 12:38:19 +00003932static SDValue getShuffleScalarElt(SDNode *N, int Index, SelectionDAG &DAG,
3933 unsigned Depth) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003934 if (Depth == 6)
3935 return SDValue(); // Limit search depth.
3936
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003937 SDValue V = SDValue(N, 0);
3938 EVT VT = V.getValueType();
3939 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003940
3941 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
3942 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
3943 Index = SV->getMaskElt(Index);
3944
3945 if (Index < 0)
3946 return DAG.getUNDEF(VT.getVectorElementType());
3947
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003948 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003949 SDValue NewV = (Index < NumElems) ? SV->getOperand(0) : SV->getOperand(1);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003950 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00003951 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003952
3953 // Recurse into target specific vector shuffles to find scalars.
3954 if (isTargetShuffle(Opcode)) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003955 int NumElems = VT.getVectorNumElements();
3956 SmallVector<unsigned, 16> ShuffleMask;
3957 SDValue ImmN;
3958
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003959 switch(Opcode) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003960 case X86ISD::SHUFPS:
3961 case X86ISD::SHUFPD:
3962 ImmN = N->getOperand(N->getNumOperands()-1);
3963 DecodeSHUFPSMask(NumElems,
3964 cast<ConstantSDNode>(ImmN)->getZExtValue(),
3965 ShuffleMask);
3966 break;
3967 case X86ISD::PUNPCKHBW:
3968 case X86ISD::PUNPCKHWD:
3969 case X86ISD::PUNPCKHDQ:
3970 case X86ISD::PUNPCKHQDQ:
3971 DecodePUNPCKHMask(NumElems, ShuffleMask);
3972 break;
3973 case X86ISD::UNPCKHPS:
3974 case X86ISD::UNPCKHPD:
3975 DecodeUNPCKHPMask(NumElems, ShuffleMask);
3976 break;
3977 case X86ISD::PUNPCKLBW:
3978 case X86ISD::PUNPCKLWD:
3979 case X86ISD::PUNPCKLDQ:
3980 case X86ISD::PUNPCKLQDQ:
David Greenec4db4e52011-02-28 19:06:56 +00003981 DecodePUNPCKLMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003982 break;
3983 case X86ISD::UNPCKLPS:
3984 case X86ISD::UNPCKLPD:
David Greenec4db4e52011-02-28 19:06:56 +00003985 case X86ISD::VUNPCKLPS:
3986 case X86ISD::VUNPCKLPD:
3987 case X86ISD::VUNPCKLPSY:
3988 case X86ISD::VUNPCKLPDY:
3989 DecodeUNPCKLPMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003990 break;
3991 case X86ISD::MOVHLPS:
3992 DecodeMOVHLPSMask(NumElems, ShuffleMask);
3993 break;
3994 case X86ISD::MOVLHPS:
3995 DecodeMOVLHPSMask(NumElems, ShuffleMask);
3996 break;
3997 case X86ISD::PSHUFD:
3998 ImmN = N->getOperand(N->getNumOperands()-1);
3999 DecodePSHUFMask(NumElems,
4000 cast<ConstantSDNode>(ImmN)->getZExtValue(),
4001 ShuffleMask);
4002 break;
4003 case X86ISD::PSHUFHW:
4004 ImmN = N->getOperand(N->getNumOperands()-1);
4005 DecodePSHUFHWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
4006 ShuffleMask);
4007 break;
4008 case X86ISD::PSHUFLW:
4009 ImmN = N->getOperand(N->getNumOperands()-1);
4010 DecodePSHUFLWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
4011 ShuffleMask);
4012 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004013 case X86ISD::MOVSS:
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00004014 case X86ISD::MOVSD: {
4015 // The index 0 always comes from the first element of the second source,
4016 // this is why MOVSS and MOVSD are used in the first place. The other
4017 // elements come from the other positions of the first source vector.
4018 unsigned OpNum = (Index == 0) ? 1 : 0;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004019 return getShuffleScalarElt(V.getOperand(OpNum).getNode(), Index, DAG,
4020 Depth+1);
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00004021 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004022 default:
4023 assert("not implemented for target shuffle node");
4024 return SDValue();
4025 }
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004026
4027 Index = ShuffleMask[Index];
4028 if (Index < 0)
4029 return DAG.getUNDEF(VT.getVectorElementType());
4030
4031 SDValue NewV = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
4032 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG,
4033 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004034 }
4035
4036 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004037 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004038 V = V.getOperand(0);
4039 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004040 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004041
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004042 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004043 return SDValue();
4044 }
4045
4046 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
4047 return (Index == 0) ? V.getOperand(0)
4048 : DAG.getUNDEF(VT.getVectorElementType());
4049
4050 if (V.getOpcode() == ISD::BUILD_VECTOR)
4051 return V.getOperand(Index);
4052
4053 return SDValue();
4054}
4055
4056/// getNumOfConsecutiveZeros - Return the number of elements of a vector
4057/// shuffle operation which come from a consecutively from a zero. The
Chris Lattner7a2bdde2011-04-15 05:18:47 +00004058/// search can start in two different directions, from left or right.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004059static
4060unsigned getNumOfConsecutiveZeros(SDNode *N, int NumElems,
4061 bool ZerosFromLeft, SelectionDAG &DAG) {
4062 int i = 0;
4063
4064 while (i < NumElems) {
4065 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004066 SDValue Elt = getShuffleScalarElt(N, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004067 if (!(Elt.getNode() &&
4068 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
4069 break;
4070 ++i;
4071 }
4072
4073 return i;
4074}
4075
4076/// isShuffleMaskConsecutive - Check if the shuffle mask indicies from MaskI to
4077/// MaskE correspond consecutively to elements from one of the vector operands,
4078/// starting from its index OpIdx. Also tell OpNum which source vector operand.
4079static
4080bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp, int MaskI, int MaskE,
4081 int OpIdx, int NumElems, unsigned &OpNum) {
4082 bool SeenV1 = false;
4083 bool SeenV2 = false;
4084
4085 for (int i = MaskI; i <= MaskE; ++i, ++OpIdx) {
4086 int Idx = SVOp->getMaskElt(i);
4087 // Ignore undef indicies
4088 if (Idx < 0)
4089 continue;
4090
4091 if (Idx < NumElems)
4092 SeenV1 = true;
4093 else
4094 SeenV2 = true;
4095
4096 // Only accept consecutive elements from the same vector
4097 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
4098 return false;
4099 }
4100
4101 OpNum = SeenV1 ? 0 : 1;
4102 return true;
4103}
4104
4105/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
4106/// logical left shift of a vector.
4107static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4108 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4109 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4110 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4111 false /* check zeros from right */, DAG);
4112 unsigned OpSrc;
4113
4114 if (!NumZeros)
4115 return false;
4116
4117 // Considering the elements in the mask that are not consecutive zeros,
4118 // check if they consecutively come from only one of the source vectors.
4119 //
4120 // V1 = {X, A, B, C} 0
4121 // \ \ \ /
4122 // vector_shuffle V1, V2 <1, 2, 3, X>
4123 //
4124 if (!isShuffleMaskConsecutive(SVOp,
4125 0, // Mask Start Index
4126 NumElems-NumZeros-1, // Mask End Index
4127 NumZeros, // Where to start looking in the src vector
4128 NumElems, // Number of elements in vector
4129 OpSrc)) // Which source operand ?
4130 return false;
4131
4132 isLeft = false;
4133 ShAmt = NumZeros;
4134 ShVal = SVOp->getOperand(OpSrc);
4135 return true;
4136}
4137
4138/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
4139/// logical left shift of a vector.
4140static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4141 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4142 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4143 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4144 true /* check zeros from left */, DAG);
4145 unsigned OpSrc;
4146
4147 if (!NumZeros)
4148 return false;
4149
4150 // Considering the elements in the mask that are not consecutive zeros,
4151 // check if they consecutively come from only one of the source vectors.
4152 //
4153 // 0 { A, B, X, X } = V2
4154 // / \ / /
4155 // vector_shuffle V1, V2 <X, X, 4, 5>
4156 //
4157 if (!isShuffleMaskConsecutive(SVOp,
4158 NumZeros, // Mask Start Index
4159 NumElems-1, // Mask End Index
4160 0, // Where to start looking in the src vector
4161 NumElems, // Number of elements in vector
4162 OpSrc)) // Which source operand ?
4163 return false;
4164
4165 isLeft = true;
4166 ShAmt = NumZeros;
4167 ShVal = SVOp->getOperand(OpSrc);
4168 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004169}
4170
4171/// isVectorShift - Returns true if the shuffle can be implemented as a
4172/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004173static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00004174 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004175 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
4176 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
4177 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004178
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004179 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00004180}
4181
Evan Chengc78d3b42006-04-24 18:01:45 +00004182/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
4183///
Dan Gohman475871a2008-07-27 21:46:04 +00004184static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00004185 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00004186 SelectionDAG &DAG,
4187 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004188 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00004189 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004190
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004191 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004192 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004193 bool First = true;
4194 for (unsigned i = 0; i < 16; ++i) {
4195 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
4196 if (ThisIsNonZero && First) {
4197 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004198 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004199 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004200 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004201 First = false;
4202 }
4203
4204 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00004205 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004206 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
4207 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004208 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004209 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00004210 }
4211 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004212 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
4213 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
4214 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00004215 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004216 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00004217 } else
4218 ThisElt = LastElt;
4219
Gabor Greifba36cb52008-08-28 21:40:38 +00004220 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00004221 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00004222 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00004223 }
4224 }
4225
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004226 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00004227}
4228
Bill Wendlinga348c562007-03-22 18:42:45 +00004229/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00004230///
Dan Gohman475871a2008-07-27 21:46:04 +00004231static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00004232 unsigned NumNonZero, unsigned NumZero,
4233 SelectionDAG &DAG,
4234 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004235 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00004236 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004237
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004238 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004239 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004240 bool First = true;
4241 for (unsigned i = 0; i < 8; ++i) {
4242 bool isNonZero = (NonZeros & (1 << i)) != 0;
4243 if (isNonZero) {
4244 if (First) {
4245 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004246 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004247 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004248 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004249 First = false;
4250 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004251 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004252 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00004253 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00004254 }
4255 }
4256
4257 return V;
4258}
4259
Evan Chengf26ffe92008-05-29 08:22:04 +00004260/// getVShift - Return a vector logical shift node.
4261///
Owen Andersone50ed302009-08-10 22:56:29 +00004262static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00004263 unsigned NumBits, SelectionDAG &DAG,
4264 const TargetLowering &TLI, DebugLoc dl) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004265 EVT ShVT = MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00004266 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004267 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
4268 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004269 DAG.getNode(Opc, dl, ShVT, SrcOp,
Owen Anderson95771af2011-02-25 21:41:48 +00004270 DAG.getConstant(NumBits,
4271 TLI.getShiftAmountTy(SrcOp.getValueType()))));
Evan Chengf26ffe92008-05-29 08:22:04 +00004272}
4273
Dan Gohman475871a2008-07-27 21:46:04 +00004274SDValue
Evan Chengc3630942009-12-09 21:00:30 +00004275X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00004276 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00004277
Evan Chengc3630942009-12-09 21:00:30 +00004278 // Check if the scalar load can be widened into a vector load. And if
4279 // the address is "base + cst" see if the cst can be "absorbed" into
4280 // the shuffle mask.
4281 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
4282 SDValue Ptr = LD->getBasePtr();
4283 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
4284 return SDValue();
4285 EVT PVT = LD->getValueType(0);
4286 if (PVT != MVT::i32 && PVT != MVT::f32)
4287 return SDValue();
4288
4289 int FI = -1;
4290 int64_t Offset = 0;
4291 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
4292 FI = FINode->getIndex();
4293 Offset = 0;
Chris Lattner0a9481f2011-02-13 22:25:43 +00004294 } else if (DAG.isBaseWithConstantOffset(Ptr) &&
Evan Chengc3630942009-12-09 21:00:30 +00004295 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4296 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4297 Offset = Ptr.getConstantOperandVal(1);
4298 Ptr = Ptr.getOperand(0);
4299 } else {
4300 return SDValue();
4301 }
4302
4303 SDValue Chain = LD->getChain();
4304 // Make sure the stack object alignment is at least 16.
4305 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
4306 if (DAG.InferPtrAlignment(Ptr) < 16) {
4307 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00004308 // Can't change the alignment. FIXME: It's possible to compute
4309 // the exact stack offset and reference FI + adjust offset instead.
4310 // If someone *really* cares about this. That's the way to implement it.
4311 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004312 } else {
4313 MFI->setObjectAlignment(FI, 16);
4314 }
4315 }
4316
4317 // (Offset % 16) must be multiple of 4. Then address is then
4318 // Ptr + (Offset & ~15).
4319 if (Offset < 0)
4320 return SDValue();
4321 if ((Offset % 16) & 3)
4322 return SDValue();
4323 int64_t StartOffset = Offset & ~15;
4324 if (StartOffset)
4325 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
4326 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
4327
4328 int EltNo = (Offset - StartOffset) >> 2;
4329 int Mask[4] = { EltNo, EltNo, EltNo, EltNo };
4330 EVT VT = (PVT == MVT::i32) ? MVT::v4i32 : MVT::v4f32;
Chris Lattner51abfe42010-09-21 06:02:19 +00004331 SDValue V1 = DAG.getLoad(VT, dl, Chain, Ptr,
4332 LD->getPointerInfo().getWithOffset(StartOffset),
David Greene67c9d422010-02-15 16:53:33 +00004333 false, false, 0);
Evan Chengc3630942009-12-09 21:00:30 +00004334 // Canonicalize it to a v4i32 shuffle.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004335 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, V1);
4336 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Chengc3630942009-12-09 21:00:30 +00004337 DAG.getVectorShuffle(MVT::v4i32, dl, V1,
Chris Lattner51abfe42010-09-21 06:02:19 +00004338 DAG.getUNDEF(MVT::v4i32),&Mask[0]));
Evan Chengc3630942009-12-09 21:00:30 +00004339 }
4340
4341 return SDValue();
4342}
4343
Michael J. Spencerec38de22010-10-10 22:04:20 +00004344/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
4345/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00004346/// load which has the same value as a build_vector whose operands are 'elts'.
4347///
4348/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00004349///
Nate Begeman1449f292010-03-24 22:19:06 +00004350/// FIXME: we'd also like to handle the case where the last elements are zero
4351/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
4352/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004353static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00004354 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004355 EVT EltVT = VT.getVectorElementType();
4356 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00004357
Nate Begemanfdea31a2010-03-24 20:49:50 +00004358 LoadSDNode *LDBase = NULL;
4359 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004360
Nate Begeman1449f292010-03-24 22:19:06 +00004361 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00004362 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00004363 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004364 for (unsigned i = 0; i < NumElems; ++i) {
4365 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00004366
Nate Begemanfdea31a2010-03-24 20:49:50 +00004367 if (!Elt.getNode() ||
4368 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
4369 return SDValue();
4370 if (!LDBase) {
4371 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
4372 return SDValue();
4373 LDBase = cast<LoadSDNode>(Elt.getNode());
4374 LastLoadedElt = i;
4375 continue;
4376 }
4377 if (Elt.getOpcode() == ISD::UNDEF)
4378 continue;
4379
4380 LoadSDNode *LD = cast<LoadSDNode>(Elt);
4381 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
4382 return SDValue();
4383 LastLoadedElt = i;
4384 }
Nate Begeman1449f292010-03-24 22:19:06 +00004385
4386 // If we have found an entire vector of loads and undefs, then return a large
4387 // load of the entire vector width starting at the base pointer. If we found
4388 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004389 if (LastLoadedElt == NumElems - 1) {
4390 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00004391 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004392 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004393 LDBase->isVolatile(), LDBase->isNonTemporal(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00004394 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004395 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004396 LDBase->isVolatile(), LDBase->isNonTemporal(),
4397 LDBase->getAlignment());
4398 } else if (NumElems == 4 && LastLoadedElt == 1) {
4399 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
4400 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Chris Lattner88641552010-09-22 00:34:38 +00004401 SDValue ResNode = DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys,
4402 Ops, 2, MVT::i32,
4403 LDBase->getMemOperand());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004404 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00004405 }
4406 return SDValue();
4407}
4408
Evan Chengc3630942009-12-09 21:00:30 +00004409SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004410X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004411 DebugLoc dl = Op.getDebugLoc();
David Greenea5f26012011-02-07 19:36:54 +00004412
David Greenef125a292011-02-08 19:04:41 +00004413 EVT VT = Op.getValueType();
4414 EVT ExtVT = VT.getVectorElementType();
4415
4416 unsigned NumElems = Op.getNumOperands();
4417
4418 // For AVX-length vectors, build the individual 128-bit pieces and
4419 // use shuffles to put them in place.
Owen Anderson95771af2011-02-25 21:41:48 +00004420 if (VT.getSizeInBits() > 256 &&
4421 Subtarget->hasAVX() &&
David Greenef125a292011-02-08 19:04:41 +00004422 !ISD::isBuildVectorAllZeros(Op.getNode())) {
4423 SmallVector<SDValue, 8> V;
4424 V.resize(NumElems);
4425 for (unsigned i = 0; i < NumElems; ++i) {
4426 V[i] = Op.getOperand(i);
4427 }
Owen Anderson95771af2011-02-25 21:41:48 +00004428
David Greenef125a292011-02-08 19:04:41 +00004429 EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElems/2);
4430
4431 // Build the lower subvector.
4432 SDValue Lower = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[0], NumElems/2);
4433 // Build the upper subvector.
4434 SDValue Upper = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[NumElems / 2],
4435 NumElems/2);
4436
4437 return ConcatVectors(Lower, Upper, DAG);
4438 }
4439
Chris Lattner6e80e442010-08-28 17:15:43 +00004440 // All zero's are handled with pxor in SSE2 and above, xorps in SSE1.
4441 // All one's are handled with pcmpeqd. In AVX, zero's are handled with
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004442 // vpxor in 128-bit and xor{pd,ps} in 256-bit, but no 256 version of pcmpeqd
4443 // is present, so AllOnes is ignored.
4444 if (ISD::isBuildVectorAllZeros(Op.getNode()) ||
4445 (Op.getValueType().getSizeInBits() != 256 &&
4446 ISD::isBuildVectorAllOnes(Op.getNode()))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004447 // Canonicalize this to <4 x i32> (SSE) to
Chris Lattner8a594482007-11-25 00:24:49 +00004448 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
4449 // eliminated on x86-32 hosts.
Dale Johannesen0488fb62010-09-30 23:57:10 +00004450 if (Op.getValueType() == MVT::v4i32)
Chris Lattner8a594482007-11-25 00:24:49 +00004451 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004452
Gabor Greifba36cb52008-08-28 21:40:38 +00004453 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00004454 return getOnesVector(Op.getValueType(), DAG, dl);
4455 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00004456 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004457
Owen Andersone50ed302009-08-10 22:56:29 +00004458 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004459
Evan Cheng0db9fe62006-04-25 20:13:52 +00004460 unsigned NumZero = 0;
4461 unsigned NumNonZero = 0;
4462 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004463 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00004464 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004465 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00004466 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00004467 if (Elt.getOpcode() == ISD::UNDEF)
4468 continue;
4469 Values.insert(Elt);
4470 if (Elt.getOpcode() != ISD::Constant &&
4471 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004472 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00004473 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00004474 NumZero++;
4475 else {
4476 NonZeros |= (1 << i);
4477 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004478 }
4479 }
4480
Chris Lattner97a2a562010-08-26 05:24:29 +00004481 // All undef vector. Return an UNDEF. All zero vectors were handled above.
4482 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00004483 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004484
Chris Lattner67f453a2008-03-09 05:42:06 +00004485 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00004486 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004487 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00004488 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00004489
Chris Lattner62098042008-03-09 01:05:04 +00004490 // If this is an insertion of an i64 value on x86-32, and if the top bits of
4491 // the value are obviously zero, truncate the value to i32 and do the
4492 // insertion that way. Only do this if the value is non-constant or if the
4493 // value is a constant being inserted into element 0. It is cheaper to do
4494 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00004495 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00004496 (!IsAllConstants || Idx == 0)) {
4497 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004498 // Handle SSE only.
4499 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
4500 EVT VecVT = MVT::v4i32;
4501 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00004502
Chris Lattner62098042008-03-09 01:05:04 +00004503 // Truncate the value (which may itself be a constant) to i32, and
4504 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00004505 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00004506 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00004507 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
4508 Subtarget->hasSSE2(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00004509
Chris Lattner62098042008-03-09 01:05:04 +00004510 // Now we have our 32-bit value zero extended in the low element of
4511 // a vector. If Idx != 0, swizzle it into place.
4512 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004513 SmallVector<int, 4> Mask;
4514 Mask.push_back(Idx);
4515 for (unsigned i = 1; i != VecElts; ++i)
4516 Mask.push_back(i);
4517 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopherfd179292009-08-27 18:07:15 +00004518 DAG.getUNDEF(Item.getValueType()),
Nate Begeman9008ca62009-04-27 18:41:29 +00004519 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004520 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004521 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00004522 }
4523 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004524
Chris Lattner19f79692008-03-08 22:59:52 +00004525 // If we have a constant or non-constant insertion into the low element of
4526 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
4527 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00004528 // depending on what the source datatype is.
4529 if (Idx == 0) {
4530 if (NumZero == 0) {
4531 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Owen Anderson825b72b2009-08-11 20:47:22 +00004532 } else if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
4533 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Eli Friedman10415532009-06-06 06:05:10 +00004534 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
4535 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
4536 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget->hasSSE2(),
4537 DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00004538 } else if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
4539 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Dale Johannesen0488fb62010-09-30 23:57:10 +00004540 assert(VT.getSizeInBits() == 128 && "Expected an SSE value type!");
4541 EVT MiddleVT = MVT::v4i32;
Eli Friedman10415532009-06-06 06:05:10 +00004542 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
4543 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
4544 Subtarget->hasSSE2(), DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004545 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00004546 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004547 }
Evan Chengf26ffe92008-05-29 08:22:04 +00004548
4549 // Is it a vector logical left shift?
4550 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00004551 X86::isZeroNode(Op.getOperand(0)) &&
4552 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004553 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00004554 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00004555 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004556 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00004557 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004558 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004559
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004560 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00004561 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004562
Chris Lattner19f79692008-03-08 22:59:52 +00004563 // Otherwise, if this is a vector with i32 or f32 elements, and the element
4564 // is a non-constant being inserted into an element other than the low one,
4565 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
4566 // movd/movss) to move this into the low element, then shuffle it into
4567 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004568 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00004569 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00004570
Evan Cheng0db9fe62006-04-25 20:13:52 +00004571 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00004572 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
4573 Subtarget->hasSSE2(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00004574 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004575 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00004576 MaskVec.push_back(i == Idx ? 0 : 1);
4577 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004578 }
4579 }
4580
Chris Lattner67f453a2008-03-09 05:42:06 +00004581 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00004582 if (Values.size() == 1) {
4583 if (EVTBits == 32) {
4584 // Instead of a shuffle like this:
4585 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
4586 // Check if it's possible to issue this instead.
4587 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
4588 unsigned Idx = CountTrailingZeros_32(NonZeros);
4589 SDValue Item = Op.getOperand(Idx);
4590 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
4591 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
4592 }
Dan Gohman475871a2008-07-27 21:46:04 +00004593 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004594 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004595
Dan Gohmana3941172007-07-24 22:55:08 +00004596 // A vector full of immediates; various special cases are already
4597 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004598 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00004599 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00004600
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00004601 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00004602 if (EVTBits == 64) {
4603 if (NumNonZero == 1) {
4604 // One half is zero or undef.
4605 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00004606 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00004607 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00004608 return getShuffleVectorZeroOrUndef(V2, Idx, true,
4609 Subtarget->hasSSE2(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00004610 }
Dan Gohman475871a2008-07-27 21:46:04 +00004611 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00004612 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004613
4614 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00004615 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00004616 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00004617 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004618 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004619 }
4620
Bill Wendling826f36f2007-03-28 00:57:11 +00004621 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00004622 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Chris Lattner97a2a562010-08-26 05:24:29 +00004623 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004624 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004625 }
4626
4627 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00004628 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00004629 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004630 if (NumElems == 4 && NumZero > 0) {
4631 for (unsigned i = 0; i < 4; ++i) {
4632 bool isZero = !(NonZeros & (1 << i));
4633 if (isZero)
Dale Johannesenace16102009-02-03 19:33:06 +00004634 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004635 else
Dale Johannesenace16102009-02-03 19:33:06 +00004636 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004637 }
4638
4639 for (unsigned i = 0; i < 2; ++i) {
4640 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
4641 default: break;
4642 case 0:
4643 V[i] = V[i*2]; // Must be a zero vector.
4644 break;
4645 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00004646 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004647 break;
4648 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00004649 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004650 break;
4651 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00004652 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004653 break;
4654 }
4655 }
4656
Nate Begeman9008ca62009-04-27 18:41:29 +00004657 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004658 bool Reverse = (NonZeros & 0x3) == 2;
4659 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004660 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004661 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
4662 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004663 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
4664 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004665 }
4666
Nate Begemanfdea31a2010-03-24 20:49:50 +00004667 if (Values.size() > 1 && VT.getSizeInBits() == 128) {
4668 // Check for a build vector of consecutive loads.
4669 for (unsigned i = 0; i < NumElems; ++i)
4670 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00004671
Nate Begemanfdea31a2010-03-24 20:49:50 +00004672 // Check for elements which are consecutive loads.
4673 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
4674 if (LD.getNode())
4675 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004676
4677 // For SSE 4.1, use insertps to put the high elements into the low element.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004678 if (getSubtarget()->hasSSE41()) {
Chris Lattner24faf612010-08-28 17:59:08 +00004679 SDValue Result;
4680 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
4681 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
4682 else
4683 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00004684
Chris Lattner24faf612010-08-28 17:59:08 +00004685 for (unsigned i = 1; i < NumElems; ++i) {
4686 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
4687 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00004688 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00004689 }
4690 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00004691 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00004692
Chris Lattner6e80e442010-08-28 17:15:43 +00004693 // Otherwise, expand into a number of unpckl*, start by extending each of
4694 // our (non-undef) elements to the full vector width with the element in the
4695 // bottom slot of the vector (which generates no code for SSE).
4696 for (unsigned i = 0; i < NumElems; ++i) {
4697 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
4698 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
4699 else
4700 V[i] = DAG.getUNDEF(VT);
4701 }
4702
4703 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004704 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
4705 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
4706 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00004707 unsigned EltStride = NumElems >> 1;
4708 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00004709 for (unsigned i = 0; i < EltStride; ++i) {
4710 // If V[i+EltStride] is undef and this is the first round of mixing,
4711 // then it is safe to just drop this shuffle: V[i] is already in the
4712 // right place, the one element (since it's the first round) being
4713 // inserted as undef can be dropped. This isn't safe for successive
4714 // rounds because they will permute elements within both vectors.
4715 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
4716 EltStride == NumElems/2)
4717 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004718
Chris Lattner6e80e442010-08-28 17:15:43 +00004719 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00004720 }
Chris Lattner6e80e442010-08-28 17:15:43 +00004721 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004722 }
4723 return V[0];
4724 }
Dan Gohman475871a2008-07-27 21:46:04 +00004725 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004726}
4727
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004728SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004729X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const {
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004730 // We support concatenate two MMX registers and place them in a MMX
4731 // register. This is better than doing a stack convert.
4732 DebugLoc dl = Op.getDebugLoc();
4733 EVT ResVT = Op.getValueType();
4734 assert(Op.getNumOperands() == 2);
4735 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
4736 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
4737 int Mask[2];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004738 SDValue InVec = DAG.getNode(ISD::BITCAST,dl, MVT::v1i64, Op.getOperand(0));
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004739 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4740 InVec = Op.getOperand(1);
4741 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4742 unsigned NumElts = ResVT.getVectorNumElements();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004743 VecOp = DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004744 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
4745 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
4746 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004747 InVec = DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, InVec);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004748 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4749 Mask[0] = 0; Mask[1] = 2;
4750 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
4751 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004752 return DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004753}
4754
Nate Begemanb9a47b82009-02-23 08:49:38 +00004755// v8i16 shuffles - Prefer shuffles in the following order:
4756// 1. [all] pshuflw, pshufhw, optional move
4757// 2. [ssse3] 1 x pshufb
4758// 3. [ssse3] 2 x pshufb + 1 x por
4759// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004760SDValue
4761X86TargetLowering::LowerVECTOR_SHUFFLEv8i16(SDValue Op,
4762 SelectionDAG &DAG) const {
4763 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00004764 SDValue V1 = SVOp->getOperand(0);
4765 SDValue V2 = SVOp->getOperand(1);
4766 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004767 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00004768
Nate Begemanb9a47b82009-02-23 08:49:38 +00004769 // Determine if more than 1 of the words in each of the low and high quadwords
4770 // of the result come from the same quadword of one of the two inputs. Undef
4771 // mask values count as coming from any quadword, for better codegen.
4772 SmallVector<unsigned, 4> LoQuad(4);
4773 SmallVector<unsigned, 4> HiQuad(4);
4774 BitVector InputQuads(4);
4775 for (unsigned i = 0; i < 8; ++i) {
4776 SmallVectorImpl<unsigned> &Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00004777 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004778 MaskVals.push_back(EltIdx);
4779 if (EltIdx < 0) {
4780 ++Quad[0];
4781 ++Quad[1];
4782 ++Quad[2];
4783 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00004784 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004785 }
4786 ++Quad[EltIdx / 4];
4787 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00004788 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004789
Nate Begemanb9a47b82009-02-23 08:49:38 +00004790 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004791 unsigned MaxQuad = 1;
4792 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004793 if (LoQuad[i] > MaxQuad) {
4794 BestLoQuad = i;
4795 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004796 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004797 }
4798
Nate Begemanb9a47b82009-02-23 08:49:38 +00004799 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004800 MaxQuad = 1;
4801 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004802 if (HiQuad[i] > MaxQuad) {
4803 BestHiQuad = i;
4804 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004805 }
4806 }
4807
Nate Begemanb9a47b82009-02-23 08:49:38 +00004808 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00004809 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00004810 // single pshufb instruction is necessary. If There are more than 2 input
4811 // quads, disable the next transformation since it does not help SSSE3.
4812 bool V1Used = InputQuads[0] || InputQuads[1];
4813 bool V2Used = InputQuads[2] || InputQuads[3];
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004814 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004815 if (InputQuads.count() == 2 && V1Used && V2Used) {
4816 BestLoQuad = InputQuads.find_first();
4817 BestHiQuad = InputQuads.find_next(BestLoQuad);
4818 }
4819 if (InputQuads.count() > 2) {
4820 BestLoQuad = -1;
4821 BestHiQuad = -1;
4822 }
4823 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004824
Nate Begemanb9a47b82009-02-23 08:49:38 +00004825 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
4826 // the shuffle mask. If a quad is scored as -1, that means that it contains
4827 // words from all 4 input quadwords.
4828 SDValue NewV;
4829 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004830 SmallVector<int, 8> MaskV;
4831 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
4832 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Eric Christopherfd179292009-08-27 18:07:15 +00004833 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004834 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
4835 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
4836 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00004837
Nate Begemanb9a47b82009-02-23 08:49:38 +00004838 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
4839 // source words for the shuffle, to aid later transformations.
4840 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00004841 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00004842 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004843 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00004844 if (idx != (int)i)
4845 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004846 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00004847 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004848 AllWordsInNewV = false;
4849 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00004850 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004851
Nate Begemanb9a47b82009-02-23 08:49:38 +00004852 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
4853 if (AllWordsInNewV) {
4854 for (int i = 0; i != 8; ++i) {
4855 int idx = MaskVals[i];
4856 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00004857 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004858 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004859 if ((idx != i) && idx < 4)
4860 pshufhw = false;
4861 if ((idx != i) && idx > 3)
4862 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00004863 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00004864 V1 = NewV;
4865 V2Used = false;
4866 BestLoQuad = 0;
4867 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004868 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004869
Nate Begemanb9a47b82009-02-23 08:49:38 +00004870 // If we've eliminated the use of V2, and the new mask is a pshuflw or
4871 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00004872 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00004873 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
4874 unsigned TargetMask = 0;
4875 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00004876 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00004877 TargetMask = pshufhw ? X86::getShufflePSHUFHWImmediate(NewV.getNode()):
4878 X86::getShufflePSHUFLWImmediate(NewV.getNode());
4879 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004880 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00004881 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004882 }
Eric Christopherfd179292009-08-27 18:07:15 +00004883
Nate Begemanb9a47b82009-02-23 08:49:38 +00004884 // If we have SSSE3, and all words of the result are from 1 input vector,
4885 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
4886 // is present, fall back to case 4.
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004887 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004888 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004889
Nate Begemanb9a47b82009-02-23 08:49:38 +00004890 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00004891 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00004892 // mask, and elements that come from V1 in the V2 mask, so that the two
4893 // results can be OR'd together.
4894 bool TwoInputs = V1Used && V2Used;
4895 for (unsigned i = 0; i != 8; ++i) {
4896 int EltIdx = MaskVals[i] * 2;
4897 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004898 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4899 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004900 continue;
4901 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004902 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
4903 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004904 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004905 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004906 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004907 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004908 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004909 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004910 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004911
Nate Begemanb9a47b82009-02-23 08:49:38 +00004912 // Calculate the shuffle mask for the second input, shuffle it, and
4913 // OR it with the first shuffled input.
4914 pshufbMask.clear();
4915 for (unsigned i = 0; i != 8; ++i) {
4916 int EltIdx = MaskVals[i] * 2;
4917 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004918 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4919 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004920 continue;
4921 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004922 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
4923 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004924 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004925 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00004926 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00004927 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004928 MVT::v16i8, &pshufbMask[0], 16));
4929 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004930 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004931 }
4932
4933 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
4934 // and update MaskVals with new element order.
4935 BitVector InOrder(8);
4936 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004937 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004938 for (int i = 0; i != 4; ++i) {
4939 int idx = MaskVals[i];
4940 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004941 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004942 InOrder.set(i);
4943 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004944 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004945 InOrder.set(i);
4946 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004947 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004948 }
4949 }
4950 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004951 MaskV.push_back(i);
Owen Anderson825b72b2009-08-11 20:47:22 +00004952 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004953 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004954
4955 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
4956 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
4957 NewV.getOperand(0),
4958 X86::getShufflePSHUFLWImmediate(NewV.getNode()),
4959 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004960 }
Eric Christopherfd179292009-08-27 18:07:15 +00004961
Nate Begemanb9a47b82009-02-23 08:49:38 +00004962 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
4963 // and update MaskVals with the new element order.
4964 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004965 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004966 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004967 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004968 for (unsigned i = 4; i != 8; ++i) {
4969 int idx = MaskVals[i];
4970 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004971 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004972 InOrder.set(i);
4973 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004974 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004975 InOrder.set(i);
4976 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004977 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004978 }
4979 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004980 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004981 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004982
4983 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
4984 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
4985 NewV.getOperand(0),
4986 X86::getShufflePSHUFHWImmediate(NewV.getNode()),
4987 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004988 }
Eric Christopherfd179292009-08-27 18:07:15 +00004989
Nate Begemanb9a47b82009-02-23 08:49:38 +00004990 // In case BestHi & BestLo were both -1, which means each quadword has a word
4991 // from each of the four input quadwords, calculate the InOrder bitvector now
4992 // before falling through to the insert/extract cleanup.
4993 if (BestLoQuad == -1 && BestHiQuad == -1) {
4994 NewV = V1;
4995 for (int i = 0; i != 8; ++i)
4996 if (MaskVals[i] < 0 || MaskVals[i] == i)
4997 InOrder.set(i);
4998 }
Eric Christopherfd179292009-08-27 18:07:15 +00004999
Nate Begemanb9a47b82009-02-23 08:49:38 +00005000 // The other elements are put in the right place using pextrw and pinsrw.
5001 for (unsigned i = 0; i != 8; ++i) {
5002 if (InOrder[i])
5003 continue;
5004 int EltIdx = MaskVals[i];
5005 if (EltIdx < 0)
5006 continue;
5007 SDValue ExtOp = (EltIdx < 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00005008 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005009 DAG.getIntPtrConstant(EltIdx))
Owen Anderson825b72b2009-08-11 20:47:22 +00005010 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005011 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00005012 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005013 DAG.getIntPtrConstant(i));
5014 }
5015 return NewV;
5016}
5017
5018// v16i8 shuffles - Prefer shuffles in the following order:
5019// 1. [ssse3] 1 x pshufb
5020// 2. [ssse3] 2 x pshufb + 1 x por
5021// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
5022static
Nate Begeman9008ca62009-04-27 18:41:29 +00005023SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00005024 SelectionDAG &DAG,
5025 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005026 SDValue V1 = SVOp->getOperand(0);
5027 SDValue V2 = SVOp->getOperand(1);
5028 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005029 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00005030 SVOp->getMask(MaskVals);
Eric Christopherfd179292009-08-27 18:07:15 +00005031
Nate Begemanb9a47b82009-02-23 08:49:38 +00005032 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00005033 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00005034 // present, fall back to case 3.
5035 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
5036 bool V1Only = true;
5037 bool V2Only = true;
5038 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005039 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00005040 if (EltIdx < 0)
5041 continue;
5042 if (EltIdx < 16)
5043 V2Only = false;
5044 else
5045 V1Only = false;
5046 }
Eric Christopherfd179292009-08-27 18:07:15 +00005047
Nate Begemanb9a47b82009-02-23 08:49:38 +00005048 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
5049 if (TLI.getSubtarget()->hasSSSE3()) {
5050 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005051
Nate Begemanb9a47b82009-02-23 08:49:38 +00005052 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00005053 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005054 //
5055 // Otherwise, we have elements from both input vectors, and must zero out
5056 // elements that come from V2 in the first mask, and V1 in the second mask
5057 // so that we can OR them together.
5058 bool TwoInputs = !(V1Only || V2Only);
5059 for (unsigned i = 0; i != 16; ++i) {
5060 int EltIdx = MaskVals[i];
5061 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005062 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005063 continue;
5064 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005065 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005066 }
5067 // If all the elements are from V2, assign it to V1 and return after
5068 // building the first pshufb.
5069 if (V2Only)
5070 V1 = V2;
Owen Anderson825b72b2009-08-11 20:47:22 +00005071 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005072 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005073 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005074 if (!TwoInputs)
5075 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00005076
Nate Begemanb9a47b82009-02-23 08:49:38 +00005077 // Calculate the shuffle mask for the second input, shuffle it, and
5078 // OR it with the first shuffled input.
5079 pshufbMask.clear();
5080 for (unsigned i = 0; i != 16; ++i) {
5081 int EltIdx = MaskVals[i];
5082 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005083 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005084 continue;
5085 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005086 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005087 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005088 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005089 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005090 MVT::v16i8, &pshufbMask[0], 16));
5091 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005092 }
Eric Christopherfd179292009-08-27 18:07:15 +00005093
Nate Begemanb9a47b82009-02-23 08:49:38 +00005094 // No SSSE3 - Calculate in place words and then fix all out of place words
5095 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
5096 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005097 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
5098 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005099 SDValue NewV = V2Only ? V2 : V1;
5100 for (int i = 0; i != 8; ++i) {
5101 int Elt0 = MaskVals[i*2];
5102 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00005103
Nate Begemanb9a47b82009-02-23 08:49:38 +00005104 // This word of the result is all undef, skip it.
5105 if (Elt0 < 0 && Elt1 < 0)
5106 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005107
Nate Begemanb9a47b82009-02-23 08:49:38 +00005108 // This word of the result is already in the correct place, skip it.
5109 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
5110 continue;
5111 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
5112 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005113
Nate Begemanb9a47b82009-02-23 08:49:38 +00005114 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
5115 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
5116 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00005117
5118 // If Elt0 and Elt1 are defined, are consecutive, and can be load
5119 // using a single extract together, load it and store it.
5120 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005121 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005122 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00005123 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005124 DAG.getIntPtrConstant(i));
5125 continue;
5126 }
5127
Nate Begemanb9a47b82009-02-23 08:49:38 +00005128 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00005129 // source byte is not also odd, shift the extracted word left 8 bits
5130 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005131 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005132 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005133 DAG.getIntPtrConstant(Elt1 / 2));
5134 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005135 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Owen Anderson95771af2011-02-25 21:41:48 +00005136 DAG.getConstant(8,
5137 TLI.getShiftAmountTy(InsElt.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005138 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005139 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
5140 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005141 }
5142 // If Elt0 is defined, extract it from the appropriate source. If the
5143 // source byte is not also even, shift the extracted word right 8 bits. If
5144 // Elt1 was also defined, OR the extracted values together before
5145 // inserting them in the result.
5146 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005147 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005148 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
5149 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005150 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Owen Anderson95771af2011-02-25 21:41:48 +00005151 DAG.getConstant(8,
5152 TLI.getShiftAmountTy(InsElt0.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005153 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005154 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
5155 DAG.getConstant(0x00FF, MVT::i16));
5156 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00005157 : InsElt0;
5158 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005159 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005160 DAG.getIntPtrConstant(i));
5161 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005162 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005163}
5164
Evan Cheng7a831ce2007-12-15 03:00:47 +00005165/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005166/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00005167/// done when every pair / quad of shuffle mask elements point to elements in
5168/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005169/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00005170static
Nate Begeman9008ca62009-04-27 18:41:29 +00005171SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005172 SelectionDAG &DAG, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00005173 EVT VT = SVOp->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00005174 SDValue V1 = SVOp->getOperand(0);
5175 SDValue V2 = SVOp->getOperand(1);
5176 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00005177 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005178 EVT NewVT;
Owen Anderson825b72b2009-08-11 20:47:22 +00005179 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005180 default: assert(false && "Unexpected!");
Owen Anderson825b72b2009-08-11 20:47:22 +00005181 case MVT::v4f32: NewVT = MVT::v2f64; break;
5182 case MVT::v4i32: NewVT = MVT::v2i64; break;
5183 case MVT::v8i16: NewVT = MVT::v4i32; break;
5184 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00005185 }
5186
Nate Begeman9008ca62009-04-27 18:41:29 +00005187 int Scale = NumElems / NewWidth;
5188 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00005189 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005190 int StartIdx = -1;
5191 for (int j = 0; j < Scale; ++j) {
5192 int EltIdx = SVOp->getMaskElt(i+j);
5193 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005194 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00005195 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00005196 StartIdx = EltIdx - (EltIdx % Scale);
5197 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00005198 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00005199 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005200 if (StartIdx == -1)
5201 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00005202 else
Nate Begeman9008ca62009-04-27 18:41:29 +00005203 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005204 }
5205
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005206 V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, V1);
5207 V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00005208 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005209}
5210
Evan Chengd880b972008-05-09 21:53:03 +00005211/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005212///
Owen Andersone50ed302009-08-10 22:56:29 +00005213static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00005214 SDValue SrcOp, SelectionDAG &DAG,
5215 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005216 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005217 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00005218 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00005219 LD = dyn_cast<LoadSDNode>(SrcOp);
5220 if (!LD) {
5221 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
5222 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00005223 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00005224 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00005225 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005226 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00005227 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005228 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00005229 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005230 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005231 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
5232 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
5233 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00005234 SrcOp.getOperand(0)
5235 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005236 }
5237 }
5238 }
5239
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005240 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005241 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005242 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00005243 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005244}
5245
Evan Chengace3c172008-07-22 21:13:36 +00005246/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
5247/// shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00005248static SDValue
Nate Begeman9008ca62009-04-27 18:41:29 +00005249LowerVECTOR_SHUFFLE_4wide(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
5250 SDValue V1 = SVOp->getOperand(0);
5251 SDValue V2 = SVOp->getOperand(1);
5252 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005253 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00005254
Evan Chengace3c172008-07-22 21:13:36 +00005255 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00005256 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00005257 SmallVector<int, 8> Mask1(4U, -1);
5258 SmallVector<int, 8> PermMask;
5259 SVOp->getMask(PermMask);
5260
Evan Chengace3c172008-07-22 21:13:36 +00005261 unsigned NumHi = 0;
5262 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00005263 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005264 int Idx = PermMask[i];
5265 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00005266 Locs[i] = std::make_pair(-1, -1);
5267 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005268 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
5269 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00005270 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00005271 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005272 NumLo++;
5273 } else {
5274 Locs[i] = std::make_pair(1, NumHi);
5275 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00005276 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005277 NumHi++;
5278 }
5279 }
5280 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005281
Evan Chengace3c172008-07-22 21:13:36 +00005282 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005283 // If no more than two elements come from either vector. This can be
5284 // implemented with two shuffles. First shuffle gather the elements.
5285 // The second shuffle, which takes the first shuffle as both of its
5286 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00005287 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005288
Nate Begeman9008ca62009-04-27 18:41:29 +00005289 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopherfd179292009-08-27 18:07:15 +00005290
Evan Chengace3c172008-07-22 21:13:36 +00005291 for (unsigned i = 0; i != 4; ++i) {
5292 if (Locs[i].first == -1)
5293 continue;
5294 else {
5295 unsigned Idx = (i < 2) ? 0 : 4;
5296 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00005297 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005298 }
5299 }
5300
Nate Begeman9008ca62009-04-27 18:41:29 +00005301 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005302 } else if (NumLo == 3 || NumHi == 3) {
5303 // Otherwise, we must have three elements from one vector, call it X, and
5304 // one element from the other, call it Y. First, use a shufps to build an
5305 // intermediate vector with the one element from Y and the element from X
5306 // that will be in the same half in the final destination (the indexes don't
5307 // matter). Then, use a shufps to build the final vector, taking the half
5308 // containing the element from Y from the intermediate, and the other half
5309 // from X.
5310 if (NumHi == 3) {
5311 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00005312 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005313 std::swap(V1, V2);
5314 }
5315
5316 // Find the element from V2.
5317 unsigned HiIndex;
5318 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005319 int Val = PermMask[HiIndex];
5320 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005321 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005322 if (Val >= 4)
5323 break;
5324 }
5325
Nate Begeman9008ca62009-04-27 18:41:29 +00005326 Mask1[0] = PermMask[HiIndex];
5327 Mask1[1] = -1;
5328 Mask1[2] = PermMask[HiIndex^1];
5329 Mask1[3] = -1;
5330 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005331
5332 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005333 Mask1[0] = PermMask[0];
5334 Mask1[1] = PermMask[1];
5335 Mask1[2] = HiIndex & 1 ? 6 : 4;
5336 Mask1[3] = HiIndex & 1 ? 4 : 6;
5337 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005338 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005339 Mask1[0] = HiIndex & 1 ? 2 : 0;
5340 Mask1[1] = HiIndex & 1 ? 0 : 2;
5341 Mask1[2] = PermMask[2];
5342 Mask1[3] = PermMask[3];
5343 if (Mask1[2] >= 0)
5344 Mask1[2] += 4;
5345 if (Mask1[3] >= 0)
5346 Mask1[3] += 4;
5347 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005348 }
Evan Chengace3c172008-07-22 21:13:36 +00005349 }
5350
5351 // Break it into (shuffle shuffle_hi, shuffle_lo).
5352 Locs.clear();
David Greenec4db4e52011-02-28 19:06:56 +00005353 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00005354 SmallVector<int,8> LoMask(4U, -1);
5355 SmallVector<int,8> HiMask(4U, -1);
5356
5357 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00005358 unsigned MaskIdx = 0;
5359 unsigned LoIdx = 0;
5360 unsigned HiIdx = 2;
5361 for (unsigned i = 0; i != 4; ++i) {
5362 if (i == 2) {
5363 MaskPtr = &HiMask;
5364 MaskIdx = 1;
5365 LoIdx = 0;
5366 HiIdx = 2;
5367 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005368 int Idx = PermMask[i];
5369 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00005370 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00005371 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00005372 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00005373 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005374 LoIdx++;
5375 } else {
5376 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00005377 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005378 HiIdx++;
5379 }
5380 }
5381
Nate Begeman9008ca62009-04-27 18:41:29 +00005382 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
5383 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
5384 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00005385 for (unsigned i = 0; i != 4; ++i) {
5386 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005387 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00005388 } else {
5389 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00005390 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00005391 }
5392 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005393 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00005394}
5395
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005396static bool MayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005397 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005398 V = V.getOperand(0);
5399 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
5400 V = V.getOperand(0);
5401 if (MayFoldLoad(V))
5402 return true;
5403 return false;
5404}
5405
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005406// FIXME: the version above should always be used. Since there's
5407// a bug where several vector shuffles can't be folded because the
5408// DAG is not updated during lowering and a node claims to have two
5409// uses while it only has one, use this version, and let isel match
5410// another instruction if the load really happens to have more than
5411// one use. Remove this version after this bug get fixed.
Evan Cheng835580f2010-10-07 20:50:20 +00005412// rdar://8434668, PR8156
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005413static bool RelaxedMayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005414 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005415 V = V.getOperand(0);
5416 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
5417 V = V.getOperand(0);
5418 if (ISD::isNormalLoad(V.getNode()))
5419 return true;
5420 return false;
5421}
5422
5423/// CanFoldShuffleIntoVExtract - Check if the current shuffle is used by
5424/// a vector extract, and if both can be later optimized into a single load.
5425/// This is done in visitEXTRACT_VECTOR_ELT and the conditions are checked
5426/// here because otherwise a target specific shuffle node is going to be
5427/// emitted for this shuffle, and the optimization not done.
5428/// FIXME: This is probably not the best approach, but fix the problem
5429/// until the right path is decided.
5430static
5431bool CanXFormVExtractWithShuffleIntoLoad(SDValue V, SelectionDAG &DAG,
5432 const TargetLowering &TLI) {
5433 EVT VT = V.getValueType();
5434 ShuffleVectorSDNode *SVOp = dyn_cast<ShuffleVectorSDNode>(V);
5435
5436 // Be sure that the vector shuffle is present in a pattern like this:
5437 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), c) -> (f32 load $addr)
5438 if (!V.hasOneUse())
5439 return false;
5440
5441 SDNode *N = *V.getNode()->use_begin();
5442 if (N->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
5443 return false;
5444
5445 SDValue EltNo = N->getOperand(1);
5446 if (!isa<ConstantSDNode>(EltNo))
5447 return false;
5448
5449 // If the bit convert changed the number of elements, it is unsafe
5450 // to examine the mask.
5451 bool HasShuffleIntoBitcast = false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005452 if (V.getOpcode() == ISD::BITCAST) {
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005453 EVT SrcVT = V.getOperand(0).getValueType();
5454 if (SrcVT.getVectorNumElements() != VT.getVectorNumElements())
5455 return false;
5456 V = V.getOperand(0);
5457 HasShuffleIntoBitcast = true;
5458 }
5459
5460 // Select the input vector, guarding against out of range extract vector.
5461 unsigned NumElems = VT.getVectorNumElements();
5462 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
5463 int Idx = (Elt > NumElems) ? -1 : SVOp->getMaskElt(Elt);
5464 V = (Idx < (int)NumElems) ? V.getOperand(0) : V.getOperand(1);
5465
5466 // Skip one more bit_convert if necessary
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005467 if (V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005468 V = V.getOperand(0);
5469
5470 if (ISD::isNormalLoad(V.getNode())) {
5471 // Is the original load suitable?
5472 LoadSDNode *LN0 = cast<LoadSDNode>(V);
5473
5474 // FIXME: avoid the multi-use bug that is preventing lots of
5475 // of foldings to be detected, this is still wrong of course, but
5476 // give the temporary desired behavior, and if it happens that
5477 // the load has real more uses, during isel it will not fold, and
5478 // will generate poor code.
5479 if (!LN0 || LN0->isVolatile()) // || !LN0->hasOneUse()
5480 return false;
5481
5482 if (!HasShuffleIntoBitcast)
5483 return true;
5484
5485 // If there's a bitcast before the shuffle, check if the load type and
5486 // alignment is valid.
5487 unsigned Align = LN0->getAlignment();
5488 unsigned NewAlign =
5489 TLI.getTargetData()->getABITypeAlignment(
5490 VT.getTypeForEVT(*DAG.getContext()));
5491
5492 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
5493 return false;
5494 }
5495
5496 return true;
5497}
5498
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005499static
Evan Cheng835580f2010-10-07 20:50:20 +00005500SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
5501 EVT VT = Op.getValueType();
5502
5503 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005504 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
5505 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00005506 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
5507 V1, DAG));
5508}
5509
5510static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005511SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
5512 bool HasSSE2) {
5513 SDValue V1 = Op.getOperand(0);
5514 SDValue V2 = Op.getOperand(1);
5515 EVT VT = Op.getValueType();
5516
5517 assert(VT != MVT::v2i64 && "unsupported shuffle type");
5518
5519 if (HasSSE2 && VT == MVT::v2f64)
5520 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
5521
5522 // v4f32 or v4i32
5523 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V2, DAG);
5524}
5525
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00005526static
5527SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
5528 SDValue V1 = Op.getOperand(0);
5529 SDValue V2 = Op.getOperand(1);
5530 EVT VT = Op.getValueType();
5531
5532 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
5533 "unsupported shuffle type");
5534
5535 if (V2.getOpcode() == ISD::UNDEF)
5536 V2 = V1;
5537
5538 // v4i32 or v4f32
5539 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
5540}
5541
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005542static
5543SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasSSE2) {
5544 SDValue V1 = Op.getOperand(0);
5545 SDValue V2 = Op.getOperand(1);
5546 EVT VT = Op.getValueType();
5547 unsigned NumElems = VT.getVectorNumElements();
5548
5549 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
5550 // operand of these instructions is only memory, so check if there's a
5551 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
5552 // same masks.
5553 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005554
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00005555 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005556 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005557 CanFoldLoad = true;
5558
5559 // When V1 is a load, it can be folded later into a store in isel, example:
5560 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
5561 // turns into:
5562 // (MOVLPSmr addr:$src1, VR128:$src2)
5563 // So, recognize this potential and also use MOVLPS or MOVLPD
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005564 if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005565 CanFoldLoad = true;
5566
Eric Christopher893a8822011-02-20 05:04:42 +00005567 // Both of them can't be memory operations though.
5568 if (MayFoldVectorLoad(V1) && MayFoldVectorLoad(V2))
5569 CanFoldLoad = false;
Owen Anderson95771af2011-02-25 21:41:48 +00005570
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005571 if (CanFoldLoad) {
5572 if (HasSSE2 && NumElems == 2)
5573 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
5574
5575 if (NumElems == 4)
5576 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
5577 }
5578
5579 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5580 // movl and movlp will both match v2i64, but v2i64 is never matched by
5581 // movl earlier because we make it strict to avoid messing with the movlp load
5582 // folding logic (see the code above getMOVLP call). Match it here then,
5583 // this is horrible, but will stay like this until we move all shuffle
5584 // matching to x86 specific nodes. Note that for the 1st condition all
5585 // types are matched with movsd.
5586 if ((HasSSE2 && NumElems == 2) || !X86::isMOVLMask(SVOp))
5587 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
5588 else if (HasSSE2)
5589 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
5590
5591
5592 assert(VT != MVT::v4i32 && "unsupported shuffle type");
5593
5594 // Invert the operand order and use SHUFPS to match it.
5595 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V2, V1,
5596 X86::getShuffleSHUFImmediate(SVOp), DAG);
5597}
5598
David Greenec4db4e52011-02-28 19:06:56 +00005599static inline unsigned getUNPCKLOpcode(EVT VT, const X86Subtarget *Subtarget) {
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005600 switch(VT.getSimpleVT().SimpleTy) {
5601 case MVT::v4i32: return X86ISD::PUNPCKLDQ;
5602 case MVT::v2i64: return X86ISD::PUNPCKLQDQ;
David Greenec4db4e52011-02-28 19:06:56 +00005603 case MVT::v4f32:
5604 return Subtarget->hasAVX() ? X86ISD::VUNPCKLPS : X86ISD::UNPCKLPS;
5605 case MVT::v2f64:
5606 return Subtarget->hasAVX() ? X86ISD::VUNPCKLPD : X86ISD::UNPCKLPD;
5607 case MVT::v8f32: return X86ISD::VUNPCKLPSY;
5608 case MVT::v4f64: return X86ISD::VUNPCKLPDY;
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005609 case MVT::v16i8: return X86ISD::PUNPCKLBW;
5610 case MVT::v8i16: return X86ISD::PUNPCKLWD;
5611 default:
Eric Christopherdd6e40a2011-02-19 03:19:09 +00005612 llvm_unreachable("Unknown type for unpckl");
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005613 }
5614 return 0;
5615}
5616
5617static inline unsigned getUNPCKHOpcode(EVT VT) {
5618 switch(VT.getSimpleVT().SimpleTy) {
5619 case MVT::v4i32: return X86ISD::PUNPCKHDQ;
5620 case MVT::v2i64: return X86ISD::PUNPCKHQDQ;
5621 case MVT::v4f32: return X86ISD::UNPCKHPS;
5622 case MVT::v2f64: return X86ISD::UNPCKHPD;
5623 case MVT::v16i8: return X86ISD::PUNPCKHBW;
5624 case MVT::v8i16: return X86ISD::PUNPCKHWD;
5625 default:
Eric Christopherdd6e40a2011-02-19 03:19:09 +00005626 llvm_unreachable("Unknown type for unpckh");
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005627 }
5628 return 0;
5629}
5630
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005631static
5632SDValue NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG,
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005633 const TargetLowering &TLI,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005634 const X86Subtarget *Subtarget) {
5635 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5636 EVT VT = Op.getValueType();
5637 DebugLoc dl = Op.getDebugLoc();
5638 SDValue V1 = Op.getOperand(0);
5639 SDValue V2 = Op.getOperand(1);
5640
5641 if (isZeroShuffle(SVOp))
5642 return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
5643
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005644 // Handle splat operations
5645 if (SVOp->isSplat()) {
5646 // Special case, this is the only place now where it's
5647 // allowed to return a vector_shuffle operation without
5648 // using a target specific node, because *hopefully* it
5649 // will be optimized away by the dag combiner.
5650 if (VT.getVectorNumElements() <= 4 &&
5651 CanXFormVExtractWithShuffleIntoLoad(Op, DAG, TLI))
5652 return Op;
5653
5654 // Handle splats by matching through known masks
5655 if (VT.getVectorNumElements() <= 4)
5656 return SDValue();
5657
Evan Cheng835580f2010-10-07 20:50:20 +00005658 // Canonicalize all of the remaining to v4f32.
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005659 return PromoteSplat(SVOp, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005660 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005661
5662 // If the shuffle can be profitably rewritten as a narrower shuffle, then
5663 // do it!
5664 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
5665 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5666 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005667 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005668 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
5669 // FIXME: Figure out a cleaner way to do this.
5670 // Try to make use of movq to zero out the top part.
5671 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
5672 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5673 if (NewOp.getNode()) {
5674 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
5675 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
5676 DAG, Subtarget, dl);
5677 }
5678 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
5679 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5680 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
5681 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
5682 DAG, Subtarget, dl);
5683 }
5684 }
5685 return SDValue();
5686}
5687
Dan Gohman475871a2008-07-27 21:46:04 +00005688SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005689X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00005690 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00005691 SDValue V1 = Op.getOperand(0);
5692 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00005693 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005694 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00005695 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005696 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005697 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
5698 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00005699 bool V1IsSplat = false;
5700 bool V2IsSplat = false;
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005701 bool HasSSE2 = Subtarget->hasSSE2() || Subtarget->hasAVX();
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00005702 bool HasSSE3 = Subtarget->hasSSE3() || Subtarget->hasAVX();
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00005703 bool HasSSSE3 = Subtarget->hasSSSE3() || Subtarget->hasAVX();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005704 MachineFunction &MF = DAG.getMachineFunction();
5705 bool OptForSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005706
Dale Johannesen0488fb62010-09-30 23:57:10 +00005707 // Shuffle operations on MMX not supported.
5708 if (isMMX)
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00005709 return Op;
5710
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005711 // Vector shuffle lowering takes 3 steps:
5712 //
5713 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
5714 // narrowing and commutation of operands should be handled.
5715 // 2) Matching of shuffles with known shuffle masks to x86 target specific
5716 // shuffle nodes.
5717 // 3) Rewriting of unmatched masks into new generic shuffle operations,
5718 // so the shuffle can be broken into other shuffles and the legalizer can
5719 // try the lowering again.
5720 //
5721 // The general ideia is that no vector_shuffle operation should be left to
5722 // be matched during isel, all of them must be converted to a target specific
5723 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00005724
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005725 // Normalize the input vectors. Here splats, zeroed vectors, profitable
5726 // narrowing and commutation of operands should be handled. The actual code
5727 // doesn't include all of those, work in progress...
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005728 SDValue NewOp = NormalizeVectorShuffle(Op, DAG, *this, Subtarget);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005729 if (NewOp.getNode())
5730 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00005731
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005732 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
5733 // unpckh_undef). Only use pshufd if speed is more important than size.
5734 if (OptForSize && X86::isUNPCKL_v_undef_Mask(SVOp))
5735 if (VT != MVT::v2i64 && VT != MVT::v2f64)
David Greenec4db4e52011-02-28 19:06:56 +00005736 return getTargetShuffleNode(getUNPCKLOpcode(VT, getSubtarget()), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005737 if (OptForSize && X86::isUNPCKH_v_undef_Mask(SVOp))
5738 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5739 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00005740
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005741 if (X86::isMOVDDUPMask(SVOp) && HasSSE3 && V2IsUndef &&
Dale Johannesen0488fb62010-09-30 23:57:10 +00005742 RelaxedMayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00005743 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005744
Dale Johannesen0488fb62010-09-30 23:57:10 +00005745 if (X86::isMOVHLPS_v_undef_Mask(SVOp))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005746 return getMOVHighToLow(Op, dl, DAG);
5747
5748 // Use to match splats
5749 if (HasSSE2 && X86::isUNPCKHMask(SVOp) && V2IsUndef &&
5750 (VT == MVT::v2f64 || VT == MVT::v2i64))
5751 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
5752
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005753 if (X86::isPSHUFDMask(SVOp)) {
5754 // The actual implementation will match the mask in the if above and then
5755 // during isel it can match several different instructions, not only pshufd
5756 // as its name says, sad but true, emulate the behavior for now...
5757 if (X86::isMOVDDUPMask(SVOp) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
5758 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
5759
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005760 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
5761
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005762 if (HasSSE2 && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005763 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
5764
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005765 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005766 return getTargetShuffleNode(X86ISD::SHUFPD, dl, VT, V1, V1,
5767 TargetMask, DAG);
5768
5769 if (VT == MVT::v4f32)
5770 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V1, V1,
5771 TargetMask, DAG);
5772 }
Eric Christopherfd179292009-08-27 18:07:15 +00005773
Evan Chengf26ffe92008-05-29 08:22:04 +00005774 // Check if this can be converted into a logical shift.
5775 bool isLeft = false;
5776 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00005777 SDValue ShVal;
Nate Begeman9008ca62009-04-27 18:41:29 +00005778 bool isShift = getSubtarget()->hasSSE2() &&
Evan Chengc3630942009-12-09 21:00:30 +00005779 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00005780 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00005781 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00005782 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00005783 EVT EltVT = VT.getVectorElementType();
5784 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00005785 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005786 }
Eric Christopherfd179292009-08-27 18:07:15 +00005787
Nate Begeman9008ca62009-04-27 18:41:29 +00005788 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005789 if (V1IsUndef)
5790 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00005791 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00005792 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Dale Johannesen0488fb62010-09-30 23:57:10 +00005793 if (!X86::isMOVLPMask(SVOp)) {
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005794 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00005795 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
5796
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005797 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00005798 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
5799 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00005800 }
Eric Christopherfd179292009-08-27 18:07:15 +00005801
Nate Begeman9008ca62009-04-27 18:41:29 +00005802 // FIXME: fold these into legal mask.
Dale Johannesen0488fb62010-09-30 23:57:10 +00005803 if (X86::isMOVLHPSMask(SVOp) && !X86::isUNPCKLMask(SVOp))
5804 return getMOVLowToHigh(Op, dl, DAG, HasSSE2);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005805
Dale Johannesen0488fb62010-09-30 23:57:10 +00005806 if (X86::isMOVHLPSMask(SVOp))
5807 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00005808
Dale Johannesen0488fb62010-09-30 23:57:10 +00005809 if (X86::isMOVSHDUPMask(SVOp) && HasSSE3 && V2IsUndef && NumElems == 4)
5810 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00005811
Dale Johannesen0488fb62010-09-30 23:57:10 +00005812 if (X86::isMOVSLDUPMask(SVOp) && HasSSE3 && V2IsUndef && NumElems == 4)
5813 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00005814
Dale Johannesen0488fb62010-09-30 23:57:10 +00005815 if (X86::isMOVLPMask(SVOp))
5816 return getMOVLP(Op, dl, DAG, HasSSE2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005817
Nate Begeman9008ca62009-04-27 18:41:29 +00005818 if (ShouldXformToMOVHLPS(SVOp) ||
5819 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
5820 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005821
Evan Chengf26ffe92008-05-29 08:22:04 +00005822 if (isShift) {
5823 // No better options. Use a vshl / vsrl.
Dan Gohman8a55ce42009-09-23 21:02:20 +00005824 EVT EltVT = VT.getVectorElementType();
5825 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00005826 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005827 }
Eric Christopherfd179292009-08-27 18:07:15 +00005828
Evan Cheng9eca5e82006-10-25 21:49:50 +00005829 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00005830 // FIXME: This should also accept a bitcast of a splat? Be careful, not
5831 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00005832 V1IsSplat = isSplatVector(V1.getNode());
5833 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00005834
Chris Lattner8a594482007-11-25 00:24:49 +00005835 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00005836 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005837 Op = CommuteVectorShuffle(SVOp, DAG);
5838 SVOp = cast<ShuffleVectorSDNode>(Op);
5839 V1 = SVOp->getOperand(0);
5840 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00005841 std::swap(V1IsSplat, V2IsSplat);
5842 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00005843 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00005844 }
5845
Nate Begeman9008ca62009-04-27 18:41:29 +00005846 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
5847 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00005848 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00005849 return V1;
5850 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
5851 // the instruction selector will not match, so get a canonical MOVL with
5852 // swapped operands to undo the commute.
5853 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00005854 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005855
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005856 if (X86::isUNPCKLMask(SVOp))
David Greenec4db4e52011-02-28 19:06:56 +00005857 return getTargetShuffleNode(getUNPCKLOpcode(VT, getSubtarget()),
5858 dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005859
5860 if (X86::isUNPCKHMask(SVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005861 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00005862
Evan Cheng9bbbb982006-10-25 20:48:19 +00005863 if (V2IsSplat) {
5864 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005865 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00005866 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00005867 SDValue NewMask = NormalizeMask(SVOp, DAG);
5868 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
5869 if (NSVOp != SVOp) {
5870 if (X86::isUNPCKLMask(NSVOp, true)) {
5871 return NewMask;
5872 } else if (X86::isUNPCKHMask(NSVOp, true)) {
5873 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005874 }
5875 }
5876 }
5877
Evan Cheng9eca5e82006-10-25 21:49:50 +00005878 if (Commuted) {
5879 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00005880 // FIXME: this seems wrong.
5881 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
5882 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005883
5884 if (X86::isUNPCKLMask(NewSVOp))
David Greenec4db4e52011-02-28 19:06:56 +00005885 return getTargetShuffleNode(getUNPCKLOpcode(VT, getSubtarget()),
5886 dl, VT, V2, V1, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005887
5888 if (X86::isUNPCKHMask(NewSVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005889 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V2, V1, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00005890 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005891
Nate Begeman9008ca62009-04-27 18:41:29 +00005892 // Normalize the node to match x86 shuffle ops if needed
Dale Johannesen0488fb62010-09-30 23:57:10 +00005893 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
Nate Begeman9008ca62009-04-27 18:41:29 +00005894 return CommuteVectorShuffle(SVOp, DAG);
5895
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00005896 // The checks below are all present in isShuffleMaskLegal, but they are
5897 // inlined here right now to enable us to directly emit target specific
5898 // nodes, and remove one by one until they don't return Op anymore.
5899 SmallVector<int, 16> M;
5900 SVOp->getMask(M);
5901
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00005902 if (isPALIGNRMask(M, VT, HasSSSE3))
5903 return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
5904 X86::getShufflePALIGNRImmediate(SVOp),
5905 DAG);
5906
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00005907 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
5908 SVOp->getSplatIndex() == 0 && V2IsUndef) {
David Greenec4db4e52011-02-28 19:06:56 +00005909 if (VT == MVT::v2f64) {
5910 X86ISD::NodeType Opcode =
5911 getSubtarget()->hasAVX() ? X86ISD::VUNPCKLPD : X86ISD::UNPCKLPD;
5912 return getTargetShuffleNode(Opcode, dl, VT, V1, V1, DAG);
5913 }
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00005914 if (VT == MVT::v2i64)
5915 return getTargetShuffleNode(X86ISD::PUNPCKLQDQ, dl, VT, V1, V1, DAG);
5916 }
5917
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00005918 if (isPSHUFHWMask(M, VT))
5919 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
5920 X86::getShufflePSHUFHWImmediate(SVOp),
5921 DAG);
5922
5923 if (isPSHUFLWMask(M, VT))
5924 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
5925 X86::getShufflePSHUFLWImmediate(SVOp),
5926 DAG);
5927
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00005928 if (isSHUFPMask(M, VT)) {
5929 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
5930 if (VT == MVT::v4f32 || VT == MVT::v4i32)
5931 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V1, V2,
5932 TargetMask, DAG);
5933 if (VT == MVT::v2f64 || VT == MVT::v2i64)
5934 return getTargetShuffleNode(X86ISD::SHUFPD, dl, VT, V1, V2,
5935 TargetMask, DAG);
5936 }
5937
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005938 if (X86::isUNPCKL_v_undef_Mask(SVOp))
5939 if (VT != MVT::v2i64 && VT != MVT::v2f64)
David Greenec4db4e52011-02-28 19:06:56 +00005940 return getTargetShuffleNode(getUNPCKLOpcode(VT, getSubtarget()),
5941 dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005942 if (X86::isUNPCKH_v_undef_Mask(SVOp))
5943 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5944 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
5945
Evan Cheng14b32e12007-12-11 01:46:18 +00005946 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
Owen Anderson825b72b2009-08-11 20:47:22 +00005947 if (VT == MVT::v8i16) {
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005948 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00005949 if (NewOp.getNode())
Evan Cheng14b32e12007-12-11 01:46:18 +00005950 return NewOp;
5951 }
5952
Owen Anderson825b72b2009-08-11 20:47:22 +00005953 if (VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005954 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005955 if (NewOp.getNode())
5956 return NewOp;
5957 }
Eric Christopherfd179292009-08-27 18:07:15 +00005958
Dale Johannesen0488fb62010-09-30 23:57:10 +00005959 // Handle all 4 wide cases with a number of shuffles.
5960 if (NumElems == 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00005961 return LowerVECTOR_SHUFFLE_4wide(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005962
Dan Gohman475871a2008-07-27 21:46:04 +00005963 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005964}
5965
Dan Gohman475871a2008-07-27 21:46:04 +00005966SDValue
5967X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00005968 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005969 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005970 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005971 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005972 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005973 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00005974 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005975 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00005976 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005977 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00005978 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
5979 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
5980 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005981 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
5982 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005983 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005984 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00005985 Op.getOperand(0)),
5986 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00005987 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005988 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00005989 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005990 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00005991 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Anderson825b72b2009-08-11 20:47:22 +00005992 } else if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00005993 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
5994 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00005995 // result has a single use which is a store or a bitcast to i32. And in
5996 // the case of a store, it's not worth it if the index is a constant 0,
5997 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00005998 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00005999 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00006000 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006001 if ((User->getOpcode() != ISD::STORE ||
6002 (isa<ConstantSDNode>(Op.getOperand(1)) &&
6003 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006004 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00006005 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00006006 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00006007 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006008 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00006009 Op.getOperand(0)),
6010 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006011 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Owen Anderson825b72b2009-08-11 20:47:22 +00006012 } else if (VT == MVT::i32) {
Mon P Wangf0fcdd82009-01-15 21:10:20 +00006013 // ExtractPS works with constant index.
6014 if (isa<ConstantSDNode>(Op.getOperand(1)))
6015 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00006016 }
Dan Gohman475871a2008-07-27 21:46:04 +00006017 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006018}
6019
6020
Dan Gohman475871a2008-07-27 21:46:04 +00006021SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006022X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
6023 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006024 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00006025 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006026
David Greene74a579d2011-02-10 16:57:36 +00006027 SDValue Vec = Op.getOperand(0);
6028 EVT VecVT = Vec.getValueType();
6029
6030 // If this is a 256-bit vector result, first extract the 128-bit
6031 // vector and then extract from the 128-bit vector.
6032 if (VecVT.getSizeInBits() > 128) {
6033 DebugLoc dl = Op.getNode()->getDebugLoc();
6034 unsigned NumElems = VecVT.getVectorNumElements();
6035 SDValue Idx = Op.getOperand(1);
6036
6037 if (!isa<ConstantSDNode>(Idx))
6038 return SDValue();
6039
6040 unsigned ExtractNumElems = NumElems / (VecVT.getSizeInBits() / 128);
6041 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
6042
6043 // Get the 128-bit vector.
6044 bool Upper = IdxVal >= ExtractNumElems;
6045 Vec = Extract128BitVector(Vec, Idx, DAG, dl);
6046
6047 // Extract from it.
6048 SDValue ScaledIdx = Idx;
6049 if (Upper)
6050 ScaledIdx = DAG.getNode(ISD::SUB, dl, Idx.getValueType(), Idx,
6051 DAG.getConstant(ExtractNumElems,
6052 Idx.getValueType()));
6053 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, Op.getValueType(), Vec,
6054 ScaledIdx);
6055 }
6056
6057 assert(Vec.getValueSizeInBits() <= 128 && "Unexpected vector length");
6058
Evan Cheng62a3f152008-03-24 21:52:23 +00006059 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006060 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006061 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00006062 return Res;
6063 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00006064
Owen Andersone50ed302009-08-10 22:56:29 +00006065 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006066 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006067 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006068 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00006069 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006070 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00006071 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006072 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6073 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006074 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006075 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00006076 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006077 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00006078 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00006079 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00006080 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00006081 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00006082 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006083 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006084 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006085 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006086 if (Idx == 0)
6087 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00006088
Evan Cheng0db9fe62006-04-25 20:13:52 +00006089 // SHUFPS the element to the lowest double word, then movss.
Nate Begeman9008ca62009-04-27 18:41:29 +00006090 int Mask[4] = { Idx, -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006091 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006092 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00006093 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00006094 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00006095 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00006096 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00006097 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
6098 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
6099 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006100 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006101 if (Idx == 0)
6102 return Op;
6103
6104 // UNPCKHPD the element to the lowest double word, then movsd.
6105 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
6106 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00006107 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006108 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006109 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00006110 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00006111 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00006112 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006113 }
6114
Dan Gohman475871a2008-07-27 21:46:04 +00006115 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006116}
6117
Dan Gohman475871a2008-07-27 21:46:04 +00006118SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006119X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
6120 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006121 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006122 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006123 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006124
Dan Gohman475871a2008-07-27 21:46:04 +00006125 SDValue N0 = Op.getOperand(0);
6126 SDValue N1 = Op.getOperand(1);
6127 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00006128
Dan Gohman8a55ce42009-09-23 21:02:20 +00006129 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00006130 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00006131 unsigned Opc;
6132 if (VT == MVT::v8i16)
6133 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00006134 else if (VT == MVT::v16i8)
6135 Opc = X86ISD::PINSRB;
6136 else
6137 Opc = X86ISD::PINSRB;
6138
Nate Begeman14d12ca2008-02-11 04:19:36 +00006139 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
6140 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00006141 if (N1.getValueType() != MVT::i32)
6142 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
6143 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006144 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00006145 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00006146 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00006147 // Bits [7:6] of the constant are the source select. This will always be
6148 // zero here. The DAG Combiner may combine an extract_elt index into these
6149 // bits. For example (insert (extract, 3), 2) could be matched by putting
6150 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00006151 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00006152 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00006153 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00006154 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006155 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00006156 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00006157 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00006158 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00006159 } else if (EltVT == MVT::i32 && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00006160 // PINSR* works with constant index.
6161 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00006162 }
Dan Gohman475871a2008-07-27 21:46:04 +00006163 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006164}
6165
Dan Gohman475871a2008-07-27 21:46:04 +00006166SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006167X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006168 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006169 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006170
David Greene6b381262011-02-09 15:32:06 +00006171 DebugLoc dl = Op.getDebugLoc();
6172 SDValue N0 = Op.getOperand(0);
6173 SDValue N1 = Op.getOperand(1);
6174 SDValue N2 = Op.getOperand(2);
6175
6176 // If this is a 256-bit vector result, first insert into a 128-bit
6177 // vector and then insert into the 256-bit vector.
6178 if (VT.getSizeInBits() > 128) {
6179 if (!isa<ConstantSDNode>(N2))
6180 return SDValue();
6181
6182 // Get the 128-bit vector.
6183 unsigned NumElems = VT.getVectorNumElements();
6184 unsigned IdxVal = cast<ConstantSDNode>(N2)->getZExtValue();
6185 bool Upper = IdxVal >= NumElems / 2;
6186
6187 SDValue SubN0 = Extract128BitVector(N0, N2, DAG, dl);
6188
6189 // Insert into it.
6190 SDValue ScaledN2 = N2;
6191 if (Upper)
6192 ScaledN2 = DAG.getNode(ISD::SUB, dl, N2.getValueType(), N2,
Owen Anderson95771af2011-02-25 21:41:48 +00006193 DAG.getConstant(NumElems /
David Greene6b381262011-02-09 15:32:06 +00006194 (VT.getSizeInBits() / 128),
6195 N2.getValueType()));
6196 Op = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, SubN0.getValueType(), SubN0,
6197 N1, ScaledN2);
6198
6199 // Insert the 128-bit vector
6200 // FIXME: Why UNDEF?
6201 return Insert128BitVector(N0, Op, N2, DAG, dl);
6202 }
6203
Nate Begeman14d12ca2008-02-11 04:19:36 +00006204 if (Subtarget->hasSSE41())
6205 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
6206
Dan Gohman8a55ce42009-09-23 21:02:20 +00006207 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00006208 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00006209
Dan Gohman8a55ce42009-09-23 21:02:20 +00006210 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00006211 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
6212 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00006213 if (N1.getValueType() != MVT::i32)
6214 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
6215 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006216 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00006217 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006218 }
Dan Gohman475871a2008-07-27 21:46:04 +00006219 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006220}
6221
Dan Gohman475871a2008-07-27 21:46:04 +00006222SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006223X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) const {
David Greene2fcdfb42011-02-10 23:11:29 +00006224 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006225 DebugLoc dl = Op.getDebugLoc();
David Greene2fcdfb42011-02-10 23:11:29 +00006226 EVT OpVT = Op.getValueType();
6227
6228 // If this is a 256-bit vector result, first insert into a 128-bit
6229 // vector and then insert into the 256-bit vector.
6230 if (OpVT.getSizeInBits() > 128) {
6231 // Insert into a 128-bit vector.
6232 EVT VT128 = EVT::getVectorVT(*Context,
6233 OpVT.getVectorElementType(),
6234 OpVT.getVectorNumElements() / 2);
6235
6236 Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT128, Op.getOperand(0));
6237
6238 // Insert the 128-bit vector.
6239 return Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, OpVT), Op,
6240 DAG.getConstant(0, MVT::i32),
6241 DAG, dl);
6242 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00006243
Chris Lattnerf172ecd2010-07-04 23:07:25 +00006244 if (Op.getValueType() == MVT::v1i64 &&
6245 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00006246 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00006247
Owen Anderson825b72b2009-08-11 20:47:22 +00006248 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Dale Johannesen0488fb62010-09-30 23:57:10 +00006249 assert(Op.getValueType().getSimpleVT().getSizeInBits() == 128 &&
6250 "Expected an SSE type!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006251 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(),
Dale Johannesen0488fb62010-09-30 23:57:10 +00006252 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006253}
6254
David Greene91585092011-01-26 15:38:49 +00006255// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
6256// a simple subregister reference or explicit instructions to grab
6257// upper bits of a vector.
6258SDValue
6259X86TargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
6260 if (Subtarget->hasAVX()) {
David Greenea5f26012011-02-07 19:36:54 +00006261 DebugLoc dl = Op.getNode()->getDebugLoc();
6262 SDValue Vec = Op.getNode()->getOperand(0);
6263 SDValue Idx = Op.getNode()->getOperand(1);
6264
6265 if (Op.getNode()->getValueType(0).getSizeInBits() == 128
6266 && Vec.getNode()->getValueType(0).getSizeInBits() == 256) {
6267 return Extract128BitVector(Vec, Idx, DAG, dl);
6268 }
David Greene91585092011-01-26 15:38:49 +00006269 }
6270 return SDValue();
6271}
6272
David Greenecfe33c42011-01-26 19:13:22 +00006273// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
6274// simple superregister reference or explicit instructions to insert
6275// the upper bits of a vector.
6276SDValue
6277X86TargetLowering::LowerINSERT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
6278 if (Subtarget->hasAVX()) {
6279 DebugLoc dl = Op.getNode()->getDebugLoc();
6280 SDValue Vec = Op.getNode()->getOperand(0);
6281 SDValue SubVec = Op.getNode()->getOperand(1);
6282 SDValue Idx = Op.getNode()->getOperand(2);
6283
6284 if (Op.getNode()->getValueType(0).getSizeInBits() == 256
6285 && SubVec.getNode()->getValueType(0).getSizeInBits() == 128) {
David Greenea5f26012011-02-07 19:36:54 +00006286 return Insert128BitVector(Vec, SubVec, Idx, DAG, dl);
David Greenecfe33c42011-01-26 19:13:22 +00006287 }
6288 }
6289 return SDValue();
6290}
6291
Bill Wendling056292f2008-09-16 21:48:12 +00006292// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
6293// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
6294// one of the above mentioned nodes. It has to be wrapped because otherwise
6295// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
6296// be used to form addressing mode. These wrapped nodes will be selected
6297// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00006298SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006299X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006300 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00006301
Chris Lattner41621a22009-06-26 19:22:52 +00006302 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6303 // global base reg.
6304 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006305 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006306 CodeModel::Model M = getTargetMachine().getCodeModel();
6307
Chris Lattner4f066492009-07-11 20:29:19 +00006308 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006309 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00006310 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00006311 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006312 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00006313 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006314 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00006315
Evan Cheng1606e8e2009-03-13 07:51:59 +00006316 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00006317 CP->getAlignment(),
6318 CP->getOffset(), OpFlag);
6319 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00006320 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006321 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00006322 if (OpFlag) {
6323 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00006324 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006325 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006326 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006327 }
6328
6329 return Result;
6330}
6331
Dan Gohmand858e902010-04-17 15:26:15 +00006332SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00006333 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00006334
Chris Lattner18c59872009-06-27 04:16:01 +00006335 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6336 // global base reg.
6337 unsigned char OpFlag = 0;
6338 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006339 CodeModel::Model M = getTargetMachine().getCodeModel();
6340
Chris Lattner4f066492009-07-11 20:29:19 +00006341 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006342 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00006343 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00006344 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006345 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00006346 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006347 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00006348
Chris Lattner18c59872009-06-27 04:16:01 +00006349 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
6350 OpFlag);
6351 DebugLoc DL = JT->getDebugLoc();
6352 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00006353
Chris Lattner18c59872009-06-27 04:16:01 +00006354 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00006355 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00006356 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6357 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006358 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00006359 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00006360
Chris Lattner18c59872009-06-27 04:16:01 +00006361 return Result;
6362}
6363
6364SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006365X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00006366 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00006367
Chris Lattner18c59872009-06-27 04:16:01 +00006368 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6369 // global base reg.
6370 unsigned char OpFlag = 0;
6371 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006372 CodeModel::Model M = getTargetMachine().getCodeModel();
6373
Chris Lattner4f066492009-07-11 20:29:19 +00006374 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006375 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00006376 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00006377 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006378 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00006379 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006380 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00006381
Chris Lattner18c59872009-06-27 04:16:01 +00006382 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00006383
Chris Lattner18c59872009-06-27 04:16:01 +00006384 DebugLoc DL = Op.getDebugLoc();
6385 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00006386
6387
Chris Lattner18c59872009-06-27 04:16:01 +00006388 // With PIC, the address is actually $g + Offset.
6389 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00006390 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00006391 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6392 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006393 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00006394 Result);
6395 }
Eric Christopherfd179292009-08-27 18:07:15 +00006396
Chris Lattner18c59872009-06-27 04:16:01 +00006397 return Result;
6398}
6399
Dan Gohman475871a2008-07-27 21:46:04 +00006400SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006401X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00006402 // Create the TargetBlockAddressAddress node.
6403 unsigned char OpFlags =
6404 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00006405 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00006406 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +00006407 DebugLoc dl = Op.getDebugLoc();
6408 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
6409 /*isTarget=*/true, OpFlags);
6410
Dan Gohmanf705adb2009-10-30 01:28:02 +00006411 if (Subtarget->isPICStyleRIPRel() &&
6412 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00006413 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
6414 else
6415 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00006416
Dan Gohman29cbade2009-11-20 23:18:13 +00006417 // With PIC, the address is actually $g + Offset.
6418 if (isGlobalRelativeToPICBase(OpFlags)) {
6419 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
6420 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
6421 Result);
6422 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00006423
6424 return Result;
6425}
6426
6427SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00006428X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00006429 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00006430 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00006431 // Create the TargetGlobalAddress node, folding in the constant
6432 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00006433 unsigned char OpFlags =
6434 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006435 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00006436 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006437 if (OpFlags == X86II::MO_NO_FLAG &&
6438 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00006439 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00006440 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00006441 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006442 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00006443 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00006444 }
Eric Christopherfd179292009-08-27 18:07:15 +00006445
Chris Lattner4f066492009-07-11 20:29:19 +00006446 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006447 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00006448 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
6449 else
6450 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00006451
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006452 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00006453 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00006454 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
6455 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006456 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006457 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006458
Chris Lattner36c25012009-07-10 07:34:39 +00006459 // For globals that require a load from a stub to get the address, emit the
6460 // load.
6461 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00006462 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00006463 MachinePointerInfo::getGOT(), false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006464
Dan Gohman6520e202008-10-18 02:06:02 +00006465 // If there was a non-zero offset that we didn't fold, create an explicit
6466 // addition for it.
6467 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00006468 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00006469 DAG.getConstant(Offset, getPointerTy()));
6470
Evan Cheng0db9fe62006-04-25 20:13:52 +00006471 return Result;
6472}
6473
Evan Chengda43bcf2008-09-24 00:05:32 +00006474SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006475X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00006476 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00006477 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006478 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00006479}
6480
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006481static SDValue
6482GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00006483 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerb903bed2009-06-26 21:20:29 +00006484 unsigned char OperandFlags) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006485 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006486 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006487 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00006488 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006489 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00006490 GA->getOffset(),
6491 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006492 if (InFlag) {
6493 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00006494 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006495 } else {
6496 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00006497 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006498 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006499
6500 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00006501 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006502
Rafael Espindola15f1b662009-04-24 12:59:40 +00006503 SDValue Flag = Chain.getValue(1);
6504 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006505}
6506
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006507// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00006508static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006509LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006510 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00006511 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00006512 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
6513 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006514 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006515 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006516 InFlag = Chain.getValue(1);
6517
Chris Lattnerb903bed2009-06-26 21:20:29 +00006518 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006519}
6520
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006521// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00006522static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006523LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006524 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00006525 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
6526 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006527}
6528
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006529// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
6530// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00006531static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006532 const EVT PtrVT, TLSModel::Model model,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00006533 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00006534 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00006535
Chris Lattnerf93b90c2010-09-22 04:39:11 +00006536 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
6537 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
6538 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00006539
Michael J. Spencerec38de22010-10-10 22:04:20 +00006540 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00006541 DAG.getIntPtrConstant(0),
6542 MachinePointerInfo(Ptr), false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00006543
Chris Lattnerb903bed2009-06-26 21:20:29 +00006544 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006545 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
6546 // initialexec.
6547 unsigned WrapperKind = X86ISD::Wrapper;
6548 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00006549 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00006550 } else if (is64Bit) {
6551 assert(model == TLSModel::InitialExec);
6552 OperandFlags = X86II::MO_GOTTPOFF;
6553 WrapperKind = X86ISD::WrapperRIP;
6554 } else {
6555 assert(model == TLSModel::InitialExec);
6556 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00006557 }
Eric Christopherfd179292009-08-27 18:07:15 +00006558
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006559 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
6560 // exec)
Michael J. Spencerec38de22010-10-10 22:04:20 +00006561 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00006562 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00006563 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00006564 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00006565
Rafael Espindola9a580232009-02-27 13:37:18 +00006566 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00006567 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00006568 MachinePointerInfo::getGOT(), false, false, 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00006569
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006570 // The address of the thread local variable is the add of the thread
6571 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00006572 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006573}
6574
Dan Gohman475871a2008-07-27 21:46:04 +00006575SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006576X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00006577
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006578 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00006579 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00006580
Eric Christopher30ef0e52010-06-03 04:07:48 +00006581 if (Subtarget->isTargetELF()) {
6582 // TODO: implement the "local dynamic" model
6583 // TODO: implement the "initial exec"model for pic executables
Michael J. Spencerec38de22010-10-10 22:04:20 +00006584
Eric Christopher30ef0e52010-06-03 04:07:48 +00006585 // If GV is an alias then use the aliasee for determining
6586 // thread-localness.
6587 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
6588 GV = GA->resolveAliasedGlobal(false);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006589
6590 TLSModel::Model model
Eric Christopher30ef0e52010-06-03 04:07:48 +00006591 = getTLSModel(GV, getTargetMachine().getRelocationModel());
Michael J. Spencerec38de22010-10-10 22:04:20 +00006592
Eric Christopher30ef0e52010-06-03 04:07:48 +00006593 switch (model) {
6594 case TLSModel::GeneralDynamic:
6595 case TLSModel::LocalDynamic: // not implemented
6596 if (Subtarget->is64Bit())
6597 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
6598 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Michael J. Spencerec38de22010-10-10 22:04:20 +00006599
Eric Christopher30ef0e52010-06-03 04:07:48 +00006600 case TLSModel::InitialExec:
6601 case TLSModel::LocalExec:
6602 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
6603 Subtarget->is64Bit());
6604 }
6605 } else if (Subtarget->isTargetDarwin()) {
6606 // Darwin only has one model of TLS. Lower to that.
6607 unsigned char OpFlag = 0;
6608 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
6609 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006610
Eric Christopher30ef0e52010-06-03 04:07:48 +00006611 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6612 // global base reg.
6613 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
6614 !Subtarget->is64Bit();
6615 if (PIC32)
6616 OpFlag = X86II::MO_TLVP_PIC_BASE;
6617 else
6618 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006619 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00006620 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00006621 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00006622 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00006623 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006624
Eric Christopher30ef0e52010-06-03 04:07:48 +00006625 // With PIC32, the address is actually $g + Offset.
6626 if (PIC32)
6627 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6628 DAG.getNode(X86ISD::GlobalBaseReg,
6629 DebugLoc(), getPointerTy()),
6630 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006631
Eric Christopher30ef0e52010-06-03 04:07:48 +00006632 // Lowering the machine isd will make sure everything is in the right
6633 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00006634 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006635 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00006636 SDValue Args[] = { Chain, Offset };
6637 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006638
Eric Christopher30ef0e52010-06-03 04:07:48 +00006639 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
6640 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
6641 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00006642
Eric Christopher30ef0e52010-06-03 04:07:48 +00006643 // And our return value (tls address) is in the standard call return value
6644 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00006645 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
6646 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy());
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006647 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00006648
Eric Christopher30ef0e52010-06-03 04:07:48 +00006649 assert(false &&
6650 "TLS not implemented for this target.");
Eric Christopherfd179292009-08-27 18:07:15 +00006651
Torok Edwinc23197a2009-07-14 16:55:14 +00006652 llvm_unreachable("Unreachable");
Chris Lattner5867de12009-04-01 22:14:45 +00006653 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006654}
6655
Evan Cheng0db9fe62006-04-25 20:13:52 +00006656
Nadav Rotem43012222011-05-11 08:12:09 +00006657/// LowerShiftParts - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00006658/// take a 2 x i32 value to shift plus a shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +00006659SDValue X86TargetLowering::LowerShiftParts(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman4c1fa612008-03-03 22:22:09 +00006660 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00006661 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006662 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006663 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006664 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00006665 SDValue ShOpLo = Op.getOperand(0);
6666 SDValue ShOpHi = Op.getOperand(1);
6667 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00006668 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00006669 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00006670 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00006671
Dan Gohman475871a2008-07-27 21:46:04 +00006672 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006673 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00006674 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
6675 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006676 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006677 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
6678 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006679 }
Evan Chenge3413162006-01-09 18:33:28 +00006680
Owen Anderson825b72b2009-08-11 20:47:22 +00006681 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
6682 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00006683 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00006684 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00006685
Dan Gohman475871a2008-07-27 21:46:04 +00006686 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00006687 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00006688 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
6689 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00006690
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006691 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00006692 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
6693 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006694 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006695 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
6696 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006697 }
6698
Dan Gohman475871a2008-07-27 21:46:04 +00006699 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00006700 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006701}
Evan Chenga3195e82006-01-12 22:54:21 +00006702
Dan Gohmand858e902010-04-17 15:26:15 +00006703SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
6704 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006705 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00006706
Dale Johannesen0488fb62010-09-30 23:57:10 +00006707 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00006708 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00006709
Owen Anderson825b72b2009-08-11 20:47:22 +00006710 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00006711 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00006712
Eli Friedman36df4992009-05-27 00:47:34 +00006713 // These are really Legal; return the operand so the caller accepts it as
6714 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00006715 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00006716 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00006717 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00006718 Subtarget->is64Bit()) {
6719 return Op;
6720 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006721
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006722 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006723 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006724 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00006725 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006726 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00006727 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00006728 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006729 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006730 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00006731 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
6732}
Evan Cheng0db9fe62006-04-25 20:13:52 +00006733
Owen Andersone50ed302009-08-10 22:56:29 +00006734SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00006735 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00006736 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006737 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00006738 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00006739 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00006740 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006741 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006742 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00006743 else
Owen Anderson825b72b2009-08-11 20:47:22 +00006744 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006745
Chris Lattner492a43e2010-09-22 01:28:21 +00006746 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006747
Stuart Hastings84be9582011-06-02 15:57:11 +00006748 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(StackSlot);
6749 MachineMemOperand *MMO;
6750 if (FI) {
6751 int SSFI = FI->getIndex();
6752 MMO =
6753 DAG.getMachineFunction()
6754 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6755 MachineMemOperand::MOLoad, ByteSize, ByteSize);
6756 } else {
6757 MMO = cast<LoadSDNode>(StackSlot)->getMemOperand();
6758 StackSlot = StackSlot.getOperand(1);
6759 }
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006760 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00006761 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
6762 X86ISD::FILD, DL,
6763 Tys, Ops, array_lengthof(Ops),
6764 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006765
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006766 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006767 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00006768 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006769
6770 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
6771 // shouldn't be necessary except that RFP cannot be live across
6772 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006773 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00006774 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
6775 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006776 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00006777 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006778 SDValue Ops[] = {
6779 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
6780 };
Chris Lattner492a43e2010-09-22 01:28:21 +00006781 MachineMemOperand *MMO =
6782 DAG.getMachineFunction()
6783 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00006784 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006785
Chris Lattner492a43e2010-09-22 01:28:21 +00006786 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
6787 Ops, array_lengthof(Ops),
6788 Op.getValueType(), MMO);
6789 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006790 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006791 false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006792 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006793
Evan Cheng0db9fe62006-04-25 20:13:52 +00006794 return Result;
6795}
6796
Bill Wendling8b8a6362009-01-17 03:56:04 +00006797// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00006798SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
6799 SelectionDAG &DAG) const {
Bill Wendling8b8a6362009-01-17 03:56:04 +00006800 // This algorithm is not obvious. Here it is in C code, more or less:
6801 /*
6802 double uint64_to_double( uint32_t hi, uint32_t lo ) {
6803 static const __m128i exp = { 0x4330000045300000ULL, 0 };
6804 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00006805
Bill Wendling8b8a6362009-01-17 03:56:04 +00006806 // Copy ints to xmm registers.
6807 __m128i xh = _mm_cvtsi32_si128( hi );
6808 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00006809
Bill Wendling8b8a6362009-01-17 03:56:04 +00006810 // Combine into low half of a single xmm register.
6811 __m128i x = _mm_unpacklo_epi32( xh, xl );
6812 __m128d d;
6813 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00006814
Bill Wendling8b8a6362009-01-17 03:56:04 +00006815 // Merge in appropriate exponents to give the integer bits the right
6816 // magnitude.
6817 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00006818
Bill Wendling8b8a6362009-01-17 03:56:04 +00006819 // Subtract away the biases to deal with the IEEE-754 double precision
6820 // implicit 1.
6821 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00006822
Bill Wendling8b8a6362009-01-17 03:56:04 +00006823 // All conversions up to here are exact. The correctly rounded result is
6824 // calculated using the current rounding mode using the following
6825 // horizontal add.
6826 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
6827 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
6828 // store doesn't really need to be here (except
6829 // maybe to zero the other double)
6830 return sd;
6831 }
6832 */
Dale Johannesen040225f2008-10-21 23:07:49 +00006833
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006834 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00006835 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00006836
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006837 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00006838 std::vector<Constant*> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00006839 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
6840 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
6841 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
6842 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00006843 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006844 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006845
Bill Wendling8b8a6362009-01-17 03:56:04 +00006846 std::vector<Constant*> CV1;
Owen Andersona90b3dc2009-07-15 21:51:10 +00006847 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006848 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
Owen Andersona90b3dc2009-07-15 21:51:10 +00006849 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006850 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00006851 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006852 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006853
Owen Anderson825b72b2009-08-11 20:47:22 +00006854 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6855 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00006856 Op.getOperand(0),
6857 DAG.getIntPtrConstant(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006858 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6859 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00006860 Op.getOperand(0),
6861 DAG.getIntPtrConstant(0)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006862 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
6863 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00006864 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006865 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00006866 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006867 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck2);
Owen Anderson825b72b2009-08-11 20:47:22 +00006868 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00006869 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006870 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00006871 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006872
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006873 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00006874 int ShufMask[2] = { 1, -1 };
Owen Anderson825b72b2009-08-11 20:47:22 +00006875 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
6876 DAG.getUNDEF(MVT::v2f64), ShufMask);
6877 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
6878 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006879 DAG.getIntPtrConstant(0));
6880}
6881
Bill Wendling8b8a6362009-01-17 03:56:04 +00006882// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00006883SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
6884 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006885 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00006886 // FP constant to bias correct the final result.
6887 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00006888 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006889
6890 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00006891 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6892 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Bill Wendling8b8a6362009-01-17 03:56:04 +00006893 Op.getOperand(0),
6894 DAG.getIntPtrConstant(0)));
6895
Owen Anderson825b72b2009-08-11 20:47:22 +00006896 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006897 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00006898 DAG.getIntPtrConstant(0));
6899
6900 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00006901 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006902 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00006903 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006904 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006905 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00006906 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006907 MVT::v2f64, Bias)));
6908 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006909 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00006910 DAG.getIntPtrConstant(0));
6911
6912 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00006913 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006914
6915 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00006916 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00006917
Owen Anderson825b72b2009-08-11 20:47:22 +00006918 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006919 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00006920 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00006921 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006922 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00006923 }
6924
6925 // Handle final rounding.
6926 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00006927}
6928
Dan Gohmand858e902010-04-17 15:26:15 +00006929SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
6930 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00006931 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006932 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00006933
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006934 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00006935 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
6936 // the optimization here.
6937 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00006938 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00006939
Owen Andersone50ed302009-08-10 22:56:29 +00006940 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006941 EVT DstVT = Op.getValueType();
6942 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00006943 return LowerUINT_TO_FP_i64(Op, DAG);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006944 else if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00006945 return LowerUINT_TO_FP_i32(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00006946
6947 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00006948 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006949 if (SrcVT == MVT::i32) {
6950 SDValue WordOff = DAG.getConstant(4, getPointerTy());
6951 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
6952 getPointerTy(), StackSlot, WordOff);
6953 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006954 StackSlot, MachinePointerInfo(),
6955 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006956 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006957 OffsetSlot, MachinePointerInfo(),
6958 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006959 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
6960 return Fild;
6961 }
6962
6963 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
6964 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006965 StackSlot, MachinePointerInfo(),
6966 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006967 // For i64 source, we need to add the appropriate power of 2 if the input
6968 // was negative. This is the same as the optimization in
6969 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
6970 // we must be careful to do the computation in x87 extended precision, not
6971 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00006972 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
6973 MachineMemOperand *MMO =
6974 DAG.getMachineFunction()
6975 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6976 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006977
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006978 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
6979 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00006980 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
6981 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006982
6983 APInt FF(32, 0x5F800000ULL);
6984
6985 // Check whether the sign bit is set.
6986 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
6987 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
6988 ISD::SETLT);
6989
6990 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
6991 SDValue FudgePtr = DAG.getConstantPool(
6992 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
6993 getPointerTy());
6994
6995 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
6996 SDValue Zero = DAG.getIntPtrConstant(0);
6997 SDValue Four = DAG.getIntPtrConstant(4);
6998 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
6999 Zero, Four);
7000 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
7001
7002 // Load the value out, extending it from f32 to f80.
7003 // FIXME: Avoid the extend by constructing the right constant pool?
Stuart Hastingsa9011292011-02-16 16:23:55 +00007004 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00007005 FudgePtr, MachinePointerInfo::getConstantPool(),
7006 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007007 // Extend everything to 80 bits to force it to be done on x87.
7008 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
7009 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00007010}
7011
Dan Gohman475871a2008-07-27 21:46:04 +00007012std::pair<SDValue,SDValue> X86TargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00007013FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) const {
Chris Lattner07290932010-09-22 01:05:16 +00007014 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00007015
Owen Andersone50ed302009-08-10 22:56:29 +00007016 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00007017
7018 if (!IsSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007019 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
7020 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00007021 }
7022
Owen Anderson825b72b2009-08-11 20:47:22 +00007023 assert(DstTy.getSimpleVT() <= MVT::i64 &&
7024 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00007025 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00007026
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007027 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007028 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00007029 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00007030 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00007031 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00007032 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00007033 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00007034 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007035
Evan Cheng87c89352007-10-15 20:11:21 +00007036 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
7037 // stack slot.
7038 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00007039 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00007040 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007041 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00007042
Michael J. Spencerec38de22010-10-10 22:04:20 +00007043
7044
Evan Cheng0db9fe62006-04-25 20:13:52 +00007045 unsigned Opc;
Owen Anderson825b72b2009-08-11 20:47:22 +00007046 switch (DstTy.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007047 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson825b72b2009-08-11 20:47:22 +00007048 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
7049 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
7050 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007051 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007052
Dan Gohman475871a2008-07-27 21:46:04 +00007053 SDValue Chain = DAG.getEntryNode();
7054 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00007055 EVT TheVT = Op.getOperand(0).getValueType();
7056 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007057 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00007058 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007059 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007060 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007061 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00007062 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00007063 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00007064 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00007065
Chris Lattner492a43e2010-09-22 01:28:21 +00007066 MachineMemOperand *MMO =
7067 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7068 MachineMemOperand::MOLoad, MemSize, MemSize);
7069 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
7070 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007071 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00007072 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007073 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
7074 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007075
Chris Lattner07290932010-09-22 01:05:16 +00007076 MachineMemOperand *MMO =
7077 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7078 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007079
Evan Cheng0db9fe62006-04-25 20:13:52 +00007080 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00007081 SDValue Ops[] = { Chain, Value, StackSlot };
Chris Lattner07290932010-09-22 01:05:16 +00007082 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
7083 Ops, 3, DstTy, MMO);
Evan Chengd9558e02006-01-06 00:43:03 +00007084
Chris Lattner27a6c732007-11-24 07:07:01 +00007085 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007086}
7087
Dan Gohmand858e902010-04-17 15:26:15 +00007088SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
7089 SelectionDAG &DAG) const {
Dale Johannesen0488fb62010-09-30 23:57:10 +00007090 if (Op.getValueType().isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007091 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007092
Eli Friedman948e95a2009-05-23 09:59:16 +00007093 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00007094 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00007095 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
7096 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00007097
Chris Lattner27a6c732007-11-24 07:07:01 +00007098 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007099 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00007100 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00007101}
7102
Dan Gohmand858e902010-04-17 15:26:15 +00007103SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
7104 SelectionDAG &DAG) const {
Eli Friedman948e95a2009-05-23 09:59:16 +00007105 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
7106 SDValue FIST = Vals.first, StackSlot = Vals.second;
7107 assert(FIST.getNode() && "Unexpected failure");
7108
7109 // Load the result.
7110 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00007111 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007112}
7113
Dan Gohmand858e902010-04-17 15:26:15 +00007114SDValue X86TargetLowering::LowerFABS(SDValue Op,
7115 SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007116 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007117 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007118 EVT VT = Op.getValueType();
7119 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007120 if (VT.isVector())
7121 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007122 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007123 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007124 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00007125 CV.push_back(C);
7126 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007127 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007128 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00007129 CV.push_back(C);
7130 CV.push_back(C);
7131 CV.push_back(C);
7132 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007133 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007134 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007135 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007136 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007137 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007138 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007139 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007140}
7141
Dan Gohmand858e902010-04-17 15:26:15 +00007142SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007143 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007144 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007145 EVT VT = Op.getValueType();
7146 EVT EltVT = VT;
Duncan Sandsda9ad382009-09-06 19:29:07 +00007147 if (VT.isVector())
Duncan Sands83ec4b62008-06-06 12:08:01 +00007148 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007149 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007150 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007151 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00007152 CV.push_back(C);
7153 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007154 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007155 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00007156 CV.push_back(C);
7157 CV.push_back(C);
7158 CV.push_back(C);
7159 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007160 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007161 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007162 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007163 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007164 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007165 false, false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00007166 if (VT.isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007167 return DAG.getNode(ISD::BITCAST, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007168 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007169 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007170 Op.getOperand(0)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007171 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00007172 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007173 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00007174 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00007175}
7176
Dan Gohmand858e902010-04-17 15:26:15 +00007177SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007178 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00007179 SDValue Op0 = Op.getOperand(0);
7180 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007181 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007182 EVT VT = Op.getValueType();
7183 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00007184
7185 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007186 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007187 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00007188 SrcVT = VT;
7189 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007190 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007191 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007192 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007193 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007194 }
7195
7196 // At this point the operands and the result should have the same
7197 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00007198
Evan Cheng68c47cb2007-01-05 07:55:56 +00007199 // First get the sign bit of second operand.
7200 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007201 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007202 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
7203 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007204 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007205 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
7206 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7207 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7208 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007209 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007210 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007211 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007212 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007213 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007214 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007215 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00007216
7217 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007218 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007219 // Op0 is MVT::f32, Op1 is MVT::f64.
7220 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
7221 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
7222 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007223 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00007224 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00007225 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007226 }
7227
Evan Cheng73d6cf12007-01-05 21:37:56 +00007228 // Clear first operand sign bit.
7229 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00007230 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007231 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
7232 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00007233 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007234 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
7235 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7236 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7237 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00007238 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007239 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007240 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007241 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007242 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007243 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007244 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00007245
7246 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00007247 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00007248}
7249
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00007250SDValue X86TargetLowering::LowerFGETSIGN(SDValue Op, SelectionDAG &DAG) const {
7251 SDValue N0 = Op.getOperand(0);
7252 DebugLoc dl = Op.getDebugLoc();
7253 EVT VT = Op.getValueType();
7254
7255 // Lower ISD::FGETSIGN to (AND (X86ISD::FGETSIGNx86 ...) 1).
7256 SDValue xFGETSIGN = DAG.getNode(X86ISD::FGETSIGNx86, dl, VT, N0,
7257 DAG.getConstant(1, VT));
7258 return DAG.getNode(ISD::AND, dl, VT, xFGETSIGN, DAG.getConstant(1, VT));
7259}
7260
Dan Gohman076aee32009-03-04 19:44:21 +00007261/// Emit nodes that will be selected as "test Op0,Op0", or something
7262/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00007263SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00007264 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00007265 DebugLoc dl = Op.getDebugLoc();
7266
Dan Gohman31125812009-03-07 01:58:32 +00007267 // CF and OF aren't always set the way we want. Determine which
7268 // of these we need.
7269 bool NeedCF = false;
7270 bool NeedOF = false;
7271 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007272 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00007273 case X86::COND_A: case X86::COND_AE:
7274 case X86::COND_B: case X86::COND_BE:
7275 NeedCF = true;
7276 break;
7277 case X86::COND_G: case X86::COND_GE:
7278 case X86::COND_L: case X86::COND_LE:
7279 case X86::COND_O: case X86::COND_NO:
7280 NeedOF = true;
7281 break;
Dan Gohman31125812009-03-07 01:58:32 +00007282 }
7283
Dan Gohman076aee32009-03-04 19:44:21 +00007284 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00007285 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
7286 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007287 if (Op.getResNo() != 0 || NeedOF || NeedCF)
7288 // Emit a CMP with 0, which is the TEST pattern.
7289 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
7290 DAG.getConstant(0, Op.getValueType()));
7291
7292 unsigned Opcode = 0;
7293 unsigned NumOperands = 0;
7294 switch (Op.getNode()->getOpcode()) {
7295 case ISD::ADD:
7296 // Due to an isel shortcoming, be conservative if this add is likely to be
7297 // selected as part of a load-modify-store instruction. When the root node
7298 // in a match is a store, isel doesn't know how to remap non-chain non-flag
7299 // uses of other nodes in the match, such as the ADD in this case. This
7300 // leads to the ADD being left around and reselected, with the result being
7301 // two adds in the output. Alas, even if none our users are stores, that
7302 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
7303 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
7304 // climbing the DAG back to the root, and it doesn't seem to be worth the
7305 // effort.
7306 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Dan Gohman076aee32009-03-04 19:44:21 +00007307 UE = Op.getNode()->use_end(); UI != UE; ++UI)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007308 if (UI->getOpcode() != ISD::CopyToReg && UI->getOpcode() != ISD::SETCC)
7309 goto default_case;
7310
7311 if (ConstantSDNode *C =
7312 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
7313 // An add of one will be selected as an INC.
7314 if (C->getAPIntValue() == 1) {
7315 Opcode = X86ISD::INC;
7316 NumOperands = 1;
7317 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00007318 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007319
7320 // An add of negative one (subtract of one) will be selected as a DEC.
7321 if (C->getAPIntValue().isAllOnesValue()) {
7322 Opcode = X86ISD::DEC;
7323 NumOperands = 1;
7324 break;
7325 }
Dan Gohman076aee32009-03-04 19:44:21 +00007326 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007327
7328 // Otherwise use a regular EFLAGS-setting add.
7329 Opcode = X86ISD::ADD;
7330 NumOperands = 2;
7331 break;
7332 case ISD::AND: {
7333 // If the primary and result isn't used, don't bother using X86ISD::AND,
7334 // because a TEST instruction will be better.
7335 bool NonFlagUse = false;
7336 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
7337 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
7338 SDNode *User = *UI;
7339 unsigned UOpNo = UI.getOperandNo();
7340 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
7341 // Look pass truncate.
7342 UOpNo = User->use_begin().getOperandNo();
7343 User = *User->use_begin();
7344 }
7345
7346 if (User->getOpcode() != ISD::BRCOND &&
7347 User->getOpcode() != ISD::SETCC &&
7348 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
7349 NonFlagUse = true;
7350 break;
7351 }
Dan Gohman076aee32009-03-04 19:44:21 +00007352 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007353
7354 if (!NonFlagUse)
7355 break;
7356 }
7357 // FALL THROUGH
7358 case ISD::SUB:
7359 case ISD::OR:
7360 case ISD::XOR:
7361 // Due to the ISEL shortcoming noted above, be conservative if this op is
7362 // likely to be selected as part of a load-modify-store instruction.
7363 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
7364 UE = Op.getNode()->use_end(); UI != UE; ++UI)
7365 if (UI->getOpcode() == ISD::STORE)
7366 goto default_case;
7367
7368 // Otherwise use a regular EFLAGS-setting instruction.
7369 switch (Op.getNode()->getOpcode()) {
7370 default: llvm_unreachable("unexpected operator!");
7371 case ISD::SUB: Opcode = X86ISD::SUB; break;
7372 case ISD::OR: Opcode = X86ISD::OR; break;
7373 case ISD::XOR: Opcode = X86ISD::XOR; break;
7374 case ISD::AND: Opcode = X86ISD::AND; break;
7375 }
7376
7377 NumOperands = 2;
7378 break;
7379 case X86ISD::ADD:
7380 case X86ISD::SUB:
7381 case X86ISD::INC:
7382 case X86ISD::DEC:
7383 case X86ISD::OR:
7384 case X86ISD::XOR:
7385 case X86ISD::AND:
7386 return SDValue(Op.getNode(), 1);
7387 default:
7388 default_case:
7389 break;
Dan Gohman076aee32009-03-04 19:44:21 +00007390 }
7391
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007392 if (Opcode == 0)
7393 // Emit a CMP with 0, which is the TEST pattern.
7394 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
7395 DAG.getConstant(0, Op.getValueType()));
7396
7397 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
7398 SmallVector<SDValue, 4> Ops;
7399 for (unsigned i = 0; i != NumOperands; ++i)
7400 Ops.push_back(Op.getOperand(i));
7401
7402 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
7403 DAG.ReplaceAllUsesWith(Op, New);
7404 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00007405}
7406
7407/// Emit nodes that will be selected as "cmp Op0,Op1", or something
7408/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00007409SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00007410 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00007411 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
7412 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00007413 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00007414
7415 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00007416 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00007417}
7418
Evan Chengd40d03e2010-01-06 19:38:29 +00007419/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
7420/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00007421SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
7422 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00007423 SDValue Op0 = And.getOperand(0);
7424 SDValue Op1 = And.getOperand(1);
7425 if (Op0.getOpcode() == ISD::TRUNCATE)
7426 Op0 = Op0.getOperand(0);
7427 if (Op1.getOpcode() == ISD::TRUNCATE)
7428 Op1 = Op1.getOperand(0);
7429
Evan Chengd40d03e2010-01-06 19:38:29 +00007430 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00007431 if (Op1.getOpcode() == ISD::SHL)
7432 std::swap(Op0, Op1);
7433 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00007434 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
7435 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00007436 // If we looked past a truncate, check that it's only truncating away
7437 // known zeros.
7438 unsigned BitWidth = Op0.getValueSizeInBits();
7439 unsigned AndBitWidth = And.getValueSizeInBits();
7440 if (BitWidth > AndBitWidth) {
7441 APInt Mask = APInt::getAllOnesValue(BitWidth), Zeros, Ones;
7442 DAG.ComputeMaskedBits(Op0, Mask, Zeros, Ones);
7443 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
7444 return SDValue();
7445 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007446 LHS = Op1;
7447 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00007448 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007449 } else if (Op1.getOpcode() == ISD::Constant) {
7450 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
7451 SDValue AndLHS = Op0;
Evan Chengd40d03e2010-01-06 19:38:29 +00007452 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
7453 LHS = AndLHS.getOperand(0);
7454 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00007455 }
Evan Chengd40d03e2010-01-06 19:38:29 +00007456 }
Evan Cheng0488db92007-09-25 01:57:46 +00007457
Evan Chengd40d03e2010-01-06 19:38:29 +00007458 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00007459 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00007460 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00007461 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00007462 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00007463 // Also promote i16 to i32 for performance / code size reason.
7464 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00007465 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00007466 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00007467
Evan Chengd40d03e2010-01-06 19:38:29 +00007468 // If the operand types disagree, extend the shift amount to match. Since
7469 // BT ignores high bits (like shifts) we can use anyextend.
7470 if (LHS.getValueType() != RHS.getValueType())
7471 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00007472
Evan Chengd40d03e2010-01-06 19:38:29 +00007473 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
7474 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
7475 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7476 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00007477 }
7478
Evan Cheng54de3ea2010-01-05 06:52:31 +00007479 return SDValue();
7480}
7481
Dan Gohmand858e902010-04-17 15:26:15 +00007482SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng54de3ea2010-01-05 06:52:31 +00007483 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
7484 SDValue Op0 = Op.getOperand(0);
7485 SDValue Op1 = Op.getOperand(1);
7486 DebugLoc dl = Op.getDebugLoc();
7487 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
7488
7489 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00007490 // Lower (X & (1 << N)) == 0 to BT(X, N).
7491 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
7492 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Andrew Trickf6c39412011-03-23 23:11:02 +00007493 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00007494 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00007495 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00007496 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
7497 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
7498 if (NewSetCC.getNode())
7499 return NewSetCC;
7500 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00007501
Chris Lattner481eebc2010-12-19 21:23:48 +00007502 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
7503 // these.
7504 if (Op1.getOpcode() == ISD::Constant &&
Andrew Trickf6c39412011-03-23 23:11:02 +00007505 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
Evan Cheng2c755ba2010-02-27 07:36:59 +00007506 cast<ConstantSDNode>(Op1)->isNullValue()) &&
7507 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007508
Chris Lattner481eebc2010-12-19 21:23:48 +00007509 // If the input is a setcc, then reuse the input setcc or use a new one with
7510 // the inverted condition.
7511 if (Op0.getOpcode() == X86ISD::SETCC) {
7512 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
7513 bool Invert = (CC == ISD::SETNE) ^
7514 cast<ConstantSDNode>(Op1)->isNullValue();
7515 if (!Invert) return Op0;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007516
Evan Cheng2c755ba2010-02-27 07:36:59 +00007517 CCode = X86::GetOppositeBranchCondition(CCode);
Chris Lattner481eebc2010-12-19 21:23:48 +00007518 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7519 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
7520 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007521 }
7522
Evan Chenge5b51ac2010-04-17 06:13:15 +00007523 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00007524 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00007525 if (X86CC == X86::COND_INVALID)
7526 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00007527
Chris Lattnerc19d1c32010-12-19 22:08:31 +00007528 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00007529 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnerc19d1c32010-12-19 22:08:31 +00007530 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
Evan Cheng0488db92007-09-25 01:57:46 +00007531}
7532
Dan Gohmand858e902010-04-17 15:26:15 +00007533SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00007534 SDValue Cond;
7535 SDValue Op0 = Op.getOperand(0);
7536 SDValue Op1 = Op.getOperand(1);
7537 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00007538 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00007539 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
7540 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007541 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00007542
7543 if (isFP) {
7544 unsigned SSECC = 8;
Owen Andersone50ed302009-08-10 22:56:29 +00007545 EVT VT0 = Op0.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00007546 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
7547 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00007548 bool Swap = false;
7549
7550 switch (SetCCOpcode) {
7551 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00007552 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00007553 case ISD::SETEQ: SSECC = 0; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00007554 case ISD::SETOGT:
Nate Begeman30a0de92008-07-17 16:51:19 +00007555 case ISD::SETGT: Swap = true; // Fallthrough
7556 case ISD::SETLT:
7557 case ISD::SETOLT: SSECC = 1; break;
7558 case ISD::SETOGE:
7559 case ISD::SETGE: Swap = true; // Fallthrough
7560 case ISD::SETLE:
7561 case ISD::SETOLE: SSECC = 2; break;
7562 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00007563 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00007564 case ISD::SETNE: SSECC = 4; break;
7565 case ISD::SETULE: Swap = true;
7566 case ISD::SETUGE: SSECC = 5; break;
7567 case ISD::SETULT: Swap = true;
7568 case ISD::SETUGT: SSECC = 6; break;
7569 case ISD::SETO: SSECC = 7; break;
7570 }
7571 if (Swap)
7572 std::swap(Op0, Op1);
7573
Nate Begemanfb8ead02008-07-25 19:05:58 +00007574 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00007575 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00007576 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00007577 SDValue UNORD, EQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00007578 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
7579 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00007580 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00007581 }
7582 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00007583 SDValue ORD, NEQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00007584 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
7585 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00007586 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00007587 }
Torok Edwinc23197a2009-07-14 16:55:14 +00007588 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00007589 }
7590 // Handle all other FP comparisons here.
Owen Anderson825b72b2009-08-11 20:47:22 +00007591 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00007592 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007593
Nate Begeman30a0de92008-07-17 16:51:19 +00007594 // We are handling one of the integer comparisons here. Since SSE only has
7595 // GT and EQ comparisons for integer, swapping operands and multiple
7596 // operations may be required for some comparisons.
7597 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
7598 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007599
Owen Anderson825b72b2009-08-11 20:47:22 +00007600 switch (VT.getSimpleVT().SimpleTy) {
Nate Begeman30a0de92008-07-17 16:51:19 +00007601 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007602 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007603 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007604 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
7605 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00007606 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007607
Nate Begeman30a0de92008-07-17 16:51:19 +00007608 switch (SetCCOpcode) {
7609 default: break;
7610 case ISD::SETNE: Invert = true;
7611 case ISD::SETEQ: Opc = EQOpc; break;
7612 case ISD::SETLT: Swap = true;
7613 case ISD::SETGT: Opc = GTOpc; break;
7614 case ISD::SETGE: Swap = true;
7615 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
7616 case ISD::SETULT: Swap = true;
7617 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
7618 case ISD::SETUGE: Swap = true;
7619 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
7620 }
7621 if (Swap)
7622 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007623
Nate Begeman30a0de92008-07-17 16:51:19 +00007624 // Since SSE has no unsigned integer comparisons, we need to flip the sign
7625 // bits of the inputs before performing those operations.
7626 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00007627 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00007628 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
7629 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00007630 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00007631 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
7632 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00007633 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
7634 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00007635 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007636
Dale Johannesenace16102009-02-03 19:33:06 +00007637 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00007638
7639 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00007640 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00007641 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00007642
Nate Begeman30a0de92008-07-17 16:51:19 +00007643 return Result;
7644}
Evan Cheng0488db92007-09-25 01:57:46 +00007645
Evan Cheng370e5342008-12-03 08:38:43 +00007646// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00007647static bool isX86LogicalCmp(SDValue Op) {
7648 unsigned Opc = Op.getNode()->getOpcode();
7649 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
7650 return true;
7651 if (Op.getResNo() == 1 &&
7652 (Opc == X86ISD::ADD ||
7653 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00007654 Opc == X86ISD::ADC ||
7655 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00007656 Opc == X86ISD::SMUL ||
7657 Opc == X86ISD::UMUL ||
7658 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00007659 Opc == X86ISD::DEC ||
7660 Opc == X86ISD::OR ||
7661 Opc == X86ISD::XOR ||
7662 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00007663 return true;
7664
Chris Lattner9637d5b2010-12-05 07:49:54 +00007665 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
7666 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007667
Dan Gohman076aee32009-03-04 19:44:21 +00007668 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00007669}
7670
Chris Lattnera2b56002010-12-05 01:23:24 +00007671static bool isZero(SDValue V) {
7672 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
7673 return C && C->isNullValue();
7674}
7675
Chris Lattner96908b12010-12-05 02:00:51 +00007676static bool isAllOnes(SDValue V) {
7677 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
7678 return C && C->isAllOnesValue();
7679}
7680
Dan Gohmand858e902010-04-17 15:26:15 +00007681SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00007682 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007683 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00007684 SDValue Op1 = Op.getOperand(1);
7685 SDValue Op2 = Op.getOperand(2);
7686 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00007687 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00007688
Dan Gohman1a492952009-10-20 16:22:37 +00007689 if (Cond.getOpcode() == ISD::SETCC) {
7690 SDValue NewCond = LowerSETCC(Cond, DAG);
7691 if (NewCond.getNode())
7692 Cond = NewCond;
7693 }
Evan Cheng734503b2006-09-11 02:19:56 +00007694
Chris Lattnera2b56002010-12-05 01:23:24 +00007695 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00007696 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00007697 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00007698 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007699 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00007700 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
7701 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007702 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007703
Chris Lattnera2b56002010-12-05 01:23:24 +00007704 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007705
7706 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00007707 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
7708 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00007709
7710 SDValue CmpOp0 = Cmp.getOperand(0);
7711 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
7712 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007713
Chris Lattner96908b12010-12-05 02:00:51 +00007714 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00007715 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
7716 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007717
Chris Lattner96908b12010-12-05 02:00:51 +00007718 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
7719 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007720
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007721 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00007722 if (N2C == 0 || !N2C->isNullValue())
7723 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
7724 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007725 }
7726 }
7727
Chris Lattnera2b56002010-12-05 01:23:24 +00007728 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00007729 if (Cond.getOpcode() == ISD::AND &&
7730 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
7731 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00007732 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00007733 Cond = Cond.getOperand(0);
7734 }
7735
Evan Cheng3f41d662007-10-08 22:16:29 +00007736 // If condition flag is set by a X86ISD::CMP, then use it as the condition
7737 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00007738 if (Cond.getOpcode() == X86ISD::SETCC ||
7739 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00007740 CC = Cond.getOperand(0);
7741
Dan Gohman475871a2008-07-27 21:46:04 +00007742 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00007743 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00007744 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00007745
Evan Cheng3f41d662007-10-08 22:16:29 +00007746 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007747 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00007748 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00007749 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00007750
Chris Lattnerd1980a52009-03-12 06:52:53 +00007751 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
7752 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00007753 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00007754 addTest = false;
7755 }
7756 }
7757
7758 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007759 // Look pass the truncate.
7760 if (Cond.getOpcode() == ISD::TRUNCATE)
7761 Cond = Cond.getOperand(0);
7762
7763 // We know the result of AND is compared against zero. Try to match
7764 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00007765 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00007766 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00007767 if (NewSetCC.getNode()) {
7768 CC = NewSetCC.getOperand(0);
7769 Cond = NewSetCC.getOperand(1);
7770 addTest = false;
7771 }
7772 }
7773 }
7774
7775 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007776 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00007777 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00007778 }
7779
Benjamin Kramere915ff32010-12-22 23:09:28 +00007780 // a < b ? -1 : 0 -> RES = ~setcc_carry
7781 // a < b ? 0 : -1 -> RES = setcc_carry
7782 // a >= b ? -1 : 0 -> RES = setcc_carry
7783 // a >= b ? 0 : -1 -> RES = ~setcc_carry
7784 if (Cond.getOpcode() == X86ISD::CMP) {
7785 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
7786
7787 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
7788 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
7789 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
7790 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
7791 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
7792 return DAG.getNOT(DL, Res, Res.getValueType());
7793 return Res;
7794 }
7795 }
7796
Evan Cheng0488db92007-09-25 01:57:46 +00007797 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
7798 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007799 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007800 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00007801 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00007802}
7803
Evan Cheng370e5342008-12-03 08:38:43 +00007804// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
7805// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
7806// from the AND / OR.
7807static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
7808 Opc = Op.getOpcode();
7809 if (Opc != ISD::OR && Opc != ISD::AND)
7810 return false;
7811 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
7812 Op.getOperand(0).hasOneUse() &&
7813 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
7814 Op.getOperand(1).hasOneUse());
7815}
7816
Evan Cheng961d6d42009-02-02 08:19:07 +00007817// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
7818// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00007819static bool isXor1OfSetCC(SDValue Op) {
7820 if (Op.getOpcode() != ISD::XOR)
7821 return false;
7822 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
7823 if (N1C && N1C->getAPIntValue() == 1) {
7824 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
7825 Op.getOperand(0).hasOneUse();
7826 }
7827 return false;
7828}
7829
Dan Gohmand858e902010-04-17 15:26:15 +00007830SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00007831 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007832 SDValue Chain = Op.getOperand(0);
7833 SDValue Cond = Op.getOperand(1);
7834 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007835 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00007836 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00007837
Dan Gohman1a492952009-10-20 16:22:37 +00007838 if (Cond.getOpcode() == ISD::SETCC) {
7839 SDValue NewCond = LowerSETCC(Cond, DAG);
7840 if (NewCond.getNode())
7841 Cond = NewCond;
7842 }
Chris Lattnere55484e2008-12-25 05:34:37 +00007843#if 0
7844 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00007845 else if (Cond.getOpcode() == X86ISD::ADD ||
7846 Cond.getOpcode() == X86ISD::SUB ||
7847 Cond.getOpcode() == X86ISD::SMUL ||
7848 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00007849 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00007850#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00007851
Evan Chengad9c0a32009-12-15 00:53:42 +00007852 // Look pass (and (setcc_carry (cmp ...)), 1).
7853 if (Cond.getOpcode() == ISD::AND &&
7854 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
7855 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00007856 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00007857 Cond = Cond.getOperand(0);
7858 }
7859
Evan Cheng3f41d662007-10-08 22:16:29 +00007860 // If condition flag is set by a X86ISD::CMP, then use it as the condition
7861 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00007862 if (Cond.getOpcode() == X86ISD::SETCC ||
7863 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00007864 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007865
Dan Gohman475871a2008-07-27 21:46:04 +00007866 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00007867 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00007868 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00007869 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00007870 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00007871 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00007872 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00007873 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00007874 default: break;
7875 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00007876 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00007877 // These can only come from an arithmetic instruction with overflow,
7878 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00007879 Cond = Cond.getNode()->getOperand(1);
7880 addTest = false;
7881 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00007882 }
Evan Cheng0488db92007-09-25 01:57:46 +00007883 }
Evan Cheng370e5342008-12-03 08:38:43 +00007884 } else {
7885 unsigned CondOpc;
7886 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
7887 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00007888 if (CondOpc == ISD::OR) {
7889 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
7890 // two branches instead of an explicit OR instruction with a
7891 // separate test.
7892 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00007893 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00007894 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007895 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00007896 Chain, Dest, CC, Cmp);
7897 CC = Cond.getOperand(1).getOperand(0);
7898 Cond = Cmp;
7899 addTest = false;
7900 }
7901 } else { // ISD::AND
7902 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
7903 // two branches instead of an explicit AND instruction with a
7904 // separate test. However, we only do this if this block doesn't
7905 // have a fall-through edge, because this requires an explicit
7906 // jmp when the condition is false.
7907 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00007908 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00007909 Op.getNode()->hasOneUse()) {
7910 X86::CondCode CCode =
7911 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
7912 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007913 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00007914 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00007915 // Look for an unconditional branch following this conditional branch.
7916 // We need this because we need to reverse the successors in order
7917 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00007918 if (User->getOpcode() == ISD::BR) {
7919 SDValue FalseBB = User->getOperand(1);
7920 SDNode *NewBR =
7921 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00007922 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00007923 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00007924 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00007925
Dale Johannesene4d209d2009-02-03 20:21:25 +00007926 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00007927 Chain, Dest, CC, Cmp);
7928 X86::CondCode CCode =
7929 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
7930 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007931 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00007932 Cond = Cmp;
7933 addTest = false;
7934 }
7935 }
Dan Gohman279c22e2008-10-21 03:29:32 +00007936 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00007937 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
7938 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
7939 // It should be transformed during dag combiner except when the condition
7940 // is set by a arithmetics with overflow node.
7941 X86::CondCode CCode =
7942 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
7943 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007944 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00007945 Cond = Cond.getOperand(0).getOperand(1);
7946 addTest = false;
Dan Gohman279c22e2008-10-21 03:29:32 +00007947 }
Evan Cheng0488db92007-09-25 01:57:46 +00007948 }
7949
7950 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007951 // Look pass the truncate.
7952 if (Cond.getOpcode() == ISD::TRUNCATE)
7953 Cond = Cond.getOperand(0);
7954
7955 // We know the result of AND is compared against zero. Try to match
7956 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00007957 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007958 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
7959 if (NewSetCC.getNode()) {
7960 CC = NewSetCC.getOperand(0);
7961 Cond = NewSetCC.getOperand(1);
7962 addTest = false;
7963 }
7964 }
7965 }
7966
7967 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007968 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00007969 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00007970 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00007971 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00007972 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00007973}
7974
Anton Korobeynikove060b532007-04-17 19:34:00 +00007975
7976// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
7977// Calls to _alloca is needed to probe the stack when allocating more than 4k
7978// bytes in one go. Touching the stack at 4K increments is necessary to ensure
7979// that the guard pages used by the OS virtual memory manager are allocated in
7980// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00007981SDValue
7982X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00007983 SelectionDAG &DAG) const {
Duncan Sands1e1ca0b2010-10-21 16:02:12 +00007984 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows()) &&
Michael J. Spencere9c253e2010-10-21 01:41:01 +00007985 "This should be used only on Windows targets");
NAKAMURA Takumia2e07622011-03-24 07:07:00 +00007986 assert(!Subtarget->isTargetEnvMacho());
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007987 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007988
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007989 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00007990 SDValue Chain = Op.getOperand(0);
7991 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007992 // FIXME: Ensure alignment here
7993
Dan Gohman475871a2008-07-27 21:46:04 +00007994 SDValue Flag;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007995
Owen Anderson825b72b2009-08-11 20:47:22 +00007996 EVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
NAKAMURA Takumia2e07622011-03-24 07:07:00 +00007997 unsigned Reg = (Subtarget->is64Bit() ? X86::RAX : X86::EAX);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007998
NAKAMURA Takumia2e07622011-03-24 07:07:00 +00007999 Chain = DAG.getCopyToReg(Chain, dl, Reg, Size, Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00008000 Flag = Chain.getValue(1);
8001
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008002 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00008003
Michael J. Spencere9c253e2010-10-21 01:41:01 +00008004 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00008005 Flag = Chain.getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008006
Dale Johannesendd64c412009-02-04 00:33:20 +00008007 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008008
Dan Gohman475871a2008-07-27 21:46:04 +00008009 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008010 return DAG.getMergeValues(Ops1, 2, dl);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008011}
8012
Dan Gohmand858e902010-04-17 15:26:15 +00008013SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00008014 MachineFunction &MF = DAG.getMachineFunction();
8015 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
8016
Dan Gohman69de1932008-02-06 22:27:42 +00008017 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00008018 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00008019
Anton Korobeynikove7beda12010-10-03 22:52:07 +00008020 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00008021 // vastart just stores the address of the VarArgsFrameIndex slot into the
8022 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00008023 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
8024 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00008025 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
8026 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008027 }
8028
8029 // __va_list_tag:
8030 // gp_offset (0 - 6 * 8)
8031 // fp_offset (48 - 48 + 8 * 16)
8032 // overflow_arg_area (point to parameters coming in memory).
8033 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00008034 SmallVector<SDValue, 8> MemOps;
8035 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00008036 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00008037 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00008038 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
8039 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008040 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008041 MemOps.push_back(Store);
8042
8043 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00008044 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008045 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008046 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00008047 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
8048 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008049 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008050 MemOps.push_back(Store);
8051
8052 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +00008053 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008054 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00008055 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
8056 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00008057 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
8058 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +00008059 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008060 MemOps.push_back(Store);
8061
8062 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +00008063 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008064 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00008065 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
8066 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00008067 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
8068 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008069 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +00008070 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00008071 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00008072}
8073
Dan Gohmand858e902010-04-17 15:26:15 +00008074SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +00008075 assert(Subtarget->is64Bit() &&
8076 "LowerVAARG only handles 64-bit va_arg!");
8077 assert((Subtarget->isTargetLinux() ||
8078 Subtarget->isTargetDarwin()) &&
8079 "Unhandled target in LowerVAARG");
8080 assert(Op.getNode()->getNumOperands() == 4);
8081 SDValue Chain = Op.getOperand(0);
8082 SDValue SrcPtr = Op.getOperand(1);
8083 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
8084 unsigned Align = Op.getConstantOperandVal(3);
8085 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +00008086
Dan Gohman320afb82010-10-12 18:00:49 +00008087 EVT ArgVT = Op.getNode()->getValueType(0);
8088 const Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
8089 uint32_t ArgSize = getTargetData()->getTypeAllocSize(ArgTy);
8090 uint8_t ArgMode;
8091
8092 // Decide which area this value should be read from.
8093 // TODO: Implement the AMD64 ABI in its entirety. This simple
8094 // selection mechanism works only for the basic types.
8095 if (ArgVT == MVT::f80) {
8096 llvm_unreachable("va_arg for f80 not yet implemented");
8097 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
8098 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
8099 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
8100 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
8101 } else {
8102 llvm_unreachable("Unhandled argument type in LowerVAARG");
8103 }
8104
8105 if (ArgMode == 2) {
8106 // Sanity Check: Make sure using fp_offset makes sense.
Michael J. Spencer87b86652010-10-19 07:32:42 +00008107 assert(!UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +00008108 !(DAG.getMachineFunction()
8109 .getFunction()->hasFnAttr(Attribute::NoImplicitFloat)) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00008110 Subtarget->hasXMM());
Dan Gohman320afb82010-10-12 18:00:49 +00008111 }
8112
8113 // Insert VAARG_64 node into the DAG
8114 // VAARG_64 returns two values: Variable Argument Address, Chain
8115 SmallVector<SDValue, 11> InstOps;
8116 InstOps.push_back(Chain);
8117 InstOps.push_back(SrcPtr);
8118 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
8119 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
8120 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
8121 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
8122 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
8123 VTs, &InstOps[0], InstOps.size(),
8124 MVT::i64,
8125 MachinePointerInfo(SV),
8126 /*Align=*/0,
8127 /*Volatile=*/false,
8128 /*ReadMem=*/true,
8129 /*WriteMem=*/true);
8130 Chain = VAARG.getValue(1);
8131
8132 // Load the next argument and return it
8133 return DAG.getLoad(ArgVT, dl,
8134 Chain,
8135 VAARG,
8136 MachinePointerInfo(),
8137 false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +00008138}
8139
Dan Gohmand858e902010-04-17 15:26:15 +00008140SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) const {
Evan Chengae642192007-03-02 23:16:35 +00008141 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00008142 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00008143 SDValue Chain = Op.getOperand(0);
8144 SDValue DstPtr = Op.getOperand(1);
8145 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00008146 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
8147 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +00008148 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00008149
Chris Lattnere72f2022010-09-21 05:40:29 +00008150 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00008151 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +00008152 false,
Chris Lattnere72f2022010-09-21 05:40:29 +00008153 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +00008154}
8155
Dan Gohman475871a2008-07-27 21:46:04 +00008156SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00008157X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008158 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00008159 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00008160 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00008161 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00008162 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00008163 case Intrinsic::x86_sse_comieq_ss:
8164 case Intrinsic::x86_sse_comilt_ss:
8165 case Intrinsic::x86_sse_comile_ss:
8166 case Intrinsic::x86_sse_comigt_ss:
8167 case Intrinsic::x86_sse_comige_ss:
8168 case Intrinsic::x86_sse_comineq_ss:
8169 case Intrinsic::x86_sse_ucomieq_ss:
8170 case Intrinsic::x86_sse_ucomilt_ss:
8171 case Intrinsic::x86_sse_ucomile_ss:
8172 case Intrinsic::x86_sse_ucomigt_ss:
8173 case Intrinsic::x86_sse_ucomige_ss:
8174 case Intrinsic::x86_sse_ucomineq_ss:
8175 case Intrinsic::x86_sse2_comieq_sd:
8176 case Intrinsic::x86_sse2_comilt_sd:
8177 case Intrinsic::x86_sse2_comile_sd:
8178 case Intrinsic::x86_sse2_comigt_sd:
8179 case Intrinsic::x86_sse2_comige_sd:
8180 case Intrinsic::x86_sse2_comineq_sd:
8181 case Intrinsic::x86_sse2_ucomieq_sd:
8182 case Intrinsic::x86_sse2_ucomilt_sd:
8183 case Intrinsic::x86_sse2_ucomile_sd:
8184 case Intrinsic::x86_sse2_ucomigt_sd:
8185 case Intrinsic::x86_sse2_ucomige_sd:
8186 case Intrinsic::x86_sse2_ucomineq_sd: {
8187 unsigned Opc = 0;
8188 ISD::CondCode CC = ISD::SETCC_INVALID;
8189 switch (IntNo) {
8190 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008191 case Intrinsic::x86_sse_comieq_ss:
8192 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008193 Opc = X86ISD::COMI;
8194 CC = ISD::SETEQ;
8195 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00008196 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008197 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008198 Opc = X86ISD::COMI;
8199 CC = ISD::SETLT;
8200 break;
8201 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008202 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008203 Opc = X86ISD::COMI;
8204 CC = ISD::SETLE;
8205 break;
8206 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008207 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008208 Opc = X86ISD::COMI;
8209 CC = ISD::SETGT;
8210 break;
8211 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008212 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008213 Opc = X86ISD::COMI;
8214 CC = ISD::SETGE;
8215 break;
8216 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008217 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008218 Opc = X86ISD::COMI;
8219 CC = ISD::SETNE;
8220 break;
8221 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008222 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008223 Opc = X86ISD::UCOMI;
8224 CC = ISD::SETEQ;
8225 break;
8226 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008227 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008228 Opc = X86ISD::UCOMI;
8229 CC = ISD::SETLT;
8230 break;
8231 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008232 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008233 Opc = X86ISD::UCOMI;
8234 CC = ISD::SETLE;
8235 break;
8236 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008237 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008238 Opc = X86ISD::UCOMI;
8239 CC = ISD::SETGT;
8240 break;
8241 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008242 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008243 Opc = X86ISD::UCOMI;
8244 CC = ISD::SETGE;
8245 break;
8246 case Intrinsic::x86_sse_ucomineq_ss:
8247 case Intrinsic::x86_sse2_ucomineq_sd:
8248 Opc = X86ISD::UCOMI;
8249 CC = ISD::SETNE;
8250 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00008251 }
Evan Cheng734503b2006-09-11 02:19:56 +00008252
Dan Gohman475871a2008-07-27 21:46:04 +00008253 SDValue LHS = Op.getOperand(1);
8254 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00008255 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00008256 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00008257 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
8258 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8259 DAG.getConstant(X86CC, MVT::i8), Cond);
8260 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00008261 }
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008262 // ptest and testp intrinsics. The intrinsic these come from are designed to
8263 // return an integer value, not just an instruction so lower it to the ptest
8264 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00008265 case Intrinsic::x86_sse41_ptestz:
8266 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008267 case Intrinsic::x86_sse41_ptestnzc:
8268 case Intrinsic::x86_avx_ptestz_256:
8269 case Intrinsic::x86_avx_ptestc_256:
8270 case Intrinsic::x86_avx_ptestnzc_256:
8271 case Intrinsic::x86_avx_vtestz_ps:
8272 case Intrinsic::x86_avx_vtestc_ps:
8273 case Intrinsic::x86_avx_vtestnzc_ps:
8274 case Intrinsic::x86_avx_vtestz_pd:
8275 case Intrinsic::x86_avx_vtestc_pd:
8276 case Intrinsic::x86_avx_vtestnzc_pd:
8277 case Intrinsic::x86_avx_vtestz_ps_256:
8278 case Intrinsic::x86_avx_vtestc_ps_256:
8279 case Intrinsic::x86_avx_vtestnzc_ps_256:
8280 case Intrinsic::x86_avx_vtestz_pd_256:
8281 case Intrinsic::x86_avx_vtestc_pd_256:
8282 case Intrinsic::x86_avx_vtestnzc_pd_256: {
8283 bool IsTestPacked = false;
Eric Christopher71c67532009-07-29 00:28:05 +00008284 unsigned X86CC = 0;
8285 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00008286 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008287 case Intrinsic::x86_avx_vtestz_ps:
8288 case Intrinsic::x86_avx_vtestz_pd:
8289 case Intrinsic::x86_avx_vtestz_ps_256:
8290 case Intrinsic::x86_avx_vtestz_pd_256:
8291 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00008292 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008293 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +00008294 // ZF = 1
8295 X86CC = X86::COND_E;
8296 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008297 case Intrinsic::x86_avx_vtestc_ps:
8298 case Intrinsic::x86_avx_vtestc_pd:
8299 case Intrinsic::x86_avx_vtestc_ps_256:
8300 case Intrinsic::x86_avx_vtestc_pd_256:
8301 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00008302 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008303 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00008304 // CF = 1
8305 X86CC = X86::COND_B;
8306 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008307 case Intrinsic::x86_avx_vtestnzc_ps:
8308 case Intrinsic::x86_avx_vtestnzc_pd:
8309 case Intrinsic::x86_avx_vtestnzc_ps_256:
8310 case Intrinsic::x86_avx_vtestnzc_pd_256:
8311 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +00008312 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008313 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00008314 // ZF and CF = 0
8315 X86CC = X86::COND_A;
8316 break;
8317 }
Eric Christopherfd179292009-08-27 18:07:15 +00008318
Eric Christopher71c67532009-07-29 00:28:05 +00008319 SDValue LHS = Op.getOperand(1);
8320 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008321 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
8322 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00008323 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
8324 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
8325 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00008326 }
Evan Cheng5759f972008-05-04 09:15:50 +00008327
8328 // Fix vector shift instructions where the last operand is a non-immediate
8329 // i32 value.
8330 case Intrinsic::x86_sse2_pslli_w:
8331 case Intrinsic::x86_sse2_pslli_d:
8332 case Intrinsic::x86_sse2_pslli_q:
8333 case Intrinsic::x86_sse2_psrli_w:
8334 case Intrinsic::x86_sse2_psrli_d:
8335 case Intrinsic::x86_sse2_psrli_q:
8336 case Intrinsic::x86_sse2_psrai_w:
8337 case Intrinsic::x86_sse2_psrai_d:
8338 case Intrinsic::x86_mmx_pslli_w:
8339 case Intrinsic::x86_mmx_pslli_d:
8340 case Intrinsic::x86_mmx_pslli_q:
8341 case Intrinsic::x86_mmx_psrli_w:
8342 case Intrinsic::x86_mmx_psrli_d:
8343 case Intrinsic::x86_mmx_psrli_q:
8344 case Intrinsic::x86_mmx_psrai_w:
8345 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00008346 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00008347 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00008348 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00008349
8350 unsigned NewIntNo = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00008351 EVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00008352 switch (IntNo) {
8353 case Intrinsic::x86_sse2_pslli_w:
8354 NewIntNo = Intrinsic::x86_sse2_psll_w;
8355 break;
8356 case Intrinsic::x86_sse2_pslli_d:
8357 NewIntNo = Intrinsic::x86_sse2_psll_d;
8358 break;
8359 case Intrinsic::x86_sse2_pslli_q:
8360 NewIntNo = Intrinsic::x86_sse2_psll_q;
8361 break;
8362 case Intrinsic::x86_sse2_psrli_w:
8363 NewIntNo = Intrinsic::x86_sse2_psrl_w;
8364 break;
8365 case Intrinsic::x86_sse2_psrli_d:
8366 NewIntNo = Intrinsic::x86_sse2_psrl_d;
8367 break;
8368 case Intrinsic::x86_sse2_psrli_q:
8369 NewIntNo = Intrinsic::x86_sse2_psrl_q;
8370 break;
8371 case Intrinsic::x86_sse2_psrai_w:
8372 NewIntNo = Intrinsic::x86_sse2_psra_w;
8373 break;
8374 case Intrinsic::x86_sse2_psrai_d:
8375 NewIntNo = Intrinsic::x86_sse2_psra_d;
8376 break;
8377 default: {
Owen Anderson825b72b2009-08-11 20:47:22 +00008378 ShAmtVT = MVT::v2i32;
Evan Cheng5759f972008-05-04 09:15:50 +00008379 switch (IntNo) {
8380 case Intrinsic::x86_mmx_pslli_w:
8381 NewIntNo = Intrinsic::x86_mmx_psll_w;
8382 break;
8383 case Intrinsic::x86_mmx_pslli_d:
8384 NewIntNo = Intrinsic::x86_mmx_psll_d;
8385 break;
8386 case Intrinsic::x86_mmx_pslli_q:
8387 NewIntNo = Intrinsic::x86_mmx_psll_q;
8388 break;
8389 case Intrinsic::x86_mmx_psrli_w:
8390 NewIntNo = Intrinsic::x86_mmx_psrl_w;
8391 break;
8392 case Intrinsic::x86_mmx_psrli_d:
8393 NewIntNo = Intrinsic::x86_mmx_psrl_d;
8394 break;
8395 case Intrinsic::x86_mmx_psrli_q:
8396 NewIntNo = Intrinsic::x86_mmx_psrl_q;
8397 break;
8398 case Intrinsic::x86_mmx_psrai_w:
8399 NewIntNo = Intrinsic::x86_mmx_psra_w;
8400 break;
8401 case Intrinsic::x86_mmx_psrai_d:
8402 NewIntNo = Intrinsic::x86_mmx_psra_d;
8403 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00008404 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00008405 }
8406 break;
8407 }
8408 }
Mon P Wangefa42202009-09-03 19:56:25 +00008409
8410 // The vector shift intrinsics with scalars uses 32b shift amounts but
8411 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
8412 // to be zero.
8413 SDValue ShOps[4];
8414 ShOps[0] = ShAmt;
8415 ShOps[1] = DAG.getConstant(0, MVT::i32);
8416 if (ShAmtVT == MVT::v4i32) {
8417 ShOps[2] = DAG.getUNDEF(MVT::i32);
8418 ShOps[3] = DAG.getUNDEF(MVT::i32);
8419 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
8420 } else {
8421 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
Dale Johannesen0488fb62010-09-30 23:57:10 +00008422// FIXME this must be lowered to get rid of the invalid type.
Mon P Wangefa42202009-09-03 19:56:25 +00008423 }
8424
Owen Andersone50ed302009-08-10 22:56:29 +00008425 EVT VT = Op.getValueType();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008426 ShAmt = DAG.getNode(ISD::BITCAST, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008427 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008428 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00008429 Op.getOperand(1), ShAmt);
8430 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00008431 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008432}
Evan Cheng72261582005-12-20 06:22:03 +00008433
Dan Gohmand858e902010-04-17 15:26:15 +00008434SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
8435 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +00008436 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
8437 MFI->setReturnAddressIsTaken(true);
8438
Bill Wendling64e87322009-01-16 19:25:27 +00008439 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008440 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00008441
8442 if (Depth > 0) {
8443 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
8444 SDValue Offset =
8445 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00008446 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008447 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00008448 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008449 FrameAddr, Offset),
Chris Lattner51abfe42010-09-21 06:02:19 +00008450 MachinePointerInfo(), false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +00008451 }
8452
8453 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00008454 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00008455 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Chris Lattner51abfe42010-09-21 06:02:19 +00008456 RetAddrFI, MachinePointerInfo(), false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00008457}
8458
Dan Gohmand858e902010-04-17 15:26:15 +00008459SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +00008460 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
8461 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00008462
Owen Andersone50ed302009-08-10 22:56:29 +00008463 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008464 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00008465 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
8466 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00008467 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00008468 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +00008469 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
8470 MachinePointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +00008471 false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00008472 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00008473}
8474
Dan Gohman475871a2008-07-27 21:46:04 +00008475SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008476 SelectionDAG &DAG) const {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00008477 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008478}
8479
Dan Gohmand858e902010-04-17 15:26:15 +00008480SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008481 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00008482 SDValue Chain = Op.getOperand(0);
8483 SDValue Offset = Op.getOperand(1);
8484 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008485 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008486
Dan Gohmand8816272010-08-11 18:14:00 +00008487 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
8488 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
8489 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008490 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008491
Dan Gohmand8816272010-08-11 18:14:00 +00008492 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
8493 DAG.getIntPtrConstant(TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008494 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +00008495 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
8496 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00008497 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008498 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008499
Dale Johannesene4d209d2009-02-03 20:21:25 +00008500 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008501 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008502 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008503}
8504
Dan Gohman475871a2008-07-27 21:46:04 +00008505SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008506 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008507 SDValue Root = Op.getOperand(0);
8508 SDValue Trmp = Op.getOperand(1); // trampoline
8509 SDValue FPtr = Op.getOperand(2); // nested function
8510 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008511 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008512
Dan Gohman69de1932008-02-06 22:27:42 +00008513 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008514
8515 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00008516 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00008517
8518 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +00008519 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
8520 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +00008521
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00008522 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
8523 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00008524
8525 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
8526
8527 // Load the pointer to the nested function into R11.
8528 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00008529 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00008530 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008531 Addr, MachinePointerInfo(TrmpAddr),
8532 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008533
Owen Anderson825b72b2009-08-11 20:47:22 +00008534 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8535 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008536 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
8537 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +00008538 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00008539
8540 // Load the 'nest' parameter value into R10.
8541 // R10 is specified in X86CallingConv.td
8542 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +00008543 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8544 DAG.getConstant(10, MVT::i64));
8545 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008546 Addr, MachinePointerInfo(TrmpAddr, 10),
8547 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008548
Owen Anderson825b72b2009-08-11 20:47:22 +00008549 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8550 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008551 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
8552 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +00008553 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00008554
8555 // Jump to the nested function.
8556 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +00008557 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8558 DAG.getConstant(20, MVT::i64));
8559 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008560 Addr, MachinePointerInfo(TrmpAddr, 20),
8561 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008562
8563 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +00008564 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8565 DAG.getConstant(22, MVT::i64));
8566 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00008567 MachinePointerInfo(TrmpAddr, 22),
8568 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008569
Dan Gohman475871a2008-07-27 21:46:04 +00008570 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00008571 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008572 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008573 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00008574 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00008575 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00008576 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00008577 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008578
8579 switch (CC) {
8580 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00008581 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00008582 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00008583 case CallingConv::X86_StdCall: {
8584 // Pass 'nest' parameter in ECX.
8585 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00008586 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008587
8588 // Check that ECX wasn't needed by an 'inreg' parameter.
8589 const FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00008590 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008591
Chris Lattner58d74912008-03-12 17:45:29 +00008592 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00008593 unsigned InRegCount = 0;
8594 unsigned Idx = 1;
8595
8596 for (FunctionType::param_iterator I = FTy->param_begin(),
8597 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00008598 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00008599 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00008600 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008601
8602 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +00008603 report_fatal_error("Nest register in use - reduce number of inreg"
8604 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00008605 }
8606 }
8607 break;
8608 }
8609 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +00008610 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00008611 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00008612 // Pass 'nest' parameter in EAX.
8613 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00008614 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008615 break;
8616 }
8617
Dan Gohman475871a2008-07-27 21:46:04 +00008618 SDValue OutChains[4];
8619 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008620
Owen Anderson825b72b2009-08-11 20:47:22 +00008621 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8622 DAG.getConstant(10, MVT::i32));
8623 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008624
Chris Lattnera62fe662010-02-05 19:20:30 +00008625 // This is storing the opcode for MOV32ri.
8626 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00008627 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00008628 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008629 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008630 Trmp, MachinePointerInfo(TrmpAddr),
8631 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008632
Owen Anderson825b72b2009-08-11 20:47:22 +00008633 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8634 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008635 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
8636 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +00008637 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008638
Chris Lattnera62fe662010-02-05 19:20:30 +00008639 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +00008640 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8641 DAG.getConstant(5, MVT::i32));
8642 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00008643 MachinePointerInfo(TrmpAddr, 5),
8644 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008645
Owen Anderson825b72b2009-08-11 20:47:22 +00008646 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8647 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008648 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
8649 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +00008650 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008651
Dan Gohman475871a2008-07-27 21:46:04 +00008652 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00008653 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008654 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008655 }
8656}
8657
Dan Gohmand858e902010-04-17 15:26:15 +00008658SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
8659 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008660 /*
8661 The rounding mode is in bits 11:10 of FPSR, and has the following
8662 settings:
8663 00 Round to nearest
8664 01 Round to -inf
8665 10 Round to +inf
8666 11 Round to 0
8667
8668 FLT_ROUNDS, on the other hand, expects the following:
8669 -1 Undefined
8670 0 Round to 0
8671 1 Round to nearest
8672 2 Round to +inf
8673 3 Round to -inf
8674
8675 To perform the conversion, we do:
8676 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
8677 */
8678
8679 MachineFunction &MF = DAG.getMachineFunction();
8680 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00008681 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008682 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00008683 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +00008684 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008685
8686 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00008687 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +00008688 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008689
Michael J. Spencerec38de22010-10-10 22:04:20 +00008690
Chris Lattner2156b792010-09-22 01:11:26 +00008691 MachineMemOperand *MMO =
8692 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8693 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008694
Chris Lattner2156b792010-09-22 01:11:26 +00008695 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
8696 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
8697 DAG.getVTList(MVT::Other),
8698 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008699
8700 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +00008701 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Chris Lattner51abfe42010-09-21 06:02:19 +00008702 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008703
8704 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00008705 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +00008706 DAG.getNode(ISD::SRL, DL, MVT::i16,
8707 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00008708 CWD, DAG.getConstant(0x800, MVT::i16)),
8709 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00008710 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +00008711 DAG.getNode(ISD::SRL, DL, MVT::i16,
8712 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00008713 CWD, DAG.getConstant(0x400, MVT::i16)),
8714 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008715
Dan Gohman475871a2008-07-27 21:46:04 +00008716 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +00008717 DAG.getNode(ISD::AND, DL, MVT::i16,
8718 DAG.getNode(ISD::ADD, DL, MVT::i16,
8719 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +00008720 DAG.getConstant(1, MVT::i16)),
8721 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008722
8723
Duncan Sands83ec4b62008-06-06 12:08:01 +00008724 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +00008725 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008726}
8727
Dan Gohmand858e902010-04-17 15:26:15 +00008728SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008729 EVT VT = Op.getValueType();
8730 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008731 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008732 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00008733
8734 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008735 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00008736 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +00008737 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00008738 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008739 }
Evan Cheng18efe262007-12-14 02:13:44 +00008740
Evan Cheng152804e2007-12-14 08:30:15 +00008741 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008742 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008743 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00008744
8745 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00008746 SDValue Ops[] = {
8747 Op,
8748 DAG.getConstant(NumBits+NumBits-1, OpVT),
8749 DAG.getConstant(X86::COND_E, MVT::i8),
8750 Op.getValue(1)
8751 };
8752 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00008753
8754 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00008755 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00008756
Owen Anderson825b72b2009-08-11 20:47:22 +00008757 if (VT == MVT::i8)
8758 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008759 return Op;
8760}
8761
Dan Gohmand858e902010-04-17 15:26:15 +00008762SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008763 EVT VT = Op.getValueType();
8764 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008765 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008766 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00008767
8768 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008769 if (VT == MVT::i8) {
8770 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00008771 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008772 }
Evan Cheng152804e2007-12-14 08:30:15 +00008773
8774 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008775 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008776 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00008777
8778 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00008779 SDValue Ops[] = {
8780 Op,
8781 DAG.getConstant(NumBits, OpVT),
8782 DAG.getConstant(X86::COND_E, MVT::i8),
8783 Op.getValue(1)
8784 };
8785 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00008786
Owen Anderson825b72b2009-08-11 20:47:22 +00008787 if (VT == MVT::i8)
8788 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008789 return Op;
8790}
8791
Dan Gohmand858e902010-04-17 15:26:15 +00008792SDValue X86TargetLowering::LowerMUL_V2I64(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008793 EVT VT = Op.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00008794 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008795 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00008796
Mon P Wangaf9b9522008-12-18 21:42:19 +00008797 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
8798 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
8799 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
8800 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
8801 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
8802 //
8803 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
8804 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
8805 // return AloBlo + AloBhi + AhiBlo;
8806
8807 SDValue A = Op.getOperand(0);
8808 SDValue B = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008809
Dale Johannesene4d209d2009-02-03 20:21:25 +00008810 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008811 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8812 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008813 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008814 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8815 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008816 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008817 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008818 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008819 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008820 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008821 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008822 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008823 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008824 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008825 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008826 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8827 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008828 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008829 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8830 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008831 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
8832 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +00008833 return Res;
8834}
8835
Nadav Rotem43012222011-05-11 08:12:09 +00008836SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
8837
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008838 EVT VT = Op.getValueType();
8839 DebugLoc dl = Op.getDebugLoc();
8840 SDValue R = Op.getOperand(0);
Nadav Rotem43012222011-05-11 08:12:09 +00008841 SDValue Amt = Op.getOperand(1);
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008842
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008843 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008844
Nadav Rotem43012222011-05-11 08:12:09 +00008845 // Must have SSE2.
8846 if (!Subtarget->hasSSE2()) return SDValue();
Nate Begeman51409212010-07-28 00:21:48 +00008847
Nadav Rotem43012222011-05-11 08:12:09 +00008848 // Optimize shl/srl/sra with constant shift amount.
8849 if (isSplatVector(Amt.getNode())) {
8850 SDValue SclrAmt = Amt->getOperand(0);
8851 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SclrAmt)) {
8852 uint64_t ShiftAmt = C->getZExtValue();
8853
8854 if (VT == MVT::v2i64 && Op.getOpcode() == ISD::SHL)
8855 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8856 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8857 R, DAG.getConstant(ShiftAmt, MVT::i32));
8858
8859 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SHL)
8860 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8861 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
8862 R, DAG.getConstant(ShiftAmt, MVT::i32));
8863
8864 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SHL)
8865 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8866 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
8867 R, DAG.getConstant(ShiftAmt, MVT::i32));
8868
8869 if (VT == MVT::v2i64 && Op.getOpcode() == ISD::SRL)
8870 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8871 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8872 R, DAG.getConstant(ShiftAmt, MVT::i32));
8873
8874 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SRL)
8875 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8876 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
8877 R, DAG.getConstant(ShiftAmt, MVT::i32));
8878
8879 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SRL)
8880 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8881 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
8882 R, DAG.getConstant(ShiftAmt, MVT::i32));
8883
8884 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SRA)
8885 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8886 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
8887 R, DAG.getConstant(ShiftAmt, MVT::i32));
8888
8889 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SRA)
8890 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8891 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
8892 R, DAG.getConstant(ShiftAmt, MVT::i32));
8893 }
8894 }
8895
8896 // Lower SHL with variable shift amount.
8897 // Cannot lower SHL without SSE4.1 or later.
8898 if (!Subtarget->hasSSE41()) return SDValue();
8899
8900 if (VT == MVT::v4i32 && Op->getOpcode() == ISD::SHL) {
Nate Begeman51409212010-07-28 00:21:48 +00008901 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8902 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
8903 Op.getOperand(1), DAG.getConstant(23, MVT::i32));
8904
8905 ConstantInt *CI = ConstantInt::get(*Context, APInt(32, 0x3f800000U));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008906
Nate Begeman51409212010-07-28 00:21:48 +00008907 std::vector<Constant*> CV(4, CI);
8908 Constant *C = ConstantVector::get(CV);
8909 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8910 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008911 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00008912 false, false, 16);
8913
8914 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008915 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +00008916 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
8917 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
8918 }
Nadav Rotem43012222011-05-11 08:12:09 +00008919 if (VT == MVT::v16i8 && Op->getOpcode() == ISD::SHL) {
Nate Begeman51409212010-07-28 00:21:48 +00008920 // a = a << 5;
8921 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8922 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
8923 Op.getOperand(1), DAG.getConstant(5, MVT::i32));
8924
8925 ConstantInt *CM1 = ConstantInt::get(*Context, APInt(8, 15));
8926 ConstantInt *CM2 = ConstantInt::get(*Context, APInt(8, 63));
8927
8928 std::vector<Constant*> CVM1(16, CM1);
8929 std::vector<Constant*> CVM2(16, CM2);
8930 Constant *C = ConstantVector::get(CVM1);
8931 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8932 SDValue M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008933 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00008934 false, false, 16);
8935
8936 // r = pblendv(r, psllw(r & (char16)15, 4), a);
8937 M = DAG.getNode(ISD::AND, dl, VT, R, M);
8938 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8939 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
8940 DAG.getConstant(4, MVT::i32));
Nate Begeman672fb622010-12-20 22:04:24 +00008941 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT, R, M, Op);
Nate Begeman51409212010-07-28 00:21:48 +00008942 // a += a
8943 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008944
Nate Begeman51409212010-07-28 00:21:48 +00008945 C = ConstantVector::get(CVM2);
8946 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8947 M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008948 MachinePointerInfo::getConstantPool(),
Chris Lattner51abfe42010-09-21 06:02:19 +00008949 false, false, 16);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008950
Nate Begeman51409212010-07-28 00:21:48 +00008951 // r = pblendv(r, psllw(r & (char16)63, 2), a);
8952 M = DAG.getNode(ISD::AND, dl, VT, R, M);
8953 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8954 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
8955 DAG.getConstant(2, MVT::i32));
Nate Begeman672fb622010-12-20 22:04:24 +00008956 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT, R, M, Op);
Nate Begeman51409212010-07-28 00:21:48 +00008957 // a += a
8958 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008959
Nate Begeman51409212010-07-28 00:21:48 +00008960 // return pblendv(r, r+r, a);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008961 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT,
Nate Begeman51409212010-07-28 00:21:48 +00008962 R, DAG.getNode(ISD::ADD, dl, VT, R, R), Op);
8963 return R;
8964 }
8965 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008966}
Mon P Wangaf9b9522008-12-18 21:42:19 +00008967
Dan Gohmand858e902010-04-17 15:26:15 +00008968SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
Bill Wendling74c37652008-12-09 22:08:41 +00008969 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
8970 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +00008971 // looks for this combo and may remove the "setcc" instruction if the "setcc"
8972 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +00008973 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +00008974 SDValue LHS = N->getOperand(0);
8975 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +00008976 unsigned BaseOp = 0;
8977 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008978 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +00008979 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008980 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +00008981 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +00008982 // A subtract of one will be selected as a INC. Note that INC doesn't
8983 // set CF, so we can't do this for UADDO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +00008984 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
8985 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +00008986 BaseOp = X86ISD::INC;
8987 Cond = X86::COND_O;
8988 break;
8989 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008990 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +00008991 Cond = X86::COND_O;
8992 break;
8993 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008994 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +00008995 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00008996 break;
8997 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +00008998 // A subtract of one will be selected as a DEC. Note that DEC doesn't
8999 // set CF, so we can't do this for USUBO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +00009000 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
9001 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +00009002 BaseOp = X86ISD::DEC;
9003 Cond = X86::COND_O;
9004 break;
9005 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00009006 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +00009007 Cond = X86::COND_O;
9008 break;
9009 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00009010 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +00009011 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00009012 break;
9013 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00009014 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +00009015 Cond = X86::COND_O;
9016 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +00009017 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
9018 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
9019 MVT::i32);
9020 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009021
Chris Lattnerb20e0b12010-12-05 07:30:36 +00009022 SDValue SetCC =
9023 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
9024 DAG.getConstant(X86::COND_O, MVT::i32),
9025 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009026
Chris Lattnerb20e0b12010-12-05 07:30:36 +00009027 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
9028 return Sum;
9029 }
Bill Wendling74c37652008-12-09 22:08:41 +00009030 }
Bill Wendling3fafd932008-11-26 22:37:40 +00009031
Bill Wendling61edeb52008-12-02 01:06:39 +00009032 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00009033 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +00009034 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +00009035
Bill Wendling61edeb52008-12-02 01:06:39 +00009036 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +00009037 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
9038 DAG.getConstant(Cond, MVT::i32),
9039 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +00009040
Bill Wendling61edeb52008-12-02 01:06:39 +00009041 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
9042 return Sum;
Bill Wendling41ea7e72008-11-24 19:21:46 +00009043}
9044
Eric Christopher9a9d2752010-07-22 02:48:34 +00009045SDValue X86TargetLowering::LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG) const{
9046 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00009047
Eric Christopherb6729dc2010-08-04 23:03:04 +00009048 if (!Subtarget->hasSSE2()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +00009049 SDValue Chain = Op.getOperand(0);
Michael J. Spencerec38de22010-10-10 22:04:20 +00009050 SDValue Zero = DAG.getConstant(0,
Eric Christopherb6729dc2010-08-04 23:03:04 +00009051 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +00009052 SDValue Ops[] = {
9053 DAG.getRegister(X86::ESP, MVT::i32), // Base
9054 DAG.getTargetConstant(1, MVT::i8), // Scale
9055 DAG.getRegister(0, MVT::i32), // Index
9056 DAG.getTargetConstant(0, MVT::i32), // Disp
9057 DAG.getRegister(0, MVT::i32), // Segment.
9058 Zero,
9059 Chain
9060 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00009061 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +00009062 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
9063 array_lengthof(Ops));
9064 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +00009065 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00009066
Eric Christopher9a9d2752010-07-22 02:48:34 +00009067 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +00009068 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +00009069 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009070
Chris Lattner132929a2010-08-14 17:26:09 +00009071 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
9072 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
9073 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
9074 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +00009075
Chris Lattner132929a2010-08-14 17:26:09 +00009076 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
9077 if (!Op1 && !Op2 && !Op3 && Op4)
9078 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009079
Chris Lattner132929a2010-08-14 17:26:09 +00009080 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
9081 if (Op1 && !Op2 && !Op3 && !Op4)
9082 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009083
9084 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +00009085 // (MFENCE)>;
9086 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +00009087}
9088
Dan Gohmand858e902010-04-17 15:26:15 +00009089SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009090 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +00009091 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +00009092 unsigned Reg = 0;
9093 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00009094 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00009095 default:
9096 assert(false && "Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +00009097 case MVT::i8: Reg = X86::AL; size = 1; break;
9098 case MVT::i16: Reg = X86::AX; size = 2; break;
9099 case MVT::i32: Reg = X86::EAX; size = 4; break;
9100 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +00009101 assert(Subtarget->is64Bit() && "Node not type legal!");
9102 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00009103 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00009104 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +00009105 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +00009106 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +00009107 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +00009108 Op.getOperand(1),
9109 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +00009110 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +00009111 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009112 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +00009113 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
9114 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
9115 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +00009116 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +00009117 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +00009118 return cpOut;
9119}
9120
Duncan Sands1607f052008-12-01 11:39:25 +00009121SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00009122 SelectionDAG &DAG) const {
Duncan Sands1607f052008-12-01 11:39:25 +00009123 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009124 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +00009125 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009126 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +00009127 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00009128 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
9129 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +00009130 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +00009131 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
9132 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +00009133 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +00009134 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +00009135 rdx.getValue(1)
9136 };
Dale Johannesene4d209d2009-02-03 20:21:25 +00009137 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009138}
9139
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009140SDValue X86TargetLowering::LowerBITCAST(SDValue Op,
Dale Johannesen7d07b482010-05-21 00:52:33 +00009141 SelectionDAG &DAG) const {
9142 EVT SrcVT = Op.getOperand(0).getValueType();
9143 EVT DstVT = Op.getValueType();
Chris Lattner2a786eb2010-12-19 20:19:20 +00009144 assert(Subtarget->is64Bit() && !Subtarget->hasSSE2() &&
9145 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +00009146 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +00009147 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009148 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +00009149 // i64 <=> MMX conversions are Legal.
9150 if (SrcVT==MVT::i64 && DstVT.isVector())
9151 return Op;
9152 if (DstVT==MVT::i64 && SrcVT.isVector())
9153 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +00009154 // MMX <=> MMX conversions are Legal.
9155 if (SrcVT.isVector() && DstVT.isVector())
9156 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +00009157 // All other conversions need to be expanded.
9158 return SDValue();
9159}
Chris Lattner5b856542010-12-20 00:59:46 +00009160
Dan Gohmand858e902010-04-17 15:26:15 +00009161SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen71d1bf52008-09-29 22:25:26 +00009162 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +00009163 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00009164 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009165 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +00009166 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009167 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009168 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +00009169 Node->getOperand(0),
9170 Node->getOperand(1), negOp,
9171 cast<AtomicSDNode>(Node)->getSrcValue(),
9172 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang63307c32008-05-05 19:05:59 +00009173}
9174
Chris Lattner5b856542010-12-20 00:59:46 +00009175static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
9176 EVT VT = Op.getNode()->getValueType(0);
9177
9178 // Let legalize expand this if it isn't a legal type yet.
9179 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
9180 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009181
Chris Lattner5b856542010-12-20 00:59:46 +00009182 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009183
Chris Lattner5b856542010-12-20 00:59:46 +00009184 unsigned Opc;
9185 bool ExtraOp = false;
9186 switch (Op.getOpcode()) {
9187 default: assert(0 && "Invalid code");
9188 case ISD::ADDC: Opc = X86ISD::ADD; break;
9189 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
9190 case ISD::SUBC: Opc = X86ISD::SUB; break;
9191 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
9192 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009193
Chris Lattner5b856542010-12-20 00:59:46 +00009194 if (!ExtraOp)
9195 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
9196 Op.getOperand(1));
9197 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
9198 Op.getOperand(1), Op.getOperand(2));
9199}
9200
Evan Cheng0db9fe62006-04-25 20:13:52 +00009201/// LowerOperation - Provide custom lowering hooks for some operations.
9202///
Dan Gohmand858e902010-04-17 15:26:15 +00009203SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00009204 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00009205 default: llvm_unreachable("Should not custom lower this!");
Eric Christopher9a9d2752010-07-22 02:48:34 +00009206 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op,DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009207 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
9208 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009209 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00009210 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009211 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
9212 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
9213 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
David Greene91585092011-01-26 15:38:49 +00009214 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
David Greenecfe33c42011-01-26 19:13:22 +00009215 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009216 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
9217 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
9218 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00009219 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +00009220 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +00009221 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009222 case ISD::SHL_PARTS:
9223 case ISD::SRA_PARTS:
Nadav Rotem43012222011-05-11 08:12:09 +00009224 case ISD::SRL_PARTS: return LowerShiftParts(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009225 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00009226 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009227 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00009228 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009229 case ISD::FABS: return LowerFABS(Op, DAG);
9230 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00009231 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00009232 case ISD::FGETSIGN: return LowerFGETSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00009233 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman30a0de92008-07-17 16:51:19 +00009234 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00009235 case ISD::SELECT: return LowerSELECT(Op, DAG);
9236 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009237 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009238 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +00009239 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +00009240 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009241 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00009242 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
9243 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009244 case ISD::FRAME_TO_ARGS_OFFSET:
9245 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009246 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009247 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009248 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00009249 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +00009250 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
9251 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Mon P Wangaf9b9522008-12-18 21:42:19 +00009252 case ISD::MUL: return LowerMUL_V2I64(Op, DAG);
Nadav Rotem43012222011-05-11 08:12:09 +00009253 case ISD::SRA:
9254 case ISD::SRL:
9255 case ISD::SHL: return LowerShift(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +00009256 case ISD::SADDO:
9257 case ISD::UADDO:
9258 case ISD::SSUBO:
9259 case ISD::USUBO:
9260 case ISD::SMULO:
9261 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00009262 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009263 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +00009264 case ISD::ADDC:
9265 case ISD::ADDE:
9266 case ISD::SUBC:
9267 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009268 }
Chris Lattner27a6c732007-11-24 07:07:01 +00009269}
9270
Duncan Sands1607f052008-12-01 11:39:25 +00009271void X86TargetLowering::
9272ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00009273 SelectionDAG &DAG, unsigned NewOp) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009274 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009275 DebugLoc dl = Node->getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00009276 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +00009277
9278 SDValue Chain = Node->getOperand(0);
9279 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00009280 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00009281 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00009282 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00009283 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +00009284 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +00009285 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +00009286 SDValue Result =
9287 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
9288 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +00009289 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +00009290 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00009291 Results.push_back(Result.getValue(2));
9292}
9293
Duncan Sands126d9072008-07-04 11:47:58 +00009294/// ReplaceNodeResults - Replace a node with an illegal result type
9295/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00009296void X86TargetLowering::ReplaceNodeResults(SDNode *N,
9297 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00009298 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +00009299 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +00009300 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +00009301 default:
Duncan Sands1607f052008-12-01 11:39:25 +00009302 assert(false && "Do not know how to custom type legalize this operation!");
9303 return;
Chris Lattner5b856542010-12-20 00:59:46 +00009304 case ISD::ADDC:
9305 case ISD::ADDE:
9306 case ISD::SUBC:
9307 case ISD::SUBE:
9308 // We don't want to expand or promote these.
9309 return;
Duncan Sands1607f052008-12-01 11:39:25 +00009310 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +00009311 std::pair<SDValue,SDValue> Vals =
9312 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +00009313 SDValue FIST = Vals.first, StackSlot = Vals.second;
9314 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +00009315 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +00009316 // Return a load from the stack slot.
Chris Lattner51abfe42010-09-21 06:02:19 +00009317 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
9318 MachinePointerInfo(), false, false, 0));
Duncan Sands1607f052008-12-01 11:39:25 +00009319 }
9320 return;
9321 }
9322 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009323 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +00009324 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009325 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00009326 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +00009327 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00009328 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00009329 eax.getValue(2));
9330 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
9331 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +00009332 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00009333 Results.push_back(edx.getValue(1));
9334 return;
9335 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009336 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +00009337 EVT T = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009338 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Duncan Sands1607f052008-12-01 11:39:25 +00009339 SDValue cpInL, cpInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00009340 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
9341 DAG.getConstant(0, MVT::i32));
9342 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
9343 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00009344 cpInL = DAG.getCopyToReg(N->getOperand(0), dl, X86::EAX, cpInL, SDValue());
9345 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl, X86::EDX, cpInH,
Duncan Sands1607f052008-12-01 11:39:25 +00009346 cpInL.getValue(1));
9347 SDValue swapInL, swapInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00009348 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
9349 DAG.getConstant(0, MVT::i32));
9350 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
9351 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00009352 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl, X86::EBX, swapInL,
Duncan Sands1607f052008-12-01 11:39:25 +00009353 cpInH.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00009354 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl, X86::ECX, swapInH,
Duncan Sands1607f052008-12-01 11:39:25 +00009355 swapInL.getValue(1));
9356 SDValue Ops[] = { swapInH.getValue(0),
9357 N->getOperand(1),
9358 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009359 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +00009360 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
9361 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG8_DAG, dl, Tys,
9362 Ops, 3, T, MMO);
Dale Johannesendd64c412009-02-04 00:33:20 +00009363 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl, X86::EAX,
Owen Anderson825b72b2009-08-11 20:47:22 +00009364 MVT::i32, Result.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00009365 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl, X86::EDX,
Owen Anderson825b72b2009-08-11 20:47:22 +00009366 MVT::i32, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +00009367 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Owen Anderson825b72b2009-08-11 20:47:22 +00009368 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00009369 Results.push_back(cpOutH.getValue(1));
9370 return;
9371 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009372 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +00009373 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
9374 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009375 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +00009376 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
9377 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009378 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +00009379 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
9380 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009381 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +00009382 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
9383 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009384 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +00009385 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
9386 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009387 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +00009388 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
9389 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009390 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +00009391 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
9392 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00009393 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00009394}
9395
Evan Cheng72261582005-12-20 06:22:03 +00009396const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
9397 switch (Opcode) {
9398 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +00009399 case X86ISD::BSF: return "X86ISD::BSF";
9400 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +00009401 case X86ISD::SHLD: return "X86ISD::SHLD";
9402 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00009403 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00009404 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00009405 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00009406 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00009407 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00009408 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00009409 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
9410 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
9411 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00009412 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00009413 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +00009414 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +00009415 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +00009416 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +00009417 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00009418 case X86ISD::COMI: return "X86ISD::COMI";
9419 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00009420 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +00009421 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Stuart Hastings865f0932011-06-03 23:53:54 +00009422 case X86ISD::FSETCCsd: return "X86ISD::FSETCCsd";
9423 case X86ISD::FSETCCss: return "X86ISD::FSETCCss";
Evan Cheng72261582005-12-20 06:22:03 +00009424 case X86ISD::CMOV: return "X86ISD::CMOV";
9425 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00009426 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00009427 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
9428 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +00009429 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00009430 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +00009431 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +00009432 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +00009433 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +00009434 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
9435 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +00009436 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +00009437 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Nate Begemanb65c1752010-12-17 22:55:37 +00009438 case X86ISD::PANDN: return "X86ISD::PANDN";
9439 case X86ISD::PSIGNB: return "X86ISD::PSIGNB";
9440 case X86ISD::PSIGNW: return "X86ISD::PSIGNW";
9441 case X86ISD::PSIGND: return "X86ISD::PSIGND";
Nate Begeman672fb622010-12-20 22:04:24 +00009442 case X86ISD::PBLENDVB: return "X86ISD::PBLENDVB";
Evan Cheng8ca29322006-11-10 21:43:37 +00009443 case X86ISD::FMAX: return "X86ISD::FMAX";
9444 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +00009445 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
9446 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00009447 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +00009448 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009449 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00009450 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009451 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +00009452 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
9453 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009454 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
9455 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
9456 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
9457 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
9458 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
9459 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +00009460 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
9461 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +00009462 case X86ISD::VSHL: return "X86ISD::VSHL";
9463 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +00009464 case X86ISD::CMPPD: return "X86ISD::CMPPD";
9465 case X86ISD::CMPPS: return "X86ISD::CMPPS";
9466 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
9467 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
9468 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
9469 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
9470 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
9471 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
9472 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
9473 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +00009474 case X86ISD::ADD: return "X86ISD::ADD";
9475 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +00009476 case X86ISD::ADC: return "X86ISD::ADC";
9477 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +00009478 case X86ISD::SMUL: return "X86ISD::SMUL";
9479 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +00009480 case X86ISD::INC: return "X86ISD::INC";
9481 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +00009482 case X86ISD::OR: return "X86ISD::OR";
9483 case X86ISD::XOR: return "X86ISD::XOR";
9484 case X86ISD::AND: return "X86ISD::AND";
Evan Cheng73f24c92009-03-30 21:36:47 +00009485 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +00009486 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009487 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009488 case X86ISD::PALIGN: return "X86ISD::PALIGN";
9489 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
9490 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
9491 case X86ISD::PSHUFHW_LD: return "X86ISD::PSHUFHW_LD";
9492 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
9493 case X86ISD::PSHUFLW_LD: return "X86ISD::PSHUFLW_LD";
9494 case X86ISD::SHUFPS: return "X86ISD::SHUFPS";
9495 case X86ISD::SHUFPD: return "X86ISD::SHUFPD";
9496 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009497 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00009498 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009499 case X86ISD::MOVHLPD: return "X86ISD::MOVHLPD";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00009500 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
9501 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009502 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
9503 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
9504 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
9505 case X86ISD::MOVSHDUP_LD: return "X86ISD::MOVSHDUP_LD";
9506 case X86ISD::MOVSLDUP_LD: return "X86ISD::MOVSLDUP_LD";
9507 case X86ISD::MOVSD: return "X86ISD::MOVSD";
9508 case X86ISD::MOVSS: return "X86ISD::MOVSS";
9509 case X86ISD::UNPCKLPS: return "X86ISD::UNPCKLPS";
9510 case X86ISD::UNPCKLPD: return "X86ISD::UNPCKLPD";
David Greenefbf05d32011-02-22 23:31:46 +00009511 case X86ISD::VUNPCKLPS: return "X86ISD::VUNPCKLPS";
9512 case X86ISD::VUNPCKLPD: return "X86ISD::VUNPCKLPD";
9513 case X86ISD::VUNPCKLPSY: return "X86ISD::VUNPCKLPSY";
9514 case X86ISD::VUNPCKLPDY: return "X86ISD::VUNPCKLPDY";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009515 case X86ISD::UNPCKHPS: return "X86ISD::UNPCKHPS";
9516 case X86ISD::UNPCKHPD: return "X86ISD::UNPCKHPD";
9517 case X86ISD::PUNPCKLBW: return "X86ISD::PUNPCKLBW";
9518 case X86ISD::PUNPCKLWD: return "X86ISD::PUNPCKLWD";
9519 case X86ISD::PUNPCKLDQ: return "X86ISD::PUNPCKLDQ";
9520 case X86ISD::PUNPCKLQDQ: return "X86ISD::PUNPCKLQDQ";
9521 case X86ISD::PUNPCKHBW: return "X86ISD::PUNPCKHBW";
9522 case X86ISD::PUNPCKHWD: return "X86ISD::PUNPCKHWD";
9523 case X86ISD::PUNPCKHDQ: return "X86ISD::PUNPCKHDQ";
9524 case X86ISD::PUNPCKHQDQ: return "X86ISD::PUNPCKHQDQ";
Dan Gohmand6708ea2009-08-15 01:38:56 +00009525 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +00009526 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +00009527 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Evan Cheng72261582005-12-20 06:22:03 +00009528 }
9529}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00009530
Chris Lattnerc9addb72007-03-30 23:15:24 +00009531// isLegalAddressingMode - Return true if the addressing mode represented
9532// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +00009533bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerc9addb72007-03-30 23:15:24 +00009534 const Type *Ty) const {
9535 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009536 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +00009537 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +00009538
Chris Lattnerc9addb72007-03-30 23:15:24 +00009539 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009540 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +00009541 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00009542
Chris Lattnerc9addb72007-03-30 23:15:24 +00009543 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +00009544 unsigned GVFlags =
9545 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009546
Chris Lattnerdfed4132009-07-10 07:38:24 +00009547 // If a reference to this global requires an extra load, we can't fold it.
9548 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +00009549 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009550
Chris Lattnerdfed4132009-07-10 07:38:24 +00009551 // If BaseGV requires a register for the PIC base, we cannot also have a
9552 // BaseReg specified.
9553 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +00009554 return false;
Evan Cheng52787842007-08-01 23:46:47 +00009555
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009556 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +00009557 if ((M != CodeModel::Small || R != Reloc::Static) &&
9558 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009559 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +00009560 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009561
Chris Lattnerc9addb72007-03-30 23:15:24 +00009562 switch (AM.Scale) {
9563 case 0:
9564 case 1:
9565 case 2:
9566 case 4:
9567 case 8:
9568 // These scales always work.
9569 break;
9570 case 3:
9571 case 5:
9572 case 9:
9573 // These scales are formed with basereg+scalereg. Only accept if there is
9574 // no basereg yet.
9575 if (AM.HasBaseReg)
9576 return false;
9577 break;
9578 default: // Other stuff never works.
9579 return false;
9580 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009581
Chris Lattnerc9addb72007-03-30 23:15:24 +00009582 return true;
9583}
9584
9585
Evan Cheng2bd122c2007-10-26 01:56:11 +00009586bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009587 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +00009588 return false;
Evan Chenge127a732007-10-29 07:57:50 +00009589 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
9590 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00009591 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +00009592 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00009593 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +00009594}
9595
Owen Andersone50ed302009-08-10 22:56:29 +00009596bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +00009597 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009598 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009599 unsigned NumBits1 = VT1.getSizeInBits();
9600 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00009601 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009602 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00009603 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009604}
Evan Cheng2bd122c2007-10-26 01:56:11 +00009605
Dan Gohman97121ba2009-04-08 00:15:30 +00009606bool X86TargetLowering::isZExtFree(const Type *Ty1, const Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00009607 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009608 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00009609}
9610
Owen Andersone50ed302009-08-10 22:56:29 +00009611bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00009612 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00009613 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00009614}
9615
Owen Andersone50ed302009-08-10 22:56:29 +00009616bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +00009617 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +00009618 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +00009619}
9620
Evan Cheng60c07e12006-07-05 22:17:51 +00009621/// isShuffleMaskLegal - Targets can use this to indicate that they only
9622/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
9623/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
9624/// are assumed to be legal.
9625bool
Eric Christopherfd179292009-08-27 18:07:15 +00009626X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +00009627 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +00009628 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +00009629 if (VT.getSizeInBits() == 64)
Eric Christophercff6f852010-04-15 01:40:20 +00009630 return isPALIGNRMask(M, VT, Subtarget->hasSSSE3());
Nate Begeman9008ca62009-04-27 18:41:29 +00009631
Nate Begemana09008b2009-10-19 02:17:23 +00009632 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +00009633 return (VT.getVectorNumElements() == 2 ||
9634 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
9635 isMOVLMask(M, VT) ||
9636 isSHUFPMask(M, VT) ||
9637 isPSHUFDMask(M, VT) ||
9638 isPSHUFHWMask(M, VT) ||
9639 isPSHUFLWMask(M, VT) ||
Nate Begemana09008b2009-10-19 02:17:23 +00009640 isPALIGNRMask(M, VT, Subtarget->hasSSSE3()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +00009641 isUNPCKLMask(M, VT) ||
9642 isUNPCKHMask(M, VT) ||
9643 isUNPCKL_v_undef_Mask(M, VT) ||
9644 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00009645}
9646
Dan Gohman7d8143f2008-04-09 20:09:42 +00009647bool
Nate Begeman5a5ca152009-04-29 05:20:52 +00009648X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +00009649 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00009650 unsigned NumElts = VT.getVectorNumElements();
9651 // FIXME: This collection of masks seems suspect.
9652 if (NumElts == 2)
9653 return true;
9654 if (NumElts == 4 && VT.getSizeInBits() == 128) {
9655 return (isMOVLMask(Mask, VT) ||
9656 isCommutedMOVLMask(Mask, VT, true) ||
9657 isSHUFPMask(Mask, VT) ||
9658 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00009659 }
9660 return false;
9661}
9662
9663//===----------------------------------------------------------------------===//
9664// X86 Scheduler Hooks
9665//===----------------------------------------------------------------------===//
9666
Mon P Wang63307c32008-05-05 19:05:59 +00009667// private utility function
9668MachineBasicBlock *
9669X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
9670 MachineBasicBlock *MBB,
9671 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009672 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009673 unsigned LoadOpc,
9674 unsigned CXchgOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009675 unsigned notOpc,
9676 unsigned EAXreg,
9677 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009678 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00009679 // For the atomic bitwise operator, we generate
9680 // thisMBB:
9681 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00009682 // ld t1 = [bitinstr.addr]
9683 // op t2 = t1, [bitinstr.val]
9684 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00009685 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
9686 // bz newMBB
9687 // fallthrough -->nextMBB
9688 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9689 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009690 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00009691 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009692
Mon P Wang63307c32008-05-05 19:05:59 +00009693 /// First build the CFG
9694 MachineFunction *F = MBB->getParent();
9695 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009696 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9697 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9698 F->insert(MBBIter, newMBB);
9699 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009700
Dan Gohman14152b42010-07-06 20:24:04 +00009701 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9702 nextMBB->splice(nextMBB->begin(), thisMBB,
9703 llvm::next(MachineBasicBlock::iterator(bInstr)),
9704 thisMBB->end());
9705 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009706
Mon P Wang63307c32008-05-05 19:05:59 +00009707 // Update thisMBB to fall through to newMBB
9708 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009709
Mon P Wang63307c32008-05-05 19:05:59 +00009710 // newMBB jumps to itself and fall through to nextMBB
9711 newMBB->addSuccessor(nextMBB);
9712 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009713
Mon P Wang63307c32008-05-05 19:05:59 +00009714 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009715 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009716 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +00009717 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00009718 MachineOperand& destOper = bInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009719 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00009720 int numArgs = bInstr->getNumOperands() - 1;
9721 for (int i=0; i < numArgs; ++i)
9722 argOpers[i] = &bInstr->getOperand(i+1);
9723
9724 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009725 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009726 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00009727
Dale Johannesen140be2d2008-08-19 18:47:28 +00009728 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009729 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00009730 for (int i=0; i <= lastAddrIndx; ++i)
9731 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009732
Dale Johannesen140be2d2008-08-19 18:47:28 +00009733 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009734 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00009735 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009736 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009737 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009738 tt = t1;
9739
Dale Johannesen140be2d2008-08-19 18:47:28 +00009740 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +00009741 assert((argOpers[valArgIndx]->isReg() ||
9742 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00009743 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +00009744 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009745 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00009746 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009747 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009748 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +00009749 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009750
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009751 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +00009752 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009753
Dale Johannesene4d209d2009-02-03 20:21:25 +00009754 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +00009755 for (int i=0; i <= lastAddrIndx; ++i)
9756 (*MIB).addOperand(*argOpers[i]);
9757 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +00009758 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009759 (*MIB).setMemRefs(bInstr->memoperands_begin(),
9760 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +00009761
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009762 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +00009763 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +00009764
Mon P Wang63307c32008-05-05 19:05:59 +00009765 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009766 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00009767
Dan Gohman14152b42010-07-06 20:24:04 +00009768 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00009769 return nextMBB;
9770}
9771
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00009772// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +00009773MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009774X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
9775 MachineBasicBlock *MBB,
9776 unsigned regOpcL,
9777 unsigned regOpcH,
9778 unsigned immOpcL,
9779 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009780 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009781 // For the atomic bitwise operator, we generate
9782 // thisMBB (instructions are in pairs, except cmpxchg8b)
9783 // ld t1,t2 = [bitinstr.addr]
9784 // newMBB:
9785 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
9786 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +00009787 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009788 // mov ECX, EBX <- t5, t6
9789 // mov EAX, EDX <- t1, t2
9790 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
9791 // mov t3, t4 <- EAX, EDX
9792 // bz newMBB
9793 // result in out1, out2
9794 // fallthrough -->nextMBB
9795
9796 const TargetRegisterClass *RC = X86::GR32RegisterClass;
9797 const unsigned LoadOpc = X86::MOV32rm;
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009798 const unsigned NotOpc = X86::NOT32r;
9799 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9800 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
9801 MachineFunction::iterator MBBIter = MBB;
9802 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009803
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009804 /// First build the CFG
9805 MachineFunction *F = MBB->getParent();
9806 MachineBasicBlock *thisMBB = MBB;
9807 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9808 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9809 F->insert(MBBIter, newMBB);
9810 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009811
Dan Gohman14152b42010-07-06 20:24:04 +00009812 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9813 nextMBB->splice(nextMBB->begin(), thisMBB,
9814 llvm::next(MachineBasicBlock::iterator(bInstr)),
9815 thisMBB->end());
9816 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009817
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009818 // Update thisMBB to fall through to newMBB
9819 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009820
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009821 // newMBB jumps to itself and fall through to nextMBB
9822 newMBB->addSuccessor(nextMBB);
9823 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009824
Dale Johannesene4d209d2009-02-03 20:21:25 +00009825 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009826 // Insert instructions into newMBB based on incoming instruction
9827 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009828 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009829 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009830 MachineOperand& dest1Oper = bInstr->getOperand(0);
9831 MachineOperand& dest2Oper = bInstr->getOperand(1);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009832 MachineOperand* argOpers[2 + X86::AddrNumOperands];
9833 for (int i=0; i < 2 + X86::AddrNumOperands; ++i) {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009834 argOpers[i] = &bInstr->getOperand(i+2);
9835
Dan Gohman71ea4e52010-05-14 21:01:44 +00009836 // We use some of the operands multiple times, so conservatively just
9837 // clear any kill flags that might be present.
9838 if (argOpers[i]->isReg() && argOpers[i]->isUse())
9839 argOpers[i]->setIsKill(false);
9840 }
9841
Evan Chengad5b52f2010-01-08 19:14:57 +00009842 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009843 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +00009844
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009845 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009846 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009847 for (int i=0; i <= lastAddrIndx; ++i)
9848 (*MIB).addOperand(*argOpers[i]);
9849 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009850 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +00009851 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +00009852 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009853 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +00009854 MachineOperand newOp3 = *(argOpers[3]);
9855 if (newOp3.isImm())
9856 newOp3.setImm(newOp3.getImm()+4);
9857 else
9858 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009859 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +00009860 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009861
9862 // t3/4 are defined later, at the bottom of the loop
9863 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
9864 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009865 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009866 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009867 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009868 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
9869
Evan Cheng306b4ca2010-01-08 23:41:50 +00009870 // The subsequent operations should be using the destination registers of
9871 //the PHI instructions.
Scott Michelfdc40a02009-02-17 22:15:04 +00009872 if (invSrc) {
Evan Cheng306b4ca2010-01-08 23:41:50 +00009873 t1 = F->getRegInfo().createVirtualRegister(RC);
9874 t2 = F->getRegInfo().createVirtualRegister(RC);
9875 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
9876 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009877 } else {
Evan Cheng306b4ca2010-01-08 23:41:50 +00009878 t1 = dest1Oper.getReg();
9879 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009880 }
9881
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009882 int valArgIndx = lastAddrIndx + 1;
9883 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +00009884 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009885 "invalid operand");
9886 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
9887 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009888 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009889 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009890 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009891 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +00009892 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00009893 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009894 (*MIB).addOperand(*argOpers[valArgIndx]);
9895 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00009896 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009897 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00009898 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009899 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009900 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009901 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009902 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +00009903 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00009904 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009905 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009906
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009907 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009908 MIB.addReg(t1);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009909 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009910 MIB.addReg(t2);
9911
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009912 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009913 MIB.addReg(t5);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009914 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009915 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +00009916
Dale Johannesene4d209d2009-02-03 20:21:25 +00009917 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009918 for (int i=0; i <= lastAddrIndx; ++i)
9919 (*MIB).addOperand(*argOpers[i]);
9920
9921 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009922 (*MIB).setMemRefs(bInstr->memoperands_begin(),
9923 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009924
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009925 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009926 MIB.addReg(X86::EAX);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009927 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009928 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +00009929
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009930 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009931 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009932
Dan Gohman14152b42010-07-06 20:24:04 +00009933 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009934 return nextMBB;
9935}
9936
9937// private utility function
9938MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +00009939X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
9940 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009941 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00009942 // For the atomic min/max operator, we generate
9943 // thisMBB:
9944 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00009945 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +00009946 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +00009947 // cmp t1, t2
9948 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +00009949 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00009950 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
9951 // bz newMBB
9952 // fallthrough -->nextMBB
9953 //
9954 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9955 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009956 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00009957 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009958
Mon P Wang63307c32008-05-05 19:05:59 +00009959 /// First build the CFG
9960 MachineFunction *F = MBB->getParent();
9961 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009962 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9963 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9964 F->insert(MBBIter, newMBB);
9965 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009966
Dan Gohman14152b42010-07-06 20:24:04 +00009967 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9968 nextMBB->splice(nextMBB->begin(), thisMBB,
9969 llvm::next(MachineBasicBlock::iterator(mInstr)),
9970 thisMBB->end());
9971 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009972
Mon P Wang63307c32008-05-05 19:05:59 +00009973 // Update thisMBB to fall through to newMBB
9974 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009975
Mon P Wang63307c32008-05-05 19:05:59 +00009976 // newMBB jumps to newMBB and fall through to nextMBB
9977 newMBB->addSuccessor(nextMBB);
9978 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009979
Dale Johannesene4d209d2009-02-03 20:21:25 +00009980 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00009981 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009982 assert(mInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009983 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +00009984 MachineOperand& destOper = mInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009985 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00009986 int numArgs = mInstr->getNumOperands() - 1;
9987 for (int i=0; i < numArgs; ++i)
9988 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009989
Mon P Wang63307c32008-05-05 19:05:59 +00009990 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009991 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009992 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00009993
Mon P Wangab3e7472008-05-05 22:56:23 +00009994 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009995 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00009996 for (int i=0; i <= lastAddrIndx; ++i)
9997 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +00009998
Mon P Wang63307c32008-05-05 19:05:59 +00009999 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +000010000 assert((argOpers[valArgIndx]->isReg() ||
10001 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +000010002 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +000010003
10004 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +000010005 if (argOpers[valArgIndx]->isReg())
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010006 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +000010007 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000010008 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +000010009 (*MIB).addOperand(*argOpers[valArgIndx]);
10010
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010011 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +000010012 MIB.addReg(t1);
10013
Dale Johannesene4d209d2009-02-03 20:21:25 +000010014 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +000010015 MIB.addReg(t1);
10016 MIB.addReg(t2);
10017
10018 // Generate movc
10019 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010020 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +000010021 MIB.addReg(t2);
10022 MIB.addReg(t1);
10023
10024 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +000010025 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +000010026 for (int i=0; i <= lastAddrIndx; ++i)
10027 (*MIB).addOperand(*argOpers[i]);
10028 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +000010029 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000010030 (*MIB).setMemRefs(mInstr->memoperands_begin(),
10031 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +000010032
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010033 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +000010034 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +000010035
Mon P Wang63307c32008-05-05 19:05:59 +000010036 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000010037 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000010038
Dan Gohman14152b42010-07-06 20:24:04 +000010039 mInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +000010040 return nextMBB;
10041}
10042
Eric Christopherf83a5de2009-08-27 18:08:16 +000010043// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010044// or XMM0_V32I8 in AVX all of this code can be replaced with that
10045// in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +000010046MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +000010047X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +000010048 unsigned numArgs, bool memArg) const {
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010049 assert((Subtarget->hasSSE42() || Subtarget->hasAVX()) &&
10050 "Target must have SSE4.2 or AVX features enabled");
10051
Eric Christopherb120ab42009-08-18 22:50:32 +000010052 DebugLoc dl = MI->getDebugLoc();
10053 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Eric Christopherb120ab42009-08-18 22:50:32 +000010054 unsigned Opc;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010055 if (!Subtarget->hasAVX()) {
10056 if (memArg)
10057 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
10058 else
10059 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
10060 } else {
10061 if (memArg)
10062 Opc = numArgs == 3 ? X86::VPCMPISTRM128rm : X86::VPCMPESTRM128rm;
10063 else
10064 Opc = numArgs == 3 ? X86::VPCMPISTRM128rr : X86::VPCMPESTRM128rr;
10065 }
Eric Christopherb120ab42009-08-18 22:50:32 +000010066
Eric Christopher41c902f2010-11-30 08:20:21 +000010067 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Eric Christopherb120ab42009-08-18 22:50:32 +000010068 for (unsigned i = 0; i < numArgs; ++i) {
10069 MachineOperand &Op = MI->getOperand(i+1);
Eric Christopherb120ab42009-08-18 22:50:32 +000010070 if (!(Op.isReg() && Op.isImplicit()))
10071 MIB.addOperand(Op);
10072 }
Eric Christopher41c902f2010-11-30 08:20:21 +000010073 BuildMI(*BB, MI, dl, TII->get(X86::MOVAPSrr), MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +000010074 .addReg(X86::XMM0);
10075
Dan Gohman14152b42010-07-06 20:24:04 +000010076 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +000010077 return BB;
10078}
10079
10080MachineBasicBlock *
Eric Christopher228232b2010-11-30 07:20:12 +000010081X86TargetLowering::EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000010082 DebugLoc dl = MI->getDebugLoc();
10083 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010084
Eric Christopher228232b2010-11-30 07:20:12 +000010085 // Address into RAX/EAX, other two args into ECX, EDX.
10086 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
10087 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
10088 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
10089 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +000010090 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010091
Eric Christopher228232b2010-11-30 07:20:12 +000010092 unsigned ValOps = X86::AddrNumOperands;
10093 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
10094 .addReg(MI->getOperand(ValOps).getReg());
10095 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
10096 .addReg(MI->getOperand(ValOps+1).getReg());
10097
10098 // The instruction doesn't actually take any operands though.
10099 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010100
Eric Christopher228232b2010-11-30 07:20:12 +000010101 MI->eraseFromParent(); // The pseudo is gone now.
10102 return BB;
10103}
10104
10105MachineBasicBlock *
10106X86TargetLowering::EmitMwait(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000010107 DebugLoc dl = MI->getDebugLoc();
10108 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010109
Eric Christopher228232b2010-11-30 07:20:12 +000010110 // First arg in ECX, the second in EAX.
10111 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
10112 .addReg(MI->getOperand(0).getReg());
10113 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EAX)
10114 .addReg(MI->getOperand(1).getReg());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010115
Eric Christopher228232b2010-11-30 07:20:12 +000010116 // The instruction doesn't actually take any operands though.
10117 BuildMI(*BB, MI, dl, TII->get(X86::MWAITrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010118
Eric Christopher228232b2010-11-30 07:20:12 +000010119 MI->eraseFromParent(); // The pseudo is gone now.
10120 return BB;
10121}
10122
10123MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +000010124X86TargetLowering::EmitVAARG64WithCustomInserter(
10125 MachineInstr *MI,
10126 MachineBasicBlock *MBB) const {
10127 // Emit va_arg instruction on X86-64.
10128
10129 // Operands to this pseudo-instruction:
10130 // 0 ) Output : destination address (reg)
10131 // 1-5) Input : va_list address (addr, i64mem)
10132 // 6 ) ArgSize : Size (in bytes) of vararg type
10133 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
10134 // 8 ) Align : Alignment of type
10135 // 9 ) EFLAGS (implicit-def)
10136
10137 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
10138 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
10139
10140 unsigned DestReg = MI->getOperand(0).getReg();
10141 MachineOperand &Base = MI->getOperand(1);
10142 MachineOperand &Scale = MI->getOperand(2);
10143 MachineOperand &Index = MI->getOperand(3);
10144 MachineOperand &Disp = MI->getOperand(4);
10145 MachineOperand &Segment = MI->getOperand(5);
10146 unsigned ArgSize = MI->getOperand(6).getImm();
10147 unsigned ArgMode = MI->getOperand(7).getImm();
10148 unsigned Align = MI->getOperand(8).getImm();
10149
10150 // Memory Reference
10151 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
10152 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
10153 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
10154
10155 // Machine Information
10156 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10157 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
10158 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
10159 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
10160 DebugLoc DL = MI->getDebugLoc();
10161
10162 // struct va_list {
10163 // i32 gp_offset
10164 // i32 fp_offset
10165 // i64 overflow_area (address)
10166 // i64 reg_save_area (address)
10167 // }
10168 // sizeof(va_list) = 24
10169 // alignment(va_list) = 8
10170
10171 unsigned TotalNumIntRegs = 6;
10172 unsigned TotalNumXMMRegs = 8;
10173 bool UseGPOffset = (ArgMode == 1);
10174 bool UseFPOffset = (ArgMode == 2);
10175 unsigned MaxOffset = TotalNumIntRegs * 8 +
10176 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
10177
10178 /* Align ArgSize to a multiple of 8 */
10179 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
10180 bool NeedsAlign = (Align > 8);
10181
10182 MachineBasicBlock *thisMBB = MBB;
10183 MachineBasicBlock *overflowMBB;
10184 MachineBasicBlock *offsetMBB;
10185 MachineBasicBlock *endMBB;
10186
10187 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
10188 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
10189 unsigned OffsetReg = 0;
10190
10191 if (!UseGPOffset && !UseFPOffset) {
10192 // If we only pull from the overflow region, we don't create a branch.
10193 // We don't need to alter control flow.
10194 OffsetDestReg = 0; // unused
10195 OverflowDestReg = DestReg;
10196
10197 offsetMBB = NULL;
10198 overflowMBB = thisMBB;
10199 endMBB = thisMBB;
10200 } else {
10201 // First emit code to check if gp_offset (or fp_offset) is below the bound.
10202 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
10203 // If not, pull from overflow_area. (branch to overflowMBB)
10204 //
10205 // thisMBB
10206 // | .
10207 // | .
10208 // offsetMBB overflowMBB
10209 // | .
10210 // | .
10211 // endMBB
10212
10213 // Registers for the PHI in endMBB
10214 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
10215 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
10216
10217 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
10218 MachineFunction *MF = MBB->getParent();
10219 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
10220 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
10221 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
10222
10223 MachineFunction::iterator MBBIter = MBB;
10224 ++MBBIter;
10225
10226 // Insert the new basic blocks
10227 MF->insert(MBBIter, offsetMBB);
10228 MF->insert(MBBIter, overflowMBB);
10229 MF->insert(MBBIter, endMBB);
10230
10231 // Transfer the remainder of MBB and its successor edges to endMBB.
10232 endMBB->splice(endMBB->begin(), thisMBB,
10233 llvm::next(MachineBasicBlock::iterator(MI)),
10234 thisMBB->end());
10235 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
10236
10237 // Make offsetMBB and overflowMBB successors of thisMBB
10238 thisMBB->addSuccessor(offsetMBB);
10239 thisMBB->addSuccessor(overflowMBB);
10240
10241 // endMBB is a successor of both offsetMBB and overflowMBB
10242 offsetMBB->addSuccessor(endMBB);
10243 overflowMBB->addSuccessor(endMBB);
10244
10245 // Load the offset value into a register
10246 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
10247 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
10248 .addOperand(Base)
10249 .addOperand(Scale)
10250 .addOperand(Index)
10251 .addDisp(Disp, UseFPOffset ? 4 : 0)
10252 .addOperand(Segment)
10253 .setMemRefs(MMOBegin, MMOEnd);
10254
10255 // Check if there is enough room left to pull this argument.
10256 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
10257 .addReg(OffsetReg)
10258 .addImm(MaxOffset + 8 - ArgSizeA8);
10259
10260 // Branch to "overflowMBB" if offset >= max
10261 // Fall through to "offsetMBB" otherwise
10262 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
10263 .addMBB(overflowMBB);
10264 }
10265
10266 // In offsetMBB, emit code to use the reg_save_area.
10267 if (offsetMBB) {
10268 assert(OffsetReg != 0);
10269
10270 // Read the reg_save_area address.
10271 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
10272 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
10273 .addOperand(Base)
10274 .addOperand(Scale)
10275 .addOperand(Index)
10276 .addDisp(Disp, 16)
10277 .addOperand(Segment)
10278 .setMemRefs(MMOBegin, MMOEnd);
10279
10280 // Zero-extend the offset
10281 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
10282 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
10283 .addImm(0)
10284 .addReg(OffsetReg)
10285 .addImm(X86::sub_32bit);
10286
10287 // Add the offset to the reg_save_area to get the final address.
10288 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
10289 .addReg(OffsetReg64)
10290 .addReg(RegSaveReg);
10291
10292 // Compute the offset for the next argument
10293 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
10294 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
10295 .addReg(OffsetReg)
10296 .addImm(UseFPOffset ? 16 : 8);
10297
10298 // Store it back into the va_list.
10299 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
10300 .addOperand(Base)
10301 .addOperand(Scale)
10302 .addOperand(Index)
10303 .addDisp(Disp, UseFPOffset ? 4 : 0)
10304 .addOperand(Segment)
10305 .addReg(NextOffsetReg)
10306 .setMemRefs(MMOBegin, MMOEnd);
10307
10308 // Jump to endMBB
10309 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
10310 .addMBB(endMBB);
10311 }
10312
10313 //
10314 // Emit code to use overflow area
10315 //
10316
10317 // Load the overflow_area address into a register.
10318 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
10319 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
10320 .addOperand(Base)
10321 .addOperand(Scale)
10322 .addOperand(Index)
10323 .addDisp(Disp, 8)
10324 .addOperand(Segment)
10325 .setMemRefs(MMOBegin, MMOEnd);
10326
10327 // If we need to align it, do so. Otherwise, just copy the address
10328 // to OverflowDestReg.
10329 if (NeedsAlign) {
10330 // Align the overflow address
10331 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
10332 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
10333
10334 // aligned_addr = (addr + (align-1)) & ~(align-1)
10335 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
10336 .addReg(OverflowAddrReg)
10337 .addImm(Align-1);
10338
10339 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
10340 .addReg(TmpReg)
10341 .addImm(~(uint64_t)(Align-1));
10342 } else {
10343 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
10344 .addReg(OverflowAddrReg);
10345 }
10346
10347 // Compute the next overflow address after this argument.
10348 // (the overflow address should be kept 8-byte aligned)
10349 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
10350 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
10351 .addReg(OverflowDestReg)
10352 .addImm(ArgSizeA8);
10353
10354 // Store the new overflow address.
10355 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
10356 .addOperand(Base)
10357 .addOperand(Scale)
10358 .addOperand(Index)
10359 .addDisp(Disp, 8)
10360 .addOperand(Segment)
10361 .addReg(NextAddrReg)
10362 .setMemRefs(MMOBegin, MMOEnd);
10363
10364 // If we branched, emit the PHI to the front of endMBB.
10365 if (offsetMBB) {
10366 BuildMI(*endMBB, endMBB->begin(), DL,
10367 TII->get(X86::PHI), DestReg)
10368 .addReg(OffsetDestReg).addMBB(offsetMBB)
10369 .addReg(OverflowDestReg).addMBB(overflowMBB);
10370 }
10371
10372 // Erase the pseudo instruction
10373 MI->eraseFromParent();
10374
10375 return endMBB;
10376}
10377
10378MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +000010379X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
10380 MachineInstr *MI,
10381 MachineBasicBlock *MBB) const {
10382 // Emit code to save XMM registers to the stack. The ABI says that the
10383 // number of registers to save is given in %al, so it's theoretically
10384 // possible to do an indirect jump trick to avoid saving all of them,
10385 // however this code takes a simpler approach and just executes all
10386 // of the stores if %al is non-zero. It's less code, and it's probably
10387 // easier on the hardware branch predictor, and stores aren't all that
10388 // expensive anyway.
10389
10390 // Create the new basic blocks. One block contains all the XMM stores,
10391 // and one block is the final destination regardless of whether any
10392 // stores were performed.
10393 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
10394 MachineFunction *F = MBB->getParent();
10395 MachineFunction::iterator MBBIter = MBB;
10396 ++MBBIter;
10397 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
10398 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
10399 F->insert(MBBIter, XMMSaveMBB);
10400 F->insert(MBBIter, EndMBB);
10401
Dan Gohman14152b42010-07-06 20:24:04 +000010402 // Transfer the remainder of MBB and its successor edges to EndMBB.
10403 EndMBB->splice(EndMBB->begin(), MBB,
10404 llvm::next(MachineBasicBlock::iterator(MI)),
10405 MBB->end());
10406 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
10407
Dan Gohmand6708ea2009-08-15 01:38:56 +000010408 // The original block will now fall through to the XMM save block.
10409 MBB->addSuccessor(XMMSaveMBB);
10410 // The XMMSaveMBB will fall through to the end block.
10411 XMMSaveMBB->addSuccessor(EndMBB);
10412
10413 // Now add the instructions.
10414 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10415 DebugLoc DL = MI->getDebugLoc();
10416
10417 unsigned CountReg = MI->getOperand(0).getReg();
10418 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
10419 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
10420
10421 if (!Subtarget->isTargetWin64()) {
10422 // If %al is 0, branch around the XMM save block.
10423 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +000010424 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010425 MBB->addSuccessor(EndMBB);
10426 }
10427
10428 // In the XMM save block, save all the XMM argument registers.
10429 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
10430 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +000010431 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +000010432 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +000010433 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +000010434 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +000010435 /*Size=*/16, /*Align=*/16);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010436 BuildMI(XMMSaveMBB, DL, TII->get(X86::MOVAPSmr))
10437 .addFrameIndex(RegSaveFrameIndex)
10438 .addImm(/*Scale=*/1)
10439 .addReg(/*IndexReg=*/0)
10440 .addImm(/*Disp=*/Offset)
10441 .addReg(/*Segment=*/0)
10442 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +000010443 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010444 }
10445
Dan Gohman14152b42010-07-06 20:24:04 +000010446 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +000010447
10448 return EndMBB;
10449}
Mon P Wang63307c32008-05-05 19:05:59 +000010450
Evan Cheng60c07e12006-07-05 22:17:51 +000010451MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +000010452X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010453 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +000010454 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10455 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +000010456
Chris Lattner52600972009-09-02 05:57:00 +000010457 // To "insert" a SELECT_CC instruction, we actually have to insert the
10458 // diamond control-flow pattern. The incoming instruction knows the
10459 // destination vreg to set, the condition code register to branch on, the
10460 // true/false values to select between, and a branch opcode to use.
10461 const BasicBlock *LLVM_BB = BB->getBasicBlock();
10462 MachineFunction::iterator It = BB;
10463 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +000010464
Chris Lattner52600972009-09-02 05:57:00 +000010465 // thisMBB:
10466 // ...
10467 // TrueVal = ...
10468 // cmpTY ccX, r1, r2
10469 // bCC copy1MBB
10470 // fallthrough --> copy0MBB
10471 MachineBasicBlock *thisMBB = BB;
10472 MachineFunction *F = BB->getParent();
10473 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
10474 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +000010475 F->insert(It, copy0MBB);
10476 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +000010477
Bill Wendling730c07e2010-06-25 20:48:10 +000010478 // If the EFLAGS register isn't dead in the terminator, then claim that it's
10479 // live into the sink and copy blocks.
10480 const MachineFunction *MF = BB->getParent();
10481 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
10482 BitVector ReservedRegs = TRI->getReservedRegs(*MF);
Bill Wendling730c07e2010-06-25 20:48:10 +000010483
Dan Gohman14152b42010-07-06 20:24:04 +000010484 for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) {
10485 const MachineOperand &MO = MI->getOperand(I);
10486 if (!MO.isReg() || !MO.isUse() || MO.isKill()) continue;
Bill Wendling730c07e2010-06-25 20:48:10 +000010487 unsigned Reg = MO.getReg();
10488 if (Reg != X86::EFLAGS) continue;
10489 copy0MBB->addLiveIn(Reg);
10490 sinkMBB->addLiveIn(Reg);
10491 }
10492
Dan Gohman14152b42010-07-06 20:24:04 +000010493 // Transfer the remainder of BB and its successor edges to sinkMBB.
10494 sinkMBB->splice(sinkMBB->begin(), BB,
10495 llvm::next(MachineBasicBlock::iterator(MI)),
10496 BB->end());
10497 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
10498
10499 // Add the true and fallthrough blocks as its successors.
10500 BB->addSuccessor(copy0MBB);
10501 BB->addSuccessor(sinkMBB);
10502
10503 // Create the conditional branch instruction.
10504 unsigned Opc =
10505 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
10506 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
10507
Chris Lattner52600972009-09-02 05:57:00 +000010508 // copy0MBB:
10509 // %FalseValue = ...
10510 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000010511 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000010512
Chris Lattner52600972009-09-02 05:57:00 +000010513 // sinkMBB:
10514 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
10515 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000010516 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
10517 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000010518 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
10519 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
10520
Dan Gohman14152b42010-07-06 20:24:04 +000010521 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000010522 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000010523}
10524
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010525MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010526X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010527 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010528 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10529 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010530
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000010531 assert(!Subtarget->isTargetEnvMacho());
10532
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010533 // The lowering is pretty easy: we're just emitting the call to _alloca. The
10534 // non-trivial part is impdef of ESP.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010535
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000010536 if (Subtarget->isTargetWin64()) {
10537 if (Subtarget->isTargetCygMing()) {
10538 // ___chkstk(Mingw64):
10539 // Clobbers R10, R11, RAX and EFLAGS.
10540 // Updates RSP.
10541 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
10542 .addExternalSymbol("___chkstk")
10543 .addReg(X86::RAX, RegState::Implicit)
10544 .addReg(X86::RSP, RegState::Implicit)
10545 .addReg(X86::RAX, RegState::Define | RegState::Implicit)
10546 .addReg(X86::RSP, RegState::Define | RegState::Implicit)
10547 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
10548 } else {
10549 // __chkstk(MSVCRT): does not update stack pointer.
10550 // Clobbers R10, R11 and EFLAGS.
10551 // FIXME: RAX(allocated size) might be reused and not killed.
10552 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
10553 .addExternalSymbol("__chkstk")
10554 .addReg(X86::RAX, RegState::Implicit)
10555 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
10556 // RAX has the offset to subtracted from RSP.
10557 BuildMI(*BB, MI, DL, TII->get(X86::SUB64rr), X86::RSP)
10558 .addReg(X86::RSP)
10559 .addReg(X86::RAX);
10560 }
10561 } else {
10562 const char *StackProbeSymbol =
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010563 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
10564
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000010565 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
10566 .addExternalSymbol(StackProbeSymbol)
10567 .addReg(X86::EAX, RegState::Implicit)
10568 .addReg(X86::ESP, RegState::Implicit)
10569 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
10570 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
10571 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
10572 }
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010573
Dan Gohman14152b42010-07-06 20:24:04 +000010574 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010575 return BB;
10576}
Chris Lattner52600972009-09-02 05:57:00 +000010577
10578MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000010579X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
10580 MachineBasicBlock *BB) const {
10581 // This is pretty easy. We're taking the value that we received from
10582 // our load from the relocation, sticking it in either RDI (x86-64)
10583 // or EAX and doing an indirect call. The return value will then
10584 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000010585 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000010586 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000010587 DebugLoc DL = MI->getDebugLoc();
10588 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000010589
10590 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000010591 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000010592
Eric Christopher30ef0e52010-06-03 04:07:48 +000010593 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000010594 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10595 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000010596 .addReg(X86::RIP)
10597 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010598 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000010599 MI->getOperand(3).getTargetFlags())
10600 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000010601 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000010602 addDirectMem(MIB, X86::RDI);
Eric Christopher61025492010-06-15 23:08:42 +000010603 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000010604 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10605 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000010606 .addReg(0)
10607 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010608 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000010609 MI->getOperand(3).getTargetFlags())
10610 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000010611 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000010612 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010613 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000010614 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10615 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000010616 .addReg(TII->getGlobalBaseReg(F))
10617 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010618 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000010619 MI->getOperand(3).getTargetFlags())
10620 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000010621 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000010622 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010623 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000010624
Dan Gohman14152b42010-07-06 20:24:04 +000010625 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000010626 return BB;
10627}
10628
10629MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000010630X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010631 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000010632 switch (MI->getOpcode()) {
10633 default: assert(false && "Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000010634 case X86::TAILJMPd64:
10635 case X86::TAILJMPr64:
10636 case X86::TAILJMPm64:
10637 assert(!"TAILJMP64 would not be touched here.");
10638 case X86::TCRETURNdi64:
10639 case X86::TCRETURNri64:
10640 case X86::TCRETURNmi64:
10641 // Defs of TCRETURNxx64 has Win64's callee-saved registers, as subset.
10642 // On AMD64, additional defs should be added before register allocation.
10643 if (!Subtarget->isTargetWin64()) {
10644 MI->addRegisterDefined(X86::RSI);
10645 MI->addRegisterDefined(X86::RDI);
10646 MI->addRegisterDefined(X86::XMM6);
10647 MI->addRegisterDefined(X86::XMM7);
10648 MI->addRegisterDefined(X86::XMM8);
10649 MI->addRegisterDefined(X86::XMM9);
10650 MI->addRegisterDefined(X86::XMM10);
10651 MI->addRegisterDefined(X86::XMM11);
10652 MI->addRegisterDefined(X86::XMM12);
10653 MI->addRegisterDefined(X86::XMM13);
10654 MI->addRegisterDefined(X86::XMM14);
10655 MI->addRegisterDefined(X86::XMM15);
10656 }
10657 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010658 case X86::WIN_ALLOCA:
10659 return EmitLoweredWinAlloca(MI, BB);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010660 case X86::TLSCall_32:
10661 case X86::TLSCall_64:
10662 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000010663 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000010664 case X86::CMOV_FR32:
10665 case X86::CMOV_FR64:
10666 case X86::CMOV_V4F32:
10667 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000010668 case X86::CMOV_V2I64:
Chris Lattner314a1132010-03-14 18:31:44 +000010669 case X86::CMOV_GR16:
10670 case X86::CMOV_GR32:
10671 case X86::CMOV_RFP32:
10672 case X86::CMOV_RFP64:
10673 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010674 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000010675
Dale Johannesen849f2142007-07-03 00:53:03 +000010676 case X86::FP32_TO_INT16_IN_MEM:
10677 case X86::FP32_TO_INT32_IN_MEM:
10678 case X86::FP32_TO_INT64_IN_MEM:
10679 case X86::FP64_TO_INT16_IN_MEM:
10680 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000010681 case X86::FP64_TO_INT64_IN_MEM:
10682 case X86::FP80_TO_INT16_IN_MEM:
10683 case X86::FP80_TO_INT32_IN_MEM:
10684 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000010685 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10686 DebugLoc DL = MI->getDebugLoc();
10687
Evan Cheng60c07e12006-07-05 22:17:51 +000010688 // Change the floating point control register to use "round towards zero"
10689 // mode when truncating to an integer value.
10690 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000010691 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000010692 addFrameReference(BuildMI(*BB, MI, DL,
10693 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010694
10695 // Load the old value of the high byte of the control word...
10696 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +000010697 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohman14152b42010-07-06 20:24:04 +000010698 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000010699 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010700
10701 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000010702 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000010703 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000010704
10705 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000010706 addFrameReference(BuildMI(*BB, MI, DL,
10707 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010708
10709 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000010710 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000010711 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000010712
10713 // Get the X86 opcode to use.
10714 unsigned Opc;
10715 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010716 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000010717 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
10718 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
10719 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
10720 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
10721 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
10722 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000010723 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
10724 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
10725 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000010726 }
10727
10728 X86AddressMode AM;
10729 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000010730 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000010731 AM.BaseType = X86AddressMode::RegBase;
10732 AM.Base.Reg = Op.getReg();
10733 } else {
10734 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000010735 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000010736 }
10737 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000010738 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000010739 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010740 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000010741 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000010742 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010743 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000010744 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000010745 AM.GV = Op.getGlobal();
10746 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000010747 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010748 }
Dan Gohman14152b42010-07-06 20:24:04 +000010749 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010750 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000010751
10752 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000010753 addFrameReference(BuildMI(*BB, MI, DL,
10754 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010755
Dan Gohman14152b42010-07-06 20:24:04 +000010756 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000010757 return BB;
10758 }
Eric Christopherb120ab42009-08-18 22:50:32 +000010759 // String/text processing lowering.
10760 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010761 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000010762 return EmitPCMP(MI, BB, 3, false /* in-mem */);
10763 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010764 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000010765 return EmitPCMP(MI, BB, 3, true /* in-mem */);
10766 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010767 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000010768 return EmitPCMP(MI, BB, 5, false /* in mem */);
10769 case X86::PCMPESTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010770 case X86::VPCMPESTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000010771 return EmitPCMP(MI, BB, 5, true /* in mem */);
10772
Eric Christopher228232b2010-11-30 07:20:12 +000010773 // Thread synchronization.
10774 case X86::MONITOR:
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010775 return EmitMonitor(MI, BB);
Eric Christopher228232b2010-11-30 07:20:12 +000010776 case X86::MWAIT:
10777 return EmitMwait(MI, BB);
10778
Eric Christopherb120ab42009-08-18 22:50:32 +000010779 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +000010780 case X86::ATOMAND32:
10781 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010782 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010783 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010784 X86::NOT32r, X86::EAX,
10785 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000010786 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +000010787 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
10788 X86::OR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010789 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010790 X86::NOT32r, X86::EAX,
10791 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000010792 case X86::ATOMXOR32:
10793 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010794 X86::XOR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010795 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010796 X86::NOT32r, X86::EAX,
10797 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010798 case X86::ATOMNAND32:
10799 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010800 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010801 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010802 X86::NOT32r, X86::EAX,
10803 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +000010804 case X86::ATOMMIN32:
10805 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
10806 case X86::ATOMMAX32:
10807 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
10808 case X86::ATOMUMIN32:
10809 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
10810 case X86::ATOMUMAX32:
10811 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +000010812
10813 case X86::ATOMAND16:
10814 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
10815 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010816 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010817 X86::NOT16r, X86::AX,
10818 X86::GR16RegisterClass);
10819 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +000010820 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010821 X86::OR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010822 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010823 X86::NOT16r, X86::AX,
10824 X86::GR16RegisterClass);
10825 case X86::ATOMXOR16:
10826 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
10827 X86::XOR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010828 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010829 X86::NOT16r, X86::AX,
10830 X86::GR16RegisterClass);
10831 case X86::ATOMNAND16:
10832 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
10833 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010834 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010835 X86::NOT16r, X86::AX,
10836 X86::GR16RegisterClass, true);
10837 case X86::ATOMMIN16:
10838 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
10839 case X86::ATOMMAX16:
10840 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
10841 case X86::ATOMUMIN16:
10842 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
10843 case X86::ATOMUMAX16:
10844 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
10845
10846 case X86::ATOMAND8:
10847 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
10848 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010849 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010850 X86::NOT8r, X86::AL,
10851 X86::GR8RegisterClass);
10852 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +000010853 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010854 X86::OR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010855 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010856 X86::NOT8r, X86::AL,
10857 X86::GR8RegisterClass);
10858 case X86::ATOMXOR8:
10859 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
10860 X86::XOR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010861 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010862 X86::NOT8r, X86::AL,
10863 X86::GR8RegisterClass);
10864 case X86::ATOMNAND8:
10865 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
10866 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010867 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010868 X86::NOT8r, X86::AL,
10869 X86::GR8RegisterClass, true);
10870 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010871 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +000010872 case X86::ATOMAND64:
10873 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010874 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010875 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010876 X86::NOT64r, X86::RAX,
10877 X86::GR64RegisterClass);
10878 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +000010879 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
10880 X86::OR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010881 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010882 X86::NOT64r, X86::RAX,
10883 X86::GR64RegisterClass);
10884 case X86::ATOMXOR64:
10885 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010886 X86::XOR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010887 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010888 X86::NOT64r, X86::RAX,
10889 X86::GR64RegisterClass);
10890 case X86::ATOMNAND64:
10891 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
10892 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010893 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010894 X86::NOT64r, X86::RAX,
10895 X86::GR64RegisterClass, true);
10896 case X86::ATOMMIN64:
10897 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
10898 case X86::ATOMMAX64:
10899 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
10900 case X86::ATOMUMIN64:
10901 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
10902 case X86::ATOMUMAX64:
10903 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010904
10905 // This group does 64-bit operations on a 32-bit host.
10906 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010907 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010908 X86::AND32rr, X86::AND32rr,
10909 X86::AND32ri, X86::AND32ri,
10910 false);
10911 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010912 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010913 X86::OR32rr, X86::OR32rr,
10914 X86::OR32ri, X86::OR32ri,
10915 false);
10916 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010917 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010918 X86::XOR32rr, X86::XOR32rr,
10919 X86::XOR32ri, X86::XOR32ri,
10920 false);
10921 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010922 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010923 X86::AND32rr, X86::AND32rr,
10924 X86::AND32ri, X86::AND32ri,
10925 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010926 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010927 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010928 X86::ADD32rr, X86::ADC32rr,
10929 X86::ADD32ri, X86::ADC32ri,
10930 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010931 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010932 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010933 X86::SUB32rr, X86::SBB32rr,
10934 X86::SUB32ri, X86::SBB32ri,
10935 false);
Dale Johannesen880ae362008-10-03 22:25:52 +000010936 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010937 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +000010938 X86::MOV32rr, X86::MOV32rr,
10939 X86::MOV32ri, X86::MOV32ri,
10940 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010941 case X86::VASTART_SAVE_XMM_REGS:
10942 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000010943
10944 case X86::VAARG_64:
10945 return EmitVAARG64WithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000010946 }
10947}
10948
10949//===----------------------------------------------------------------------===//
10950// X86 Optimization Hooks
10951//===----------------------------------------------------------------------===//
10952
Dan Gohman475871a2008-07-27 21:46:04 +000010953void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +000010954 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000010955 APInt &KnownZero,
10956 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000010957 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000010958 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010959 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000010960 assert((Opc >= ISD::BUILTIN_OP_END ||
10961 Opc == ISD::INTRINSIC_WO_CHAIN ||
10962 Opc == ISD::INTRINSIC_W_CHAIN ||
10963 Opc == ISD::INTRINSIC_VOID) &&
10964 "Should use MaskedValueIsZero if you don't know whether Op"
10965 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010966
Dan Gohmanf4f92f52008-02-13 23:07:24 +000010967 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010968 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000010969 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000010970 case X86ISD::ADD:
10971 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000010972 case X86ISD::ADC:
10973 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000010974 case X86ISD::SMUL:
10975 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000010976 case X86ISD::INC:
10977 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000010978 case X86ISD::OR:
10979 case X86ISD::XOR:
10980 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000010981 // These nodes' second result is a boolean.
10982 if (Op.getResNo() == 0)
10983 break;
10984 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010985 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000010986 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
10987 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000010988 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010989 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010990}
Chris Lattner259e97c2006-01-31 19:43:35 +000010991
Owen Andersonbc146b02010-09-21 20:42:50 +000010992unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
10993 unsigned Depth) const {
10994 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
10995 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
10996 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000010997
Owen Andersonbc146b02010-09-21 20:42:50 +000010998 // Fallback case.
10999 return 1;
11000}
11001
Evan Cheng206ee9d2006-07-07 08:33:52 +000011002/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000011003/// node is a GlobalAddress + offset.
11004bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000011005 const GlobalValue* &GA,
11006 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000011007 if (N->getOpcode() == X86ISD::Wrapper) {
11008 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000011009 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000011010 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000011011 return true;
11012 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000011013 }
Evan Chengad4196b2008-05-12 19:56:52 +000011014 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000011015}
11016
Evan Cheng206ee9d2006-07-07 08:33:52 +000011017/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
11018/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
11019/// if the load addresses are consecutive, non-overlapping, and in the right
Nate Begemanfdea31a2010-03-24 20:49:50 +000011020/// order.
Dan Gohman475871a2008-07-27 21:46:04 +000011021static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Mon P Wanga0fd0d52010-12-19 23:55:53 +000011022 TargetLowering::DAGCombinerInfo &DCI) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000011023 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000011024 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000011025
Eli Friedman7a5e5552009-06-07 06:52:44 +000011026 if (VT.getSizeInBits() != 128)
11027 return SDValue();
11028
Mon P Wanga0fd0d52010-12-19 23:55:53 +000011029 // Don't create instructions with illegal types after legalize types has run.
11030 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
11031 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
11032 return SDValue();
11033
Nate Begemanfdea31a2010-03-24 20:49:50 +000011034 SmallVector<SDValue, 16> Elts;
11035 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000011036 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000011037
Nate Begemanfdea31a2010-03-24 20:49:50 +000011038 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000011039}
Evan Chengd880b972008-05-09 21:53:03 +000011040
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000011041/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
11042/// generation and convert it from being a bunch of shuffles and extracts
11043/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000011044static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
11045 const TargetLowering &TLI) {
11046 SDValue InputVector = N->getOperand(0);
11047
11048 // Only operate on vectors of 4 elements, where the alternative shuffling
11049 // gets to be more expensive.
11050 if (InputVector.getValueType() != MVT::v4i32)
11051 return SDValue();
11052
11053 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
11054 // single use which is a sign-extend or zero-extend, and all elements are
11055 // used.
11056 SmallVector<SDNode *, 4> Uses;
11057 unsigned ExtractedElements = 0;
11058 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
11059 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
11060 if (UI.getUse().getResNo() != InputVector.getResNo())
11061 return SDValue();
11062
11063 SDNode *Extract = *UI;
11064 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
11065 return SDValue();
11066
11067 if (Extract->getValueType(0) != MVT::i32)
11068 return SDValue();
11069 if (!Extract->hasOneUse())
11070 return SDValue();
11071 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
11072 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
11073 return SDValue();
11074 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
11075 return SDValue();
11076
11077 // Record which element was extracted.
11078 ExtractedElements |=
11079 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
11080
11081 Uses.push_back(Extract);
11082 }
11083
11084 // If not all the elements were used, this may not be worthwhile.
11085 if (ExtractedElements != 15)
11086 return SDValue();
11087
11088 // Ok, we've now decided to do the transformation.
11089 DebugLoc dl = InputVector.getDebugLoc();
11090
11091 // Store the value to a temporary stack slot.
11092 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000011093 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
11094 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000011095
11096 // Replace each use (extract) with a load of the appropriate element.
11097 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
11098 UE = Uses.end(); UI != UE; ++UI) {
11099 SDNode *Extract = *UI;
11100
Nadav Rotem86694292011-05-17 08:31:57 +000011101 // cOMpute the element's address.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000011102 SDValue Idx = Extract->getOperand(1);
11103 unsigned EltSize =
11104 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
11105 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
11106 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
11107
Nadav Rotem86694292011-05-17 08:31:57 +000011108 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
Chris Lattner51abfe42010-09-21 06:02:19 +000011109 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000011110
11111 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000011112 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000011113 ScalarAddr, MachinePointerInfo(),
11114 false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000011115
11116 // Replace the exact with the load.
11117 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
11118 }
11119
11120 // The replacement was made in place; don't return anything.
11121 return SDValue();
11122}
11123
Chris Lattner83e6c992006-10-04 06:57:07 +000011124/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000011125static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +000011126 const X86Subtarget *Subtarget) {
11127 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000011128 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000011129 // Get the LHS/RHS of the select.
11130 SDValue LHS = N->getOperand(1);
11131 SDValue RHS = N->getOperand(2);
Eric Christopherfd179292009-08-27 18:07:15 +000011132
Dan Gohman670e5392009-09-21 18:03:22 +000011133 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000011134 // instructions match the semantics of the common C idiom x<y?x:y but not
11135 // x<=y?x:y, because of how they handle negative zero (which can be
11136 // ignored in unsafe-math mode).
Chris Lattner83e6c992006-10-04 06:57:07 +000011137 if (Subtarget->hasSSE2() &&
Owen Anderson825b72b2009-08-11 20:47:22 +000011138 (LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64) &&
Chris Lattner47b4ce82009-03-11 05:48:52 +000011139 Cond.getOpcode() == ISD::SETCC) {
11140 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011141
Chris Lattner47b4ce82009-03-11 05:48:52 +000011142 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000011143 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000011144 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
11145 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000011146 switch (CC) {
11147 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000011148 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000011149 // Converting this to a min would handle NaNs incorrectly, and swapping
11150 // the operands would cause it to handle comparisons between positive
11151 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000011152 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000011153 if (!UnsafeFPMath &&
11154 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
11155 break;
11156 std::swap(LHS, RHS);
11157 }
Dan Gohman670e5392009-09-21 18:03:22 +000011158 Opcode = X86ISD::FMIN;
11159 break;
11160 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000011161 // Converting this to a min would handle comparisons between positive
11162 // and negative zero incorrectly.
11163 if (!UnsafeFPMath &&
11164 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
11165 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011166 Opcode = X86ISD::FMIN;
11167 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000011168 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000011169 // Converting this to a min would handle both negative zeros and NaNs
11170 // incorrectly, but we can swap the operands to fix both.
11171 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000011172 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011173 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000011174 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011175 Opcode = X86ISD::FMIN;
11176 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011177
Dan Gohman670e5392009-09-21 18:03:22 +000011178 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000011179 // Converting this to a max would handle comparisons between positive
11180 // and negative zero incorrectly.
11181 if (!UnsafeFPMath &&
11182 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(LHS))
11183 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011184 Opcode = X86ISD::FMAX;
11185 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000011186 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000011187 // Converting this to a max would handle NaNs incorrectly, and swapping
11188 // the operands would cause it to handle comparisons between positive
11189 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000011190 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000011191 if (!UnsafeFPMath &&
11192 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
11193 break;
11194 std::swap(LHS, RHS);
11195 }
Dan Gohman670e5392009-09-21 18:03:22 +000011196 Opcode = X86ISD::FMAX;
11197 break;
11198 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000011199 // Converting this to a max would handle both negative zeros and NaNs
11200 // incorrectly, but we can swap the operands to fix both.
11201 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000011202 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011203 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011204 case ISD::SETGE:
11205 Opcode = X86ISD::FMAX;
11206 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000011207 }
Dan Gohman670e5392009-09-21 18:03:22 +000011208 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000011209 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
11210 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000011211 switch (CC) {
11212 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000011213 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000011214 // Converting this to a min would handle comparisons between positive
11215 // and negative zero incorrectly, and swapping the operands would
11216 // cause it to handle NaNs incorrectly.
11217 if (!UnsafeFPMath &&
11218 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000011219 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000011220 break;
11221 std::swap(LHS, RHS);
11222 }
Dan Gohman670e5392009-09-21 18:03:22 +000011223 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000011224 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011225 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000011226 // Converting this to a min would handle NaNs incorrectly.
11227 if (!UnsafeFPMath &&
11228 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
11229 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011230 Opcode = X86ISD::FMIN;
11231 break;
11232 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000011233 // Converting this to a min would handle both negative zeros and NaNs
11234 // incorrectly, but we can swap the operands to fix both.
11235 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000011236 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011237 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011238 case ISD::SETGE:
11239 Opcode = X86ISD::FMIN;
11240 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011241
Dan Gohman670e5392009-09-21 18:03:22 +000011242 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000011243 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000011244 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000011245 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011246 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000011247 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011248 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000011249 // Converting this to a max would handle comparisons between positive
11250 // and negative zero incorrectly, and swapping the operands would
11251 // cause it to handle NaNs incorrectly.
11252 if (!UnsafeFPMath &&
11253 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000011254 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000011255 break;
11256 std::swap(LHS, RHS);
11257 }
Dan Gohman670e5392009-09-21 18:03:22 +000011258 Opcode = X86ISD::FMAX;
11259 break;
11260 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000011261 // Converting this to a max would handle both negative zeros and NaNs
11262 // incorrectly, but we can swap the operands to fix both.
11263 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000011264 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011265 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000011266 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011267 Opcode = X86ISD::FMAX;
11268 break;
11269 }
Chris Lattner83e6c992006-10-04 06:57:07 +000011270 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011271
Chris Lattner47b4ce82009-03-11 05:48:52 +000011272 if (Opcode)
11273 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000011274 }
Eric Christopherfd179292009-08-27 18:07:15 +000011275
Chris Lattnerd1980a52009-03-12 06:52:53 +000011276 // If this is a select between two integer constants, try to do some
11277 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000011278 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
11279 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000011280 // Don't do this for crazy integer types.
11281 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
11282 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000011283 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000011284 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000011285
Chris Lattnercee56e72009-03-13 05:53:31 +000011286 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000011287 // Efficiently invertible.
11288 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
11289 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
11290 isa<ConstantSDNode>(Cond.getOperand(1))))) {
11291 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000011292 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000011293 }
Eric Christopherfd179292009-08-27 18:07:15 +000011294
Chris Lattnerd1980a52009-03-12 06:52:53 +000011295 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000011296 if (FalseC->getAPIntValue() == 0 &&
11297 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000011298 if (NeedsCondInvert) // Invert the condition if needed.
11299 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
11300 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011301
Chris Lattnerd1980a52009-03-12 06:52:53 +000011302 // Zero extend the condition if needed.
11303 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011304
Chris Lattnercee56e72009-03-13 05:53:31 +000011305 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000011306 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000011307 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000011308 }
Eric Christopherfd179292009-08-27 18:07:15 +000011309
Chris Lattner97a29a52009-03-13 05:22:11 +000011310 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000011311 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000011312 if (NeedsCondInvert) // Invert the condition if needed.
11313 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
11314 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011315
Chris Lattner97a29a52009-03-13 05:22:11 +000011316 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000011317 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
11318 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000011319 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000011320 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000011321 }
Eric Christopherfd179292009-08-27 18:07:15 +000011322
Chris Lattnercee56e72009-03-13 05:53:31 +000011323 // Optimize cases that will turn into an LEA instruction. This requires
11324 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000011325 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000011326 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000011327 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000011328
Chris Lattnercee56e72009-03-13 05:53:31 +000011329 bool isFastMultiplier = false;
11330 if (Diff < 10) {
11331 switch ((unsigned char)Diff) {
11332 default: break;
11333 case 1: // result = add base, cond
11334 case 2: // result = lea base( , cond*2)
11335 case 3: // result = lea base(cond, cond*2)
11336 case 4: // result = lea base( , cond*4)
11337 case 5: // result = lea base(cond, cond*4)
11338 case 8: // result = lea base( , cond*8)
11339 case 9: // result = lea base(cond, cond*8)
11340 isFastMultiplier = true;
11341 break;
11342 }
11343 }
Eric Christopherfd179292009-08-27 18:07:15 +000011344
Chris Lattnercee56e72009-03-13 05:53:31 +000011345 if (isFastMultiplier) {
11346 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
11347 if (NeedsCondInvert) // Invert the condition if needed.
11348 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
11349 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011350
Chris Lattnercee56e72009-03-13 05:53:31 +000011351 // Zero extend the condition if needed.
11352 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
11353 Cond);
11354 // Scale the condition by the difference.
11355 if (Diff != 1)
11356 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
11357 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011358
Chris Lattnercee56e72009-03-13 05:53:31 +000011359 // Add the base if non-zero.
11360 if (FalseC->getAPIntValue() != 0)
11361 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
11362 SDValue(FalseC, 0));
11363 return Cond;
11364 }
Eric Christopherfd179292009-08-27 18:07:15 +000011365 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000011366 }
11367 }
Eric Christopherfd179292009-08-27 18:07:15 +000011368
Dan Gohman475871a2008-07-27 21:46:04 +000011369 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000011370}
11371
Chris Lattnerd1980a52009-03-12 06:52:53 +000011372/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
11373static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
11374 TargetLowering::DAGCombinerInfo &DCI) {
11375 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000011376
Chris Lattnerd1980a52009-03-12 06:52:53 +000011377 // If the flag operand isn't dead, don't touch this CMOV.
11378 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
11379 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000011380
Evan Chengb5a55d92011-05-24 01:48:22 +000011381 SDValue FalseOp = N->getOperand(0);
11382 SDValue TrueOp = N->getOperand(1);
11383 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
11384 SDValue Cond = N->getOperand(3);
11385 if (CC == X86::COND_E || CC == X86::COND_NE) {
11386 switch (Cond.getOpcode()) {
11387 default: break;
11388 case X86ISD::BSR:
11389 case X86ISD::BSF:
11390 // If operand of BSR / BSF are proven never zero, then ZF cannot be set.
11391 if (DAG.isKnownNeverZero(Cond.getOperand(0)))
11392 return (CC == X86::COND_E) ? FalseOp : TrueOp;
11393 }
11394 }
11395
Chris Lattnerd1980a52009-03-12 06:52:53 +000011396 // If this is a select between two integer constants, try to do some
11397 // optimizations. Note that the operands are ordered the opposite of SELECT
11398 // operands.
Evan Chengb5a55d92011-05-24 01:48:22 +000011399 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(TrueOp)) {
11400 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(FalseOp)) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000011401 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
11402 // larger than FalseC (the false value).
Chris Lattnerd1980a52009-03-12 06:52:53 +000011403 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
11404 CC = X86::GetOppositeBranchCondition(CC);
11405 std::swap(TrueC, FalseC);
11406 }
Eric Christopherfd179292009-08-27 18:07:15 +000011407
Chris Lattnerd1980a52009-03-12 06:52:53 +000011408 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000011409 // This is efficient for any integer data type (including i8/i16) and
11410 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000011411 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000011412 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11413 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011414
Chris Lattnerd1980a52009-03-12 06:52:53 +000011415 // Zero extend the condition if needed.
11416 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011417
Chris Lattnerd1980a52009-03-12 06:52:53 +000011418 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
11419 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000011420 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000011421 if (N->getNumValues() == 2) // Dead flag value?
11422 return DCI.CombineTo(N, Cond, SDValue());
11423 return Cond;
11424 }
Eric Christopherfd179292009-08-27 18:07:15 +000011425
Chris Lattnercee56e72009-03-13 05:53:31 +000011426 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
11427 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000011428 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000011429 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11430 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011431
Chris Lattner97a29a52009-03-13 05:22:11 +000011432 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000011433 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
11434 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000011435 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
11436 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000011437
Chris Lattner97a29a52009-03-13 05:22:11 +000011438 if (N->getNumValues() == 2) // Dead flag value?
11439 return DCI.CombineTo(N, Cond, SDValue());
11440 return Cond;
11441 }
Eric Christopherfd179292009-08-27 18:07:15 +000011442
Chris Lattnercee56e72009-03-13 05:53:31 +000011443 // Optimize cases that will turn into an LEA instruction. This requires
11444 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000011445 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000011446 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000011447 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000011448
Chris Lattnercee56e72009-03-13 05:53:31 +000011449 bool isFastMultiplier = false;
11450 if (Diff < 10) {
11451 switch ((unsigned char)Diff) {
11452 default: break;
11453 case 1: // result = add base, cond
11454 case 2: // result = lea base( , cond*2)
11455 case 3: // result = lea base(cond, cond*2)
11456 case 4: // result = lea base( , cond*4)
11457 case 5: // result = lea base(cond, cond*4)
11458 case 8: // result = lea base( , cond*8)
11459 case 9: // result = lea base(cond, cond*8)
11460 isFastMultiplier = true;
11461 break;
11462 }
11463 }
Eric Christopherfd179292009-08-27 18:07:15 +000011464
Chris Lattnercee56e72009-03-13 05:53:31 +000011465 if (isFastMultiplier) {
11466 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000011467 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11468 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000011469 // Zero extend the condition if needed.
11470 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
11471 Cond);
11472 // Scale the condition by the difference.
11473 if (Diff != 1)
11474 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
11475 DAG.getConstant(Diff, Cond.getValueType()));
11476
11477 // Add the base if non-zero.
11478 if (FalseC->getAPIntValue() != 0)
11479 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
11480 SDValue(FalseC, 0));
11481 if (N->getNumValues() == 2) // Dead flag value?
11482 return DCI.CombineTo(N, Cond, SDValue());
11483 return Cond;
11484 }
Eric Christopherfd179292009-08-27 18:07:15 +000011485 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000011486 }
11487 }
11488 return SDValue();
11489}
11490
11491
Evan Cheng0b0cd912009-03-28 05:57:29 +000011492/// PerformMulCombine - Optimize a single multiply with constant into two
11493/// in order to implement it with two cheaper instructions, e.g.
11494/// LEA + SHL, LEA + LEA.
11495static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
11496 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000011497 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
11498 return SDValue();
11499
Owen Andersone50ed302009-08-10 22:56:29 +000011500 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000011501 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000011502 return SDValue();
11503
11504 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
11505 if (!C)
11506 return SDValue();
11507 uint64_t MulAmt = C->getZExtValue();
11508 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
11509 return SDValue();
11510
11511 uint64_t MulAmt1 = 0;
11512 uint64_t MulAmt2 = 0;
11513 if ((MulAmt % 9) == 0) {
11514 MulAmt1 = 9;
11515 MulAmt2 = MulAmt / 9;
11516 } else if ((MulAmt % 5) == 0) {
11517 MulAmt1 = 5;
11518 MulAmt2 = MulAmt / 5;
11519 } else if ((MulAmt % 3) == 0) {
11520 MulAmt1 = 3;
11521 MulAmt2 = MulAmt / 3;
11522 }
11523 if (MulAmt2 &&
11524 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
11525 DebugLoc DL = N->getDebugLoc();
11526
11527 if (isPowerOf2_64(MulAmt2) &&
11528 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
11529 // If second multiplifer is pow2, issue it first. We want the multiply by
11530 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
11531 // is an add.
11532 std::swap(MulAmt1, MulAmt2);
11533
11534 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000011535 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000011536 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000011537 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000011538 else
Evan Cheng73f24c92009-03-30 21:36:47 +000011539 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000011540 DAG.getConstant(MulAmt1, VT));
11541
Eric Christopherfd179292009-08-27 18:07:15 +000011542 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000011543 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000011544 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000011545 else
Evan Cheng73f24c92009-03-30 21:36:47 +000011546 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000011547 DAG.getConstant(MulAmt2, VT));
11548
11549 // Do not add new nodes to DAG combiner worklist.
11550 DCI.CombineTo(N, NewMul, false);
11551 }
11552 return SDValue();
11553}
11554
Evan Chengad9c0a32009-12-15 00:53:42 +000011555static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
11556 SDValue N0 = N->getOperand(0);
11557 SDValue N1 = N->getOperand(1);
11558 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
11559 EVT VT = N0.getValueType();
11560
11561 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
11562 // since the result of setcc_c is all zero's or all ones.
11563 if (N1C && N0.getOpcode() == ISD::AND &&
11564 N0.getOperand(1).getOpcode() == ISD::Constant) {
11565 SDValue N00 = N0.getOperand(0);
11566 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
11567 ((N00.getOpcode() == ISD::ANY_EXTEND ||
11568 N00.getOpcode() == ISD::ZERO_EXTEND) &&
11569 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
11570 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
11571 APInt ShAmt = N1C->getAPIntValue();
11572 Mask = Mask.shl(ShAmt);
11573 if (Mask != 0)
11574 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
11575 N00, DAG.getConstant(Mask, VT));
11576 }
11577 }
11578
11579 return SDValue();
11580}
Evan Cheng0b0cd912009-03-28 05:57:29 +000011581
Nate Begeman740ab032009-01-26 00:52:55 +000011582/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
11583/// when possible.
11584static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
11585 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000011586 EVT VT = N->getValueType(0);
11587 if (!VT.isVector() && VT.isInteger() &&
11588 N->getOpcode() == ISD::SHL)
11589 return PerformSHLCombine(N, DAG);
11590
Nate Begeman740ab032009-01-26 00:52:55 +000011591 // On X86 with SSE2 support, we can transform this to a vector shift if
11592 // all elements are shifted by the same amount. We can't do this in legalize
11593 // because the a constant vector is typically transformed to a constant pool
11594 // so we have no knowledge of the shift amount.
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011595 if (!Subtarget->hasSSE2())
11596 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000011597
Owen Anderson825b72b2009-08-11 20:47:22 +000011598 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011599 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000011600
Mon P Wang3becd092009-01-28 08:12:05 +000011601 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000011602 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000011603 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000011604 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000011605 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
11606 unsigned NumElts = VT.getVectorNumElements();
11607 unsigned i = 0;
11608 for (; i != NumElts; ++i) {
11609 SDValue Arg = ShAmtOp.getOperand(i);
11610 if (Arg.getOpcode() == ISD::UNDEF) continue;
11611 BaseShAmt = Arg;
11612 break;
11613 }
11614 for (; i != NumElts; ++i) {
11615 SDValue Arg = ShAmtOp.getOperand(i);
11616 if (Arg.getOpcode() == ISD::UNDEF) continue;
11617 if (Arg != BaseShAmt) {
11618 return SDValue();
11619 }
11620 }
11621 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000011622 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000011623 SDValue InVec = ShAmtOp.getOperand(0);
11624 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
11625 unsigned NumElts = InVec.getValueType().getVectorNumElements();
11626 unsigned i = 0;
11627 for (; i != NumElts; ++i) {
11628 SDValue Arg = InVec.getOperand(i);
11629 if (Arg.getOpcode() == ISD::UNDEF) continue;
11630 BaseShAmt = Arg;
11631 break;
11632 }
11633 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
11634 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000011635 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000011636 if (C->getZExtValue() == SplatIdx)
11637 BaseShAmt = InVec.getOperand(1);
11638 }
11639 }
11640 if (BaseShAmt.getNode() == 0)
11641 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
11642 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +000011643 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011644 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000011645
Mon P Wangefa42202009-09-03 19:56:25 +000011646 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000011647 if (EltVT.bitsGT(MVT::i32))
11648 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
11649 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000011650 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000011651
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011652 // The shift amount is identical so we can do a vector shift.
11653 SDValue ValOp = N->getOperand(0);
11654 switch (N->getOpcode()) {
11655 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000011656 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011657 break;
11658 case ISD::SHL:
Owen Anderson825b72b2009-08-11 20:47:22 +000011659 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011660 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011661 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011662 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011663 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011664 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011665 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011666 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011667 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011668 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011669 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011670 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011671 break;
11672 case ISD::SRA:
Owen Anderson825b72b2009-08-11 20:47:22 +000011673 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011674 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011675 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011676 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011677 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011678 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011679 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011680 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011681 break;
11682 case ISD::SRL:
Owen Anderson825b72b2009-08-11 20:47:22 +000011683 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011684 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011685 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011686 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011687 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011688 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011689 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011690 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011691 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011692 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011693 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011694 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011695 break;
Nate Begeman740ab032009-01-26 00:52:55 +000011696 }
11697 return SDValue();
11698}
11699
Nate Begemanb65c1752010-12-17 22:55:37 +000011700
Stuart Hastings865f0932011-06-03 23:53:54 +000011701// CMPEQCombine - Recognize the distinctive (AND (setcc ...) (setcc ..))
11702// where both setccs reference the same FP CMP, and rewrite for CMPEQSS
11703// and friends. Likewise for OR -> CMPNEQSS.
11704static SDValue CMPEQCombine(SDNode *N, SelectionDAG &DAG,
11705 TargetLowering::DAGCombinerInfo &DCI,
11706 const X86Subtarget *Subtarget) {
11707 unsigned opcode;
11708
11709 // SSE1 supports CMP{eq|ne}SS, and SSE2 added CMP{eq|ne}SD, but
11710 // we're requiring SSE2 for both.
11711 if (Subtarget->hasSSE2() && isAndOrOfSetCCs(SDValue(N, 0U), opcode)) {
11712 SDValue N0 = N->getOperand(0);
11713 SDValue N1 = N->getOperand(1);
11714 SDValue CMP0 = N0->getOperand(1);
11715 SDValue CMP1 = N1->getOperand(1);
11716 DebugLoc DL = N->getDebugLoc();
11717
11718 // The SETCCs should both refer to the same CMP.
11719 if (CMP0.getOpcode() != X86ISD::CMP || CMP0 != CMP1)
11720 return SDValue();
11721
11722 SDValue CMP00 = CMP0->getOperand(0);
11723 SDValue CMP01 = CMP0->getOperand(1);
11724 EVT VT = CMP00.getValueType();
11725
11726 if (VT == MVT::f32 || VT == MVT::f64) {
11727 bool ExpectingFlags = false;
11728 // Check for any users that want flags:
11729 for (SDNode::use_iterator UI = N->use_begin(),
11730 UE = N->use_end();
11731 !ExpectingFlags && UI != UE; ++UI)
11732 switch (UI->getOpcode()) {
11733 default:
11734 case ISD::BR_CC:
11735 case ISD::BRCOND:
11736 case ISD::SELECT:
11737 ExpectingFlags = true;
11738 break;
11739 case ISD::CopyToReg:
11740 case ISD::SIGN_EXTEND:
11741 case ISD::ZERO_EXTEND:
11742 case ISD::ANY_EXTEND:
11743 break;
11744 }
11745
11746 if (!ExpectingFlags) {
11747 enum X86::CondCode cc0 = (enum X86::CondCode)N0.getConstantOperandVal(0);
11748 enum X86::CondCode cc1 = (enum X86::CondCode)N1.getConstantOperandVal(0);
11749
11750 if (cc1 == X86::COND_E || cc1 == X86::COND_NE) {
11751 X86::CondCode tmp = cc0;
11752 cc0 = cc1;
11753 cc1 = tmp;
11754 }
11755
11756 if ((cc0 == X86::COND_E && cc1 == X86::COND_NP) ||
11757 (cc0 == X86::COND_NE && cc1 == X86::COND_P)) {
11758 bool is64BitFP = (CMP00.getValueType() == MVT::f64);
11759 X86ISD::NodeType NTOperator = is64BitFP ?
11760 X86ISD::FSETCCsd : X86ISD::FSETCCss;
11761 // FIXME: need symbolic constants for these magic numbers.
11762 // See X86ATTInstPrinter.cpp:printSSECC().
11763 unsigned x86cc = (cc0 == X86::COND_E) ? 0 : 4;
11764 SDValue OnesOrZeroesF = DAG.getNode(NTOperator, DL, MVT::f32, CMP00, CMP01,
11765 DAG.getConstant(x86cc, MVT::i8));
11766 SDValue OnesOrZeroesI = DAG.getNode(ISD::BITCAST, DL, MVT::i32,
11767 OnesOrZeroesF);
11768 SDValue ANDed = DAG.getNode(ISD::AND, DL, MVT::i32, OnesOrZeroesI,
11769 DAG.getConstant(1, MVT::i32));
11770 SDValue OneBitOfTruth = DAG.getNode(ISD::TRUNCATE, DL, MVT::i8, ANDed);
11771 return OneBitOfTruth;
11772 }
11773 }
11774 }
11775 }
11776 return SDValue();
11777}
11778
Nate Begemanb65c1752010-12-17 22:55:37 +000011779static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
11780 TargetLowering::DAGCombinerInfo &DCI,
11781 const X86Subtarget *Subtarget) {
11782 if (DCI.isBeforeLegalizeOps())
11783 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011784
Stuart Hastings865f0932011-06-03 23:53:54 +000011785 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
11786 if (R.getNode())
11787 return R;
11788
Nate Begemanb65c1752010-12-17 22:55:37 +000011789 // Want to form PANDN nodes, in the hopes of then easily combining them with
11790 // OR and AND nodes to form PBLEND/PSIGN.
11791 EVT VT = N->getValueType(0);
11792 if (VT != MVT::v2i64)
11793 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011794
Nate Begemanb65c1752010-12-17 22:55:37 +000011795 SDValue N0 = N->getOperand(0);
11796 SDValue N1 = N->getOperand(1);
11797 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011798
Nate Begemanb65c1752010-12-17 22:55:37 +000011799 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011800 if (N0.getOpcode() == ISD::XOR &&
Nate Begemanb65c1752010-12-17 22:55:37 +000011801 ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
11802 return DAG.getNode(X86ISD::PANDN, DL, VT, N0.getOperand(0), N1);
11803
11804 // Check RHS for vnot
11805 if (N1.getOpcode() == ISD::XOR &&
11806 ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
11807 return DAG.getNode(X86ISD::PANDN, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011808
Nate Begemanb65c1752010-12-17 22:55:37 +000011809 return SDValue();
11810}
11811
Evan Cheng760d1942010-01-04 21:22:48 +000011812static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000011813 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000011814 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +000011815 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000011816 return SDValue();
11817
Stuart Hastings865f0932011-06-03 23:53:54 +000011818 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
11819 if (R.getNode())
11820 return R;
11821
Evan Cheng760d1942010-01-04 21:22:48 +000011822 EVT VT = N->getValueType(0);
Nate Begemanb65c1752010-12-17 22:55:37 +000011823 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64 && VT != MVT::v2i64)
Evan Cheng760d1942010-01-04 21:22:48 +000011824 return SDValue();
11825
Evan Cheng760d1942010-01-04 21:22:48 +000011826 SDValue N0 = N->getOperand(0);
11827 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011828
Nate Begemanb65c1752010-12-17 22:55:37 +000011829 // look for psign/blend
11830 if (Subtarget->hasSSSE3()) {
11831 if (VT == MVT::v2i64) {
11832 // Canonicalize pandn to RHS
11833 if (N0.getOpcode() == X86ISD::PANDN)
11834 std::swap(N0, N1);
11835 // or (and (m, x), (pandn m, y))
11836 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::PANDN) {
11837 SDValue Mask = N1.getOperand(0);
11838 SDValue X = N1.getOperand(1);
11839 SDValue Y;
11840 if (N0.getOperand(0) == Mask)
11841 Y = N0.getOperand(1);
11842 if (N0.getOperand(1) == Mask)
11843 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011844
Nate Begemanb65c1752010-12-17 22:55:37 +000011845 // Check to see if the mask appeared in both the AND and PANDN and
11846 if (!Y.getNode())
11847 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011848
Nate Begemanb65c1752010-12-17 22:55:37 +000011849 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
11850 if (Mask.getOpcode() != ISD::BITCAST ||
11851 X.getOpcode() != ISD::BITCAST ||
11852 Y.getOpcode() != ISD::BITCAST)
11853 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011854
Nate Begemanb65c1752010-12-17 22:55:37 +000011855 // Look through mask bitcast.
11856 Mask = Mask.getOperand(0);
11857 EVT MaskVT = Mask.getValueType();
11858
11859 // Validate that the Mask operand is a vector sra node. The sra node
11860 // will be an intrinsic.
11861 if (Mask.getOpcode() != ISD::INTRINSIC_WO_CHAIN)
11862 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011863
Nate Begemanb65c1752010-12-17 22:55:37 +000011864 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
11865 // there is no psrai.b
11866 switch (cast<ConstantSDNode>(Mask.getOperand(0))->getZExtValue()) {
11867 case Intrinsic::x86_sse2_psrai_w:
11868 case Intrinsic::x86_sse2_psrai_d:
11869 break;
11870 default: return SDValue();
11871 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011872
Nate Begemanb65c1752010-12-17 22:55:37 +000011873 // Check that the SRA is all signbits.
11874 SDValue SraC = Mask.getOperand(2);
11875 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
11876 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
11877 if ((SraAmt + 1) != EltBits)
11878 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011879
Nate Begemanb65c1752010-12-17 22:55:37 +000011880 DebugLoc DL = N->getDebugLoc();
11881
11882 // Now we know we at least have a plendvb with the mask val. See if
11883 // we can form a psignb/w/d.
11884 // psign = x.type == y.type == mask.type && y = sub(0, x);
11885 X = X.getOperand(0);
11886 Y = Y.getOperand(0);
11887 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
11888 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
11889 X.getValueType() == MaskVT && X.getValueType() == Y.getValueType()){
11890 unsigned Opc = 0;
11891 switch (EltBits) {
11892 case 8: Opc = X86ISD::PSIGNB; break;
11893 case 16: Opc = X86ISD::PSIGNW; break;
11894 case 32: Opc = X86ISD::PSIGND; break;
11895 default: break;
11896 }
11897 if (Opc) {
11898 SDValue Sign = DAG.getNode(Opc, DL, MaskVT, X, Mask.getOperand(1));
11899 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Sign);
11900 }
11901 }
11902 // PBLENDVB only available on SSE 4.1
11903 if (!Subtarget->hasSSE41())
11904 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011905
Nate Begemanb65c1752010-12-17 22:55:37 +000011906 X = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, X);
11907 Y = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Y);
11908 Mask = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Mask);
Nate Begeman672fb622010-12-20 22:04:24 +000011909 Mask = DAG.getNode(X86ISD::PBLENDVB, DL, MVT::v16i8, X, Y, Mask);
Nate Begemanb65c1752010-12-17 22:55:37 +000011910 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Mask);
11911 }
11912 }
11913 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011914
Nate Begemanb65c1752010-12-17 22:55:37 +000011915 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000011916 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
11917 std::swap(N0, N1);
11918 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
11919 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000011920 if (!N0.hasOneUse() || !N1.hasOneUse())
11921 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000011922
11923 SDValue ShAmt0 = N0.getOperand(1);
11924 if (ShAmt0.getValueType() != MVT::i8)
11925 return SDValue();
11926 SDValue ShAmt1 = N1.getOperand(1);
11927 if (ShAmt1.getValueType() != MVT::i8)
11928 return SDValue();
11929 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
11930 ShAmt0 = ShAmt0.getOperand(0);
11931 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
11932 ShAmt1 = ShAmt1.getOperand(0);
11933
11934 DebugLoc DL = N->getDebugLoc();
11935 unsigned Opc = X86ISD::SHLD;
11936 SDValue Op0 = N0.getOperand(0);
11937 SDValue Op1 = N1.getOperand(0);
11938 if (ShAmt0.getOpcode() == ISD::SUB) {
11939 Opc = X86ISD::SHRD;
11940 std::swap(Op0, Op1);
11941 std::swap(ShAmt0, ShAmt1);
11942 }
11943
Evan Cheng8b1190a2010-04-28 01:18:01 +000011944 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000011945 if (ShAmt1.getOpcode() == ISD::SUB) {
11946 SDValue Sum = ShAmt1.getOperand(0);
11947 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000011948 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
11949 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
11950 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
11951 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000011952 return DAG.getNode(Opc, DL, VT,
11953 Op0, Op1,
11954 DAG.getNode(ISD::TRUNCATE, DL,
11955 MVT::i8, ShAmt0));
11956 }
11957 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
11958 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
11959 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000011960 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000011961 return DAG.getNode(Opc, DL, VT,
11962 N0.getOperand(0), N1.getOperand(0),
11963 DAG.getNode(ISD::TRUNCATE, DL,
11964 MVT::i8, ShAmt0));
11965 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011966
Evan Cheng760d1942010-01-04 21:22:48 +000011967 return SDValue();
11968}
11969
Chris Lattner149a4e52008-02-22 02:09:43 +000011970/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000011971static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000011972 const X86Subtarget *Subtarget) {
Chris Lattner149a4e52008-02-22 02:09:43 +000011973 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
11974 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000011975 // A preferable solution to the general problem is to figure out the right
11976 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000011977
11978 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng7e2ff772008-05-08 00:57:18 +000011979 StoreSDNode *St = cast<StoreSDNode>(N);
Owen Andersone50ed302009-08-10 22:56:29 +000011980 EVT VT = St->getValue().getValueType();
Evan Cheng536e6672009-03-12 05:59:15 +000011981 if (VT.getSizeInBits() != 64)
11982 return SDValue();
11983
Devang Patel578efa92009-06-05 21:57:13 +000011984 const Function *F = DAG.getMachineFunction().getFunction();
11985 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Eric Christopherfd179292009-08-27 18:07:15 +000011986 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
Devang Patel578efa92009-06-05 21:57:13 +000011987 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +000011988 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000011989 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000011990 isa<LoadSDNode>(St->getValue()) &&
11991 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
11992 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000011993 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011994 LoadSDNode *Ld = 0;
11995 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000011996 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000011997 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011998 // Must be a store of a load. We currently handle two cases: the load
11999 // is a direct child, and it's under an intervening TokenFactor. It is
12000 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000012001 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000012002 Ld = cast<LoadSDNode>(St->getChain());
12003 else if (St->getValue().hasOneUse() &&
12004 ChainVal->getOpcode() == ISD::TokenFactor) {
12005 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000012006 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000012007 TokenFactorIndex = i;
12008 Ld = cast<LoadSDNode>(St->getValue());
12009 } else
12010 Ops.push_back(ChainVal->getOperand(i));
12011 }
12012 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000012013
Evan Cheng536e6672009-03-12 05:59:15 +000012014 if (!Ld || !ISD::isNormalLoad(Ld))
12015 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000012016
Evan Cheng536e6672009-03-12 05:59:15 +000012017 // If this is not the MMX case, i.e. we are just turning i64 load/store
12018 // into f64 load/store, avoid the transformation if there are multiple
12019 // uses of the loaded value.
12020 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
12021 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000012022
Evan Cheng536e6672009-03-12 05:59:15 +000012023 DebugLoc LdDL = Ld->getDebugLoc();
12024 DebugLoc StDL = N->getDebugLoc();
12025 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
12026 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
12027 // pair instead.
12028 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000012029 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000012030 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
12031 Ld->getPointerInfo(), Ld->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000012032 Ld->isNonTemporal(), Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000012033 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000012034 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000012035 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000012036 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000012037 Ops.size());
12038 }
Evan Cheng536e6672009-03-12 05:59:15 +000012039 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000012040 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000012041 St->isVolatile(), St->isNonTemporal(),
12042 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000012043 }
Evan Cheng536e6672009-03-12 05:59:15 +000012044
12045 // Otherwise, lower to two pairs of 32-bit loads / stores.
12046 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000012047 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
12048 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000012049
Owen Anderson825b72b2009-08-11 20:47:22 +000012050 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000012051 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000012052 Ld->isVolatile(), Ld->isNonTemporal(),
12053 Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000012054 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000012055 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000012056 Ld->isVolatile(), Ld->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000012057 MinAlign(Ld->getAlignment(), 4));
12058
12059 SDValue NewChain = LoLd.getValue(1);
12060 if (TokenFactorIndex != -1) {
12061 Ops.push_back(LoLd);
12062 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000012063 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000012064 Ops.size());
12065 }
12066
12067 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000012068 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
12069 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000012070
12071 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000012072 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000012073 St->isVolatile(), St->isNonTemporal(),
12074 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000012075 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000012076 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000012077 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000012078 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000012079 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000012080 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000012081 }
Dan Gohman475871a2008-07-27 21:46:04 +000012082 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000012083}
12084
Chris Lattner6cf73262008-01-25 06:14:17 +000012085/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
12086/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000012087static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000012088 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
12089 // F[X]OR(0.0, x) -> x
12090 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000012091 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
12092 if (C->getValueAPF().isPosZero())
12093 return N->getOperand(1);
12094 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
12095 if (C->getValueAPF().isPosZero())
12096 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000012097 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000012098}
12099
12100/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000012101static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000012102 // FAND(0.0, x) -> 0.0
12103 // FAND(x, 0.0) -> 0.0
12104 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
12105 if (C->getValueAPF().isPosZero())
12106 return N->getOperand(0);
12107 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
12108 if (C->getValueAPF().isPosZero())
12109 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000012110 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000012111}
12112
Dan Gohmane5af2d32009-01-29 01:59:02 +000012113static SDValue PerformBTCombine(SDNode *N,
12114 SelectionDAG &DAG,
12115 TargetLowering::DAGCombinerInfo &DCI) {
12116 // BT ignores high bits in the bit index operand.
12117 SDValue Op1 = N->getOperand(1);
12118 if (Op1.hasOneUse()) {
12119 unsigned BitWidth = Op1.getValueSizeInBits();
12120 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
12121 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000012122 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
12123 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000012124 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000012125 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
12126 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
12127 DCI.CommitTargetLoweringOpt(TLO);
12128 }
12129 return SDValue();
12130}
Chris Lattner83e6c992006-10-04 06:57:07 +000012131
Eli Friedman7a5e5552009-06-07 06:52:44 +000012132static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
12133 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000012134 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000012135 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000012136 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000012137 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000012138 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000012139 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000012140 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000012141 }
12142 return SDValue();
12143}
12144
Evan Cheng2e489c42009-12-16 00:53:11 +000012145static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
12146 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
12147 // (and (i32 x86isd::setcc_carry), 1)
12148 // This eliminates the zext. This transformation is necessary because
12149 // ISD::SETCC is always legalized to i8.
12150 DebugLoc dl = N->getDebugLoc();
12151 SDValue N0 = N->getOperand(0);
12152 EVT VT = N->getValueType(0);
12153 if (N0.getOpcode() == ISD::AND &&
12154 N0.hasOneUse() &&
12155 N0.getOperand(0).hasOneUse()) {
12156 SDValue N00 = N0.getOperand(0);
12157 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
12158 return SDValue();
12159 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
12160 if (!C || C->getZExtValue() != 1)
12161 return SDValue();
12162 return DAG.getNode(ISD::AND, dl, VT,
12163 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
12164 N00.getOperand(0), N00.getOperand(1)),
12165 DAG.getConstant(1, VT));
12166 }
12167
12168 return SDValue();
12169}
12170
Chris Lattnerc19d1c32010-12-19 22:08:31 +000012171// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
12172static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG) {
12173 unsigned X86CC = N->getConstantOperandVal(0);
12174 SDValue EFLAG = N->getOperand(1);
12175 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012176
Chris Lattnerc19d1c32010-12-19 22:08:31 +000012177 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
12178 // a zext and produces an all-ones bit which is more useful than 0/1 in some
12179 // cases.
12180 if (X86CC == X86::COND_B)
12181 return DAG.getNode(ISD::AND, DL, MVT::i8,
12182 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
12183 DAG.getConstant(X86CC, MVT::i8), EFLAG),
12184 DAG.getConstant(1, MVT::i8));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012185
Chris Lattnerc19d1c32010-12-19 22:08:31 +000012186 return SDValue();
12187}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012188
Benjamin Kramer1396c402011-06-18 11:09:41 +000012189static SDValue PerformSINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG,
12190 const X86TargetLowering *XTLI) {
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000012191 SDValue Op0 = N->getOperand(0);
12192 // Transform (SINT_TO_FP (i64 ...)) into an x87 operation if we have
12193 // a 32-bit target where SSE doesn't support i64->FP operations.
12194 if (Op0.getOpcode() == ISD::LOAD) {
12195 LoadSDNode *Ld = cast<LoadSDNode>(Op0.getNode());
12196 EVT VT = Ld->getValueType(0);
12197 if (!Ld->isVolatile() && !N->getValueType(0).isVector() &&
12198 ISD::isNON_EXTLoad(Op0.getNode()) && Op0.hasOneUse() &&
12199 !XTLI->getSubtarget()->is64Bit() &&
12200 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Benjamin Kramer1396c402011-06-18 11:09:41 +000012201 SDValue FILDChain = XTLI->BuildFILD(SDValue(N, 0), Ld->getValueType(0),
12202 Ld->getChain(), Op0, DAG);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000012203 DAG.ReplaceAllUsesOfValueWith(Op0.getValue(1), FILDChain.getValue(1));
12204 return FILDChain;
12205 }
12206 }
12207 return SDValue();
12208}
12209
Chris Lattner23a01992010-12-20 01:37:09 +000012210// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
12211static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
12212 X86TargetLowering::DAGCombinerInfo &DCI) {
12213 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
12214 // the result is either zero or one (depending on the input carry bit).
12215 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
12216 if (X86::isZeroNode(N->getOperand(0)) &&
12217 X86::isZeroNode(N->getOperand(1)) &&
12218 // We don't have a good way to replace an EFLAGS use, so only do this when
12219 // dead right now.
12220 SDValue(N, 1).use_empty()) {
12221 DebugLoc DL = N->getDebugLoc();
12222 EVT VT = N->getValueType(0);
12223 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
12224 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
12225 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
12226 DAG.getConstant(X86::COND_B,MVT::i8),
12227 N->getOperand(2)),
12228 DAG.getConstant(1, VT));
12229 return DCI.CombineTo(N, Res1, CarryOut);
12230 }
12231
12232 return SDValue();
12233}
12234
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000012235// fold (add Y, (sete X, 0)) -> adc 0, Y
12236// (add Y, (setne X, 0)) -> sbb -1, Y
12237// (sub (sete X, 0), Y) -> sbb 0, Y
12238// (sub (setne X, 0), Y) -> adc -1, Y
12239static SDValue OptimizeConditonalInDecrement(SDNode *N, SelectionDAG &DAG) {
12240 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012241
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000012242 // Look through ZExts.
12243 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
12244 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
12245 return SDValue();
12246
12247 SDValue SetCC = Ext.getOperand(0);
12248 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
12249 return SDValue();
12250
12251 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
12252 if (CC != X86::COND_E && CC != X86::COND_NE)
12253 return SDValue();
12254
12255 SDValue Cmp = SetCC.getOperand(1);
12256 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000012257 !X86::isZeroNode(Cmp.getOperand(1)) ||
12258 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000012259 return SDValue();
12260
12261 SDValue CmpOp0 = Cmp.getOperand(0);
12262 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
12263 DAG.getConstant(1, CmpOp0.getValueType()));
12264
12265 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
12266 if (CC == X86::COND_NE)
12267 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
12268 DL, OtherVal.getValueType(), OtherVal,
12269 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
12270 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
12271 DL, OtherVal.getValueType(), OtherVal,
12272 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
12273}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000012274
Dan Gohman475871a2008-07-27 21:46:04 +000012275SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000012276 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000012277 SelectionDAG &DAG = DCI.DAG;
12278 switch (N->getOpcode()) {
12279 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012280 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerc19d1c32010-12-19 22:08:31 +000012281 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, *this);
Chris Lattneraf723b92008-01-25 05:46:26 +000012282 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +000012283 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000012284 case ISD::ADD:
12285 case ISD::SUB: return OptimizeConditonalInDecrement(N, DAG);
Chris Lattner23a01992010-12-20 01:37:09 +000012286 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000012287 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000012288 case ISD::SHL:
12289 case ISD::SRA:
12290 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000012291 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000012292 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000012293 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000012294 case ISD::SINT_TO_FP: return PerformSINT_TO_FPCombine(N, DAG, this);
Chris Lattner6cf73262008-01-25 06:14:17 +000012295 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000012296 case X86ISD::FOR: return PerformFORCombine(N, DAG);
12297 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000012298 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000012299 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Evan Cheng2e489c42009-12-16 00:53:11 +000012300 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG);
Chris Lattnerc19d1c32010-12-19 22:08:31 +000012301 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000012302 case X86ISD::SHUFPS: // Handle all target specific shuffles
12303 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000012304 case X86ISD::PALIGN:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000012305 case X86ISD::PUNPCKHBW:
12306 case X86ISD::PUNPCKHWD:
12307 case X86ISD::PUNPCKHDQ:
12308 case X86ISD::PUNPCKHQDQ:
12309 case X86ISD::UNPCKHPS:
12310 case X86ISD::UNPCKHPD:
12311 case X86ISD::PUNPCKLBW:
12312 case X86ISD::PUNPCKLWD:
12313 case X86ISD::PUNPCKLDQ:
12314 case X86ISD::PUNPCKLQDQ:
12315 case X86ISD::UNPCKLPS:
12316 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +000012317 case X86ISD::VUNPCKLPS:
12318 case X86ISD::VUNPCKLPD:
12319 case X86ISD::VUNPCKLPSY:
12320 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000012321 case X86ISD::MOVHLPS:
12322 case X86ISD::MOVLHPS:
12323 case X86ISD::PSHUFD:
12324 case X86ISD::PSHUFHW:
12325 case X86ISD::PSHUFLW:
12326 case X86ISD::MOVSS:
12327 case X86ISD::MOVSD:
Mon P Wanga0fd0d52010-12-19 23:55:53 +000012328 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI);
Evan Cheng206ee9d2006-07-07 08:33:52 +000012329 }
12330
Dan Gohman475871a2008-07-27 21:46:04 +000012331 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000012332}
12333
Evan Chenge5b51ac2010-04-17 06:13:15 +000012334/// isTypeDesirableForOp - Return true if the target has native support for
12335/// the specified value type and it is 'desirable' to use the type for the
12336/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
12337/// instruction encodings are longer and some i16 instructions are slow.
12338bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
12339 if (!isTypeLegal(VT))
12340 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000012341 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000012342 return true;
12343
12344 switch (Opc) {
12345 default:
12346 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000012347 case ISD::LOAD:
12348 case ISD::SIGN_EXTEND:
12349 case ISD::ZERO_EXTEND:
12350 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000012351 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000012352 case ISD::SRL:
12353 case ISD::SUB:
12354 case ISD::ADD:
12355 case ISD::MUL:
12356 case ISD::AND:
12357 case ISD::OR:
12358 case ISD::XOR:
12359 return false;
12360 }
12361}
12362
12363/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000012364/// beneficial for dag combiner to promote the specified node. If true, it
12365/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000012366bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000012367 EVT VT = Op.getValueType();
12368 if (VT != MVT::i16)
12369 return false;
12370
Evan Cheng4c26e932010-04-19 19:29:22 +000012371 bool Promote = false;
12372 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000012373 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000012374 default: break;
12375 case ISD::LOAD: {
12376 LoadSDNode *LD = cast<LoadSDNode>(Op);
12377 // If the non-extending load has a single use and it's not live out, then it
12378 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000012379 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
12380 Op.hasOneUse()*/) {
12381 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
12382 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
12383 // The only case where we'd want to promote LOAD (rather then it being
12384 // promoted as an operand is when it's only use is liveout.
12385 if (UI->getOpcode() != ISD::CopyToReg)
12386 return false;
12387 }
12388 }
Evan Cheng4c26e932010-04-19 19:29:22 +000012389 Promote = true;
12390 break;
12391 }
12392 case ISD::SIGN_EXTEND:
12393 case ISD::ZERO_EXTEND:
12394 case ISD::ANY_EXTEND:
12395 Promote = true;
12396 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000012397 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000012398 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000012399 SDValue N0 = Op.getOperand(0);
12400 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000012401 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000012402 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000012403 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000012404 break;
12405 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000012406 case ISD::ADD:
12407 case ISD::MUL:
12408 case ISD::AND:
12409 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000012410 case ISD::XOR:
12411 Commute = true;
12412 // fallthrough
12413 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000012414 SDValue N0 = Op.getOperand(0);
12415 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000012416 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000012417 return false;
12418 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000012419 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000012420 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000012421 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000012422 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000012423 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000012424 }
12425 }
12426
12427 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000012428 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000012429}
12430
Evan Cheng60c07e12006-07-05 22:17:51 +000012431//===----------------------------------------------------------------------===//
12432// X86 Inline Assembly Support
12433//===----------------------------------------------------------------------===//
12434
Chris Lattnerb8105652009-07-20 17:51:36 +000012435bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
12436 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000012437
12438 std::string AsmStr = IA->getAsmString();
12439
12440 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000012441 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000012442 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000012443
12444 switch (AsmPieces.size()) {
12445 default: return false;
12446 case 1:
12447 AsmStr = AsmPieces[0];
12448 AsmPieces.clear();
12449 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
12450
Chris Lattner7a2bdde2011-04-15 05:18:47 +000012451 // FIXME: this should verify that we are targeting a 486 or better. If not,
Evan Cheng55d42002011-01-08 01:24:27 +000012452 // we will turn this bswap into something that will be lowered to logical ops
12453 // instead of emitting the bswap asm. For now, we don't support 486 or lower
12454 // so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000012455 // bswap $0
12456 if (AsmPieces.size() == 2 &&
12457 (AsmPieces[0] == "bswap" ||
12458 AsmPieces[0] == "bswapq" ||
12459 AsmPieces[0] == "bswapl") &&
12460 (AsmPieces[1] == "$0" ||
12461 AsmPieces[1] == "${0:q}")) {
12462 // No need to check constraints, nothing other than the equivalent of
12463 // "=r,0" would be valid here.
Evan Cheng55d42002011-01-08 01:24:27 +000012464 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
12465 if (!Ty || Ty->getBitWidth() % 16 != 0)
12466 return false;
12467 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000012468 }
12469 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000012470 if (CI->getType()->isIntegerTy(16) &&
Chris Lattnerb8105652009-07-20 17:51:36 +000012471 AsmPieces.size() == 3 &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000012472 (AsmPieces[0] == "rorw" || AsmPieces[0] == "rolw") &&
Chris Lattnerb8105652009-07-20 17:51:36 +000012473 AsmPieces[1] == "$$8," &&
12474 AsmPieces[2] == "${0:w}" &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000012475 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
12476 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000012477 const std::string &ConstraintsStr = IA->getConstraintString();
12478 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000012479 std::sort(AsmPieces.begin(), AsmPieces.end());
12480 if (AsmPieces.size() == 4 &&
12481 AsmPieces[0] == "~{cc}" &&
12482 AsmPieces[1] == "~{dirflag}" &&
12483 AsmPieces[2] == "~{flags}" &&
12484 AsmPieces[3] == "~{fpsr}") {
Evan Cheng55d42002011-01-08 01:24:27 +000012485 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
12486 if (!Ty || Ty->getBitWidth() % 16 != 0)
12487 return false;
12488 return IntrinsicLowering::LowerToByteSwap(CI);
Dan Gohman0ef701e2010-03-04 19:58:08 +000012489 }
Chris Lattnerb8105652009-07-20 17:51:36 +000012490 }
12491 break;
12492 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000012493 if (CI->getType()->isIntegerTy(32) &&
12494 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
12495 SmallVector<StringRef, 4> Words;
12496 SplitString(AsmPieces[0], Words, " \t,");
12497 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
12498 Words[2] == "${0:w}") {
12499 Words.clear();
12500 SplitString(AsmPieces[1], Words, " \t,");
12501 if (Words.size() == 3 && Words[0] == "rorl" && Words[1] == "$$16" &&
12502 Words[2] == "$0") {
12503 Words.clear();
12504 SplitString(AsmPieces[2], Words, " \t,");
12505 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
12506 Words[2] == "${0:w}") {
12507 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000012508 const std::string &ConstraintsStr = IA->getConstraintString();
12509 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Peter Collingbourne948cf022010-11-13 19:54:30 +000012510 std::sort(AsmPieces.begin(), AsmPieces.end());
12511 if (AsmPieces.size() == 4 &&
12512 AsmPieces[0] == "~{cc}" &&
12513 AsmPieces[1] == "~{dirflag}" &&
12514 AsmPieces[2] == "~{flags}" &&
12515 AsmPieces[3] == "~{fpsr}") {
Evan Cheng55d42002011-01-08 01:24:27 +000012516 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
12517 if (!Ty || Ty->getBitWidth() % 16 != 0)
12518 return false;
12519 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000012520 }
12521 }
12522 }
12523 }
12524 }
Evan Cheng55d42002011-01-08 01:24:27 +000012525
12526 if (CI->getType()->isIntegerTy(64)) {
12527 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
12528 if (Constraints.size() >= 2 &&
12529 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
12530 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
12531 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
12532 SmallVector<StringRef, 4> Words;
12533 SplitString(AsmPieces[0], Words, " \t");
12534 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
Chris Lattnerb8105652009-07-20 17:51:36 +000012535 Words.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000012536 SplitString(AsmPieces[1], Words, " \t");
12537 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
12538 Words.clear();
12539 SplitString(AsmPieces[2], Words, " \t,");
12540 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
12541 Words[2] == "%edx") {
12542 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
12543 if (!Ty || Ty->getBitWidth() % 16 != 0)
12544 return false;
12545 return IntrinsicLowering::LowerToByteSwap(CI);
12546 }
Chris Lattnerb8105652009-07-20 17:51:36 +000012547 }
12548 }
12549 }
12550 }
12551 break;
12552 }
12553 return false;
12554}
12555
12556
12557
Chris Lattnerf4dff842006-07-11 02:54:03 +000012558/// getConstraintType - Given a constraint letter, return the type of
12559/// constraint it is for this target.
12560X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000012561X86TargetLowering::getConstraintType(const std::string &Constraint) const {
12562 if (Constraint.size() == 1) {
12563 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000012564 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000012565 case 'q':
12566 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000012567 case 'f':
12568 case 't':
12569 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000012570 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000012571 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000012572 case 'Y':
12573 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000012574 case 'a':
12575 case 'b':
12576 case 'c':
12577 case 'd':
12578 case 'S':
12579 case 'D':
12580 case 'A':
12581 return C_Register;
12582 case 'I':
12583 case 'J':
12584 case 'K':
12585 case 'L':
12586 case 'M':
12587 case 'N':
12588 case 'G':
12589 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000012590 case 'e':
12591 case 'Z':
12592 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000012593 default:
12594 break;
12595 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000012596 }
Chris Lattner4234f572007-03-25 02:14:49 +000012597 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000012598}
12599
John Thompson44ab89e2010-10-29 17:29:13 +000012600/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000012601/// This object must already have been set up with the operand type
12602/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000012603TargetLowering::ConstraintWeight
12604 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000012605 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000012606 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012607 Value *CallOperandVal = info.CallOperandVal;
12608 // If we don't have a value, we can't do a match,
12609 // but allow it at the lowest weight.
12610 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000012611 return CW_Default;
12612 const Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000012613 // Look at the constraint type.
12614 switch (*constraint) {
12615 default:
John Thompson44ab89e2010-10-29 17:29:13 +000012616 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
12617 case 'R':
12618 case 'q':
12619 case 'Q':
12620 case 'a':
12621 case 'b':
12622 case 'c':
12623 case 'd':
12624 case 'S':
12625 case 'D':
12626 case 'A':
12627 if (CallOperandVal->getType()->isIntegerTy())
12628 weight = CW_SpecificReg;
12629 break;
12630 case 'f':
12631 case 't':
12632 case 'u':
12633 if (type->isFloatingPointTy())
12634 weight = CW_SpecificReg;
12635 break;
12636 case 'y':
Chris Lattner2a786eb2010-12-19 20:19:20 +000012637 if (type->isX86_MMXTy() && Subtarget->hasMMX())
John Thompson44ab89e2010-10-29 17:29:13 +000012638 weight = CW_SpecificReg;
12639 break;
12640 case 'x':
12641 case 'Y':
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012642 if ((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasXMM())
John Thompson44ab89e2010-10-29 17:29:13 +000012643 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012644 break;
12645 case 'I':
12646 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
12647 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000012648 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012649 }
12650 break;
John Thompson44ab89e2010-10-29 17:29:13 +000012651 case 'J':
12652 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12653 if (C->getZExtValue() <= 63)
12654 weight = CW_Constant;
12655 }
12656 break;
12657 case 'K':
12658 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12659 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
12660 weight = CW_Constant;
12661 }
12662 break;
12663 case 'L':
12664 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12665 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
12666 weight = CW_Constant;
12667 }
12668 break;
12669 case 'M':
12670 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12671 if (C->getZExtValue() <= 3)
12672 weight = CW_Constant;
12673 }
12674 break;
12675 case 'N':
12676 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12677 if (C->getZExtValue() <= 0xff)
12678 weight = CW_Constant;
12679 }
12680 break;
12681 case 'G':
12682 case 'C':
12683 if (dyn_cast<ConstantFP>(CallOperandVal)) {
12684 weight = CW_Constant;
12685 }
12686 break;
12687 case 'e':
12688 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12689 if ((C->getSExtValue() >= -0x80000000LL) &&
12690 (C->getSExtValue() <= 0x7fffffffLL))
12691 weight = CW_Constant;
12692 }
12693 break;
12694 case 'Z':
12695 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12696 if (C->getZExtValue() <= 0xffffffff)
12697 weight = CW_Constant;
12698 }
12699 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012700 }
12701 return weight;
12702}
12703
Dale Johannesenba2a0b92008-01-29 02:21:21 +000012704/// LowerXConstraint - try to replace an X constraint, which matches anything,
12705/// with another that has more specific requirements based on the type of the
12706/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000012707const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000012708LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000012709 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
12710 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000012711 if (ConstraintVT.isFloatingPoint()) {
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012712 if (Subtarget->hasXMMInt())
Chris Lattner5e764232008-04-26 23:02:14 +000012713 return "Y";
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012714 if (Subtarget->hasXMM())
Chris Lattner5e764232008-04-26 23:02:14 +000012715 return "x";
12716 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012717
Chris Lattner5e764232008-04-26 23:02:14 +000012718 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000012719}
12720
Chris Lattner48884cd2007-08-25 00:47:38 +000012721/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
12722/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000012723void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +000012724 std::string &Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000012725 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000012726 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000012727 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000012728
Eric Christopher100c8332011-06-02 23:16:42 +000012729 // Only support length 1 constraints for now.
12730 if (Constraint.length() > 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +000012731
Eric Christopher100c8332011-06-02 23:16:42 +000012732 char ConstraintLetter = Constraint[0];
12733 switch (ConstraintLetter) {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012734 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000012735 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000012736 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000012737 if (C->getZExtValue() <= 31) {
12738 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000012739 break;
12740 }
Devang Patel84f7fd22007-03-17 00:13:28 +000012741 }
Chris Lattner48884cd2007-08-25 00:47:38 +000012742 return;
Evan Cheng364091e2008-09-22 23:57:37 +000012743 case 'J':
12744 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000012745 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000012746 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
12747 break;
12748 }
12749 }
12750 return;
12751 case 'K':
12752 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000012753 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000012754 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
12755 break;
12756 }
12757 }
12758 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000012759 case 'N':
12760 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000012761 if (C->getZExtValue() <= 255) {
12762 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000012763 break;
12764 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000012765 }
Chris Lattner48884cd2007-08-25 00:47:38 +000012766 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000012767 case 'e': {
12768 // 32-bit signed value
12769 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000012770 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
12771 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000012772 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000012773 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000012774 break;
12775 }
12776 // FIXME gcc accepts some relocatable values here too, but only in certain
12777 // memory models; it's complicated.
12778 }
12779 return;
12780 }
12781 case 'Z': {
12782 // 32-bit unsigned value
12783 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000012784 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
12785 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000012786 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
12787 break;
12788 }
12789 }
12790 // FIXME gcc accepts some relocatable values here too, but only in certain
12791 // memory models; it's complicated.
12792 return;
12793 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012794 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012795 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000012796 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000012797 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000012798 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000012799 break;
12800 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012801
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000012802 // In any sort of PIC mode addresses need to be computed at runtime by
12803 // adding in a register or some sort of table lookup. These can't
12804 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000012805 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000012806 return;
12807
Chris Lattnerdc43a882007-05-03 16:52:29 +000012808 // If we are in non-pic codegen mode, we allow the address of a global (with
12809 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000012810 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000012811 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000012812
Chris Lattner49921962009-05-08 18:23:14 +000012813 // Match either (GA), (GA+C), (GA+C1+C2), etc.
12814 while (1) {
12815 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
12816 Offset += GA->getOffset();
12817 break;
12818 } else if (Op.getOpcode() == ISD::ADD) {
12819 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
12820 Offset += C->getZExtValue();
12821 Op = Op.getOperand(0);
12822 continue;
12823 }
12824 } else if (Op.getOpcode() == ISD::SUB) {
12825 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
12826 Offset += -C->getZExtValue();
12827 Op = Op.getOperand(0);
12828 continue;
12829 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012830 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012831
Chris Lattner49921962009-05-08 18:23:14 +000012832 // Otherwise, this isn't something we can handle, reject it.
12833 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000012834 }
Eric Christopherfd179292009-08-27 18:07:15 +000012835
Dan Gohman46510a72010-04-15 01:51:59 +000012836 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012837 // If we require an extra load to get this address, as in PIC mode, we
12838 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000012839 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
12840 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012841 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000012842
Devang Patel0d881da2010-07-06 22:08:15 +000012843 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
12844 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000012845 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012846 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012847 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012848
Gabor Greifba36cb52008-08-28 21:40:38 +000012849 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000012850 Ops.push_back(Result);
12851 return;
12852 }
Dale Johannesen1784d162010-06-25 21:55:36 +000012853 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012854}
12855
Chris Lattner259e97c2006-01-31 19:43:35 +000012856std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +000012857getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000012858 EVT VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +000012859 if (Constraint.size() == 1) {
12860 // FIXME: not handling fp-stack yet!
Chris Lattner259e97c2006-01-31 19:43:35 +000012861 switch (Constraint[0]) { // GCC X86 Constraint Letters
Chris Lattnerf4dff842006-07-11 02:54:03 +000012862 default: break; // Unknown constraint letter
Evan Cheng47e9fab2009-07-17 22:13:25 +000012863 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
12864 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000012865 if (VT == MVT::i32)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012866 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
12867 X86::ESI, X86::EDI, X86::R8D, X86::R9D,
12868 X86::R10D,X86::R11D,X86::R12D,
12869 X86::R13D,X86::R14D,X86::R15D,
12870 X86::EBP, X86::ESP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012871 else if (VT == MVT::i16)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012872 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
12873 X86::SI, X86::DI, X86::R8W,X86::R9W,
12874 X86::R10W,X86::R11W,X86::R12W,
12875 X86::R13W,X86::R14W,X86::R15W,
12876 X86::BP, X86::SP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012877 else if (VT == MVT::i8)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012878 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL,
12879 X86::SIL, X86::DIL, X86::R8B,X86::R9B,
12880 X86::R10B,X86::R11B,X86::R12B,
12881 X86::R13B,X86::R14B,X86::R15B,
12882 X86::BPL, X86::SPL, 0);
12883
Owen Anderson825b72b2009-08-11 20:47:22 +000012884 else if (VT == MVT::i64)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012885 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX,
12886 X86::RSI, X86::RDI, X86::R8, X86::R9,
12887 X86::R10, X86::R11, X86::R12,
12888 X86::R13, X86::R14, X86::R15,
12889 X86::RBP, X86::RSP, 0);
12890
12891 break;
12892 }
Eric Christopherfd179292009-08-27 18:07:15 +000012893 // 32-bit fallthrough
Chris Lattner259e97c2006-01-31 19:43:35 +000012894 case 'Q': // Q_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000012895 if (VT == MVT::i32)
Chris Lattner80a7ecc2006-05-06 00:29:37 +000012896 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012897 else if (VT == MVT::i16)
Chris Lattner80a7ecc2006-05-06 00:29:37 +000012898 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012899 else if (VT == MVT::i8)
Evan Cheng12914382007-08-13 23:27:11 +000012900 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012901 else if (VT == MVT::i64)
Chris Lattner03e6c702007-11-04 06:51:12 +000012902 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
12903 break;
Chris Lattner259e97c2006-01-31 19:43:35 +000012904 }
12905 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012906
Chris Lattner1efa40f2006-02-22 00:56:39 +000012907 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +000012908}
Chris Lattnerf76d1802006-07-31 23:26:50 +000012909
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012910std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000012911X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000012912 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000012913 // First, see if this is a constraint that directly corresponds to an LLVM
12914 // register class.
12915 if (Constraint.size() == 1) {
12916 // GCC Constraint Letters
12917 switch (Constraint[0]) {
12918 default: break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000012919 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000012920 case 'l': // INDEX_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000012921 if (VT == MVT::i8)
Chris Lattner0f65cad2007-04-09 05:49:22 +000012922 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012923 if (VT == MVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +000012924 return std::make_pair(0U, X86::GR16RegisterClass);
Eric Christopher2bbecd82011-05-19 21:33:47 +000012925 if (VT == MVT::i32 || VT == MVT::f32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +000012926 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +000012927 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000012928 case 'R': // LEGACY_REGS
12929 if (VT == MVT::i8)
12930 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
12931 if (VT == MVT::i16)
12932 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
12933 if (VT == MVT::i32 || !Subtarget->is64Bit())
12934 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
12935 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000012936 case 'f': // FP Stack registers.
12937 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
12938 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000012939 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000012940 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012941 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000012942 return std::make_pair(0U, X86::RFP64RegisterClass);
12943 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000012944 case 'y': // MMX_REGS if MMX allowed.
12945 if (!Subtarget->hasMMX()) break;
12946 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000012947 case 'Y': // SSE_REGS if SSE2 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012948 if (!Subtarget->hasXMMInt()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000012949 // FALL THROUGH.
12950 case 'x': // SSE_REGS if SSE1 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012951 if (!Subtarget->hasXMM()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000012952
Owen Anderson825b72b2009-08-11 20:47:22 +000012953 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000012954 default: break;
12955 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000012956 case MVT::f32:
12957 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +000012958 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012959 case MVT::f64:
12960 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +000012961 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000012962 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000012963 case MVT::v16i8:
12964 case MVT::v8i16:
12965 case MVT::v4i32:
12966 case MVT::v2i64:
12967 case MVT::v4f32:
12968 case MVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +000012969 return std::make_pair(0U, X86::VR128RegisterClass);
12970 }
Chris Lattnerad043e82007-04-09 05:11:28 +000012971 break;
12972 }
12973 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012974
Chris Lattnerf76d1802006-07-31 23:26:50 +000012975 // Use the default implementation in TargetLowering to convert the register
12976 // constraint into a member of a register class.
12977 std::pair<unsigned, const TargetRegisterClass*> Res;
12978 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000012979
12980 // Not found as a standard register?
12981 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000012982 // Map st(0) -> st(7) -> ST0
12983 if (Constraint.size() == 7 && Constraint[0] == '{' &&
12984 tolower(Constraint[1]) == 's' &&
12985 tolower(Constraint[2]) == 't' &&
12986 Constraint[3] == '(' &&
12987 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
12988 Constraint[5] == ')' &&
12989 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000012990
Chris Lattner56d77c72009-09-13 22:41:48 +000012991 Res.first = X86::ST0+Constraint[4]-'0';
12992 Res.second = X86::RFP80RegisterClass;
12993 return Res;
12994 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000012995
Chris Lattner56d77c72009-09-13 22:41:48 +000012996 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000012997 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000012998 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +000012999 Res.second = X86::RFP80RegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000013000 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000013001 }
Chris Lattner56d77c72009-09-13 22:41:48 +000013002
13003 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000013004 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000013005 Res.first = X86::EFLAGS;
13006 Res.second = X86::CCRRegisterClass;
13007 return Res;
13008 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000013009
Dale Johannesen330169f2008-11-13 21:52:36 +000013010 // 'A' means EAX + EDX.
13011 if (Constraint == "A") {
13012 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +000013013 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000013014 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000013015 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000013016 return Res;
13017 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013018
Chris Lattnerf76d1802006-07-31 23:26:50 +000013019 // Otherwise, check to see if this is a register class of the wrong value
13020 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
13021 // turn into {ax},{dx}.
13022 if (Res.second->hasType(VT))
13023 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013024
Chris Lattnerf76d1802006-07-31 23:26:50 +000013025 // All of the single-register GCC register classes map their values onto
13026 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
13027 // really want an 8-bit or 32-bit register, map to the appropriate register
13028 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +000013029 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000013030 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000013031 unsigned DestReg = 0;
13032 switch (Res.first) {
13033 default: break;
13034 case X86::AX: DestReg = X86::AL; break;
13035 case X86::DX: DestReg = X86::DL; break;
13036 case X86::CX: DestReg = X86::CL; break;
13037 case X86::BX: DestReg = X86::BL; break;
13038 }
13039 if (DestReg) {
13040 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000013041 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000013042 }
Owen Anderson825b72b2009-08-11 20:47:22 +000013043 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000013044 unsigned DestReg = 0;
13045 switch (Res.first) {
13046 default: break;
13047 case X86::AX: DestReg = X86::EAX; break;
13048 case X86::DX: DestReg = X86::EDX; break;
13049 case X86::CX: DestReg = X86::ECX; break;
13050 case X86::BX: DestReg = X86::EBX; break;
13051 case X86::SI: DestReg = X86::ESI; break;
13052 case X86::DI: DestReg = X86::EDI; break;
13053 case X86::BP: DestReg = X86::EBP; break;
13054 case X86::SP: DestReg = X86::ESP; break;
13055 }
13056 if (DestReg) {
13057 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000013058 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000013059 }
Owen Anderson825b72b2009-08-11 20:47:22 +000013060 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000013061 unsigned DestReg = 0;
13062 switch (Res.first) {
13063 default: break;
13064 case X86::AX: DestReg = X86::RAX; break;
13065 case X86::DX: DestReg = X86::RDX; break;
13066 case X86::CX: DestReg = X86::RCX; break;
13067 case X86::BX: DestReg = X86::RBX; break;
13068 case X86::SI: DestReg = X86::RSI; break;
13069 case X86::DI: DestReg = X86::RDI; break;
13070 case X86::BP: DestReg = X86::RBP; break;
13071 case X86::SP: DestReg = X86::RSP; break;
13072 }
13073 if (DestReg) {
13074 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000013075 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000013076 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000013077 }
Chris Lattner6ba50a92008-08-26 06:19:02 +000013078 } else if (Res.second == X86::FR32RegisterClass ||
13079 Res.second == X86::FR64RegisterClass ||
13080 Res.second == X86::VR128RegisterClass) {
13081 // Handle references to XMM physical registers that got mapped into the
13082 // wrong class. This can happen with constraints like {xmm0} where the
13083 // target independent register mapper will just pick the first match it can
13084 // find, ignoring the required type.
Owen Anderson825b72b2009-08-11 20:47:22 +000013085 if (VT == MVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +000013086 Res.second = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +000013087 else if (VT == MVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +000013088 Res.second = X86::FR64RegisterClass;
13089 else if (X86::VR128RegisterClass->hasType(VT))
13090 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000013091 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013092
Chris Lattnerf76d1802006-07-31 23:26:50 +000013093 return Res;
13094}