blob: 97fd2a3f9065714989ecb75254e9907c3f58ba20 [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 Christopherde5e1012011-03-11 01:05:58 +0000225
226 // 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
Evan Chengd25e9e82006-02-02 00:28:23 +0000577 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000578 setOperationAction(ISD::FSIN , MVT::f64, Expand);
579 setOperationAction(ISD::FCOS , MVT::f64, Expand);
580 setOperationAction(ISD::FSIN , MVT::f32, Expand);
581 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000582
Chris Lattnera54aa942006-01-29 06:26:08 +0000583 // Expand FP immediates into loads from the stack, except for the special
584 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000585 addLegalFPImmediate(APFloat(+0.0)); // xorpd
586 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Evan Chengc7ce29b2009-02-13 22:36:38 +0000587 } else if (!UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000588 // Use SSE for f32, x87 for f64.
589 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000590 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
591 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000592
593 // Use ANDPS to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000594 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000595
596 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000597 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000598
Owen Anderson825b72b2009-08-11 20:47:22 +0000599 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000600
601 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000602 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
603 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000604
605 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000606 setOperationAction(ISD::FSIN , MVT::f32, Expand);
607 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000608
Nate Begemane1795842008-02-14 08:57:00 +0000609 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000610 addLegalFPImmediate(APFloat(+0.0f)); // xorps
611 addLegalFPImmediate(APFloat(+0.0)); // FLD0
612 addLegalFPImmediate(APFloat(+1.0)); // FLD1
613 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
614 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
615
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000616 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000617 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
618 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000619 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000620 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000621 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000622 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000623 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
624 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000625
Owen Anderson825b72b2009-08-11 20:47:22 +0000626 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
627 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
628 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
629 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000630
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000631 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000632 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
633 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000634 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000635 addLegalFPImmediate(APFloat(+0.0)); // FLD0
636 addLegalFPImmediate(APFloat(+1.0)); // FLD1
637 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
638 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000639 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
640 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
641 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
642 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000643 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000644
Dale Johannesen59a58732007-08-05 18:49:15 +0000645 // Long double always uses X87.
Evan Cheng92722532009-03-26 23:06:32 +0000646 if (!UseSoftFloat) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000647 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
648 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
649 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000650 {
Benjamin Kramer98383962010-12-04 14:22:24 +0000651 APFloat TmpFlt = APFloat::getZero(APFloat::x87DoubleExtended);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000652 addLegalFPImmediate(TmpFlt); // FLD0
653 TmpFlt.changeSign();
654 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
Benjamin Kramer98383962010-12-04 14:22:24 +0000655
656 bool ignored;
Evan Chengc7ce29b2009-02-13 22:36:38 +0000657 APFloat TmpFlt2(+1.0);
658 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
659 &ignored);
660 addLegalFPImmediate(TmpFlt2); // FLD1
661 TmpFlt2.changeSign();
662 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
663 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000664
Evan Chengc7ce29b2009-02-13 22:36:38 +0000665 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000666 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
667 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000668 }
Dale Johannesen2f429012007-09-26 21:10:55 +0000669 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000670
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000671 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000672 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
673 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
674 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000675
Owen Anderson825b72b2009-08-11 20:47:22 +0000676 setOperationAction(ISD::FLOG, MVT::f80, Expand);
677 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
678 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
679 setOperationAction(ISD::FEXP, MVT::f80, Expand);
680 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000681
Mon P Wangf007a8b2008-11-06 05:31:54 +0000682 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000683 // (for widening) or expand (for scalarization). Then we will selectively
684 // turn on ones that can be effectively codegen'd.
Owen Anderson825b72b2009-08-11 20:47:22 +0000685 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
686 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
687 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
688 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
689 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
690 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
691 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
692 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
693 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
694 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
695 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
696 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
697 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
698 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
699 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
700 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
701 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000702 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
David Greenecfe33c42011-01-26 19:13:22 +0000703 setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
704 setOperationAction(ISD::INSERT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000705 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
706 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
707 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
708 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
709 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
710 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
711 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
712 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
713 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
714 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
715 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
716 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
717 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
718 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
719 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
720 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
721 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
722 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
723 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
724 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
725 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
726 setOperationAction(ISD::VSETCC, (MVT::SimpleValueType)VT, Expand);
727 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
728 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
729 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
730 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
731 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
732 setOperationAction(ISD::FP_TO_UINT, (MVT::SimpleValueType)VT, Expand);
733 setOperationAction(ISD::FP_TO_SINT, (MVT::SimpleValueType)VT, Expand);
734 setOperationAction(ISD::UINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
735 setOperationAction(ISD::SINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
Dan Gohman87862e72009-12-11 21:31:27 +0000736 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000737 setOperationAction(ISD::TRUNCATE, (MVT::SimpleValueType)VT, Expand);
738 setOperationAction(ISD::SIGN_EXTEND, (MVT::SimpleValueType)VT, Expand);
739 setOperationAction(ISD::ZERO_EXTEND, (MVT::SimpleValueType)VT, Expand);
740 setOperationAction(ISD::ANY_EXTEND, (MVT::SimpleValueType)VT, Expand);
741 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
742 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
743 setTruncStoreAction((MVT::SimpleValueType)VT,
744 (MVT::SimpleValueType)InnerVT, Expand);
745 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
746 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
747 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000748 }
749
Evan Chengc7ce29b2009-02-13 22:36:38 +0000750 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
751 // with -msoft-float, disable use of MMX as well.
Chris Lattner2a786eb2010-12-19 20:19:20 +0000752 if (!UseSoftFloat && Subtarget->hasMMX()) {
Dale Johannesene93d99c2010-10-20 21:32:10 +0000753 addRegisterClass(MVT::x86mmx, X86::VR64RegisterClass);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000754 // No operations on x86mmx supported, everything uses intrinsics.
Evan Cheng470a6ad2006-02-22 02:26:30 +0000755 }
756
Dale Johannesen0488fb62010-09-30 23:57:10 +0000757 // MMX-sized vectors (other than x86mmx) are expected to be expanded
758 // into smaller operations.
759 setOperationAction(ISD::MULHS, MVT::v8i8, Expand);
760 setOperationAction(ISD::MULHS, MVT::v4i16, Expand);
761 setOperationAction(ISD::MULHS, MVT::v2i32, Expand);
762 setOperationAction(ISD::MULHS, MVT::v1i64, Expand);
763 setOperationAction(ISD::AND, MVT::v8i8, Expand);
764 setOperationAction(ISD::AND, MVT::v4i16, Expand);
765 setOperationAction(ISD::AND, MVT::v2i32, Expand);
766 setOperationAction(ISD::AND, MVT::v1i64, Expand);
767 setOperationAction(ISD::OR, MVT::v8i8, Expand);
768 setOperationAction(ISD::OR, MVT::v4i16, Expand);
769 setOperationAction(ISD::OR, MVT::v2i32, Expand);
770 setOperationAction(ISD::OR, MVT::v1i64, Expand);
771 setOperationAction(ISD::XOR, MVT::v8i8, Expand);
772 setOperationAction(ISD::XOR, MVT::v4i16, Expand);
773 setOperationAction(ISD::XOR, MVT::v2i32, Expand);
774 setOperationAction(ISD::XOR, MVT::v1i64, Expand);
775 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Expand);
776 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Expand);
777 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Expand);
778 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Expand);
779 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v1i64, Expand);
780 setOperationAction(ISD::SELECT, MVT::v8i8, Expand);
781 setOperationAction(ISD::SELECT, MVT::v4i16, Expand);
782 setOperationAction(ISD::SELECT, MVT::v2i32, Expand);
783 setOperationAction(ISD::SELECT, MVT::v1i64, Expand);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000784 setOperationAction(ISD::BITCAST, MVT::v8i8, Expand);
785 setOperationAction(ISD::BITCAST, MVT::v4i16, Expand);
786 setOperationAction(ISD::BITCAST, MVT::v2i32, Expand);
787 setOperationAction(ISD::BITCAST, MVT::v1i64, Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000788
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000789 if (!UseSoftFloat && Subtarget->hasXMM()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000790 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000791
Owen Anderson825b72b2009-08-11 20:47:22 +0000792 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
793 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
794 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
795 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
796 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
797 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
798 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
799 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
800 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
801 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
802 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
803 setOperationAction(ISD::VSETCC, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000804 }
805
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000806 if (!UseSoftFloat && Subtarget->hasXMMInt()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000807 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000808
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000809 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
810 // registers cannot be used even for integer operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000811 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
812 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
813 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
814 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000815
Owen Anderson825b72b2009-08-11 20:47:22 +0000816 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
817 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
818 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
819 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
820 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
821 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
822 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
823 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
824 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
825 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
826 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
827 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
828 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
829 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
830 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
831 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000832
Owen Anderson825b72b2009-08-11 20:47:22 +0000833 setOperationAction(ISD::VSETCC, MVT::v2f64, Custom);
834 setOperationAction(ISD::VSETCC, MVT::v16i8, Custom);
835 setOperationAction(ISD::VSETCC, MVT::v8i16, Custom);
836 setOperationAction(ISD::VSETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000837
Owen Anderson825b72b2009-08-11 20:47:22 +0000838 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
839 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
840 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
841 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
842 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000843
Mon P Wangeb38ebf2010-01-24 00:05:03 +0000844 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2f64, Custom);
845 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2i64, Custom);
846 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i8, Custom);
847 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i16, Custom);
848 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Custom);
849
Evan Cheng2c3ae372006-04-12 21:21:57 +0000850 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Owen Anderson825b72b2009-08-11 20:47:22 +0000851 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
852 EVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000853 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000854 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000855 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000856 // Do not attempt to custom lower non-128-bit vectors
857 if (!VT.is128BitVector())
858 continue;
Owen Anderson825b72b2009-08-11 20:47:22 +0000859 setOperationAction(ISD::BUILD_VECTOR,
860 VT.getSimpleVT().SimpleTy, Custom);
861 setOperationAction(ISD::VECTOR_SHUFFLE,
862 VT.getSimpleVT().SimpleTy, Custom);
863 setOperationAction(ISD::EXTRACT_VECTOR_ELT,
864 VT.getSimpleVT().SimpleTy, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000865 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000866
Owen Anderson825b72b2009-08-11 20:47:22 +0000867 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
868 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
869 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
870 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
871 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
872 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000873
Nate Begemancdd1eec2008-02-12 22:51:28 +0000874 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000875 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
876 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000877 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000878
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000879 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000880 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; i++) {
881 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
Owen Andersone50ed302009-08-10 22:56:29 +0000882 EVT VT = SVT;
David Greene9b9838d2009-06-29 16:47:10 +0000883
884 // Do not attempt to promote non-128-bit vectors
Chris Lattner32b4b5a2010-07-05 05:53:14 +0000885 if (!VT.is128BitVector())
David Greene9b9838d2009-06-29 16:47:10 +0000886 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +0000887
Owen Andersond6662ad2009-08-10 20:46:15 +0000888 setOperationAction(ISD::AND, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000889 AddPromotedToType (ISD::AND, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000890 setOperationAction(ISD::OR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000891 AddPromotedToType (ISD::OR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000892 setOperationAction(ISD::XOR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000893 AddPromotedToType (ISD::XOR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000894 setOperationAction(ISD::LOAD, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000895 AddPromotedToType (ISD::LOAD, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000896 setOperationAction(ISD::SELECT, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000897 AddPromotedToType (ISD::SELECT, SVT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000898 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000899
Owen Anderson825b72b2009-08-11 20:47:22 +0000900 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000901
Evan Cheng2c3ae372006-04-12 21:21:57 +0000902 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000903 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
904 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
905 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
906 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000907
Owen Anderson825b72b2009-08-11 20:47:22 +0000908 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
909 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000910 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000911
Nate Begeman14d12ca2008-02-11 04:19:36 +0000912 if (Subtarget->hasSSE41()) {
Dale Johannesen54feef22010-05-27 20:12:41 +0000913 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
914 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
915 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
916 setOperationAction(ISD::FRINT, MVT::f32, Legal);
917 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
918 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
919 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
920 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
921 setOperationAction(ISD::FRINT, MVT::f64, Legal);
922 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
923
Nate Begeman14d12ca2008-02-11 04:19:36 +0000924 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +0000925 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000926
Nate Begemanbdcb5af2010-07-27 22:37:06 +0000927 // Can turn SHL into an integer multiply.
928 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
Nate Begeman51409212010-07-28 00:21:48 +0000929 setOperationAction(ISD::SHL, MVT::v16i8, Custom);
Nadav Rotem06cc3242011-03-19 13:09:10 +0000930 setOperationAction(ISD::SRL, MVT::v4i32, Legal);
Nate Begemanbdcb5af2010-07-27 22:37:06 +0000931
Nate Begeman14d12ca2008-02-11 04:19:36 +0000932 // i8 and i16 vectors are custom , because the source register and source
933 // source memory operand types are not the same width. f32 vectors are
934 // custom since the immediate controlling the insert encodes additional
935 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +0000936 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
937 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
938 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
939 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000940
Owen Anderson825b72b2009-08-11 20:47:22 +0000941 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
942 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
943 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
944 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000945
946 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000947 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
948 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000949 }
950 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000951
Chris Lattnera34b3cf2010-12-19 20:03:11 +0000952 if (Subtarget->hasSSE42())
Owen Anderson825b72b2009-08-11 20:47:22 +0000953 setOperationAction(ISD::VSETCC, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000954
David Greene9b9838d2009-06-29 16:47:10 +0000955 if (!UseSoftFloat && Subtarget->hasAVX()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000956 addRegisterClass(MVT::v8f32, X86::VR256RegisterClass);
957 addRegisterClass(MVT::v4f64, X86::VR256RegisterClass);
958 addRegisterClass(MVT::v8i32, X86::VR256RegisterClass);
959 addRegisterClass(MVT::v4i64, X86::VR256RegisterClass);
Bruno Cardoso Lopes405f11b2010-08-10 01:43:16 +0000960 addRegisterClass(MVT::v32i8, X86::VR256RegisterClass);
David Greened94c1012009-06-29 22:50:51 +0000961
Owen Anderson825b72b2009-08-11 20:47:22 +0000962 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
963 setOperationAction(ISD::LOAD, MVT::v8i32, Legal);
964 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
965 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
David Greene54d8eba2011-01-27 22:38:56 +0000966
Owen Anderson825b72b2009-08-11 20:47:22 +0000967 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
968 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
969 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
970 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
971 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
972 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000973
Owen Anderson825b72b2009-08-11 20:47:22 +0000974 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
975 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
976 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
977 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
978 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
979 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000980
David Greene54d8eba2011-01-27 22:38:56 +0000981 // Custom lower build_vector, vector_shuffle, scalar_to_vector,
982 // insert_vector_elt extract_subvector and extract_vector_elt for
983 // 256-bit types.
984 for (unsigned i = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
985 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE;
986 ++i) {
987 MVT::SimpleValueType VT = (MVT::SimpleValueType)i;
988 // Do not attempt to custom lower non-256-bit vectors
989 if (!isPowerOf2_32(MVT(VT).getVectorNumElements())
990 || (MVT(VT).getSizeInBits() < 256))
David Greene9b9838d2009-06-29 16:47:10 +0000991 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000992 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
993 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
David Greene54d8eba2011-01-27 22:38:56 +0000994 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000995 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
David Greene54d8eba2011-01-27 22:38:56 +0000996 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000997 }
David Greene54d8eba2011-01-27 22:38:56 +0000998 // Custom-lower insert_subvector and extract_subvector based on
999 // the result type.
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()))
David Greene9b9838d2009-06-29 16:47:10 +00001006 continue;
David Greene54d8eba2011-01-27 22:38:56 +00001007
1008 if (MVT(VT).getSizeInBits() == 128) {
1009 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001010 }
David Greene54d8eba2011-01-27 22:38:56 +00001011 else if (MVT(VT).getSizeInBits() == 256) {
1012 setOperationAction(ISD::INSERT_SUBVECTOR, VT, Custom);
1013 }
David Greene9b9838d2009-06-29 16:47:10 +00001014 }
1015
David Greene54d8eba2011-01-27 22:38:56 +00001016 // Promote v32i8, v16i16, v8i32 select, and, or, xor to v4i64.
1017 // Don't promote loads because we need them for VPERM vector index versions.
1018
1019 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
1020 VT != (unsigned)MVT::LAST_VECTOR_VALUETYPE;
1021 VT++) {
1022 if (!isPowerOf2_32(MVT((MVT::SimpleValueType)VT).getVectorNumElements())
1023 || (MVT((MVT::SimpleValueType)VT).getSizeInBits() < 256))
1024 continue;
1025 setOperationAction(ISD::AND, (MVT::SimpleValueType)VT, Promote);
1026 AddPromotedToType (ISD::AND, (MVT::SimpleValueType)VT, MVT::v4i64);
1027 setOperationAction(ISD::OR, (MVT::SimpleValueType)VT, Promote);
1028 AddPromotedToType (ISD::OR, (MVT::SimpleValueType)VT, MVT::v4i64);
1029 setOperationAction(ISD::XOR, (MVT::SimpleValueType)VT, Promote);
1030 AddPromotedToType (ISD::XOR, (MVT::SimpleValueType)VT, MVT::v4i64);
1031 //setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Promote);
1032 //AddPromotedToType (ISD::LOAD, (MVT::SimpleValueType)VT, MVT::v4i64);
1033 setOperationAction(ISD::SELECT, (MVT::SimpleValueType)VT, Promote);
1034 AddPromotedToType (ISD::SELECT, (MVT::SimpleValueType)VT, MVT::v4i64);
1035 }
David Greene9b9838d2009-06-29 16:47:10 +00001036 }
1037
Evan Cheng6be2c582006-04-05 23:38:46 +00001038 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +00001039 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +00001040
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001041
Eli Friedman962f5492010-06-02 19:35:46 +00001042 // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
1043 // handle type legalization for these operations here.
Dan Gohman71c62a22010-06-02 19:13:40 +00001044 //
Eli Friedman962f5492010-06-02 19:35:46 +00001045 // FIXME: We really should do custom legalization for addition and
1046 // subtraction on x86-32 once PR3203 is fixed. We really can't do much better
1047 // than generic legalization for 64-bit multiplication-with-overflow, though.
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001048 for (unsigned i = 0, e = 3+Subtarget->is64Bit(); i != e; ++i) {
1049 // Add/Sub/Mul with overflow operations are custom lowered.
1050 MVT VT = IntVTs[i];
1051 setOperationAction(ISD::SADDO, VT, Custom);
1052 setOperationAction(ISD::UADDO, VT, Custom);
1053 setOperationAction(ISD::SSUBO, VT, Custom);
1054 setOperationAction(ISD::USUBO, VT, Custom);
1055 setOperationAction(ISD::SMULO, VT, Custom);
1056 setOperationAction(ISD::UMULO, VT, Custom);
Eli Friedmana993f0a2010-06-02 00:27:18 +00001057 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001058
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001059 // There are no 8-bit 3-address imul/mul instructions
1060 setOperationAction(ISD::SMULO, MVT::i8, Expand);
1061 setOperationAction(ISD::UMULO, MVT::i8, Expand);
Bill Wendling41ea7e72008-11-24 19:21:46 +00001062
Evan Chengd54f2d52009-03-31 19:38:51 +00001063 if (!Subtarget->is64Bit()) {
1064 // These libcalls are not available in 32-bit.
1065 setLibcallName(RTLIB::SHL_I128, 0);
1066 setLibcallName(RTLIB::SRL_I128, 0);
1067 setLibcallName(RTLIB::SRA_I128, 0);
1068 }
1069
Evan Cheng206ee9d2006-07-07 08:33:52 +00001070 // We have target-specific dag combine patterns for the following nodes:
1071 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00001072 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Evan Chengd880b972008-05-09 21:53:03 +00001073 setTargetDAGCombine(ISD::BUILD_VECTOR);
Chris Lattner83e6c992006-10-04 06:57:07 +00001074 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +00001075 setTargetDAGCombine(ISD::SHL);
1076 setTargetDAGCombine(ISD::SRA);
1077 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +00001078 setTargetDAGCombine(ISD::OR);
Nate Begemanb65c1752010-12-17 22:55:37 +00001079 setTargetDAGCombine(ISD::AND);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001080 setTargetDAGCombine(ISD::ADD);
1081 setTargetDAGCombine(ISD::SUB);
Chris Lattner149a4e52008-02-22 02:09:43 +00001082 setTargetDAGCombine(ISD::STORE);
Evan Cheng2e489c42009-12-16 00:53:11 +00001083 setTargetDAGCombine(ISD::ZERO_EXTEND);
Evan Cheng0b0cd912009-03-28 05:57:29 +00001084 if (Subtarget->is64Bit())
1085 setTargetDAGCombine(ISD::MUL);
Evan Cheng206ee9d2006-07-07 08:33:52 +00001086
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001087 computeRegisterProperties();
1088
Evan Cheng05219282011-01-06 06:52:41 +00001089 // On Darwin, -Os means optimize for size without hurting performance,
1090 // do not reduce the limit.
Dan Gohman87060f52008-06-30 21:00:56 +00001091 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001092 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 16 : 8;
Evan Cheng255f20f2010-04-01 06:04:33 +00001093 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001094 maxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
1095 maxStoresPerMemmove = 8; // For @llvm.memmove -> sequence of stores
1096 maxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Evan Chengfb8075d2008-02-28 00:43:03 +00001097 setPrefLoopAlignment(16);
Evan Cheng6ebf7bc2009-05-13 21:42:09 +00001098 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +00001099
1100 setPrefFunctionAlignment(4);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001101}
1102
Scott Michel5b8f82e2008-03-10 15:42:14 +00001103
Owen Anderson825b72b2009-08-11 20:47:22 +00001104MVT::SimpleValueType X86TargetLowering::getSetCCResultType(EVT VT) const {
1105 return MVT::i8;
Scott Michel5b8f82e2008-03-10 15:42:14 +00001106}
1107
1108
Evan Cheng29286502008-01-23 23:17:41 +00001109/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1110/// the desired ByVal argument alignment.
1111static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
1112 if (MaxAlign == 16)
1113 return;
1114 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
1115 if (VTy->getBitWidth() == 128)
1116 MaxAlign = 16;
Evan Cheng29286502008-01-23 23:17:41 +00001117 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
1118 unsigned EltAlign = 0;
1119 getMaxByValAlign(ATy->getElementType(), EltAlign);
1120 if (EltAlign > MaxAlign)
1121 MaxAlign = EltAlign;
1122 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
1123 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1124 unsigned EltAlign = 0;
1125 getMaxByValAlign(STy->getElementType(i), EltAlign);
1126 if (EltAlign > MaxAlign)
1127 MaxAlign = EltAlign;
1128 if (MaxAlign == 16)
1129 break;
1130 }
1131 }
1132 return;
1133}
1134
1135/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1136/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001137/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1138/// are at 4-byte boundaries.
Evan Cheng29286502008-01-23 23:17:41 +00001139unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001140 if (Subtarget->is64Bit()) {
1141 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001142 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001143 if (TyAlign > 8)
1144 return TyAlign;
1145 return 8;
1146 }
1147
Evan Cheng29286502008-01-23 23:17:41 +00001148 unsigned Align = 4;
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001149 if (Subtarget->hasXMM())
Dale Johannesen0c191872008-02-08 19:48:20 +00001150 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001151 return Align;
1152}
Chris Lattner2b02a442007-02-25 08:29:00 +00001153
Evan Chengf0df0312008-05-15 08:39:06 +00001154/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001155/// and store operations as a result of memset, memcpy, and memmove
1156/// lowering. If DstAlign is zero that means it's safe to destination
1157/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1158/// means there isn't a need to check it against alignment requirement,
1159/// probably because the source does not need to be loaded. If
1160/// 'NonScalarIntSafe' is true, that means it's safe to return a
1161/// non-scalar-integer type, e.g. empty string source, constant, or loaded
1162/// from memory. 'MemcpyStrSrc' indicates whether the memcpy source is
1163/// constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001164/// It returns EVT::Other if the type should be determined using generic
1165/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001166EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001167X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1168 unsigned DstAlign, unsigned SrcAlign,
Evan Chengf28f8bc2010-04-02 19:36:14 +00001169 bool NonScalarIntSafe,
Evan Chengc3b0c342010-04-08 07:37:57 +00001170 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001171 MachineFunction &MF) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001172 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1173 // linux. This is because the stack realignment code can't handle certain
1174 // cases like PR2962. This should be removed when PR2962 is fixed.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001175 const Function *F = MF.getFunction();
Evan Chenga5e13622011-01-07 19:35:30 +00001176 if (NonScalarIntSafe &&
1177 !F->hasFnAttr(Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001178 if (Size >= 16 &&
Evan Chenga5e13622011-01-07 19:35:30 +00001179 (Subtarget->isUnalignedMemAccessFast() ||
1180 ((DstAlign == 0 || DstAlign >= 16) &&
1181 (SrcAlign == 0 || SrcAlign >= 16))) &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001182 Subtarget->getStackAlignment() >= 16) {
1183 if (Subtarget->hasSSE2())
1184 return MVT::v4i32;
Evan Chengf28f8bc2010-04-02 19:36:14 +00001185 if (Subtarget->hasSSE1())
Evan Cheng255f20f2010-04-01 06:04:33 +00001186 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001187 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001188 !Subtarget->is64Bit() &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001189 Subtarget->getStackAlignment() >= 8 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001190 Subtarget->hasXMMInt()) {
Evan Chengc3b0c342010-04-08 07:37:57 +00001191 // Do not use f64 to lower memcpy if source is string constant. It's
1192 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001193 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001194 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001195 }
Evan Chengf0df0312008-05-15 08:39:06 +00001196 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001197 return MVT::i64;
1198 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001199}
1200
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001201/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1202/// current function. The returned value is a member of the
1203/// MachineJumpTableInfo::JTEntryKind enum.
1204unsigned X86TargetLowering::getJumpTableEncoding() const {
1205 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1206 // symbol.
1207 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1208 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001209 return MachineJumpTableInfo::EK_Custom32;
Michael J. Spencerec38de22010-10-10 22:04:20 +00001210
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001211 // Otherwise, use the normal jump table encoding heuristics.
1212 return TargetLowering::getJumpTableEncoding();
1213}
1214
Chris Lattnerc64daab2010-01-26 05:02:42 +00001215const MCExpr *
1216X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1217 const MachineBasicBlock *MBB,
1218 unsigned uid,MCContext &Ctx) const{
1219 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1220 Subtarget->isPICStyleGOT());
1221 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1222 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001223 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1224 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001225}
1226
Evan Chengcc415862007-11-09 01:32:10 +00001227/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1228/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001229SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001230 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001231 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001232 // This doesn't have DebugLoc associated with it, but is not really the
1233 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001234 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001235 return Table;
1236}
1237
Chris Lattner589c6f62010-01-26 06:28:43 +00001238/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1239/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1240/// MCExpr.
1241const MCExpr *X86TargetLowering::
1242getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1243 MCContext &Ctx) const {
1244 // X86-64 uses RIP relative addressing based on the jump table label.
1245 if (Subtarget->isPICStyleRIPRel())
1246 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1247
1248 // Otherwise, the reference is relative to the PIC base.
Chris Lattner142b5312010-11-14 22:48:15 +00001249 return MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
Chris Lattner589c6f62010-01-26 06:28:43 +00001250}
1251
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001252// FIXME: Why this routine is here? Move to RegInfo!
Evan Chengdee81012010-07-26 21:50:05 +00001253std::pair<const TargetRegisterClass*, uint8_t>
1254X86TargetLowering::findRepresentativeClass(EVT VT) const{
1255 const TargetRegisterClass *RRC = 0;
1256 uint8_t Cost = 1;
1257 switch (VT.getSimpleVT().SimpleTy) {
1258 default:
1259 return TargetLowering::findRepresentativeClass(VT);
1260 case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
1261 RRC = (Subtarget->is64Bit()
1262 ? X86::GR64RegisterClass : X86::GR32RegisterClass);
1263 break;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001264 case MVT::x86mmx:
Evan Chengdee81012010-07-26 21:50:05 +00001265 RRC = X86::VR64RegisterClass;
1266 break;
1267 case MVT::f32: case MVT::f64:
1268 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1269 case MVT::v4f32: case MVT::v2f64:
1270 case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1271 case MVT::v4f64:
1272 RRC = X86::VR128RegisterClass;
1273 break;
1274 }
1275 return std::make_pair(RRC, Cost);
1276}
1277
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001278bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1279 unsigned &Offset) const {
1280 if (!Subtarget->isTargetLinux())
1281 return false;
1282
1283 if (Subtarget->is64Bit()) {
1284 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1285 Offset = 0x28;
1286 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1287 AddressSpace = 256;
1288 else
1289 AddressSpace = 257;
1290 } else {
1291 // %gs:0x14 on i386
1292 Offset = 0x14;
1293 AddressSpace = 256;
1294 }
1295 return true;
1296}
1297
1298
Chris Lattner2b02a442007-02-25 08:29:00 +00001299//===----------------------------------------------------------------------===//
1300// Return Value Calling Convention Implementation
1301//===----------------------------------------------------------------------===//
1302
Chris Lattner59ed56b2007-02-28 04:55:35 +00001303#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001304
Michael J. Spencerec38de22010-10-10 22:04:20 +00001305bool
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001306X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001307 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001308 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001309 SmallVector<CCValAssign, 16> RVLocs;
1310 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001311 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001312 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001313}
1314
Dan Gohman98ca4f22009-08-05 01:29:28 +00001315SDValue
1316X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001317 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001318 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001319 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001320 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001321 MachineFunction &MF = DAG.getMachineFunction();
1322 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001323
Chris Lattner9774c912007-02-27 05:28:59 +00001324 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001325 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1326 RVLocs, *DAG.getContext());
1327 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001328
Evan Chengdcea1632010-02-04 02:40:39 +00001329 // Add the regs to the liveout set for the function.
1330 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1331 for (unsigned i = 0; i != RVLocs.size(); ++i)
1332 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1333 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001334
Dan Gohman475871a2008-07-27 21:46:04 +00001335 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001336
Dan Gohman475871a2008-07-27 21:46:04 +00001337 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001338 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1339 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001340 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1341 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001342
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001343 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001344 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1345 CCValAssign &VA = RVLocs[i];
1346 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001347 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001348 EVT ValVT = ValToCopy.getValueType();
1349
Dale Johannesenc4510512010-09-24 19:05:48 +00001350 // If this is x86-64, and we disabled SSE, we can't return FP values,
1351 // or SSE or MMX vectors.
1352 if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
1353 VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001354 (Subtarget->is64Bit() && !Subtarget->hasXMM())) {
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001355 report_fatal_error("SSE register return with SSE disabled");
1356 }
1357 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1358 // llvm-gcc has never done it right and no one has noticed, so this
1359 // should be OK for now.
1360 if (ValVT == MVT::f64 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001361 (Subtarget->is64Bit() && !Subtarget->hasXMMInt()))
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001362 report_fatal_error("SSE2 register return with SSE2 disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001363
Chris Lattner447ff682008-03-11 03:23:40 +00001364 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1365 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001366 if (VA.getLocReg() == X86::ST0 ||
1367 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001368 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1369 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001370 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001371 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001372 RetOps.push_back(ValToCopy);
1373 // Don't emit a copytoreg.
1374 continue;
1375 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001376
Evan Cheng242b38b2009-02-23 09:03:22 +00001377 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1378 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001379 if (Subtarget->is64Bit()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001380 if (ValVT == MVT::x86mmx) {
Chris Lattner97a2a562010-08-26 05:24:29 +00001381 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001382 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ValToCopy);
Eric Christopher90eb4022010-07-22 00:26:08 +00001383 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1384 ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001385 // If we don't have SSE2 available, convert to v4f32 so the generated
1386 // register is legal.
1387 if (!Subtarget->hasSSE2())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001388 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32,ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001389 }
Evan Cheng242b38b2009-02-23 09:03:22 +00001390 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001391 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00001392
Dale Johannesendd64c412009-02-04 00:33:20 +00001393 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001394 Flag = Chain.getValue(1);
1395 }
Dan Gohman61a92132008-04-21 23:59:07 +00001396
1397 // The x86-64 ABI for returning structs by value requires that we copy
1398 // the sret argument into %rax for the return. We saved the argument into
1399 // a virtual register in the entry block, so now we copy the value out
1400 // and into %rax.
1401 if (Subtarget->is64Bit() &&
1402 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1403 MachineFunction &MF = DAG.getMachineFunction();
1404 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1405 unsigned Reg = FuncInfo->getSRetReturnReg();
Michael J. Spencerec38de22010-10-10 22:04:20 +00001406 assert(Reg &&
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001407 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001408 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001409
Dale Johannesendd64c412009-02-04 00:33:20 +00001410 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001411 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001412
1413 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001414 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001415 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001416
Chris Lattner447ff682008-03-11 03:23:40 +00001417 RetOps[0] = Chain; // Update chain.
1418
1419 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001420 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001421 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001422
1423 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001424 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001425}
1426
Evan Cheng3d2125c2010-11-30 23:55:39 +00001427bool X86TargetLowering::isUsedByReturnOnly(SDNode *N) const {
1428 if (N->getNumValues() != 1)
1429 return false;
1430 if (!N->hasNUsesOfValue(1, 0))
1431 return false;
1432
1433 SDNode *Copy = *N->use_begin();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001434 if (Copy->getOpcode() != ISD::CopyToReg &&
1435 Copy->getOpcode() != ISD::FP_EXTEND)
Evan Cheng3d2125c2010-11-30 23:55:39 +00001436 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001437
1438 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001439 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001440 UI != UE; ++UI) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001441 if (UI->getOpcode() != X86ISD::RET_FLAG)
1442 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001443 HasRet = true;
1444 }
Evan Cheng3d2125c2010-11-30 23:55:39 +00001445
Evan Cheng1bf891a2010-12-01 22:59:46 +00001446 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001447}
1448
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001449EVT
1450X86TargetLowering::getTypeForExtArgOrReturn(LLVMContext &Context, EVT VT,
Cameron Zwarich44579682011-03-17 14:21:56 +00001451 ISD::NodeType ExtendKind) const {
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001452 MVT ReturnMVT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001453 // TODO: Is this also valid on 32-bit?
1454 if (Subtarget->is64Bit() && VT == MVT::i1 && ExtendKind == ISD::ZERO_EXTEND)
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001455 ReturnMVT = MVT::i8;
1456 else
1457 ReturnMVT = MVT::i32;
1458
1459 EVT MinVT = getRegisterType(Context, ReturnMVT);
1460 return VT.bitsLT(MinVT) ? MinVT : VT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001461}
1462
Dan Gohman98ca4f22009-08-05 01:29:28 +00001463/// LowerCallResult - Lower the result values of a call into the
1464/// appropriate copies out of appropriate physical registers.
1465///
1466SDValue
1467X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001468 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001469 const SmallVectorImpl<ISD::InputArg> &Ins,
1470 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001471 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001472
Chris Lattnere32bbf62007-02-28 07:09:55 +00001473 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001474 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001475 bool Is64Bit = Subtarget->is64Bit();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001476 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +00001477 RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001478 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001479
Chris Lattner3085e152007-02-25 08:59:22 +00001480 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001481 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001482 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001483 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001484
Torok Edwin3f142c32009-02-01 18:15:56 +00001485 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001486 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001487 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasXMM())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001488 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001489 }
1490
Evan Cheng79fb3b42009-02-20 20:43:02 +00001491 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001492
1493 // If this is a call to a function that returns an fp value on the floating
1494 // point stack, we must guarantee the the value is popped from the stack, so
1495 // a CopyFromReg is not good enough - the copy instruction may be eliminated
1496 // if the return value is not used. We use the FpGET_ST0 instructions
1497 // instead.
1498 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1499 // If we prefer to use the value in xmm registers, copy it out as f80 and
1500 // use a truncate to move it from fp stack reg to xmm reg.
1501 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
1502 bool isST0 = VA.getLocReg() == X86::ST0;
1503 unsigned Opc = 0;
1504 if (CopyVT == MVT::f32) Opc = isST0 ? X86::FpGET_ST0_32:X86::FpGET_ST1_32;
1505 if (CopyVT == MVT::f64) Opc = isST0 ? X86::FpGET_ST0_64:X86::FpGET_ST1_64;
1506 if (CopyVT == MVT::f80) Opc = isST0 ? X86::FpGET_ST0_80:X86::FpGET_ST1_80;
1507 SDValue Ops[] = { Chain, InFlag };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001508 Chain = SDValue(DAG.getMachineNode(Opc, dl, CopyVT, MVT::Other, MVT::Glue,
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001509 Ops, 2), 1);
1510 Val = Chain.getValue(0);
1511
1512 // Round the f80 to the right size, which also moves it to the appropriate
1513 // xmm register.
1514 if (CopyVT != VA.getValVT())
1515 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1516 // This truncation won't change the value.
1517 DAG.getIntPtrConstant(1));
1518 } else if (Is64Bit && CopyVT.isVector() && CopyVT.getSizeInBits() == 64) {
Evan Cheng242b38b2009-02-23 09:03:22 +00001519 // For x86-64, MMX values are returned in XMM0 / XMM1 except for v1i64.
1520 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
1521 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001522 MVT::v2i64, InFlag).getValue(1);
Evan Cheng242b38b2009-02-23 09:03:22 +00001523 Val = Chain.getValue(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00001524 Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1525 Val, DAG.getConstant(0, MVT::i64));
Evan Cheng242b38b2009-02-23 09:03:22 +00001526 } else {
1527 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001528 MVT::i64, InFlag).getValue(1);
Evan Cheng242b38b2009-02-23 09:03:22 +00001529 Val = Chain.getValue(0);
1530 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001531 Val = DAG.getNode(ISD::BITCAST, dl, CopyVT, Val);
Evan Cheng79fb3b42009-02-20 20:43:02 +00001532 } else {
1533 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1534 CopyVT, InFlag).getValue(1);
1535 Val = Chain.getValue(0);
1536 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001537 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001538 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001539 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001540
Dan Gohman98ca4f22009-08-05 01:29:28 +00001541 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001542}
1543
1544
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001545//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001546// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001547//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001548// StdCall calling convention seems to be standard for many Windows' API
1549// routines and around. It differs from C calling convention just a little:
1550// callee should clean up the stack, not caller. Symbols should be also
1551// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001552// For info on fast calling convention see Fast Calling Convention (tail call)
1553// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001554
Dan Gohman98ca4f22009-08-05 01:29:28 +00001555/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001556/// semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001557static bool CallIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
1558 if (Outs.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001559 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001560
Dan Gohman98ca4f22009-08-05 01:29:28 +00001561 return Outs[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001562}
1563
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001564/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001565/// return semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001566static bool
1567ArgsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
1568 if (Ins.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001569 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001570
Dan Gohman98ca4f22009-08-05 01:29:28 +00001571 return Ins[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001572}
1573
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001574/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1575/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001576/// the specific parameter attribute. The copy will be passed as a byval
1577/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001578static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001579CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001580 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1581 DebugLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00001582 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Michael J. Spencerec38de22010-10-10 22:04:20 +00001583
Dale Johannesendd64c412009-02-04 00:33:20 +00001584 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Stuart Hastings03d58262011-03-10 00:25:53 +00001585 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001586 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001587}
1588
Chris Lattner29689432010-03-11 00:22:57 +00001589/// IsTailCallConvention - Return true if the calling convention is one that
1590/// supports tail call optimization.
1591static bool IsTailCallConvention(CallingConv::ID CC) {
1592 return (CC == CallingConv::Fast || CC == CallingConv::GHC);
1593}
1594
Evan Cheng485fafc2011-03-21 01:19:09 +00001595bool X86TargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
1596 if (!CI->isTailCall())
1597 return false;
1598
1599 CallSite CS(CI);
1600 CallingConv::ID CalleeCC = CS.getCallingConv();
1601 if (!IsTailCallConvention(CalleeCC) && CalleeCC != CallingConv::C)
1602 return false;
1603
1604 return true;
1605}
1606
Evan Cheng0c439eb2010-01-27 00:07:07 +00001607/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1608/// a tailcall target by changing its ABI.
1609static bool FuncIsMadeTailCallSafe(CallingConv::ID CC) {
Chris Lattner29689432010-03-11 00:22:57 +00001610 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001611}
1612
Dan Gohman98ca4f22009-08-05 01:29:28 +00001613SDValue
1614X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001615 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001616 const SmallVectorImpl<ISD::InputArg> &Ins,
1617 DebugLoc dl, SelectionDAG &DAG,
1618 const CCValAssign &VA,
1619 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001620 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001621 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001622 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Evan Cheng0c439eb2010-01-27 00:07:07 +00001623 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001624 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001625 EVT ValVT;
1626
1627 // If value is passed by pointer we have address passed instead of the value
1628 // itself.
1629 if (VA.getLocInfo() == CCValAssign::Indirect)
1630 ValVT = VA.getLocVT();
1631 else
1632 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001633
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001634 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001635 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001636 // In case of tail call optimization mark all arguments mutable. Since they
1637 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001638 if (Flags.isByVal()) {
Evan Chengee2e0e32011-03-30 23:44:13 +00001639 unsigned Bytes = Flags.getByValSize();
1640 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
1641 int FI = MFI->CreateFixedObject(Bytes, VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001642 return DAG.getFrameIndex(FI, getPointerTy());
1643 } else {
1644 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001645 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001646 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1647 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001648 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00001649 false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001650 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001651}
1652
Dan Gohman475871a2008-07-27 21:46:04 +00001653SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001654X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001655 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001656 bool isVarArg,
1657 const SmallVectorImpl<ISD::InputArg> &Ins,
1658 DebugLoc dl,
1659 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001660 SmallVectorImpl<SDValue> &InVals)
1661 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001662 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001663 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001664
Gordon Henriksen86737662008-01-05 16:56:59 +00001665 const Function* Fn = MF.getFunction();
1666 if (Fn->hasExternalLinkage() &&
1667 Subtarget->isTargetCygMing() &&
1668 Fn->getName() == "main")
1669 FuncInfo->setForceFramePointer(true);
1670
Evan Cheng1bc78042006-04-26 01:20:17 +00001671 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001672 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001673 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001674
Chris Lattner29689432010-03-11 00:22:57 +00001675 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1676 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001677
Chris Lattner638402b2007-02-28 07:00:42 +00001678 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001679 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001680 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1681 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001682
1683 // Allocate shadow area for Win64
1684 if (IsWin64) {
1685 CCInfo.AllocateStack(32, 8);
1686 }
1687
Duncan Sands45907662010-10-31 13:21:44 +00001688 CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001689
Chris Lattnerf39f7712007-02-28 05:46:49 +00001690 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001691 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001692 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1693 CCValAssign &VA = ArgLocs[i];
1694 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1695 // places.
1696 assert(VA.getValNo() != LastVal &&
1697 "Don't support value assigned to multiple locs yet");
1698 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001699
Chris Lattnerf39f7712007-02-28 05:46:49 +00001700 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001701 EVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001702 TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001703 if (RegVT == MVT::i32)
Chris Lattnerf39f7712007-02-28 05:46:49 +00001704 RC = X86::GR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001705 else if (Is64Bit && RegVT == MVT::i64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001706 RC = X86::GR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001707 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001708 RC = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001709 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001710 RC = X86::FR64RegisterClass;
Bruno Cardoso Lopesac098352010-08-05 23:35:51 +00001711 else if (RegVT.isVector() && RegVT.getSizeInBits() == 256)
1712 RC = X86::VR256RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001713 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001714 RC = X86::VR128RegisterClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001715 else if (RegVT == MVT::x86mmx)
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001716 RC = X86::VR64RegisterClass;
1717 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001718 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001719
Devang Patel68e6bee2011-02-21 23:21:26 +00001720 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001721 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001722
Chris Lattnerf39f7712007-02-28 05:46:49 +00001723 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1724 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1725 // right size.
1726 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001727 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001728 DAG.getValueType(VA.getValVT()));
1729 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001730 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001731 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001732 else if (VA.getLocInfo() == CCValAssign::BCvt)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001733 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001734
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001735 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001736 // Handle MMX values passed in XMM regs.
1737 if (RegVT.isVector()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001738 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(),
1739 ArgValue);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001740 } else
1741 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001742 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001743 } else {
1744 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001745 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001746 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001747
1748 // If value is passed via pointer - do a load.
1749 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00001750 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
1751 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001752
Dan Gohman98ca4f22009-08-05 01:29:28 +00001753 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001754 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001755
Dan Gohman61a92132008-04-21 23:59:07 +00001756 // The x86-64 ABI for returning structs by value requires that we copy
1757 // the sret argument into %rax for the return. Save the argument into
1758 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001759 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001760 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1761 unsigned Reg = FuncInfo->getSRetReturnReg();
1762 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001763 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001764 FuncInfo->setSRetReturnReg(Reg);
1765 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001766 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001767 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001768 }
1769
Chris Lattnerf39f7712007-02-28 05:46:49 +00001770 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001771 // Align stack specially for tail calls.
1772 if (FuncIsMadeTailCallSafe(CallConv))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001773 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001774
Evan Cheng1bc78042006-04-26 01:20:17 +00001775 // If the function takes variable number of arguments, make a frame index for
1776 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001777 if (isVarArg) {
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001778 if (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
1779 CallConv != CallingConv::X86_ThisCall)) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00001780 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00001781 }
1782 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001783 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1784
1785 // FIXME: We should really autogenerate these arrays
1786 static const unsigned GPR64ArgRegsWin64[] = {
1787 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001788 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001789 static const unsigned GPR64ArgRegs64Bit[] = {
1790 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1791 };
1792 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001793 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1794 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1795 };
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001796 const unsigned *GPR64ArgRegs;
1797 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001798
1799 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001800 // The XMM registers which might contain var arg parameters are shadowed
1801 // in their paired GPR. So we only need to save the GPR to their home
1802 // slots.
1803 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001804 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001805 } else {
1806 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1807 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001808
1809 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit, TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001810 }
1811 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1812 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001813
Devang Patel578efa92009-06-05 21:57:13 +00001814 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001815 assert(!(NumXMMRegs && !Subtarget->hasXMM()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001816 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001817 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001818 "SSE register cannot be used when SSE is disabled!");
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001819 if (UseSoftFloat || NoImplicitFloatOps || !Subtarget->hasXMM())
Torok Edwin3f142c32009-02-01 18:15:56 +00001820 // Kernel mode asks for SSE to be disabled, so don't push them
1821 // on the stack.
1822 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001823
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001824 if (IsWin64) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001825 const TargetFrameLowering &TFI = *getTargetMachine().getFrameLowering();
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001826 // Get to the caller-allocated home save location. Add 8 to account
1827 // for the return address.
1828 int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001829 FuncInfo->setRegSaveFrameIndex(
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001830 MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001831 // Fixup to set vararg frame on shadow area (4 x i64).
1832 if (NumIntRegs < 4)
1833 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001834 } else {
1835 // For X86-64, if there are vararg parameters that are passed via
1836 // registers, then we must store them to their spots on the stack so they
1837 // may be loaded by deferencing the result of va_next.
1838 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
1839 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
1840 FuncInfo->setRegSaveFrameIndex(
1841 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00001842 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001843 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001844
Gordon Henriksen86737662008-01-05 16:56:59 +00001845 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001846 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00001847 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
1848 getPointerTy());
1849 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001850 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00001851 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
1852 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00001853 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00001854 X86::GR64RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00001855 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001856 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001857 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001858 MachinePointerInfo::getFixedStack(
1859 FuncInfo->getRegSaveFrameIndex(), Offset),
1860 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001861 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001862 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001863 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001864
Dan Gohmanface41a2009-08-16 21:24:25 +00001865 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
1866 // Now store the XMM (fp + vector) parameter registers.
1867 SmallVector<SDValue, 11> SaveXMMOps;
1868 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001869
Devang Patel68e6bee2011-02-21 23:21:26 +00001870 unsigned AL = MF.addLiveIn(X86::AL, X86::GR8RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00001871 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
1872 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001873
Dan Gohman1e93df62010-04-17 14:41:14 +00001874 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1875 FuncInfo->getRegSaveFrameIndex()));
1876 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1877 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00001878
Dan Gohmanface41a2009-08-16 21:24:25 +00001879 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001880 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00001881 X86::VR128RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00001882 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
1883 SaveXMMOps.push_back(Val);
1884 }
1885 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
1886 MVT::Other,
1887 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00001888 }
Dan Gohmanface41a2009-08-16 21:24:25 +00001889
1890 if (!MemOps.empty())
1891 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1892 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001893 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001894 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001895
Gordon Henriksen86737662008-01-05 16:56:59 +00001896 // Some CCs need callee pop.
Dan Gohman4d3d6e12010-05-27 18:43:40 +00001897 if (Subtarget->IsCalleePop(isVarArg, CallConv)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001898 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001899 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00001900 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001901 // If this is an sret function, the return should pop the hidden pointer.
Chris Lattner29689432010-03-11 00:22:57 +00001902 if (!Is64Bit && !IsTailCallConvention(CallConv) && ArgsAreStructReturn(Ins))
Dan Gohman1e93df62010-04-17 14:41:14 +00001903 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001904 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001905
Gordon Henriksen86737662008-01-05 16:56:59 +00001906 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001907 // RegSaveFrameIndex is X86-64 only.
1908 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001909 if (CallConv == CallingConv::X86_FastCall ||
1910 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00001911 // fastcc functions can't have varargs.
1912 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00001913 }
Evan Cheng25caf632006-05-23 21:06:34 +00001914
Dan Gohman98ca4f22009-08-05 01:29:28 +00001915 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001916}
1917
Dan Gohman475871a2008-07-27 21:46:04 +00001918SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001919X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
1920 SDValue StackPtr, SDValue Arg,
1921 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00001922 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001923 ISD::ArgFlagsTy Flags) const {
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001924 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00001925 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00001926 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001927 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00001928 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001929
1930 return DAG.getStore(Chain, dl, Arg, PtrOff,
1931 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00001932 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00001933}
1934
Bill Wendling64e87322009-01-16 19:25:27 +00001935/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001936/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00001937SDValue
1938X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00001939 SDValue &OutRetAddr, SDValue Chain,
1940 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00001941 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001942 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00001943 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001944 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00001945
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001946 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00001947 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
1948 false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001949 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001950}
1951
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001952/// EmitTailCallStoreRetAddr - Emit a store of the return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001953/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00001954static SDValue
1955EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00001956 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00001957 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001958 // Store the return address to the appropriate stack slot.
1959 if (!FPDiff) return Chain;
1960 // Calculate the new stack slot for the return address.
1961 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00001962 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00001963 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00001964 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00001965 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001966 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00001967 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00001968 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001969 return Chain;
1970}
1971
Dan Gohman98ca4f22009-08-05 01:29:28 +00001972SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001973X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001974 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001975 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001976 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001977 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001978 const SmallVectorImpl<ISD::InputArg> &Ins,
1979 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001980 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001981 MachineFunction &MF = DAG.getMachineFunction();
1982 bool Is64Bit = Subtarget->is64Bit();
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00001983 bool IsWin64 = Subtarget->isTargetWin64();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001984 bool IsStructRet = CallIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00001985 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001986
Evan Cheng5f941932010-02-05 02:21:12 +00001987 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00001988 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00001989 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1990 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001991 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00001992
1993 // Sibcalls are automatically detected tailcalls which do not require
1994 // ABI changes.
Dan Gohman1797ed52010-02-08 20:27:50 +00001995 if (!GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00001996 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00001997
1998 if (isTailCall)
1999 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00002000 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00002001
Chris Lattner29689432010-03-11 00:22:57 +00002002 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
2003 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00002004
Chris Lattner638402b2007-02-28 07:00:42 +00002005 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00002006 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002007 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
2008 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002009
2010 // Allocate shadow area for Win64
2011 if (IsWin64) {
2012 CCInfo.AllocateStack(32, 8);
2013 }
2014
Duncan Sands45907662010-10-31 13:21:44 +00002015 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00002016
Chris Lattner423c5f42007-02-28 05:31:48 +00002017 // Get a count of how many bytes are to be pushed on the stack.
2018 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00002019 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00002020 // This is a sibcall. The memory operands are available in caller's
2021 // own caller's stack.
2022 NumBytes = 0;
Chris Lattner29689432010-03-11 00:22:57 +00002023 else if (GuaranteedTailCallOpt && IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00002024 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002025
Gordon Henriksen86737662008-01-05 16:56:59 +00002026 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00002027 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002028 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00002029 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00002030 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
2031 FPDiff = NumBytesCallerPushed - NumBytes;
2032
2033 // Set the delta of movement of the returnaddr stackslot.
2034 // But only set if delta is greater than previous delta.
2035 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
2036 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
2037 }
2038
Evan Chengf22f9b32010-02-06 03:28:46 +00002039 if (!IsSibcall)
2040 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002041
Dan Gohman475871a2008-07-27 21:46:04 +00002042 SDValue RetAddrFrIdx;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002043 // Load return address for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00002044 if (isTailCall && FPDiff)
2045 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
2046 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002047
Dan Gohman475871a2008-07-27 21:46:04 +00002048 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
2049 SmallVector<SDValue, 8> MemOpChains;
2050 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00002051
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002052 // Walk the register/memloc assignments, inserting copies/loads. In the case
2053 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00002054 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2055 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00002056 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00002057 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002058 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00002059 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00002060
Chris Lattner423c5f42007-02-28 05:31:48 +00002061 // Promote the value if needed.
2062 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002063 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00002064 case CCValAssign::Full: break;
2065 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002066 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002067 break;
2068 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002069 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002070 break;
2071 case CCValAssign::AExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002072 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
2073 // Special case: passing MMX values in XMM registers.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002074 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00002075 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
2076 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002077 } else
2078 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
2079 break;
2080 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002081 Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002082 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002083 case CCValAssign::Indirect: {
2084 // Store the argument.
2085 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00002086 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002087 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00002088 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002089 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002090 Arg = SpillSlot;
2091 break;
2092 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00002093 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002094
Chris Lattner423c5f42007-02-28 05:31:48 +00002095 if (VA.isRegLoc()) {
2096 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002097 if (isVarArg && IsWin64) {
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002098 // Win64 ABI requires argument XMM reg to be copied to the corresponding
2099 // shadow reg if callee is a varargs function.
2100 unsigned ShadowReg = 0;
2101 switch (VA.getLocReg()) {
2102 case X86::XMM0: ShadowReg = X86::RCX; break;
2103 case X86::XMM1: ShadowReg = X86::RDX; break;
2104 case X86::XMM2: ShadowReg = X86::R8; break;
2105 case X86::XMM3: ShadowReg = X86::R9; break;
2106 }
2107 if (ShadowReg)
2108 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
2109 }
Evan Chengf22f9b32010-02-06 03:28:46 +00002110 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00002111 assert(VA.isMemLoc());
2112 if (StackPtr.getNode() == 0)
2113 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
2114 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
2115 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002116 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002117 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002118
Evan Cheng32fe1032006-05-25 00:59:30 +00002119 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002120 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002121 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002122
Evan Cheng347d5f72006-04-28 21:29:37 +00002123 // Build a sequence of copy-to-reg nodes chained together with token chain
2124 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002125 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002126 // Tail call byval lowering might overwrite argument registers so in case of
2127 // tail call optimization the copies to registers are lowered later.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002128 if (!isTailCall)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002129 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002130 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002131 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002132 InFlag = Chain.getValue(1);
2133 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002134
Chris Lattner88e1fd52009-07-09 04:24:46 +00002135 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002136 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2137 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002138 if (!isTailCall) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002139 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
2140 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00002141 DebugLoc(), getPointerTy()),
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002142 InFlag);
2143 InFlag = Chain.getValue(1);
2144 } else {
2145 // If we are tail calling and generating PIC/GOT style code load the
2146 // address of the callee into ECX. The value in ecx is used as target of
2147 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2148 // for tail calls on PIC/GOT architectures. Normally we would just put the
2149 // address of GOT into ebx and then call target@PLT. But for tail calls
2150 // ebx would be restored (since ebx is callee saved) before jumping to the
2151 // target@PLT.
2152
2153 // Note: The actual moving to ECX is done further down.
2154 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2155 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2156 !G->getGlobal()->hasProtectedVisibility())
2157 Callee = LowerGlobalAddress(Callee, DAG);
2158 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002159 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002160 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002161 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002162
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002163 if (Is64Bit && isVarArg && !IsWin64) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002164 // From AMD64 ABI document:
2165 // For calls that may call functions that use varargs or stdargs
2166 // (prototype-less calls or calls to functions containing ellipsis (...) in
2167 // the declaration) %al is used as hidden argument to specify the number
2168 // of SSE registers used. The contents of %al do not need to match exactly
2169 // the number of registers, but must be an ubound on the number of SSE
2170 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002171
Gordon Henriksen86737662008-01-05 16:56:59 +00002172 // Count the number of XMM registers allocated.
2173 static const unsigned XMMArgRegs[] = {
2174 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2175 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2176 };
2177 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00002178 assert((Subtarget->hasXMM() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002179 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002180
Dale Johannesendd64c412009-02-04 00:33:20 +00002181 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Owen Anderson825b72b2009-08-11 20:47:22 +00002182 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002183 InFlag = Chain.getValue(1);
2184 }
2185
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002186
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002187 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002188 if (isTailCall) {
2189 // Force all the incoming stack arguments to be loaded from the stack
2190 // before any new outgoing arguments are stored to the stack, because the
2191 // outgoing stack slots may alias the incoming argument stack slots, and
2192 // the alias isn't otherwise explicit. This is slightly more conservative
2193 // than necessary, because it means that each store effectively depends
2194 // on every argument instead of just those arguments it would clobber.
2195 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2196
Dan Gohman475871a2008-07-27 21:46:04 +00002197 SmallVector<SDValue, 8> MemOpChains2;
2198 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002199 int FI = 0;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002200 // Do not flag preceding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00002201 InFlag = SDValue();
Dan Gohman1797ed52010-02-08 20:27:50 +00002202 if (GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002203 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2204 CCValAssign &VA = ArgLocs[i];
2205 if (VA.isRegLoc())
2206 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002207 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002208 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002209 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002210 // Create frame index.
2211 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002212 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002213 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002214 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002215
Duncan Sands276dcbd2008-03-21 09:14:45 +00002216 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002217 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002218 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002219 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002220 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00002221 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002222 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002223
Dan Gohman98ca4f22009-08-05 01:29:28 +00002224 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2225 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002226 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002227 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002228 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002229 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002230 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002231 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002232 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002233 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002234 }
2235 }
2236
2237 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002238 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002239 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002240
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002241 // Copy arguments to their registers.
2242 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002243 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002244 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002245 InFlag = Chain.getValue(1);
2246 }
Dan Gohman475871a2008-07-27 21:46:04 +00002247 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002248
Gordon Henriksen86737662008-01-05 16:56:59 +00002249 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002250 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002251 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002252 }
2253
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002254 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2255 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2256 // In the 64-bit large code model, we have to make all calls
2257 // through a register, since the call instruction's 32-bit
2258 // pc-relative offset may not be large enough to hold the whole
2259 // address.
2260 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002261 // If the callee is a GlobalAddress node (quite common, every direct call
2262 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2263 // it.
2264
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002265 // We should use extra load for direct calls to dllimported functions in
2266 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002267 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002268 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002269 unsigned char OpFlags = 0;
Eric Christopherfd179292009-08-27 18:07:15 +00002270
Chris Lattner48a7d022009-07-09 05:02:21 +00002271 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2272 // external symbols most go through the PLT in PIC mode. If the symbol
2273 // has hidden or protected visibility, or if it is static or local, then
2274 // we don't need to use the PLT - we can directly call it.
2275 if (Subtarget->isTargetELF() &&
2276 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002277 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002278 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002279 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002280 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002281 (!Subtarget->getTargetTriple().isMacOSX() ||
2282 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002283 // PC-relative references to external symbols should go through $stub,
2284 // unless we're building with the leopard linker or later, which
2285 // automatically synthesizes these stubs.
2286 OpFlags = X86II::MO_DARWIN_STUB;
2287 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002288
Devang Patel0d881da2010-07-06 22:08:15 +00002289 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002290 G->getOffset(), OpFlags);
2291 }
Bill Wendling056292f2008-09-16 21:48:12 +00002292 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002293 unsigned char OpFlags = 0;
2294
Evan Cheng1bf891a2010-12-01 22:59:46 +00002295 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2296 // external symbols should go through the PLT.
2297 if (Subtarget->isTargetELF() &&
2298 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2299 OpFlags = X86II::MO_PLT;
2300 } else if (Subtarget->isPICStyleStubAny() &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002301 (!Subtarget->getTargetTriple().isMacOSX() ||
2302 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Evan Cheng1bf891a2010-12-01 22:59:46 +00002303 // PC-relative references to external symbols should go through $stub,
2304 // unless we're building with the leopard linker or later, which
2305 // automatically synthesizes these stubs.
2306 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002307 }
Eric Christopherfd179292009-08-27 18:07:15 +00002308
Chris Lattner48a7d022009-07-09 05:02:21 +00002309 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2310 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002311 }
2312
Chris Lattnerd96d0722007-02-25 06:40:16 +00002313 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002314 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002315 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002316
Evan Chengf22f9b32010-02-06 03:28:46 +00002317 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002318 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2319 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002320 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002321 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002322
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002323 Ops.push_back(Chain);
2324 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002325
Dan Gohman98ca4f22009-08-05 01:29:28 +00002326 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002327 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002328
Gordon Henriksen86737662008-01-05 16:56:59 +00002329 // Add argument registers to the end of the list so that they are known live
2330 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002331 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2332 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2333 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002334
Evan Cheng586ccac2008-03-18 23:36:35 +00002335 // Add an implicit use GOT pointer in EBX.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002336 if (!isTailCall && Subtarget->isPICStyleGOT())
Evan Cheng586ccac2008-03-18 23:36:35 +00002337 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
2338
Anton Korobeynikov3a1e54a2010-08-17 21:06:07 +00002339 // Add an implicit use of AL for non-Windows x86 64-bit vararg functions.
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002340 if (Is64Bit && isVarArg && !IsWin64)
Owen Anderson825b72b2009-08-11 20:47:22 +00002341 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
Evan Cheng586ccac2008-03-18 23:36:35 +00002342
Gabor Greifba36cb52008-08-28 21:40:38 +00002343 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002344 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002345
Dan Gohman98ca4f22009-08-05 01:29:28 +00002346 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002347 // We used to do:
2348 //// If this is the first return lowered for this function, add the regs
2349 //// to the liveout set for the function.
2350 // This isn't right, although it's probably harmless on x86; liveouts
2351 // should be computed from returns not tail calls. Consider a void
2352 // function making a tail call to a function returning int.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002353 return DAG.getNode(X86ISD::TC_RETURN, dl,
2354 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002355 }
2356
Dale Johannesenace16102009-02-03 19:33:06 +00002357 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002358 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002359
Chris Lattner2d297092006-05-23 18:50:38 +00002360 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002361 unsigned NumBytesForCalleeToPush;
Dan Gohman4d3d6e12010-05-27 18:43:40 +00002362 if (Subtarget->IsCalleePop(isVarArg, CallConv))
Gordon Henriksen86737662008-01-05 16:56:59 +00002363 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Chris Lattner29689432010-03-11 00:22:57 +00002364 else if (!Is64Bit && !IsTailCallConvention(CallConv) && IsStructRet)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002365 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002366 // pops the hidden struct pointer, so we have to push it back.
2367 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002368 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002369 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002370 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002371
Gordon Henriksenae636f82008-01-03 16:47:34 +00002372 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002373 if (!IsSibcall) {
2374 Chain = DAG.getCALLSEQ_END(Chain,
2375 DAG.getIntPtrConstant(NumBytes, true),
2376 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2377 true),
2378 InFlag);
2379 InFlag = Chain.getValue(1);
2380 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002381
Chris Lattner3085e152007-02-25 08:59:22 +00002382 // Handle result values, copying them out of physregs into vregs that we
2383 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002384 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2385 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002386}
2387
Evan Cheng25ab6902006-09-08 06:48:29 +00002388
2389//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002390// Fast Calling Convention (tail call) implementation
2391//===----------------------------------------------------------------------===//
2392
2393// Like std call, callee cleans arguments, convention except that ECX is
2394// reserved for storing the tail called function address. Only 2 registers are
2395// free for argument passing (inreg). Tail call optimization is performed
2396// provided:
2397// * tailcallopt is enabled
2398// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002399// On X86_64 architecture with GOT-style position independent code only local
2400// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002401// To keep the stack aligned according to platform abi the function
2402// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2403// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002404// If a tail called function callee has more arguments than the caller the
2405// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002406// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002407// original REtADDR, but before the saved framepointer or the spilled registers
2408// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2409// stack layout:
2410// arg1
2411// arg2
2412// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002413// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002414// move area ]
2415// (possible EBP)
2416// ESI
2417// EDI
2418// local1 ..
2419
2420/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2421/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002422unsigned
2423X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2424 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002425 MachineFunction &MF = DAG.getMachineFunction();
2426 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002427 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002428 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002429 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002430 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002431 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002432 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2433 // Number smaller than 12 so just add the difference.
2434 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2435 } else {
2436 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002437 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002438 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002439 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002440 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002441}
2442
Evan Cheng5f941932010-02-05 02:21:12 +00002443/// MatchingStackOffset - Return true if the given stack call argument is
2444/// already available in the same position (relatively) of the caller's
2445/// incoming argument stack.
2446static
2447bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2448 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2449 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002450 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2451 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002452 if (Arg.getOpcode() == ISD::CopyFromReg) {
2453 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00002454 if (!TargetRegisterInfo::isVirtualRegister(VR))
Evan Cheng5f941932010-02-05 02:21:12 +00002455 return false;
2456 MachineInstr *Def = MRI->getVRegDef(VR);
2457 if (!Def)
2458 return false;
2459 if (!Flags.isByVal()) {
2460 if (!TII->isLoadFromStackSlot(Def, FI))
2461 return false;
2462 } else {
2463 unsigned Opcode = Def->getOpcode();
2464 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2465 Def->getOperand(1).isFI()) {
2466 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002467 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002468 } else
2469 return false;
2470 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002471 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2472 if (Flags.isByVal())
2473 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002474 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002475 // define @foo(%struct.X* %A) {
2476 // tail call @bar(%struct.X* byval %A)
2477 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002478 return false;
2479 SDValue Ptr = Ld->getBasePtr();
2480 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2481 if (!FINode)
2482 return false;
2483 FI = FINode->getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002484 } else
2485 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002486
Evan Cheng4cae1332010-03-05 08:38:04 +00002487 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002488 if (!MFI->isFixedObjectIndex(FI))
2489 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002490 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002491}
2492
Dan Gohman98ca4f22009-08-05 01:29:28 +00002493/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2494/// for tail call optimization. Targets which want to do tail call
2495/// optimization should implement this function.
2496bool
2497X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002498 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002499 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002500 bool isCalleeStructRet,
2501 bool isCallerStructRet,
Evan Chengb1712452010-01-27 06:25:16 +00002502 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002503 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002504 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002505 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002506 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002507 CalleeCC != CallingConv::C)
2508 return false;
2509
Evan Cheng7096ae42010-01-29 06:45:59 +00002510 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002511 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002512 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Cheng13617962010-04-30 01:12:32 +00002513 CallingConv::ID CallerCC = CallerF->getCallingConv();
2514 bool CCMatch = CallerCC == CalleeCC;
2515
Dan Gohman1797ed52010-02-08 20:27:50 +00002516 if (GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002517 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002518 return true;
2519 return false;
2520 }
2521
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002522 // Look for obvious safe cases to perform tail call optimization that do not
2523 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002524
Evan Cheng2c12cb42010-03-26 16:26:03 +00002525 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2526 // emit a special epilogue.
2527 if (RegInfo->needsStackRealignment(MF))
2528 return false;
2529
Eric Christopher90eb4022010-07-22 00:26:08 +00002530 // Do not sibcall optimize vararg calls unless the call site is not passing
2531 // any arguments.
Evan Cheng3c262ee2010-03-26 02:13:13 +00002532 if (isVarArg && !Outs.empty())
Evan Cheng843bd692010-01-31 06:44:49 +00002533 return false;
2534
Evan Chenga375d472010-03-15 18:54:48 +00002535 // Also avoid sibcall optimization if either caller or callee uses struct
2536 // return semantics.
2537 if (isCalleeStructRet || isCallerStructRet)
2538 return false;
2539
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002540 // If the call result is in ST0 / ST1, it needs to be popped off the x87 stack.
2541 // Therefore if it's not used by the call it is not safe to optimize this into
2542 // a sibcall.
2543 bool Unused = false;
2544 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2545 if (!Ins[i].Used) {
2546 Unused = true;
2547 break;
2548 }
2549 }
2550 if (Unused) {
2551 SmallVector<CCValAssign, 16> RVLocs;
2552 CCState CCInfo(CalleeCC, false, getTargetMachine(),
2553 RVLocs, *DAG.getContext());
2554 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002555 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002556 CCValAssign &VA = RVLocs[i];
2557 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2558 return false;
2559 }
2560 }
2561
Evan Cheng13617962010-04-30 01:12:32 +00002562 // If the calling conventions do not match, then we'd better make sure the
2563 // results are returned in the same way as what the caller expects.
2564 if (!CCMatch) {
2565 SmallVector<CCValAssign, 16> RVLocs1;
2566 CCState CCInfo1(CalleeCC, false, getTargetMachine(),
2567 RVLocs1, *DAG.getContext());
2568 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2569
2570 SmallVector<CCValAssign, 16> RVLocs2;
2571 CCState CCInfo2(CallerCC, false, getTargetMachine(),
2572 RVLocs2, *DAG.getContext());
2573 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2574
2575 if (RVLocs1.size() != RVLocs2.size())
2576 return false;
2577 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2578 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2579 return false;
2580 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2581 return false;
2582 if (RVLocs1[i].isRegLoc()) {
2583 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2584 return false;
2585 } else {
2586 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2587 return false;
2588 }
2589 }
2590 }
2591
Evan Chenga6bff982010-01-30 01:22:00 +00002592 // If the callee takes no arguments then go on to check the results of the
2593 // call.
2594 if (!Outs.empty()) {
2595 // Check if stack adjustment is needed. For now, do not do this if any
2596 // argument is passed on the stack.
2597 SmallVector<CCValAssign, 16> ArgLocs;
2598 CCState CCInfo(CalleeCC, isVarArg, getTargetMachine(),
2599 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002600
2601 // Allocate shadow area for Win64
2602 if (Subtarget->isTargetWin64()) {
2603 CCInfo.AllocateStack(32, 8);
2604 }
2605
Duncan Sands45907662010-10-31 13:21:44 +00002606 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Evan Chengb2c92902010-02-02 02:22:50 +00002607 if (CCInfo.getNextStackOffset()) {
2608 MachineFunction &MF = DAG.getMachineFunction();
2609 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2610 return false;
Evan Chengb2c92902010-02-02 02:22:50 +00002611
2612 // Check if the arguments are already laid out in the right way as
2613 // the caller's fixed stack objects.
2614 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002615 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2616 const X86InstrInfo *TII =
2617 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002618 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2619 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002620 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002621 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002622 if (VA.getLocInfo() == CCValAssign::Indirect)
2623 return false;
2624 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002625 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2626 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002627 return false;
2628 }
2629 }
2630 }
Evan Cheng9c044672010-05-29 01:35:22 +00002631
2632 // If the tailcall address may be in a register, then make sure it's
2633 // possible to register allocate for it. In 32-bit, the call address can
2634 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002635 // callee-saved registers are restored. These happen to be the same
2636 // registers used to pass 'inreg' arguments so watch out for those.
2637 if (!Subtarget->is64Bit() &&
2638 !isa<GlobalAddressSDNode>(Callee) &&
Evan Cheng9c044672010-05-29 01:35:22 +00002639 !isa<ExternalSymbolSDNode>(Callee)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002640 unsigned NumInRegs = 0;
2641 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2642 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002643 if (!VA.isRegLoc())
2644 continue;
2645 unsigned Reg = VA.getLocReg();
2646 switch (Reg) {
2647 default: break;
2648 case X86::EAX: case X86::EDX: case X86::ECX:
2649 if (++NumInRegs == 3)
Evan Cheng9c044672010-05-29 01:35:22 +00002650 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002651 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002652 }
2653 }
2654 }
Evan Chenga6bff982010-01-30 01:22:00 +00002655 }
Evan Chengb1712452010-01-27 06:25:16 +00002656
Dale Johannesend155d7e2010-10-25 22:17:05 +00002657 // An stdcall caller is expected to clean up its arguments; the callee
Dale Johannesen0e034562010-11-12 00:43:18 +00002658 // isn't going to do that.
Dale Johannesend155d7e2010-10-25 22:17:05 +00002659 if (!CCMatch && CallerCC==CallingConv::X86_StdCall)
2660 return false;
2661
Evan Cheng86809cc2010-02-03 03:28:02 +00002662 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002663}
2664
Dan Gohman3df24e62008-09-03 23:12:08 +00002665FastISel *
Dan Gohmana4160c32010-07-07 16:29:44 +00002666X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
2667 return X86::createFastISel(funcInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00002668}
2669
2670
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002671//===----------------------------------------------------------------------===//
2672// Other Lowering Hooks
2673//===----------------------------------------------------------------------===//
2674
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00002675static bool MayFoldLoad(SDValue Op) {
2676 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
2677}
2678
2679static bool MayFoldIntoStore(SDValue Op) {
2680 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
2681}
2682
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002683static bool isTargetShuffle(unsigned Opcode) {
2684 switch(Opcode) {
2685 default: return false;
2686 case X86ISD::PSHUFD:
2687 case X86ISD::PSHUFHW:
2688 case X86ISD::PSHUFLW:
2689 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002690 case X86ISD::PALIGN:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002691 case X86ISD::SHUFPS:
2692 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002693 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002694 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002695 case X86ISD::MOVLPS:
2696 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002697 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002698 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002699 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002700 case X86ISD::MOVSS:
2701 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002702 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002703 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +00002704 case X86ISD::VUNPCKLPS:
2705 case X86ISD::VUNPCKLPD:
2706 case X86ISD::VUNPCKLPSY:
2707 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002708 case X86ISD::PUNPCKLWD:
2709 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002710 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002711 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002712 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002713 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002714 case X86ISD::PUNPCKHWD:
2715 case X86ISD::PUNPCKHBW:
2716 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002717 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002718 return true;
2719 }
2720 return false;
2721}
2722
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002723static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002724 SDValue V1, SelectionDAG &DAG) {
2725 switch(Opc) {
2726 default: llvm_unreachable("Unknown x86 shuffle node");
2727 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002728 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002729 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002730 return DAG.getNode(Opc, dl, VT, V1);
2731 }
2732
2733 return SDValue();
2734}
2735
2736static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00002737 SDValue V1, unsigned TargetMask, SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002738 switch(Opc) {
2739 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002740 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002741 case X86ISD::PSHUFHW:
2742 case X86ISD::PSHUFLW:
2743 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
2744 }
2745
2746 return SDValue();
2747}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002748
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002749static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2750 SDValue V1, SDValue V2, unsigned TargetMask, SelectionDAG &DAG) {
2751 switch(Opc) {
2752 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002753 case X86ISD::PALIGN:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002754 case X86ISD::SHUFPD:
2755 case X86ISD::SHUFPS:
2756 return DAG.getNode(Opc, dl, VT, V1, V2,
2757 DAG.getConstant(TargetMask, MVT::i8));
2758 }
2759 return SDValue();
2760}
2761
2762static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2763 SDValue V1, SDValue V2, SelectionDAG &DAG) {
2764 switch(Opc) {
2765 default: llvm_unreachable("Unknown x86 shuffle node");
2766 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00002767 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002768 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002769 case X86ISD::MOVLPS:
2770 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002771 case X86ISD::MOVSS:
2772 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002773 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002774 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +00002775 case X86ISD::VUNPCKLPS:
2776 case X86ISD::VUNPCKLPD:
2777 case X86ISD::VUNPCKLPSY:
2778 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002779 case X86ISD::PUNPCKLWD:
2780 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002781 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002782 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002783 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002784 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002785 case X86ISD::PUNPCKHWD:
2786 case X86ISD::PUNPCKHBW:
2787 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002788 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002789 return DAG.getNode(Opc, dl, VT, V1, V2);
2790 }
2791 return SDValue();
2792}
2793
Dan Gohmand858e902010-04-17 15:26:15 +00002794SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002795 MachineFunction &MF = DAG.getMachineFunction();
2796 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2797 int ReturnAddrIndex = FuncInfo->getRAIndex();
2798
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002799 if (ReturnAddrIndex == 0) {
2800 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002801 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002802 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00002803 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002804 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002805 }
2806
Evan Cheng25ab6902006-09-08 06:48:29 +00002807 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002808}
2809
2810
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002811bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2812 bool hasSymbolicDisplacement) {
2813 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00002814 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002815 return false;
2816
2817 // If we don't have a symbolic displacement - we don't have any extra
2818 // restrictions.
2819 if (!hasSymbolicDisplacement)
2820 return true;
2821
2822 // FIXME: Some tweaks might be needed for medium code model.
2823 if (M != CodeModel::Small && M != CodeModel::Kernel)
2824 return false;
2825
2826 // For small code model we assume that latest object is 16MB before end of 31
2827 // bits boundary. We may also accept pretty large negative constants knowing
2828 // that all objects are in the positive half of address space.
2829 if (M == CodeModel::Small && Offset < 16*1024*1024)
2830 return true;
2831
2832 // For kernel code model we know that all object resist in the negative half
2833 // of 32bits address space. We may not accept negative offsets, since they may
2834 // be just off and we may accept pretty large positive ones.
2835 if (M == CodeModel::Kernel && Offset > 0)
2836 return true;
2837
2838 return false;
2839}
2840
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002841/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2842/// specific condition code, returning the condition code and the LHS/RHS of the
2843/// comparison to make.
2844static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2845 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00002846 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002847 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2848 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2849 // X > -1 -> X == 0, jump !sign.
2850 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002851 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002852 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2853 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002854 return X86::COND_S;
Andrew Trickf6c39412011-03-23 23:11:02 +00002855 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00002856 // X < 1 -> X <= 0
2857 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002858 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002859 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002860 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002861
Evan Chengd9558e02006-01-06 00:43:03 +00002862 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002863 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002864 case ISD::SETEQ: return X86::COND_E;
2865 case ISD::SETGT: return X86::COND_G;
2866 case ISD::SETGE: return X86::COND_GE;
2867 case ISD::SETLT: return X86::COND_L;
2868 case ISD::SETLE: return X86::COND_LE;
2869 case ISD::SETNE: return X86::COND_NE;
2870 case ISD::SETULT: return X86::COND_B;
2871 case ISD::SETUGT: return X86::COND_A;
2872 case ISD::SETULE: return X86::COND_BE;
2873 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00002874 }
Chris Lattner4c78e022008-12-23 23:42:27 +00002875 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002876
Chris Lattner4c78e022008-12-23 23:42:27 +00002877 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00002878
Chris Lattner4c78e022008-12-23 23:42:27 +00002879 // If LHS is a foldable load, but RHS is not, flip the condition.
Rafael Espindolaf297c932011-02-03 03:58:05 +00002880 if (ISD::isNON_EXTLoad(LHS.getNode()) &&
2881 !ISD::isNON_EXTLoad(RHS.getNode())) {
Chris Lattner4c78e022008-12-23 23:42:27 +00002882 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
2883 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00002884 }
2885
Chris Lattner4c78e022008-12-23 23:42:27 +00002886 switch (SetCCOpcode) {
2887 default: break;
2888 case ISD::SETOLT:
2889 case ISD::SETOLE:
2890 case ISD::SETUGT:
2891 case ISD::SETUGE:
2892 std::swap(LHS, RHS);
2893 break;
2894 }
2895
2896 // On a floating point condition, the flags are set as follows:
2897 // ZF PF CF op
2898 // 0 | 0 | 0 | X > Y
2899 // 0 | 0 | 1 | X < Y
2900 // 1 | 0 | 0 | X == Y
2901 // 1 | 1 | 1 | unordered
2902 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002903 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00002904 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002905 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00002906 case ISD::SETOLT: // flipped
2907 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002908 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00002909 case ISD::SETOLE: // flipped
2910 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002911 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002912 case ISD::SETUGT: // flipped
2913 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002914 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00002915 case ISD::SETUGE: // flipped
2916 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002917 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002918 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002919 case ISD::SETNE: return X86::COND_NE;
2920 case ISD::SETUO: return X86::COND_P;
2921 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00002922 case ISD::SETOEQ:
2923 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00002924 }
Evan Chengd9558e02006-01-06 00:43:03 +00002925}
2926
Evan Cheng4a460802006-01-11 00:33:36 +00002927/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2928/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00002929/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00002930static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00002931 switch (X86CC) {
2932 default:
2933 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00002934 case X86::COND_B:
2935 case X86::COND_BE:
2936 case X86::COND_E:
2937 case X86::COND_P:
2938 case X86::COND_A:
2939 case X86::COND_AE:
2940 case X86::COND_NE:
2941 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00002942 return true;
2943 }
2944}
2945
Evan Chengeb2f9692009-10-27 19:56:55 +00002946/// isFPImmLegal - Returns true if the target can instruction select the
2947/// specified FP immediate natively. If false, the legalizer will
2948/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00002949bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00002950 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
2951 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
2952 return true;
2953 }
2954 return false;
2955}
2956
Nate Begeman9008ca62009-04-27 18:41:29 +00002957/// isUndefOrInRange - Return true if Val is undef or if its value falls within
2958/// the specified range (L, H].
2959static bool isUndefOrInRange(int Val, int Low, int Hi) {
2960 return (Val < 0) || (Val >= Low && Val < Hi);
2961}
2962
2963/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
2964/// specified value.
2965static bool isUndefOrEqual(int Val, int CmpVal) {
2966 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002967 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00002968 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00002969}
2970
Nate Begeman9008ca62009-04-27 18:41:29 +00002971/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
2972/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
2973/// the second operand.
Owen Andersone50ed302009-08-10 22:56:29 +00002974static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00002975 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00002976 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002977 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00002978 return (Mask[0] < 2 && Mask[1] < 2);
2979 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002980}
2981
Nate Begeman9008ca62009-04-27 18:41:29 +00002982bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002983 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002984 N->getMask(M);
2985 return ::isPSHUFDMask(M, N->getValueType(0));
2986}
Evan Cheng0188ecb2006-03-22 18:59:22 +00002987
Nate Begeman9008ca62009-04-27 18:41:29 +00002988/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
2989/// is suitable for input to PSHUFHW.
Owen Andersone50ed302009-08-10 22:56:29 +00002990static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002991 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00002992 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002993
Nate Begeman9008ca62009-04-27 18:41:29 +00002994 // Lower quadword copied in order or undef.
2995 for (int i = 0; i != 4; ++i)
2996 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00002997 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002998
Evan Cheng506d3df2006-03-29 23:07:14 +00002999 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00003000 for (int i = 4; i != 8; ++i)
3001 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00003002 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003003
Evan Cheng506d3df2006-03-29 23:07:14 +00003004 return true;
3005}
3006
Nate Begeman9008ca62009-04-27 18:41:29 +00003007bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003008 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003009 N->getMask(M);
3010 return ::isPSHUFHWMask(M, N->getValueType(0));
3011}
Evan Cheng506d3df2006-03-29 23:07:14 +00003012
Nate Begeman9008ca62009-04-27 18:41:29 +00003013/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
3014/// is suitable for input to PSHUFLW.
Owen Andersone50ed302009-08-10 22:56:29 +00003015static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003016 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00003017 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003018
Rafael Espindola15684b22009-04-24 12:40:33 +00003019 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00003020 for (int i = 4; i != 8; ++i)
3021 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00003022 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003023
Rafael Espindola15684b22009-04-24 12:40:33 +00003024 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00003025 for (int i = 0; i != 4; ++i)
3026 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00003027 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003028
Rafael Espindola15684b22009-04-24 12:40:33 +00003029 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003030}
3031
Nate Begeman9008ca62009-04-27 18:41:29 +00003032bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003033 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003034 N->getMask(M);
3035 return ::isPSHUFLWMask(M, N->getValueType(0));
3036}
3037
Nate Begemana09008b2009-10-19 02:17:23 +00003038/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
3039/// is suitable for input to PALIGNR.
3040static bool isPALIGNRMask(const SmallVectorImpl<int> &Mask, EVT VT,
3041 bool hasSSSE3) {
3042 int i, e = VT.getVectorNumElements();
Michael J. Spencerec38de22010-10-10 22:04:20 +00003043
Nate Begemana09008b2009-10-19 02:17:23 +00003044 // Do not handle v2i64 / v2f64 shuffles with palignr.
3045 if (e < 4 || !hasSSSE3)
3046 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003047
Nate Begemana09008b2009-10-19 02:17:23 +00003048 for (i = 0; i != e; ++i)
3049 if (Mask[i] >= 0)
3050 break;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003051
Nate Begemana09008b2009-10-19 02:17:23 +00003052 // All undef, not a palignr.
3053 if (i == e)
3054 return false;
3055
3056 // Determine if it's ok to perform a palignr with only the LHS, since we
3057 // don't have access to the actual shuffle elements to see if RHS is undef.
3058 bool Unary = Mask[i] < (int)e;
3059 bool NeedsUnary = false;
3060
3061 int s = Mask[i] - i;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003062
Nate Begemana09008b2009-10-19 02:17:23 +00003063 // Check the rest of the elements to see if they are consecutive.
3064 for (++i; i != e; ++i) {
3065 int m = Mask[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00003066 if (m < 0)
Nate Begemana09008b2009-10-19 02:17:23 +00003067 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003068
Nate Begemana09008b2009-10-19 02:17:23 +00003069 Unary = Unary && (m < (int)e);
3070 NeedsUnary = NeedsUnary || (m < s);
3071
3072 if (NeedsUnary && !Unary)
3073 return false;
3074 if (Unary && m != ((s+i) & (e-1)))
3075 return false;
3076 if (!Unary && m != (s+i))
3077 return false;
3078 }
3079 return true;
3080}
3081
3082bool X86::isPALIGNRMask(ShuffleVectorSDNode *N) {
3083 SmallVector<int, 8> M;
3084 N->getMask(M);
3085 return ::isPALIGNRMask(M, N->getValueType(0), true);
3086}
3087
Evan Cheng14aed5e2006-03-24 01:18:28 +00003088/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
3089/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Owen Andersone50ed302009-08-10 22:56:29 +00003090static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003091 int NumElems = VT.getVectorNumElements();
3092 if (NumElems != 2 && NumElems != 4)
3093 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003094
Nate Begeman9008ca62009-04-27 18:41:29 +00003095 int Half = NumElems / 2;
3096 for (int i = 0; i < Half; ++i)
3097 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003098 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003099 for (int i = Half; i < NumElems; ++i)
3100 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00003101 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003102
Evan Cheng14aed5e2006-03-24 01:18:28 +00003103 return true;
3104}
3105
Nate Begeman9008ca62009-04-27 18:41:29 +00003106bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
3107 SmallVector<int, 8> M;
3108 N->getMask(M);
3109 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003110}
3111
Evan Cheng213d2cf2007-05-17 18:45:50 +00003112/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00003113/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
3114/// half elements to come from vector 1 (which would equal the dest.) and
3115/// the upper half to come from vector 2.
Owen Andersone50ed302009-08-10 22:56:29 +00003116static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003117 int NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003118
3119 if (NumElems != 2 && NumElems != 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00003120 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003121
Nate Begeman9008ca62009-04-27 18:41:29 +00003122 int Half = NumElems / 2;
3123 for (int i = 0; i < Half; ++i)
3124 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00003125 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003126 for (int i = Half; i < NumElems; ++i)
3127 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003128 return false;
3129 return true;
3130}
3131
Nate Begeman9008ca62009-04-27 18:41:29 +00003132static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
3133 SmallVector<int, 8> M;
3134 N->getMask(M);
3135 return isCommutedSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003136}
3137
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003138/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
3139/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00003140bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
3141 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003142 return false;
3143
Evan Cheng2064a2b2006-03-28 06:50:32 +00003144 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00003145 return isUndefOrEqual(N->getMaskElt(0), 6) &&
3146 isUndefOrEqual(N->getMaskElt(1), 7) &&
3147 isUndefOrEqual(N->getMaskElt(2), 2) &&
3148 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003149}
3150
Nate Begeman0b10b912009-11-07 23:17:15 +00003151/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3152/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3153/// <2, 3, 2, 3>
3154bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
3155 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Michael J. Spencerec38de22010-10-10 22:04:20 +00003156
Nate Begeman0b10b912009-11-07 23:17:15 +00003157 if (NumElems != 4)
3158 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003159
Nate Begeman0b10b912009-11-07 23:17:15 +00003160 return isUndefOrEqual(N->getMaskElt(0), 2) &&
3161 isUndefOrEqual(N->getMaskElt(1), 3) &&
3162 isUndefOrEqual(N->getMaskElt(2), 2) &&
3163 isUndefOrEqual(N->getMaskElt(3), 3);
3164}
3165
Evan Cheng5ced1d82006-04-06 23:23:56 +00003166/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3167/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00003168bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
3169 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003170
Evan Cheng5ced1d82006-04-06 23:23:56 +00003171 if (NumElems != 2 && NumElems != 4)
3172 return false;
3173
Evan Chengc5cdff22006-04-07 21:53:05 +00003174 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003175 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003176 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003177
Evan Chengc5cdff22006-04-07 21:53:05 +00003178 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003179 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003180 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003181
3182 return true;
3183}
3184
Nate Begeman0b10b912009-11-07 23:17:15 +00003185/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3186/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
3187bool X86::isMOVLHPSMask(ShuffleVectorSDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003188 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003189
David Greenea20244d2011-03-02 17:23:43 +00003190 if ((NumElems != 2 && NumElems != 4)
3191 || N->getValueType(0).getSizeInBits() > 128)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003192 return false;
3193
Evan Chengc5cdff22006-04-07 21:53:05 +00003194 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003195 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003196 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003197
Nate Begeman9008ca62009-04-27 18:41:29 +00003198 for (unsigned i = 0; i < NumElems/2; ++i)
3199 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003200 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003201
3202 return true;
3203}
3204
Evan Cheng0038e592006-03-28 00:39:58 +00003205/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3206/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Owen Andersone50ed302009-08-10 22:56:29 +00003207static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003208 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003209 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003210 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng0038e592006-03-28 00:39:58 +00003211 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003212
David Greenea20244d2011-03-02 17:23:43 +00003213 // Handle vector lengths > 128 bits. Define a "section" as a set of
3214 // 128 bits. AVX defines UNPCK* to operate independently on 128-bit
3215 // sections.
3216 unsigned NumSections = VT.getSizeInBits() / 128;
3217 if (NumSections == 0 ) NumSections = 1; // Handle MMX
3218 unsigned NumSectionElts = NumElts / NumSections;
3219
3220 unsigned Start = 0;
3221 unsigned End = NumSectionElts;
3222 for (unsigned s = 0; s < NumSections; ++s) {
3223 for (unsigned i = Start, j = s * NumSectionElts;
3224 i != End;
3225 i += 2, ++j) {
3226 int BitI = Mask[i];
3227 int BitI1 = Mask[i+1];
3228 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003229 return false;
David Greenea20244d2011-03-02 17:23:43 +00003230 if (V2IsSplat) {
3231 if (!isUndefOrEqual(BitI1, NumElts))
3232 return false;
3233 } else {
3234 if (!isUndefOrEqual(BitI1, j + NumElts))
3235 return false;
3236 }
Evan Cheng39623da2006-04-20 08:58:49 +00003237 }
David Greenea20244d2011-03-02 17:23:43 +00003238 // Process the next 128 bits.
3239 Start += NumSectionElts;
3240 End += NumSectionElts;
Evan Cheng0038e592006-03-28 00:39:58 +00003241 }
David Greenea20244d2011-03-02 17:23:43 +00003242
Evan Cheng0038e592006-03-28 00:39:58 +00003243 return true;
3244}
3245
Nate Begeman9008ca62009-04-27 18:41:29 +00003246bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3247 SmallVector<int, 8> M;
3248 N->getMask(M);
3249 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003250}
3251
Evan Cheng4fcb9222006-03-28 02:43:26 +00003252/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3253/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Eric Christopherfd179292009-08-27 18:07:15 +00003254static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003255 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003256 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003257 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng4fcb9222006-03-28 02:43:26 +00003258 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003259
Nate Begeman9008ca62009-04-27 18:41:29 +00003260 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
3261 int BitI = Mask[i];
3262 int BitI1 = Mask[i+1];
Chris Lattner5a88b832007-02-25 07:10:00 +00003263 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengc5cdff22006-04-07 21:53:05 +00003264 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00003265 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00003266 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003267 return false;
3268 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00003269 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003270 return false;
3271 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003272 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003273 return true;
3274}
3275
Nate Begeman9008ca62009-04-27 18:41:29 +00003276bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3277 SmallVector<int, 8> M;
3278 N->getMask(M);
3279 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003280}
3281
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003282/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3283/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3284/// <0, 0, 1, 1>
Owen Andersone50ed302009-08-10 22:56:29 +00003285static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003286 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003287 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003288 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003289
David Greenea20244d2011-03-02 17:23:43 +00003290 // Handle vector lengths > 128 bits. Define a "section" as a set of
3291 // 128 bits. AVX defines UNPCK* to operate independently on 128-bit
3292 // sections.
3293 unsigned NumSections = VT.getSizeInBits() / 128;
3294 if (NumSections == 0 ) NumSections = 1; // Handle MMX
3295 unsigned NumSectionElts = NumElems / NumSections;
3296
3297 for (unsigned s = 0; s < NumSections; ++s) {
3298 for (unsigned i = s * NumSectionElts, j = s * NumSectionElts;
3299 i != NumSectionElts * (s + 1);
3300 i += 2, ++j) {
3301 int BitI = Mask[i];
3302 int BitI1 = Mask[i+1];
3303
3304 if (!isUndefOrEqual(BitI, j))
3305 return false;
3306 if (!isUndefOrEqual(BitI1, j))
3307 return false;
3308 }
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003309 }
David Greenea20244d2011-03-02 17:23:43 +00003310
Rafael Espindola15684b22009-04-24 12:40:33 +00003311 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003312}
3313
Nate Begeman9008ca62009-04-27 18:41:29 +00003314bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
3315 SmallVector<int, 8> M;
3316 N->getMask(M);
3317 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
3318}
3319
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003320/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3321/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3322/// <2, 2, 3, 3>
Owen Andersone50ed302009-08-10 22:56:29 +00003323static bool isUNPCKH_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)
3326 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003327
Nate Begeman9008ca62009-04-27 18:41:29 +00003328 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
3329 int BitI = Mask[i];
3330 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003331 if (!isUndefOrEqual(BitI, j))
3332 return false;
3333 if (!isUndefOrEqual(BitI1, j))
3334 return false;
3335 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003336 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003337}
3338
Nate Begeman9008ca62009-04-27 18:41:29 +00003339bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
3340 SmallVector<int, 8> M;
3341 N->getMask(M);
3342 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
3343}
3344
Evan Cheng017dcc62006-04-21 01:05:10 +00003345/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3346/// specifies a shuffle of elements that is suitable for input to MOVSS,
3347/// MOVSD, and MOVD, i.e. setting the lowest element.
Owen Andersone50ed302009-08-10 22:56:29 +00003348static bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003349 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003350 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003351
3352 int NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003353
Nate Begeman9008ca62009-04-27 18:41:29 +00003354 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003355 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003356
Nate Begeman9008ca62009-04-27 18:41:29 +00003357 for (int i = 1; i < NumElts; ++i)
3358 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003359 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003360
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003361 return true;
3362}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003363
Nate Begeman9008ca62009-04-27 18:41:29 +00003364bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
3365 SmallVector<int, 8> M;
3366 N->getMask(M);
3367 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003368}
3369
Evan Cheng017dcc62006-04-21 01:05:10 +00003370/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
3371/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003372/// element of vector 2 and the other elements to come from vector 1 in order.
Owen Andersone50ed302009-08-10 22:56:29 +00003373static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003374 bool V2IsSplat = false, bool V2IsUndef = false) {
3375 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003376 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003377 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003378
Nate Begeman9008ca62009-04-27 18:41:29 +00003379 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003380 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003381
Nate Begeman9008ca62009-04-27 18:41:29 +00003382 for (int i = 1; i < NumOps; ++i)
3383 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3384 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3385 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003386 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003387
Evan Cheng39623da2006-04-20 08:58:49 +00003388 return true;
3389}
3390
Nate Begeman9008ca62009-04-27 18:41:29 +00003391static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00003392 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003393 SmallVector<int, 8> M;
3394 N->getMask(M);
3395 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00003396}
3397
Evan Chengd9539472006-04-14 21:59:03 +00003398/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3399/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003400bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N) {
3401 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003402 return false;
3403
3404 // Expect 1, 1, 3, 3
Rafael Espindola15684b22009-04-24 12:40:33 +00003405 for (unsigned i = 0; i < 2; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003406 int Elt = N->getMaskElt(i);
3407 if (Elt >= 0 && Elt != 1)
3408 return false;
Rafael Espindola15684b22009-04-24 12:40:33 +00003409 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003410
3411 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003412 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003413 int Elt = N->getMaskElt(i);
3414 if (Elt >= 0 && Elt != 3)
3415 return false;
3416 if (Elt == 3)
3417 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003418 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003419 // Don't use movshdup if it can be done with a shufps.
Nate Begeman9008ca62009-04-27 18:41:29 +00003420 // FIXME: verify that matching u, u, 3, 3 is what we want.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003421 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003422}
3423
3424/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3425/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003426bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N) {
3427 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003428 return false;
3429
3430 // Expect 0, 0, 2, 2
Nate Begeman9008ca62009-04-27 18:41:29 +00003431 for (unsigned i = 0; i < 2; ++i)
3432 if (N->getMaskElt(i) > 0)
3433 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003434
3435 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003436 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003437 int Elt = N->getMaskElt(i);
3438 if (Elt >= 0 && Elt != 2)
3439 return false;
3440 if (Elt == 2)
3441 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003442 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003443 // Don't use movsldup if it can be done with a shufps.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003444 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003445}
3446
Evan Cheng0b457f02008-09-25 20:50:48 +00003447/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3448/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003449bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
3450 int e = N->getValueType(0).getVectorNumElements() / 2;
Eric Christopherfd179292009-08-27 18:07:15 +00003451
Nate Begeman9008ca62009-04-27 18:41:29 +00003452 for (int i = 0; i < e; ++i)
3453 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003454 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003455 for (int i = 0; i < e; ++i)
3456 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003457 return false;
3458 return true;
3459}
3460
David Greenec38a03e2011-02-03 15:50:00 +00003461/// isVEXTRACTF128Index - Return true if the specified
3462/// EXTRACT_SUBVECTOR operand specifies a vector extract that is
3463/// suitable for input to VEXTRACTF128.
3464bool X86::isVEXTRACTF128Index(SDNode *N) {
3465 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3466 return false;
3467
3468 // The index should be aligned on a 128-bit boundary.
3469 uint64_t Index =
3470 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
3471
3472 unsigned VL = N->getValueType(0).getVectorNumElements();
3473 unsigned VBits = N->getValueType(0).getSizeInBits();
3474 unsigned ElSize = VBits / VL;
3475 bool Result = (Index * ElSize) % 128 == 0;
3476
3477 return Result;
3478}
3479
David Greeneccacdc12011-02-04 16:08:29 +00003480/// isVINSERTF128Index - Return true if the specified INSERT_SUBVECTOR
3481/// operand specifies a subvector insert that is suitable for input to
3482/// VINSERTF128.
3483bool X86::isVINSERTF128Index(SDNode *N) {
3484 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
3485 return false;
3486
3487 // The index should be aligned on a 128-bit boundary.
3488 uint64_t Index =
3489 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
3490
3491 unsigned VL = N->getValueType(0).getVectorNumElements();
3492 unsigned VBits = N->getValueType(0).getSizeInBits();
3493 unsigned ElSize = VBits / VL;
3494 bool Result = (Index * ElSize) % 128 == 0;
3495
3496 return Result;
3497}
3498
Evan Cheng63d33002006-03-22 08:01:21 +00003499/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003500/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Evan Cheng63d33002006-03-22 08:01:21 +00003501unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003502 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3503 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
3504
Evan Chengb9df0ca2006-03-22 02:53:00 +00003505 unsigned Shift = (NumOperands == 4) ? 2 : 1;
3506 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00003507 for (int i = 0; i < NumOperands; ++i) {
3508 int Val = SVOp->getMaskElt(NumOperands-i-1);
3509 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00003510 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00003511 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00003512 if (i != NumOperands - 1)
3513 Mask <<= Shift;
3514 }
Evan Cheng63d33002006-03-22 08:01:21 +00003515 return Mask;
3516}
3517
Evan Cheng506d3df2006-03-29 23:07:14 +00003518/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003519/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003520unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003521 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003522 unsigned Mask = 0;
3523 // 8 nodes, but we only care about the last 4.
3524 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003525 int Val = SVOp->getMaskElt(i);
3526 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00003527 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00003528 if (i != 4)
3529 Mask <<= 2;
3530 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003531 return Mask;
3532}
3533
3534/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003535/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003536unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003537 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003538 unsigned Mask = 0;
3539 // 8 nodes, but we only care about the first 4.
3540 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003541 int Val = SVOp->getMaskElt(i);
3542 if (Val >= 0)
3543 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00003544 if (i != 0)
3545 Mask <<= 2;
3546 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003547 return Mask;
3548}
3549
Nate Begemana09008b2009-10-19 02:17:23 +00003550/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
3551/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
3552unsigned X86::getShufflePALIGNRImmediate(SDNode *N) {
3553 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3554 EVT VVT = N->getValueType(0);
3555 unsigned EltSize = VVT.getVectorElementType().getSizeInBits() >> 3;
3556 int Val = 0;
3557
3558 unsigned i, e;
3559 for (i = 0, e = VVT.getVectorNumElements(); i != e; ++i) {
3560 Val = SVOp->getMaskElt(i);
3561 if (Val >= 0)
3562 break;
3563 }
3564 return (Val - i) * EltSize;
3565}
3566
David Greenec38a03e2011-02-03 15:50:00 +00003567/// getExtractVEXTRACTF128Immediate - Return the appropriate immediate
3568/// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF128
3569/// instructions.
3570unsigned X86::getExtractVEXTRACTF128Immediate(SDNode *N) {
3571 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3572 llvm_unreachable("Illegal extract subvector for VEXTRACTF128");
3573
3574 uint64_t Index =
3575 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
3576
3577 EVT VecVT = N->getOperand(0).getValueType();
3578 EVT ElVT = VecVT.getVectorElementType();
3579
3580 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
3581
3582 return Index / NumElemsPerChunk;
3583}
3584
David Greeneccacdc12011-02-04 16:08:29 +00003585/// getInsertVINSERTF128Immediate - Return the appropriate immediate
3586/// to insert at the specified INSERT_SUBVECTOR index with VINSERTF128
3587/// instructions.
3588unsigned X86::getInsertVINSERTF128Immediate(SDNode *N) {
3589 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
3590 llvm_unreachable("Illegal insert subvector for VINSERTF128");
3591
3592 uint64_t Index =
NAKAMURA Takumi27635382011-02-05 15:10:54 +00003593 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
David Greeneccacdc12011-02-04 16:08:29 +00003594
3595 EVT VecVT = N->getValueType(0);
3596 EVT ElVT = VecVT.getVectorElementType();
3597
3598 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
3599
3600 return Index / NumElemsPerChunk;
3601}
3602
Evan Cheng37b73872009-07-30 08:33:02 +00003603/// isZeroNode - Returns true if Elt is a constant zero or a floating point
3604/// constant +0.0.
3605bool X86::isZeroNode(SDValue Elt) {
3606 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00003607 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00003608 (isa<ConstantFPSDNode>(Elt) &&
3609 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
3610}
3611
Nate Begeman9008ca62009-04-27 18:41:29 +00003612/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
3613/// their permute mask.
3614static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
3615 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003616 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003617 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00003618 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00003619
Nate Begeman5a5ca152009-04-29 05:20:52 +00003620 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003621 int idx = SVOp->getMaskElt(i);
3622 if (idx < 0)
3623 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003624 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003625 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003626 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003627 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003628 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003629 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
3630 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003631}
3632
Evan Cheng779ccea2007-12-07 21:30:01 +00003633/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3634/// the two vector operands have swapped position.
Owen Andersone50ed302009-08-10 22:56:29 +00003635static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00003636 unsigned NumElems = VT.getVectorNumElements();
3637 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003638 int idx = Mask[i];
3639 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003640 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003641 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003642 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003643 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003644 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003645 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003646}
3647
Evan Cheng533a0aa2006-04-19 20:35:22 +00003648/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
3649/// match movhlps. The lower half elements should come from upper half of
3650/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003651/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00003652static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
3653 if (Op->getValueType(0).getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00003654 return false;
3655 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003656 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003657 return false;
3658 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003659 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003660 return false;
3661 return true;
3662}
3663
Evan Cheng5ced1d82006-04-06 23:23:56 +00003664/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00003665/// is promoted to a vector. It also returns the LoadSDNode by reference if
3666/// required.
3667static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00003668 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
3669 return false;
3670 N = N->getOperand(0).getNode();
3671 if (!ISD::isNON_EXTLoad(N))
3672 return false;
3673 if (LD)
3674 *LD = cast<LoadSDNode>(N);
3675 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003676}
3677
Evan Cheng533a0aa2006-04-19 20:35:22 +00003678/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
3679/// match movlp{s|d}. The lower half elements should come from lower half of
3680/// V1 (and in order), and the upper half elements should come from the upper
3681/// half of V2 (and in order). And since V1 will become the source of the
3682/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003683static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
3684 ShuffleVectorSDNode *Op) {
Evan Cheng466685d2006-10-09 20:57:25 +00003685 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003686 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00003687 // Is V2 is a vector load, don't do this transformation. We will try to use
3688 // load folding shufps op.
3689 if (ISD::isNON_EXTLoad(V2))
3690 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003691
Nate Begeman5a5ca152009-04-29 05:20:52 +00003692 unsigned NumElems = Op->getValueType(0).getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003693
Evan Cheng533a0aa2006-04-19 20:35:22 +00003694 if (NumElems != 2 && NumElems != 4)
3695 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003696 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003697 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003698 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003699 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003700 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003701 return false;
3702 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003703}
3704
Evan Cheng39623da2006-04-20 08:58:49 +00003705/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
3706/// all the same.
3707static bool isSplatVector(SDNode *N) {
3708 if (N->getOpcode() != ISD::BUILD_VECTOR)
3709 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003710
Dan Gohman475871a2008-07-27 21:46:04 +00003711 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00003712 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
3713 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003714 return false;
3715 return true;
3716}
3717
Evan Cheng213d2cf2007-05-17 18:45:50 +00003718/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00003719/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00003720/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00003721static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00003722 SDValue V1 = N->getOperand(0);
3723 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003724 unsigned NumElems = N->getValueType(0).getVectorNumElements();
3725 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003726 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003727 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003728 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00003729 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
3730 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003731 if (Opc != ISD::BUILD_VECTOR ||
3732 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00003733 return false;
3734 } else if (Idx >= 0) {
3735 unsigned Opc = V1.getOpcode();
3736 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
3737 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003738 if (Opc != ISD::BUILD_VECTOR ||
3739 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00003740 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00003741 }
3742 }
3743 return true;
3744}
3745
3746/// getZeroVector - Returns a vector of specified type with all zero elements.
3747///
Owen Andersone50ed302009-08-10 22:56:29 +00003748static SDValue getZeroVector(EVT VT, bool HasSSE2, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00003749 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003750 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003751
Dale Johannesen0488fb62010-09-30 23:57:10 +00003752 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003753 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00003754 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00003755 if (VT.getSizeInBits() == 128) { // SSE
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003756 if (HasSSE2) { // SSE2
3757 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3758 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
3759 } else { // SSE1
3760 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
3761 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
3762 }
3763 } else if (VT.getSizeInBits() == 256) { // AVX
3764 // 256-bit logic and arithmetic instructions in AVX are
3765 // all floating-point, no support for integer ops. Default
3766 // to emitting fp zeroed vectors then.
Owen Anderson825b72b2009-08-11 20:47:22 +00003767 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003768 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
3769 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
Evan Chengf0df0312008-05-15 08:39:06 +00003770 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003771 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00003772}
3773
Chris Lattner8a594482007-11-25 00:24:49 +00003774/// getOnesVector - Returns a vector of specified type with all bits set.
3775///
Owen Andersone50ed302009-08-10 22:56:29 +00003776static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003777 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003778
Chris Lattner8a594482007-11-25 00:24:49 +00003779 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
3780 // type. This ensures they get CSE'd.
Owen Anderson825b72b2009-08-11 20:47:22 +00003781 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00003782 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00003783 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003784 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00003785}
3786
3787
Evan Cheng39623da2006-04-20 08:58:49 +00003788/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
3789/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00003790static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003791 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003792 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003793
Evan Cheng39623da2006-04-20 08:58:49 +00003794 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003795 SmallVector<int, 8> MaskVec;
3796 SVOp->getMask(MaskVec);
Eric Christopherfd179292009-08-27 18:07:15 +00003797
Nate Begeman5a5ca152009-04-29 05:20:52 +00003798 for (unsigned i = 0; i != NumElems; ++i) {
3799 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003800 MaskVec[i] = NumElems;
3801 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00003802 }
Evan Cheng39623da2006-04-20 08:58:49 +00003803 }
Evan Cheng39623da2006-04-20 08:58:49 +00003804 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00003805 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
3806 SVOp->getOperand(1), &MaskVec[0]);
3807 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00003808}
3809
Evan Cheng017dcc62006-04-21 01:05:10 +00003810/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
3811/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00003812static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003813 SDValue V2) {
3814 unsigned NumElems = VT.getVectorNumElements();
3815 SmallVector<int, 8> Mask;
3816 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00003817 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003818 Mask.push_back(i);
3819 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00003820}
3821
Nate Begeman9008ca62009-04-27 18:41:29 +00003822/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003823static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003824 SDValue V2) {
3825 unsigned NumElems = VT.getVectorNumElements();
3826 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00003827 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003828 Mask.push_back(i);
3829 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00003830 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003831 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00003832}
3833
Nate Begeman9008ca62009-04-27 18:41:29 +00003834/// getUnpackhMask - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003835static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003836 SDValue V2) {
3837 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00003838 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00003839 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00003840 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003841 Mask.push_back(i + Half);
3842 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00003843 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003844 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003845}
3846
Bruno Cardoso Lopesbb0a9482010-08-13 17:50:47 +00003847/// PromoteSplat - Promote a splat of v4i32, v8i16 or v16i8 to v4f32.
3848static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003849 EVT PVT = MVT::v4f32;
Owen Andersone50ed302009-08-10 22:56:29 +00003850 EVT VT = SV->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00003851 DebugLoc dl = SV->getDebugLoc();
3852 SDValue V1 = SV->getOperand(0);
3853 int NumElems = VT.getVectorNumElements();
3854 int EltNo = SV->getSplatIndex();
Rafael Espindola15684b22009-04-24 12:40:33 +00003855
Nate Begeman9008ca62009-04-27 18:41:29 +00003856 // unpack elements to the correct location
3857 while (NumElems > 4) {
3858 if (EltNo < NumElems/2) {
3859 V1 = getUnpackl(DAG, dl, VT, V1, V1);
3860 } else {
3861 V1 = getUnpackh(DAG, dl, VT, V1, V1);
3862 EltNo -= NumElems/2;
3863 }
3864 NumElems >>= 1;
3865 }
Eric Christopherfd179292009-08-27 18:07:15 +00003866
Nate Begeman9008ca62009-04-27 18:41:29 +00003867 // Perform the splat.
3868 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003869 V1 = DAG.getNode(ISD::BITCAST, dl, PVT, V1);
Nate Begeman9008ca62009-04-27 18:41:29 +00003870 V1 = DAG.getVectorShuffle(PVT, dl, V1, DAG.getUNDEF(PVT), &SplatMask[0]);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003871 return DAG.getNode(ISD::BITCAST, dl, VT, V1);
Evan Chengc575ca22006-04-17 20:43:08 +00003872}
3873
Evan Chengba05f722006-04-21 23:03:30 +00003874/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00003875/// vector of zero or undef vector. This produces a shuffle where the low
3876/// element of V2 is swizzled into the zero/undef vector, landing at element
3877/// 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 +00003878static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Chengf0df0312008-05-15 08:39:06 +00003879 bool isZero, bool HasSSE2,
3880 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003881 EVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00003882 SDValue V1 = isZero
Nate Begeman9008ca62009-04-27 18:41:29 +00003883 ? getZeroVector(VT, HasSSE2, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
3884 unsigned NumElems = VT.getVectorNumElements();
3885 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00003886 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003887 // If this is the insertion idx, put the low elt of V2 here.
3888 MaskVec.push_back(i == Idx ? NumElems : i);
3889 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00003890}
3891
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003892/// getShuffleScalarElt - Returns the scalar element that will make up the ith
3893/// element of the result of the vector shuffle.
Benjamin Kramer050db522011-03-26 12:38:19 +00003894static SDValue getShuffleScalarElt(SDNode *N, int Index, SelectionDAG &DAG,
3895 unsigned Depth) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003896 if (Depth == 6)
3897 return SDValue(); // Limit search depth.
3898
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003899 SDValue V = SDValue(N, 0);
3900 EVT VT = V.getValueType();
3901 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003902
3903 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
3904 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
3905 Index = SV->getMaskElt(Index);
3906
3907 if (Index < 0)
3908 return DAG.getUNDEF(VT.getVectorElementType());
3909
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003910 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003911 SDValue NewV = (Index < NumElems) ? SV->getOperand(0) : SV->getOperand(1);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003912 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00003913 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003914
3915 // Recurse into target specific vector shuffles to find scalars.
3916 if (isTargetShuffle(Opcode)) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003917 int NumElems = VT.getVectorNumElements();
3918 SmallVector<unsigned, 16> ShuffleMask;
3919 SDValue ImmN;
3920
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003921 switch(Opcode) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003922 case X86ISD::SHUFPS:
3923 case X86ISD::SHUFPD:
3924 ImmN = N->getOperand(N->getNumOperands()-1);
3925 DecodeSHUFPSMask(NumElems,
3926 cast<ConstantSDNode>(ImmN)->getZExtValue(),
3927 ShuffleMask);
3928 break;
3929 case X86ISD::PUNPCKHBW:
3930 case X86ISD::PUNPCKHWD:
3931 case X86ISD::PUNPCKHDQ:
3932 case X86ISD::PUNPCKHQDQ:
3933 DecodePUNPCKHMask(NumElems, ShuffleMask);
3934 break;
3935 case X86ISD::UNPCKHPS:
3936 case X86ISD::UNPCKHPD:
3937 DecodeUNPCKHPMask(NumElems, ShuffleMask);
3938 break;
3939 case X86ISD::PUNPCKLBW:
3940 case X86ISD::PUNPCKLWD:
3941 case X86ISD::PUNPCKLDQ:
3942 case X86ISD::PUNPCKLQDQ:
David Greenec4db4e52011-02-28 19:06:56 +00003943 DecodePUNPCKLMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003944 break;
3945 case X86ISD::UNPCKLPS:
3946 case X86ISD::UNPCKLPD:
David Greenec4db4e52011-02-28 19:06:56 +00003947 case X86ISD::VUNPCKLPS:
3948 case X86ISD::VUNPCKLPD:
3949 case X86ISD::VUNPCKLPSY:
3950 case X86ISD::VUNPCKLPDY:
3951 DecodeUNPCKLPMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003952 break;
3953 case X86ISD::MOVHLPS:
3954 DecodeMOVHLPSMask(NumElems, ShuffleMask);
3955 break;
3956 case X86ISD::MOVLHPS:
3957 DecodeMOVLHPSMask(NumElems, ShuffleMask);
3958 break;
3959 case X86ISD::PSHUFD:
3960 ImmN = N->getOperand(N->getNumOperands()-1);
3961 DecodePSHUFMask(NumElems,
3962 cast<ConstantSDNode>(ImmN)->getZExtValue(),
3963 ShuffleMask);
3964 break;
3965 case X86ISD::PSHUFHW:
3966 ImmN = N->getOperand(N->getNumOperands()-1);
3967 DecodePSHUFHWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
3968 ShuffleMask);
3969 break;
3970 case X86ISD::PSHUFLW:
3971 ImmN = N->getOperand(N->getNumOperands()-1);
3972 DecodePSHUFLWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
3973 ShuffleMask);
3974 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003975 case X86ISD::MOVSS:
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00003976 case X86ISD::MOVSD: {
3977 // The index 0 always comes from the first element of the second source,
3978 // this is why MOVSS and MOVSD are used in the first place. The other
3979 // elements come from the other positions of the first source vector.
3980 unsigned OpNum = (Index == 0) ? 1 : 0;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003981 return getShuffleScalarElt(V.getOperand(OpNum).getNode(), Index, DAG,
3982 Depth+1);
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00003983 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003984 default:
3985 assert("not implemented for target shuffle node");
3986 return SDValue();
3987 }
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003988
3989 Index = ShuffleMask[Index];
3990 if (Index < 0)
3991 return DAG.getUNDEF(VT.getVectorElementType());
3992
3993 SDValue NewV = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
3994 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG,
3995 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003996 }
3997
3998 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003999 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004000 V = V.getOperand(0);
4001 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004002 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004003
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004004 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004005 return SDValue();
4006 }
4007
4008 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
4009 return (Index == 0) ? V.getOperand(0)
4010 : DAG.getUNDEF(VT.getVectorElementType());
4011
4012 if (V.getOpcode() == ISD::BUILD_VECTOR)
4013 return V.getOperand(Index);
4014
4015 return SDValue();
4016}
4017
4018/// getNumOfConsecutiveZeros - Return the number of elements of a vector
4019/// shuffle operation which come from a consecutively from a zero. The
Chris Lattner7a2bdde2011-04-15 05:18:47 +00004020/// search can start in two different directions, from left or right.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004021static
4022unsigned getNumOfConsecutiveZeros(SDNode *N, int NumElems,
4023 bool ZerosFromLeft, SelectionDAG &DAG) {
4024 int i = 0;
4025
4026 while (i < NumElems) {
4027 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004028 SDValue Elt = getShuffleScalarElt(N, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004029 if (!(Elt.getNode() &&
4030 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
4031 break;
4032 ++i;
4033 }
4034
4035 return i;
4036}
4037
4038/// isShuffleMaskConsecutive - Check if the shuffle mask indicies from MaskI to
4039/// MaskE correspond consecutively to elements from one of the vector operands,
4040/// starting from its index OpIdx. Also tell OpNum which source vector operand.
4041static
4042bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp, int MaskI, int MaskE,
4043 int OpIdx, int NumElems, unsigned &OpNum) {
4044 bool SeenV1 = false;
4045 bool SeenV2 = false;
4046
4047 for (int i = MaskI; i <= MaskE; ++i, ++OpIdx) {
4048 int Idx = SVOp->getMaskElt(i);
4049 // Ignore undef indicies
4050 if (Idx < 0)
4051 continue;
4052
4053 if (Idx < NumElems)
4054 SeenV1 = true;
4055 else
4056 SeenV2 = true;
4057
4058 // Only accept consecutive elements from the same vector
4059 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
4060 return false;
4061 }
4062
4063 OpNum = SeenV1 ? 0 : 1;
4064 return true;
4065}
4066
4067/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
4068/// logical left shift of a vector.
4069static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4070 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4071 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4072 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4073 false /* check zeros from right */, DAG);
4074 unsigned OpSrc;
4075
4076 if (!NumZeros)
4077 return false;
4078
4079 // Considering the elements in the mask that are not consecutive zeros,
4080 // check if they consecutively come from only one of the source vectors.
4081 //
4082 // V1 = {X, A, B, C} 0
4083 // \ \ \ /
4084 // vector_shuffle V1, V2 <1, 2, 3, X>
4085 //
4086 if (!isShuffleMaskConsecutive(SVOp,
4087 0, // Mask Start Index
4088 NumElems-NumZeros-1, // Mask End Index
4089 NumZeros, // Where to start looking in the src vector
4090 NumElems, // Number of elements in vector
4091 OpSrc)) // Which source operand ?
4092 return false;
4093
4094 isLeft = false;
4095 ShAmt = NumZeros;
4096 ShVal = SVOp->getOperand(OpSrc);
4097 return true;
4098}
4099
4100/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
4101/// logical left shift of a vector.
4102static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4103 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4104 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4105 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4106 true /* check zeros from left */, DAG);
4107 unsigned OpSrc;
4108
4109 if (!NumZeros)
4110 return false;
4111
4112 // Considering the elements in the mask that are not consecutive zeros,
4113 // check if they consecutively come from only one of the source vectors.
4114 //
4115 // 0 { A, B, X, X } = V2
4116 // / \ / /
4117 // vector_shuffle V1, V2 <X, X, 4, 5>
4118 //
4119 if (!isShuffleMaskConsecutive(SVOp,
4120 NumZeros, // Mask Start Index
4121 NumElems-1, // Mask End Index
4122 0, // Where to start looking in the src vector
4123 NumElems, // Number of elements in vector
4124 OpSrc)) // Which source operand ?
4125 return false;
4126
4127 isLeft = true;
4128 ShAmt = NumZeros;
4129 ShVal = SVOp->getOperand(OpSrc);
4130 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004131}
4132
4133/// isVectorShift - Returns true if the shuffle can be implemented as a
4134/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004135static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00004136 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004137 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
4138 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
4139 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004140
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004141 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00004142}
4143
Evan Chengc78d3b42006-04-24 18:01:45 +00004144/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
4145///
Dan Gohman475871a2008-07-27 21:46:04 +00004146static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00004147 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00004148 SelectionDAG &DAG,
4149 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004150 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00004151 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004152
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004153 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004154 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004155 bool First = true;
4156 for (unsigned i = 0; i < 16; ++i) {
4157 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
4158 if (ThisIsNonZero && First) {
4159 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004160 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004161 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004162 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004163 First = false;
4164 }
4165
4166 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00004167 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004168 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
4169 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004170 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004171 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00004172 }
4173 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004174 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
4175 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
4176 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00004177 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004178 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00004179 } else
4180 ThisElt = LastElt;
4181
Gabor Greifba36cb52008-08-28 21:40:38 +00004182 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00004183 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00004184 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00004185 }
4186 }
4187
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004188 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00004189}
4190
Bill Wendlinga348c562007-03-22 18:42:45 +00004191/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00004192///
Dan Gohman475871a2008-07-27 21:46:04 +00004193static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00004194 unsigned NumNonZero, unsigned NumZero,
4195 SelectionDAG &DAG,
4196 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004197 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00004198 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004199
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004200 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004201 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004202 bool First = true;
4203 for (unsigned i = 0; i < 8; ++i) {
4204 bool isNonZero = (NonZeros & (1 << i)) != 0;
4205 if (isNonZero) {
4206 if (First) {
4207 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004208 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004209 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004210 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004211 First = false;
4212 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004213 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004214 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00004215 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00004216 }
4217 }
4218
4219 return V;
4220}
4221
Evan Chengf26ffe92008-05-29 08:22:04 +00004222/// getVShift - Return a vector logical shift node.
4223///
Owen Andersone50ed302009-08-10 22:56:29 +00004224static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00004225 unsigned NumBits, SelectionDAG &DAG,
4226 const TargetLowering &TLI, DebugLoc dl) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004227 EVT ShVT = MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00004228 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004229 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
4230 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004231 DAG.getNode(Opc, dl, ShVT, SrcOp,
Owen Anderson95771af2011-02-25 21:41:48 +00004232 DAG.getConstant(NumBits,
4233 TLI.getShiftAmountTy(SrcOp.getValueType()))));
Evan Chengf26ffe92008-05-29 08:22:04 +00004234}
4235
Dan Gohman475871a2008-07-27 21:46:04 +00004236SDValue
Evan Chengc3630942009-12-09 21:00:30 +00004237X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00004238 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00004239
Evan Chengc3630942009-12-09 21:00:30 +00004240 // Check if the scalar load can be widened into a vector load. And if
4241 // the address is "base + cst" see if the cst can be "absorbed" into
4242 // the shuffle mask.
4243 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
4244 SDValue Ptr = LD->getBasePtr();
4245 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
4246 return SDValue();
4247 EVT PVT = LD->getValueType(0);
4248 if (PVT != MVT::i32 && PVT != MVT::f32)
4249 return SDValue();
4250
4251 int FI = -1;
4252 int64_t Offset = 0;
4253 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
4254 FI = FINode->getIndex();
4255 Offset = 0;
Chris Lattner0a9481f2011-02-13 22:25:43 +00004256 } else if (DAG.isBaseWithConstantOffset(Ptr) &&
Evan Chengc3630942009-12-09 21:00:30 +00004257 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4258 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4259 Offset = Ptr.getConstantOperandVal(1);
4260 Ptr = Ptr.getOperand(0);
4261 } else {
4262 return SDValue();
4263 }
4264
4265 SDValue Chain = LD->getChain();
4266 // Make sure the stack object alignment is at least 16.
4267 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
4268 if (DAG.InferPtrAlignment(Ptr) < 16) {
4269 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00004270 // Can't change the alignment. FIXME: It's possible to compute
4271 // the exact stack offset and reference FI + adjust offset instead.
4272 // If someone *really* cares about this. That's the way to implement it.
4273 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004274 } else {
4275 MFI->setObjectAlignment(FI, 16);
4276 }
4277 }
4278
4279 // (Offset % 16) must be multiple of 4. Then address is then
4280 // Ptr + (Offset & ~15).
4281 if (Offset < 0)
4282 return SDValue();
4283 if ((Offset % 16) & 3)
4284 return SDValue();
4285 int64_t StartOffset = Offset & ~15;
4286 if (StartOffset)
4287 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
4288 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
4289
4290 int EltNo = (Offset - StartOffset) >> 2;
4291 int Mask[4] = { EltNo, EltNo, EltNo, EltNo };
4292 EVT VT = (PVT == MVT::i32) ? MVT::v4i32 : MVT::v4f32;
Chris Lattner51abfe42010-09-21 06:02:19 +00004293 SDValue V1 = DAG.getLoad(VT, dl, Chain, Ptr,
4294 LD->getPointerInfo().getWithOffset(StartOffset),
David Greene67c9d422010-02-15 16:53:33 +00004295 false, false, 0);
Evan Chengc3630942009-12-09 21:00:30 +00004296 // Canonicalize it to a v4i32 shuffle.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004297 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, V1);
4298 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Chengc3630942009-12-09 21:00:30 +00004299 DAG.getVectorShuffle(MVT::v4i32, dl, V1,
Chris Lattner51abfe42010-09-21 06:02:19 +00004300 DAG.getUNDEF(MVT::v4i32),&Mask[0]));
Evan Chengc3630942009-12-09 21:00:30 +00004301 }
4302
4303 return SDValue();
4304}
4305
Michael J. Spencerec38de22010-10-10 22:04:20 +00004306/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
4307/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00004308/// load which has the same value as a build_vector whose operands are 'elts'.
4309///
4310/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00004311///
Nate Begeman1449f292010-03-24 22:19:06 +00004312/// FIXME: we'd also like to handle the case where the last elements are zero
4313/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
4314/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004315static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00004316 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004317 EVT EltVT = VT.getVectorElementType();
4318 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00004319
Nate Begemanfdea31a2010-03-24 20:49:50 +00004320 LoadSDNode *LDBase = NULL;
4321 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004322
Nate Begeman1449f292010-03-24 22:19:06 +00004323 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00004324 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00004325 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004326 for (unsigned i = 0; i < NumElems; ++i) {
4327 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00004328
Nate Begemanfdea31a2010-03-24 20:49:50 +00004329 if (!Elt.getNode() ||
4330 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
4331 return SDValue();
4332 if (!LDBase) {
4333 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
4334 return SDValue();
4335 LDBase = cast<LoadSDNode>(Elt.getNode());
4336 LastLoadedElt = i;
4337 continue;
4338 }
4339 if (Elt.getOpcode() == ISD::UNDEF)
4340 continue;
4341
4342 LoadSDNode *LD = cast<LoadSDNode>(Elt);
4343 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
4344 return SDValue();
4345 LastLoadedElt = i;
4346 }
Nate Begeman1449f292010-03-24 22:19:06 +00004347
4348 // If we have found an entire vector of loads and undefs, then return a large
4349 // load of the entire vector width starting at the base pointer. If we found
4350 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004351 if (LastLoadedElt == NumElems - 1) {
4352 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00004353 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004354 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004355 LDBase->isVolatile(), LDBase->isNonTemporal(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00004356 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004357 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004358 LDBase->isVolatile(), LDBase->isNonTemporal(),
4359 LDBase->getAlignment());
4360 } else if (NumElems == 4 && LastLoadedElt == 1) {
4361 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
4362 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Chris Lattner88641552010-09-22 00:34:38 +00004363 SDValue ResNode = DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys,
4364 Ops, 2, MVT::i32,
4365 LDBase->getMemOperand());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004366 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00004367 }
4368 return SDValue();
4369}
4370
Evan Chengc3630942009-12-09 21:00:30 +00004371SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004372X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004373 DebugLoc dl = Op.getDebugLoc();
David Greenea5f26012011-02-07 19:36:54 +00004374
David Greenef125a292011-02-08 19:04:41 +00004375 EVT VT = Op.getValueType();
4376 EVT ExtVT = VT.getVectorElementType();
4377
4378 unsigned NumElems = Op.getNumOperands();
4379
4380 // For AVX-length vectors, build the individual 128-bit pieces and
4381 // use shuffles to put them in place.
Owen Anderson95771af2011-02-25 21:41:48 +00004382 if (VT.getSizeInBits() > 256 &&
4383 Subtarget->hasAVX() &&
David Greenef125a292011-02-08 19:04:41 +00004384 !ISD::isBuildVectorAllZeros(Op.getNode())) {
4385 SmallVector<SDValue, 8> V;
4386 V.resize(NumElems);
4387 for (unsigned i = 0; i < NumElems; ++i) {
4388 V[i] = Op.getOperand(i);
4389 }
Owen Anderson95771af2011-02-25 21:41:48 +00004390
David Greenef125a292011-02-08 19:04:41 +00004391 EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElems/2);
4392
4393 // Build the lower subvector.
4394 SDValue Lower = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[0], NumElems/2);
4395 // Build the upper subvector.
4396 SDValue Upper = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[NumElems / 2],
4397 NumElems/2);
4398
4399 return ConcatVectors(Lower, Upper, DAG);
4400 }
4401
Chris Lattner6e80e442010-08-28 17:15:43 +00004402 // All zero's are handled with pxor in SSE2 and above, xorps in SSE1.
4403 // All one's are handled with pcmpeqd. In AVX, zero's are handled with
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004404 // vpxor in 128-bit and xor{pd,ps} in 256-bit, but no 256 version of pcmpeqd
4405 // is present, so AllOnes is ignored.
4406 if (ISD::isBuildVectorAllZeros(Op.getNode()) ||
4407 (Op.getValueType().getSizeInBits() != 256 &&
4408 ISD::isBuildVectorAllOnes(Op.getNode()))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004409 // Canonicalize this to <4 x i32> (SSE) to
Chris Lattner8a594482007-11-25 00:24:49 +00004410 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
4411 // eliminated on x86-32 hosts.
Dale Johannesen0488fb62010-09-30 23:57:10 +00004412 if (Op.getValueType() == MVT::v4i32)
Chris Lattner8a594482007-11-25 00:24:49 +00004413 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004414
Gabor Greifba36cb52008-08-28 21:40:38 +00004415 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00004416 return getOnesVector(Op.getValueType(), DAG, dl);
4417 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00004418 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004419
Owen Andersone50ed302009-08-10 22:56:29 +00004420 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004421
Evan Cheng0db9fe62006-04-25 20:13:52 +00004422 unsigned NumZero = 0;
4423 unsigned NumNonZero = 0;
4424 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004425 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00004426 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004427 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00004428 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00004429 if (Elt.getOpcode() == ISD::UNDEF)
4430 continue;
4431 Values.insert(Elt);
4432 if (Elt.getOpcode() != ISD::Constant &&
4433 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004434 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00004435 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00004436 NumZero++;
4437 else {
4438 NonZeros |= (1 << i);
4439 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004440 }
4441 }
4442
Chris Lattner97a2a562010-08-26 05:24:29 +00004443 // All undef vector. Return an UNDEF. All zero vectors were handled above.
4444 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00004445 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004446
Chris Lattner67f453a2008-03-09 05:42:06 +00004447 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00004448 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004449 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00004450 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00004451
Chris Lattner62098042008-03-09 01:05:04 +00004452 // If this is an insertion of an i64 value on x86-32, and if the top bits of
4453 // the value are obviously zero, truncate the value to i32 and do the
4454 // insertion that way. Only do this if the value is non-constant or if the
4455 // value is a constant being inserted into element 0. It is cheaper to do
4456 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00004457 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00004458 (!IsAllConstants || Idx == 0)) {
4459 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004460 // Handle SSE only.
4461 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
4462 EVT VecVT = MVT::v4i32;
4463 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00004464
Chris Lattner62098042008-03-09 01:05:04 +00004465 // Truncate the value (which may itself be a constant) to i32, and
4466 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00004467 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00004468 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00004469 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
4470 Subtarget->hasSSE2(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00004471
Chris Lattner62098042008-03-09 01:05:04 +00004472 // Now we have our 32-bit value zero extended in the low element of
4473 // a vector. If Idx != 0, swizzle it into place.
4474 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004475 SmallVector<int, 4> Mask;
4476 Mask.push_back(Idx);
4477 for (unsigned i = 1; i != VecElts; ++i)
4478 Mask.push_back(i);
4479 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopherfd179292009-08-27 18:07:15 +00004480 DAG.getUNDEF(Item.getValueType()),
Nate Begeman9008ca62009-04-27 18:41:29 +00004481 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004482 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004483 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00004484 }
4485 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004486
Chris Lattner19f79692008-03-08 22:59:52 +00004487 // If we have a constant or non-constant insertion into the low element of
4488 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
4489 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00004490 // depending on what the source datatype is.
4491 if (Idx == 0) {
4492 if (NumZero == 0) {
4493 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Owen Anderson825b72b2009-08-11 20:47:22 +00004494 } else if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
4495 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Eli Friedman10415532009-06-06 06:05:10 +00004496 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
4497 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
4498 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget->hasSSE2(),
4499 DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00004500 } else if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
4501 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Dale Johannesen0488fb62010-09-30 23:57:10 +00004502 assert(VT.getSizeInBits() == 128 && "Expected an SSE value type!");
4503 EVT MiddleVT = MVT::v4i32;
Eli Friedman10415532009-06-06 06:05:10 +00004504 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
4505 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
4506 Subtarget->hasSSE2(), DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004507 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00004508 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004509 }
Evan Chengf26ffe92008-05-29 08:22:04 +00004510
4511 // Is it a vector logical left shift?
4512 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00004513 X86::isZeroNode(Op.getOperand(0)) &&
4514 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004515 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00004516 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00004517 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004518 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00004519 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004520 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004521
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004522 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00004523 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004524
Chris Lattner19f79692008-03-08 22:59:52 +00004525 // Otherwise, if this is a vector with i32 or f32 elements, and the element
4526 // is a non-constant being inserted into an element other than the low one,
4527 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
4528 // movd/movss) to move this into the low element, then shuffle it into
4529 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004530 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00004531 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00004532
Evan Cheng0db9fe62006-04-25 20:13:52 +00004533 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00004534 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
4535 Subtarget->hasSSE2(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00004536 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004537 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00004538 MaskVec.push_back(i == Idx ? 0 : 1);
4539 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004540 }
4541 }
4542
Chris Lattner67f453a2008-03-09 05:42:06 +00004543 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00004544 if (Values.size() == 1) {
4545 if (EVTBits == 32) {
4546 // Instead of a shuffle like this:
4547 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
4548 // Check if it's possible to issue this instead.
4549 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
4550 unsigned Idx = CountTrailingZeros_32(NonZeros);
4551 SDValue Item = Op.getOperand(Idx);
4552 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
4553 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
4554 }
Dan Gohman475871a2008-07-27 21:46:04 +00004555 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004556 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004557
Dan Gohmana3941172007-07-24 22:55:08 +00004558 // A vector full of immediates; various special cases are already
4559 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004560 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00004561 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00004562
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00004563 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00004564 if (EVTBits == 64) {
4565 if (NumNonZero == 1) {
4566 // One half is zero or undef.
4567 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00004568 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00004569 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00004570 return getShuffleVectorZeroOrUndef(V2, Idx, true,
4571 Subtarget->hasSSE2(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00004572 }
Dan Gohman475871a2008-07-27 21:46:04 +00004573 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00004574 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004575
4576 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00004577 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00004578 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00004579 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004580 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004581 }
4582
Bill Wendling826f36f2007-03-28 00:57:11 +00004583 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00004584 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Chris Lattner97a2a562010-08-26 05:24:29 +00004585 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004586 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004587 }
4588
4589 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00004590 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00004591 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004592 if (NumElems == 4 && NumZero > 0) {
4593 for (unsigned i = 0; i < 4; ++i) {
4594 bool isZero = !(NonZeros & (1 << i));
4595 if (isZero)
Dale Johannesenace16102009-02-03 19:33:06 +00004596 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004597 else
Dale Johannesenace16102009-02-03 19:33:06 +00004598 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004599 }
4600
4601 for (unsigned i = 0; i < 2; ++i) {
4602 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
4603 default: break;
4604 case 0:
4605 V[i] = V[i*2]; // Must be a zero vector.
4606 break;
4607 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00004608 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004609 break;
4610 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00004611 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004612 break;
4613 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00004614 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004615 break;
4616 }
4617 }
4618
Nate Begeman9008ca62009-04-27 18:41:29 +00004619 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004620 bool Reverse = (NonZeros & 0x3) == 2;
4621 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004622 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004623 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
4624 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004625 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
4626 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004627 }
4628
Nate Begemanfdea31a2010-03-24 20:49:50 +00004629 if (Values.size() > 1 && VT.getSizeInBits() == 128) {
4630 // Check for a build vector of consecutive loads.
4631 for (unsigned i = 0; i < NumElems; ++i)
4632 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00004633
Nate Begemanfdea31a2010-03-24 20:49:50 +00004634 // Check for elements which are consecutive loads.
4635 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
4636 if (LD.getNode())
4637 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004638
4639 // For SSE 4.1, use insertps to put the high elements into the low element.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004640 if (getSubtarget()->hasSSE41()) {
Chris Lattner24faf612010-08-28 17:59:08 +00004641 SDValue Result;
4642 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
4643 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
4644 else
4645 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00004646
Chris Lattner24faf612010-08-28 17:59:08 +00004647 for (unsigned i = 1; i < NumElems; ++i) {
4648 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
4649 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00004650 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00004651 }
4652 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00004653 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00004654
Chris Lattner6e80e442010-08-28 17:15:43 +00004655 // Otherwise, expand into a number of unpckl*, start by extending each of
4656 // our (non-undef) elements to the full vector width with the element in the
4657 // bottom slot of the vector (which generates no code for SSE).
4658 for (unsigned i = 0; i < NumElems; ++i) {
4659 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
4660 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
4661 else
4662 V[i] = DAG.getUNDEF(VT);
4663 }
4664
4665 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004666 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
4667 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
4668 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00004669 unsigned EltStride = NumElems >> 1;
4670 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00004671 for (unsigned i = 0; i < EltStride; ++i) {
4672 // If V[i+EltStride] is undef and this is the first round of mixing,
4673 // then it is safe to just drop this shuffle: V[i] is already in the
4674 // right place, the one element (since it's the first round) being
4675 // inserted as undef can be dropped. This isn't safe for successive
4676 // rounds because they will permute elements within both vectors.
4677 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
4678 EltStride == NumElems/2)
4679 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004680
Chris Lattner6e80e442010-08-28 17:15:43 +00004681 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00004682 }
Chris Lattner6e80e442010-08-28 17:15:43 +00004683 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004684 }
4685 return V[0];
4686 }
Dan Gohman475871a2008-07-27 21:46:04 +00004687 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004688}
4689
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004690SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004691X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const {
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004692 // We support concatenate two MMX registers and place them in a MMX
4693 // register. This is better than doing a stack convert.
4694 DebugLoc dl = Op.getDebugLoc();
4695 EVT ResVT = Op.getValueType();
4696 assert(Op.getNumOperands() == 2);
4697 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
4698 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
4699 int Mask[2];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004700 SDValue InVec = DAG.getNode(ISD::BITCAST,dl, MVT::v1i64, Op.getOperand(0));
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004701 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4702 InVec = Op.getOperand(1);
4703 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4704 unsigned NumElts = ResVT.getVectorNumElements();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004705 VecOp = DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004706 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
4707 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
4708 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004709 InVec = DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, InVec);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004710 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4711 Mask[0] = 0; Mask[1] = 2;
4712 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
4713 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004714 return DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004715}
4716
Nate Begemanb9a47b82009-02-23 08:49:38 +00004717// v8i16 shuffles - Prefer shuffles in the following order:
4718// 1. [all] pshuflw, pshufhw, optional move
4719// 2. [ssse3] 1 x pshufb
4720// 3. [ssse3] 2 x pshufb + 1 x por
4721// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004722SDValue
4723X86TargetLowering::LowerVECTOR_SHUFFLEv8i16(SDValue Op,
4724 SelectionDAG &DAG) const {
4725 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00004726 SDValue V1 = SVOp->getOperand(0);
4727 SDValue V2 = SVOp->getOperand(1);
4728 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004729 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00004730
Nate Begemanb9a47b82009-02-23 08:49:38 +00004731 // Determine if more than 1 of the words in each of the low and high quadwords
4732 // of the result come from the same quadword of one of the two inputs. Undef
4733 // mask values count as coming from any quadword, for better codegen.
4734 SmallVector<unsigned, 4> LoQuad(4);
4735 SmallVector<unsigned, 4> HiQuad(4);
4736 BitVector InputQuads(4);
4737 for (unsigned i = 0; i < 8; ++i) {
4738 SmallVectorImpl<unsigned> &Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00004739 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004740 MaskVals.push_back(EltIdx);
4741 if (EltIdx < 0) {
4742 ++Quad[0];
4743 ++Quad[1];
4744 ++Quad[2];
4745 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00004746 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004747 }
4748 ++Quad[EltIdx / 4];
4749 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00004750 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004751
Nate Begemanb9a47b82009-02-23 08:49:38 +00004752 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004753 unsigned MaxQuad = 1;
4754 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004755 if (LoQuad[i] > MaxQuad) {
4756 BestLoQuad = i;
4757 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004758 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004759 }
4760
Nate Begemanb9a47b82009-02-23 08:49:38 +00004761 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004762 MaxQuad = 1;
4763 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004764 if (HiQuad[i] > MaxQuad) {
4765 BestHiQuad = i;
4766 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004767 }
4768 }
4769
Nate Begemanb9a47b82009-02-23 08:49:38 +00004770 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00004771 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00004772 // single pshufb instruction is necessary. If There are more than 2 input
4773 // quads, disable the next transformation since it does not help SSSE3.
4774 bool V1Used = InputQuads[0] || InputQuads[1];
4775 bool V2Used = InputQuads[2] || InputQuads[3];
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004776 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004777 if (InputQuads.count() == 2 && V1Used && V2Used) {
4778 BestLoQuad = InputQuads.find_first();
4779 BestHiQuad = InputQuads.find_next(BestLoQuad);
4780 }
4781 if (InputQuads.count() > 2) {
4782 BestLoQuad = -1;
4783 BestHiQuad = -1;
4784 }
4785 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004786
Nate Begemanb9a47b82009-02-23 08:49:38 +00004787 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
4788 // the shuffle mask. If a quad is scored as -1, that means that it contains
4789 // words from all 4 input quadwords.
4790 SDValue NewV;
4791 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004792 SmallVector<int, 8> MaskV;
4793 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
4794 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Eric Christopherfd179292009-08-27 18:07:15 +00004795 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004796 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
4797 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
4798 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00004799
Nate Begemanb9a47b82009-02-23 08:49:38 +00004800 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
4801 // source words for the shuffle, to aid later transformations.
4802 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00004803 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00004804 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004805 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00004806 if (idx != (int)i)
4807 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004808 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00004809 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004810 AllWordsInNewV = false;
4811 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00004812 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004813
Nate Begemanb9a47b82009-02-23 08:49:38 +00004814 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
4815 if (AllWordsInNewV) {
4816 for (int i = 0; i != 8; ++i) {
4817 int idx = MaskVals[i];
4818 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00004819 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004820 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004821 if ((idx != i) && idx < 4)
4822 pshufhw = false;
4823 if ((idx != i) && idx > 3)
4824 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00004825 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00004826 V1 = NewV;
4827 V2Used = false;
4828 BestLoQuad = 0;
4829 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004830 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004831
Nate Begemanb9a47b82009-02-23 08:49:38 +00004832 // If we've eliminated the use of V2, and the new mask is a pshuflw or
4833 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00004834 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00004835 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
4836 unsigned TargetMask = 0;
4837 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00004838 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00004839 TargetMask = pshufhw ? X86::getShufflePSHUFHWImmediate(NewV.getNode()):
4840 X86::getShufflePSHUFLWImmediate(NewV.getNode());
4841 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004842 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00004843 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004844 }
Eric Christopherfd179292009-08-27 18:07:15 +00004845
Nate Begemanb9a47b82009-02-23 08:49:38 +00004846 // If we have SSSE3, and all words of the result are from 1 input vector,
4847 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
4848 // is present, fall back to case 4.
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004849 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004850 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004851
Nate Begemanb9a47b82009-02-23 08:49:38 +00004852 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00004853 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00004854 // mask, and elements that come from V1 in the V2 mask, so that the two
4855 // results can be OR'd together.
4856 bool TwoInputs = V1Used && V2Used;
4857 for (unsigned i = 0; i != 8; ++i) {
4858 int EltIdx = MaskVals[i] * 2;
4859 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004860 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4861 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004862 continue;
4863 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004864 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
4865 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004866 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004867 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004868 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004869 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004870 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004871 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004872 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004873
Nate Begemanb9a47b82009-02-23 08:49:38 +00004874 // Calculate the shuffle mask for the second input, shuffle it, and
4875 // OR it with the first shuffled input.
4876 pshufbMask.clear();
4877 for (unsigned i = 0; i != 8; ++i) {
4878 int EltIdx = MaskVals[i] * 2;
4879 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004880 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4881 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004882 continue;
4883 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004884 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
4885 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004886 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004887 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00004888 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00004889 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004890 MVT::v16i8, &pshufbMask[0], 16));
4891 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004892 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004893 }
4894
4895 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
4896 // and update MaskVals with new element order.
4897 BitVector InOrder(8);
4898 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004899 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004900 for (int i = 0; i != 4; ++i) {
4901 int idx = MaskVals[i];
4902 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004903 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004904 InOrder.set(i);
4905 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004906 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004907 InOrder.set(i);
4908 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004909 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004910 }
4911 }
4912 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004913 MaskV.push_back(i);
Owen Anderson825b72b2009-08-11 20:47:22 +00004914 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004915 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004916
4917 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
4918 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
4919 NewV.getOperand(0),
4920 X86::getShufflePSHUFLWImmediate(NewV.getNode()),
4921 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004922 }
Eric Christopherfd179292009-08-27 18:07:15 +00004923
Nate Begemanb9a47b82009-02-23 08:49:38 +00004924 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
4925 // and update MaskVals with the new element order.
4926 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004927 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004928 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004929 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004930 for (unsigned i = 4; i != 8; ++i) {
4931 int idx = MaskVals[i];
4932 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004933 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004934 InOrder.set(i);
4935 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004936 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004937 InOrder.set(i);
4938 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004939 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004940 }
4941 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004942 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004943 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004944
4945 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
4946 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
4947 NewV.getOperand(0),
4948 X86::getShufflePSHUFHWImmediate(NewV.getNode()),
4949 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004950 }
Eric Christopherfd179292009-08-27 18:07:15 +00004951
Nate Begemanb9a47b82009-02-23 08:49:38 +00004952 // In case BestHi & BestLo were both -1, which means each quadword has a word
4953 // from each of the four input quadwords, calculate the InOrder bitvector now
4954 // before falling through to the insert/extract cleanup.
4955 if (BestLoQuad == -1 && BestHiQuad == -1) {
4956 NewV = V1;
4957 for (int i = 0; i != 8; ++i)
4958 if (MaskVals[i] < 0 || MaskVals[i] == i)
4959 InOrder.set(i);
4960 }
Eric Christopherfd179292009-08-27 18:07:15 +00004961
Nate Begemanb9a47b82009-02-23 08:49:38 +00004962 // The other elements are put in the right place using pextrw and pinsrw.
4963 for (unsigned i = 0; i != 8; ++i) {
4964 if (InOrder[i])
4965 continue;
4966 int EltIdx = MaskVals[i];
4967 if (EltIdx < 0)
4968 continue;
4969 SDValue ExtOp = (EltIdx < 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00004970 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004971 DAG.getIntPtrConstant(EltIdx))
Owen Anderson825b72b2009-08-11 20:47:22 +00004972 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004973 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00004974 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004975 DAG.getIntPtrConstant(i));
4976 }
4977 return NewV;
4978}
4979
4980// v16i8 shuffles - Prefer shuffles in the following order:
4981// 1. [ssse3] 1 x pshufb
4982// 2. [ssse3] 2 x pshufb + 1 x por
4983// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
4984static
Nate Begeman9008ca62009-04-27 18:41:29 +00004985SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00004986 SelectionDAG &DAG,
4987 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004988 SDValue V1 = SVOp->getOperand(0);
4989 SDValue V2 = SVOp->getOperand(1);
4990 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004991 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00004992 SVOp->getMask(MaskVals);
Eric Christopherfd179292009-08-27 18:07:15 +00004993
Nate Begemanb9a47b82009-02-23 08:49:38 +00004994 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00004995 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00004996 // present, fall back to case 3.
4997 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
4998 bool V1Only = true;
4999 bool V2Only = true;
5000 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005001 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00005002 if (EltIdx < 0)
5003 continue;
5004 if (EltIdx < 16)
5005 V2Only = false;
5006 else
5007 V1Only = false;
5008 }
Eric Christopherfd179292009-08-27 18:07:15 +00005009
Nate Begemanb9a47b82009-02-23 08:49:38 +00005010 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
5011 if (TLI.getSubtarget()->hasSSSE3()) {
5012 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005013
Nate Begemanb9a47b82009-02-23 08:49:38 +00005014 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00005015 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005016 //
5017 // Otherwise, we have elements from both input vectors, and must zero out
5018 // elements that come from V2 in the first mask, and V1 in the second mask
5019 // so that we can OR them together.
5020 bool TwoInputs = !(V1Only || V2Only);
5021 for (unsigned i = 0; i != 16; ++i) {
5022 int EltIdx = MaskVals[i];
5023 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005024 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005025 continue;
5026 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005027 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005028 }
5029 // If all the elements are from V2, assign it to V1 and return after
5030 // building the first pshufb.
5031 if (V2Only)
5032 V1 = V2;
Owen Anderson825b72b2009-08-11 20:47:22 +00005033 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005034 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005035 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005036 if (!TwoInputs)
5037 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00005038
Nate Begemanb9a47b82009-02-23 08:49:38 +00005039 // Calculate the shuffle mask for the second input, shuffle it, and
5040 // OR it with the first shuffled input.
5041 pshufbMask.clear();
5042 for (unsigned i = 0; i != 16; ++i) {
5043 int EltIdx = MaskVals[i];
5044 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005045 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005046 continue;
5047 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005048 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005049 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005050 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005051 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005052 MVT::v16i8, &pshufbMask[0], 16));
5053 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005054 }
Eric Christopherfd179292009-08-27 18:07:15 +00005055
Nate Begemanb9a47b82009-02-23 08:49:38 +00005056 // No SSSE3 - Calculate in place words and then fix all out of place words
5057 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
5058 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005059 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
5060 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005061 SDValue NewV = V2Only ? V2 : V1;
5062 for (int i = 0; i != 8; ++i) {
5063 int Elt0 = MaskVals[i*2];
5064 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00005065
Nate Begemanb9a47b82009-02-23 08:49:38 +00005066 // This word of the result is all undef, skip it.
5067 if (Elt0 < 0 && Elt1 < 0)
5068 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005069
Nate Begemanb9a47b82009-02-23 08:49:38 +00005070 // This word of the result is already in the correct place, skip it.
5071 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
5072 continue;
5073 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
5074 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005075
Nate Begemanb9a47b82009-02-23 08:49:38 +00005076 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
5077 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
5078 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00005079
5080 // If Elt0 and Elt1 are defined, are consecutive, and can be load
5081 // using a single extract together, load it and store it.
5082 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005083 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005084 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00005085 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005086 DAG.getIntPtrConstant(i));
5087 continue;
5088 }
5089
Nate Begemanb9a47b82009-02-23 08:49:38 +00005090 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00005091 // source byte is not also odd, shift the extracted word left 8 bits
5092 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005093 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005094 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005095 DAG.getIntPtrConstant(Elt1 / 2));
5096 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005097 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Owen Anderson95771af2011-02-25 21:41:48 +00005098 DAG.getConstant(8,
5099 TLI.getShiftAmountTy(InsElt.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005100 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005101 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
5102 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005103 }
5104 // If Elt0 is defined, extract it from the appropriate source. If the
5105 // source byte is not also even, shift the extracted word right 8 bits. If
5106 // Elt1 was also defined, OR the extracted values together before
5107 // inserting them in the result.
5108 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005109 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005110 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
5111 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005112 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Owen Anderson95771af2011-02-25 21:41:48 +00005113 DAG.getConstant(8,
5114 TLI.getShiftAmountTy(InsElt0.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005115 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005116 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
5117 DAG.getConstant(0x00FF, MVT::i16));
5118 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00005119 : InsElt0;
5120 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005121 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005122 DAG.getIntPtrConstant(i));
5123 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005124 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005125}
5126
Evan Cheng7a831ce2007-12-15 03:00:47 +00005127/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005128/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00005129/// done when every pair / quad of shuffle mask elements point to elements in
5130/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005131/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00005132static
Nate Begeman9008ca62009-04-27 18:41:29 +00005133SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005134 SelectionDAG &DAG, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00005135 EVT VT = SVOp->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00005136 SDValue V1 = SVOp->getOperand(0);
5137 SDValue V2 = SVOp->getOperand(1);
5138 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00005139 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005140 EVT NewVT;
Owen Anderson825b72b2009-08-11 20:47:22 +00005141 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005142 default: assert(false && "Unexpected!");
Owen Anderson825b72b2009-08-11 20:47:22 +00005143 case MVT::v4f32: NewVT = MVT::v2f64; break;
5144 case MVT::v4i32: NewVT = MVT::v2i64; break;
5145 case MVT::v8i16: NewVT = MVT::v4i32; break;
5146 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00005147 }
5148
Nate Begeman9008ca62009-04-27 18:41:29 +00005149 int Scale = NumElems / NewWidth;
5150 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00005151 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005152 int StartIdx = -1;
5153 for (int j = 0; j < Scale; ++j) {
5154 int EltIdx = SVOp->getMaskElt(i+j);
5155 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005156 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00005157 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00005158 StartIdx = EltIdx - (EltIdx % Scale);
5159 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00005160 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00005161 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005162 if (StartIdx == -1)
5163 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00005164 else
Nate Begeman9008ca62009-04-27 18:41:29 +00005165 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005166 }
5167
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005168 V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, V1);
5169 V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00005170 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005171}
5172
Evan Chengd880b972008-05-09 21:53:03 +00005173/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005174///
Owen Andersone50ed302009-08-10 22:56:29 +00005175static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00005176 SDValue SrcOp, SelectionDAG &DAG,
5177 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005178 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005179 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00005180 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00005181 LD = dyn_cast<LoadSDNode>(SrcOp);
5182 if (!LD) {
5183 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
5184 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00005185 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00005186 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00005187 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005188 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00005189 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005190 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00005191 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005192 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005193 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
5194 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
5195 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00005196 SrcOp.getOperand(0)
5197 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005198 }
5199 }
5200 }
5201
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005202 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005203 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005204 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00005205 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005206}
5207
Evan Chengace3c172008-07-22 21:13:36 +00005208/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
5209/// shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00005210static SDValue
Nate Begeman9008ca62009-04-27 18:41:29 +00005211LowerVECTOR_SHUFFLE_4wide(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
5212 SDValue V1 = SVOp->getOperand(0);
5213 SDValue V2 = SVOp->getOperand(1);
5214 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005215 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00005216
Evan Chengace3c172008-07-22 21:13:36 +00005217 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00005218 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00005219 SmallVector<int, 8> Mask1(4U, -1);
5220 SmallVector<int, 8> PermMask;
5221 SVOp->getMask(PermMask);
5222
Evan Chengace3c172008-07-22 21:13:36 +00005223 unsigned NumHi = 0;
5224 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00005225 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005226 int Idx = PermMask[i];
5227 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00005228 Locs[i] = std::make_pair(-1, -1);
5229 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005230 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
5231 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00005232 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00005233 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005234 NumLo++;
5235 } else {
5236 Locs[i] = std::make_pair(1, NumHi);
5237 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00005238 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005239 NumHi++;
5240 }
5241 }
5242 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005243
Evan Chengace3c172008-07-22 21:13:36 +00005244 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005245 // If no more than two elements come from either vector. This can be
5246 // implemented with two shuffles. First shuffle gather the elements.
5247 // The second shuffle, which takes the first shuffle as both of its
5248 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00005249 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005250
Nate Begeman9008ca62009-04-27 18:41:29 +00005251 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopherfd179292009-08-27 18:07:15 +00005252
Evan Chengace3c172008-07-22 21:13:36 +00005253 for (unsigned i = 0; i != 4; ++i) {
5254 if (Locs[i].first == -1)
5255 continue;
5256 else {
5257 unsigned Idx = (i < 2) ? 0 : 4;
5258 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00005259 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005260 }
5261 }
5262
Nate Begeman9008ca62009-04-27 18:41:29 +00005263 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005264 } else if (NumLo == 3 || NumHi == 3) {
5265 // Otherwise, we must have three elements from one vector, call it X, and
5266 // one element from the other, call it Y. First, use a shufps to build an
5267 // intermediate vector with the one element from Y and the element from X
5268 // that will be in the same half in the final destination (the indexes don't
5269 // matter). Then, use a shufps to build the final vector, taking the half
5270 // containing the element from Y from the intermediate, and the other half
5271 // from X.
5272 if (NumHi == 3) {
5273 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00005274 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005275 std::swap(V1, V2);
5276 }
5277
5278 // Find the element from V2.
5279 unsigned HiIndex;
5280 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005281 int Val = PermMask[HiIndex];
5282 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005283 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005284 if (Val >= 4)
5285 break;
5286 }
5287
Nate Begeman9008ca62009-04-27 18:41:29 +00005288 Mask1[0] = PermMask[HiIndex];
5289 Mask1[1] = -1;
5290 Mask1[2] = PermMask[HiIndex^1];
5291 Mask1[3] = -1;
5292 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005293
5294 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005295 Mask1[0] = PermMask[0];
5296 Mask1[1] = PermMask[1];
5297 Mask1[2] = HiIndex & 1 ? 6 : 4;
5298 Mask1[3] = HiIndex & 1 ? 4 : 6;
5299 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005300 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005301 Mask1[0] = HiIndex & 1 ? 2 : 0;
5302 Mask1[1] = HiIndex & 1 ? 0 : 2;
5303 Mask1[2] = PermMask[2];
5304 Mask1[3] = PermMask[3];
5305 if (Mask1[2] >= 0)
5306 Mask1[2] += 4;
5307 if (Mask1[3] >= 0)
5308 Mask1[3] += 4;
5309 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005310 }
Evan Chengace3c172008-07-22 21:13:36 +00005311 }
5312
5313 // Break it into (shuffle shuffle_hi, shuffle_lo).
5314 Locs.clear();
David Greenec4db4e52011-02-28 19:06:56 +00005315 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00005316 SmallVector<int,8> LoMask(4U, -1);
5317 SmallVector<int,8> HiMask(4U, -1);
5318
5319 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00005320 unsigned MaskIdx = 0;
5321 unsigned LoIdx = 0;
5322 unsigned HiIdx = 2;
5323 for (unsigned i = 0; i != 4; ++i) {
5324 if (i == 2) {
5325 MaskPtr = &HiMask;
5326 MaskIdx = 1;
5327 LoIdx = 0;
5328 HiIdx = 2;
5329 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005330 int Idx = PermMask[i];
5331 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00005332 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00005333 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00005334 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00005335 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005336 LoIdx++;
5337 } else {
5338 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00005339 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005340 HiIdx++;
5341 }
5342 }
5343
Nate Begeman9008ca62009-04-27 18:41:29 +00005344 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
5345 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
5346 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00005347 for (unsigned i = 0; i != 4; ++i) {
5348 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005349 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00005350 } else {
5351 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00005352 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00005353 }
5354 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005355 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00005356}
5357
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005358static bool MayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005359 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005360 V = V.getOperand(0);
5361 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
5362 V = V.getOperand(0);
5363 if (MayFoldLoad(V))
5364 return true;
5365 return false;
5366}
5367
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005368// FIXME: the version above should always be used. Since there's
5369// a bug where several vector shuffles can't be folded because the
5370// DAG is not updated during lowering and a node claims to have two
5371// uses while it only has one, use this version, and let isel match
5372// another instruction if the load really happens to have more than
5373// one use. Remove this version after this bug get fixed.
Evan Cheng835580f2010-10-07 20:50:20 +00005374// rdar://8434668, PR8156
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005375static bool RelaxedMayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005376 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005377 V = V.getOperand(0);
5378 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
5379 V = V.getOperand(0);
5380 if (ISD::isNormalLoad(V.getNode()))
5381 return true;
5382 return false;
5383}
5384
5385/// CanFoldShuffleIntoVExtract - Check if the current shuffle is used by
5386/// a vector extract, and if both can be later optimized into a single load.
5387/// This is done in visitEXTRACT_VECTOR_ELT and the conditions are checked
5388/// here because otherwise a target specific shuffle node is going to be
5389/// emitted for this shuffle, and the optimization not done.
5390/// FIXME: This is probably not the best approach, but fix the problem
5391/// until the right path is decided.
5392static
5393bool CanXFormVExtractWithShuffleIntoLoad(SDValue V, SelectionDAG &DAG,
5394 const TargetLowering &TLI) {
5395 EVT VT = V.getValueType();
5396 ShuffleVectorSDNode *SVOp = dyn_cast<ShuffleVectorSDNode>(V);
5397
5398 // Be sure that the vector shuffle is present in a pattern like this:
5399 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), c) -> (f32 load $addr)
5400 if (!V.hasOneUse())
5401 return false;
5402
5403 SDNode *N = *V.getNode()->use_begin();
5404 if (N->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
5405 return false;
5406
5407 SDValue EltNo = N->getOperand(1);
5408 if (!isa<ConstantSDNode>(EltNo))
5409 return false;
5410
5411 // If the bit convert changed the number of elements, it is unsafe
5412 // to examine the mask.
5413 bool HasShuffleIntoBitcast = false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005414 if (V.getOpcode() == ISD::BITCAST) {
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005415 EVT SrcVT = V.getOperand(0).getValueType();
5416 if (SrcVT.getVectorNumElements() != VT.getVectorNumElements())
5417 return false;
5418 V = V.getOperand(0);
5419 HasShuffleIntoBitcast = true;
5420 }
5421
5422 // Select the input vector, guarding against out of range extract vector.
5423 unsigned NumElems = VT.getVectorNumElements();
5424 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
5425 int Idx = (Elt > NumElems) ? -1 : SVOp->getMaskElt(Elt);
5426 V = (Idx < (int)NumElems) ? V.getOperand(0) : V.getOperand(1);
5427
5428 // Skip one more bit_convert if necessary
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005429 if (V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005430 V = V.getOperand(0);
5431
5432 if (ISD::isNormalLoad(V.getNode())) {
5433 // Is the original load suitable?
5434 LoadSDNode *LN0 = cast<LoadSDNode>(V);
5435
5436 // FIXME: avoid the multi-use bug that is preventing lots of
5437 // of foldings to be detected, this is still wrong of course, but
5438 // give the temporary desired behavior, and if it happens that
5439 // the load has real more uses, during isel it will not fold, and
5440 // will generate poor code.
5441 if (!LN0 || LN0->isVolatile()) // || !LN0->hasOneUse()
5442 return false;
5443
5444 if (!HasShuffleIntoBitcast)
5445 return true;
5446
5447 // If there's a bitcast before the shuffle, check if the load type and
5448 // alignment is valid.
5449 unsigned Align = LN0->getAlignment();
5450 unsigned NewAlign =
5451 TLI.getTargetData()->getABITypeAlignment(
5452 VT.getTypeForEVT(*DAG.getContext()));
5453
5454 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
5455 return false;
5456 }
5457
5458 return true;
5459}
5460
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005461static
Evan Cheng835580f2010-10-07 20:50:20 +00005462SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
5463 EVT VT = Op.getValueType();
5464
5465 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005466 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
5467 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00005468 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
5469 V1, DAG));
5470}
5471
5472static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005473SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
5474 bool HasSSE2) {
5475 SDValue V1 = Op.getOperand(0);
5476 SDValue V2 = Op.getOperand(1);
5477 EVT VT = Op.getValueType();
5478
5479 assert(VT != MVT::v2i64 && "unsupported shuffle type");
5480
5481 if (HasSSE2 && VT == MVT::v2f64)
5482 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
5483
5484 // v4f32 or v4i32
5485 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V2, DAG);
5486}
5487
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00005488static
5489SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
5490 SDValue V1 = Op.getOperand(0);
5491 SDValue V2 = Op.getOperand(1);
5492 EVT VT = Op.getValueType();
5493
5494 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
5495 "unsupported shuffle type");
5496
5497 if (V2.getOpcode() == ISD::UNDEF)
5498 V2 = V1;
5499
5500 // v4i32 or v4f32
5501 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
5502}
5503
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005504static
5505SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasSSE2) {
5506 SDValue V1 = Op.getOperand(0);
5507 SDValue V2 = Op.getOperand(1);
5508 EVT VT = Op.getValueType();
5509 unsigned NumElems = VT.getVectorNumElements();
5510
5511 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
5512 // operand of these instructions is only memory, so check if there's a
5513 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
5514 // same masks.
5515 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005516
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00005517 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005518 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005519 CanFoldLoad = true;
5520
5521 // When V1 is a load, it can be folded later into a store in isel, example:
5522 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
5523 // turns into:
5524 // (MOVLPSmr addr:$src1, VR128:$src2)
5525 // So, recognize this potential and also use MOVLPS or MOVLPD
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005526 if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005527 CanFoldLoad = true;
5528
Eric Christopher893a8822011-02-20 05:04:42 +00005529 // Both of them can't be memory operations though.
5530 if (MayFoldVectorLoad(V1) && MayFoldVectorLoad(V2))
5531 CanFoldLoad = false;
Owen Anderson95771af2011-02-25 21:41:48 +00005532
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005533 if (CanFoldLoad) {
5534 if (HasSSE2 && NumElems == 2)
5535 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
5536
5537 if (NumElems == 4)
5538 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
5539 }
5540
5541 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5542 // movl and movlp will both match v2i64, but v2i64 is never matched by
5543 // movl earlier because we make it strict to avoid messing with the movlp load
5544 // folding logic (see the code above getMOVLP call). Match it here then,
5545 // this is horrible, but will stay like this until we move all shuffle
5546 // matching to x86 specific nodes. Note that for the 1st condition all
5547 // types are matched with movsd.
5548 if ((HasSSE2 && NumElems == 2) || !X86::isMOVLMask(SVOp))
5549 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
5550 else if (HasSSE2)
5551 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
5552
5553
5554 assert(VT != MVT::v4i32 && "unsupported shuffle type");
5555
5556 // Invert the operand order and use SHUFPS to match it.
5557 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V2, V1,
5558 X86::getShuffleSHUFImmediate(SVOp), DAG);
5559}
5560
David Greenec4db4e52011-02-28 19:06:56 +00005561static inline unsigned getUNPCKLOpcode(EVT VT, const X86Subtarget *Subtarget) {
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005562 switch(VT.getSimpleVT().SimpleTy) {
5563 case MVT::v4i32: return X86ISD::PUNPCKLDQ;
5564 case MVT::v2i64: return X86ISD::PUNPCKLQDQ;
David Greenec4db4e52011-02-28 19:06:56 +00005565 case MVT::v4f32:
5566 return Subtarget->hasAVX() ? X86ISD::VUNPCKLPS : X86ISD::UNPCKLPS;
5567 case MVT::v2f64:
5568 return Subtarget->hasAVX() ? X86ISD::VUNPCKLPD : X86ISD::UNPCKLPD;
5569 case MVT::v8f32: return X86ISD::VUNPCKLPSY;
5570 case MVT::v4f64: return X86ISD::VUNPCKLPDY;
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005571 case MVT::v16i8: return X86ISD::PUNPCKLBW;
5572 case MVT::v8i16: return X86ISD::PUNPCKLWD;
5573 default:
Eric Christopherdd6e40a2011-02-19 03:19:09 +00005574 llvm_unreachable("Unknown type for unpckl");
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005575 }
5576 return 0;
5577}
5578
5579static inline unsigned getUNPCKHOpcode(EVT VT) {
5580 switch(VT.getSimpleVT().SimpleTy) {
5581 case MVT::v4i32: return X86ISD::PUNPCKHDQ;
5582 case MVT::v2i64: return X86ISD::PUNPCKHQDQ;
5583 case MVT::v4f32: return X86ISD::UNPCKHPS;
5584 case MVT::v2f64: return X86ISD::UNPCKHPD;
5585 case MVT::v16i8: return X86ISD::PUNPCKHBW;
5586 case MVT::v8i16: return X86ISD::PUNPCKHWD;
5587 default:
Eric Christopherdd6e40a2011-02-19 03:19:09 +00005588 llvm_unreachable("Unknown type for unpckh");
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005589 }
5590 return 0;
5591}
5592
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005593static
5594SDValue NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG,
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005595 const TargetLowering &TLI,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005596 const X86Subtarget *Subtarget) {
5597 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5598 EVT VT = Op.getValueType();
5599 DebugLoc dl = Op.getDebugLoc();
5600 SDValue V1 = Op.getOperand(0);
5601 SDValue V2 = Op.getOperand(1);
5602
5603 if (isZeroShuffle(SVOp))
5604 return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
5605
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005606 // Handle splat operations
5607 if (SVOp->isSplat()) {
5608 // Special case, this is the only place now where it's
5609 // allowed to return a vector_shuffle operation without
5610 // using a target specific node, because *hopefully* it
5611 // will be optimized away by the dag combiner.
5612 if (VT.getVectorNumElements() <= 4 &&
5613 CanXFormVExtractWithShuffleIntoLoad(Op, DAG, TLI))
5614 return Op;
5615
5616 // Handle splats by matching through known masks
5617 if (VT.getVectorNumElements() <= 4)
5618 return SDValue();
5619
Evan Cheng835580f2010-10-07 20:50:20 +00005620 // Canonicalize all of the remaining to v4f32.
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005621 return PromoteSplat(SVOp, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005622 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005623
5624 // If the shuffle can be profitably rewritten as a narrower shuffle, then
5625 // do it!
5626 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
5627 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5628 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005629 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005630 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
5631 // FIXME: Figure out a cleaner way to do this.
5632 // Try to make use of movq to zero out the top part.
5633 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
5634 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5635 if (NewOp.getNode()) {
5636 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
5637 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
5638 DAG, Subtarget, dl);
5639 }
5640 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
5641 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5642 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
5643 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
5644 DAG, Subtarget, dl);
5645 }
5646 }
5647 return SDValue();
5648}
5649
Dan Gohman475871a2008-07-27 21:46:04 +00005650SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005651X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00005652 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00005653 SDValue V1 = Op.getOperand(0);
5654 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00005655 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005656 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00005657 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005658 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005659 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
5660 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00005661 bool V1IsSplat = false;
5662 bool V2IsSplat = false;
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005663 bool HasSSE2 = Subtarget->hasSSE2() || Subtarget->hasAVX();
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00005664 bool HasSSE3 = Subtarget->hasSSE3() || Subtarget->hasAVX();
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00005665 bool HasSSSE3 = Subtarget->hasSSSE3() || Subtarget->hasAVX();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005666 MachineFunction &MF = DAG.getMachineFunction();
5667 bool OptForSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005668
Dale Johannesen0488fb62010-09-30 23:57:10 +00005669 // Shuffle operations on MMX not supported.
5670 if (isMMX)
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00005671 return Op;
5672
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005673 // Vector shuffle lowering takes 3 steps:
5674 //
5675 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
5676 // narrowing and commutation of operands should be handled.
5677 // 2) Matching of shuffles with known shuffle masks to x86 target specific
5678 // shuffle nodes.
5679 // 3) Rewriting of unmatched masks into new generic shuffle operations,
5680 // so the shuffle can be broken into other shuffles and the legalizer can
5681 // try the lowering again.
5682 //
5683 // The general ideia is that no vector_shuffle operation should be left to
5684 // be matched during isel, all of them must be converted to a target specific
5685 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00005686
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005687 // Normalize the input vectors. Here splats, zeroed vectors, profitable
5688 // narrowing and commutation of operands should be handled. The actual code
5689 // doesn't include all of those, work in progress...
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005690 SDValue NewOp = NormalizeVectorShuffle(Op, DAG, *this, Subtarget);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005691 if (NewOp.getNode())
5692 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00005693
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005694 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
5695 // unpckh_undef). Only use pshufd if speed is more important than size.
5696 if (OptForSize && X86::isUNPCKL_v_undef_Mask(SVOp))
5697 if (VT != MVT::v2i64 && VT != MVT::v2f64)
David Greenec4db4e52011-02-28 19:06:56 +00005698 return getTargetShuffleNode(getUNPCKLOpcode(VT, getSubtarget()), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005699 if (OptForSize && X86::isUNPCKH_v_undef_Mask(SVOp))
5700 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5701 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00005702
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005703 if (X86::isMOVDDUPMask(SVOp) && HasSSE3 && V2IsUndef &&
Dale Johannesen0488fb62010-09-30 23:57:10 +00005704 RelaxedMayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00005705 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005706
Dale Johannesen0488fb62010-09-30 23:57:10 +00005707 if (X86::isMOVHLPS_v_undef_Mask(SVOp))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005708 return getMOVHighToLow(Op, dl, DAG);
5709
5710 // Use to match splats
5711 if (HasSSE2 && X86::isUNPCKHMask(SVOp) && V2IsUndef &&
5712 (VT == MVT::v2f64 || VT == MVT::v2i64))
5713 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
5714
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005715 if (X86::isPSHUFDMask(SVOp)) {
5716 // The actual implementation will match the mask in the if above and then
5717 // during isel it can match several different instructions, not only pshufd
5718 // as its name says, sad but true, emulate the behavior for now...
5719 if (X86::isMOVDDUPMask(SVOp) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
5720 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
5721
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005722 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
5723
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005724 if (HasSSE2 && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005725 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
5726
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005727 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005728 return getTargetShuffleNode(X86ISD::SHUFPD, dl, VT, V1, V1,
5729 TargetMask, DAG);
5730
5731 if (VT == MVT::v4f32)
5732 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V1, V1,
5733 TargetMask, DAG);
5734 }
Eric Christopherfd179292009-08-27 18:07:15 +00005735
Evan Chengf26ffe92008-05-29 08:22:04 +00005736 // Check if this can be converted into a logical shift.
5737 bool isLeft = false;
5738 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00005739 SDValue ShVal;
Nate Begeman9008ca62009-04-27 18:41:29 +00005740 bool isShift = getSubtarget()->hasSSE2() &&
Evan Chengc3630942009-12-09 21:00:30 +00005741 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00005742 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00005743 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00005744 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00005745 EVT EltVT = VT.getVectorElementType();
5746 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00005747 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005748 }
Eric Christopherfd179292009-08-27 18:07:15 +00005749
Nate Begeman9008ca62009-04-27 18:41:29 +00005750 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005751 if (V1IsUndef)
5752 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00005753 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00005754 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Dale Johannesen0488fb62010-09-30 23:57:10 +00005755 if (!X86::isMOVLPMask(SVOp)) {
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005756 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00005757 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
5758
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005759 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00005760 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
5761 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00005762 }
Eric Christopherfd179292009-08-27 18:07:15 +00005763
Nate Begeman9008ca62009-04-27 18:41:29 +00005764 // FIXME: fold these into legal mask.
Dale Johannesen0488fb62010-09-30 23:57:10 +00005765 if (X86::isMOVLHPSMask(SVOp) && !X86::isUNPCKLMask(SVOp))
5766 return getMOVLowToHigh(Op, dl, DAG, HasSSE2);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005767
Dale Johannesen0488fb62010-09-30 23:57:10 +00005768 if (X86::isMOVHLPSMask(SVOp))
5769 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00005770
Dale Johannesen0488fb62010-09-30 23:57:10 +00005771 if (X86::isMOVSHDUPMask(SVOp) && HasSSE3 && V2IsUndef && NumElems == 4)
5772 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00005773
Dale Johannesen0488fb62010-09-30 23:57:10 +00005774 if (X86::isMOVSLDUPMask(SVOp) && HasSSE3 && V2IsUndef && NumElems == 4)
5775 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00005776
Dale Johannesen0488fb62010-09-30 23:57:10 +00005777 if (X86::isMOVLPMask(SVOp))
5778 return getMOVLP(Op, dl, DAG, HasSSE2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005779
Nate Begeman9008ca62009-04-27 18:41:29 +00005780 if (ShouldXformToMOVHLPS(SVOp) ||
5781 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
5782 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005783
Evan Chengf26ffe92008-05-29 08:22:04 +00005784 if (isShift) {
5785 // No better options. Use a vshl / vsrl.
Dan Gohman8a55ce42009-09-23 21:02:20 +00005786 EVT EltVT = VT.getVectorElementType();
5787 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00005788 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005789 }
Eric Christopherfd179292009-08-27 18:07:15 +00005790
Evan Cheng9eca5e82006-10-25 21:49:50 +00005791 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00005792 // FIXME: This should also accept a bitcast of a splat? Be careful, not
5793 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00005794 V1IsSplat = isSplatVector(V1.getNode());
5795 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00005796
Chris Lattner8a594482007-11-25 00:24:49 +00005797 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00005798 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005799 Op = CommuteVectorShuffle(SVOp, DAG);
5800 SVOp = cast<ShuffleVectorSDNode>(Op);
5801 V1 = SVOp->getOperand(0);
5802 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00005803 std::swap(V1IsSplat, V2IsSplat);
5804 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00005805 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00005806 }
5807
Nate Begeman9008ca62009-04-27 18:41:29 +00005808 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
5809 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00005810 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00005811 return V1;
5812 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
5813 // the instruction selector will not match, so get a canonical MOVL with
5814 // swapped operands to undo the commute.
5815 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00005816 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005817
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005818 if (X86::isUNPCKLMask(SVOp))
David Greenec4db4e52011-02-28 19:06:56 +00005819 return getTargetShuffleNode(getUNPCKLOpcode(VT, getSubtarget()),
5820 dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005821
5822 if (X86::isUNPCKHMask(SVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005823 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00005824
Evan Cheng9bbbb982006-10-25 20:48:19 +00005825 if (V2IsSplat) {
5826 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005827 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00005828 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00005829 SDValue NewMask = NormalizeMask(SVOp, DAG);
5830 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
5831 if (NSVOp != SVOp) {
5832 if (X86::isUNPCKLMask(NSVOp, true)) {
5833 return NewMask;
5834 } else if (X86::isUNPCKHMask(NSVOp, true)) {
5835 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005836 }
5837 }
5838 }
5839
Evan Cheng9eca5e82006-10-25 21:49:50 +00005840 if (Commuted) {
5841 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00005842 // FIXME: this seems wrong.
5843 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
5844 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005845
5846 if (X86::isUNPCKLMask(NewSVOp))
David Greenec4db4e52011-02-28 19:06:56 +00005847 return getTargetShuffleNode(getUNPCKLOpcode(VT, getSubtarget()),
5848 dl, VT, V2, V1, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005849
5850 if (X86::isUNPCKHMask(NewSVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005851 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V2, V1, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00005852 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005853
Nate Begeman9008ca62009-04-27 18:41:29 +00005854 // Normalize the node to match x86 shuffle ops if needed
Dale Johannesen0488fb62010-09-30 23:57:10 +00005855 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
Nate Begeman9008ca62009-04-27 18:41:29 +00005856 return CommuteVectorShuffle(SVOp, DAG);
5857
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00005858 // The checks below are all present in isShuffleMaskLegal, but they are
5859 // inlined here right now to enable us to directly emit target specific
5860 // nodes, and remove one by one until they don't return Op anymore.
5861 SmallVector<int, 16> M;
5862 SVOp->getMask(M);
5863
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00005864 if (isPALIGNRMask(M, VT, HasSSSE3))
5865 return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
5866 X86::getShufflePALIGNRImmediate(SVOp),
5867 DAG);
5868
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00005869 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
5870 SVOp->getSplatIndex() == 0 && V2IsUndef) {
David Greenec4db4e52011-02-28 19:06:56 +00005871 if (VT == MVT::v2f64) {
5872 X86ISD::NodeType Opcode =
5873 getSubtarget()->hasAVX() ? X86ISD::VUNPCKLPD : X86ISD::UNPCKLPD;
5874 return getTargetShuffleNode(Opcode, dl, VT, V1, V1, DAG);
5875 }
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00005876 if (VT == MVT::v2i64)
5877 return getTargetShuffleNode(X86ISD::PUNPCKLQDQ, dl, VT, V1, V1, DAG);
5878 }
5879
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00005880 if (isPSHUFHWMask(M, VT))
5881 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
5882 X86::getShufflePSHUFHWImmediate(SVOp),
5883 DAG);
5884
5885 if (isPSHUFLWMask(M, VT))
5886 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
5887 X86::getShufflePSHUFLWImmediate(SVOp),
5888 DAG);
5889
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00005890 if (isSHUFPMask(M, VT)) {
5891 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
5892 if (VT == MVT::v4f32 || VT == MVT::v4i32)
5893 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V1, V2,
5894 TargetMask, DAG);
5895 if (VT == MVT::v2f64 || VT == MVT::v2i64)
5896 return getTargetShuffleNode(X86ISD::SHUFPD, dl, VT, V1, V2,
5897 TargetMask, DAG);
5898 }
5899
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005900 if (X86::isUNPCKL_v_undef_Mask(SVOp))
5901 if (VT != MVT::v2i64 && VT != MVT::v2f64)
David Greenec4db4e52011-02-28 19:06:56 +00005902 return getTargetShuffleNode(getUNPCKLOpcode(VT, getSubtarget()),
5903 dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005904 if (X86::isUNPCKH_v_undef_Mask(SVOp))
5905 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5906 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
5907
Evan Cheng14b32e12007-12-11 01:46:18 +00005908 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
Owen Anderson825b72b2009-08-11 20:47:22 +00005909 if (VT == MVT::v8i16) {
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005910 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00005911 if (NewOp.getNode())
Evan Cheng14b32e12007-12-11 01:46:18 +00005912 return NewOp;
5913 }
5914
Owen Anderson825b72b2009-08-11 20:47:22 +00005915 if (VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005916 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005917 if (NewOp.getNode())
5918 return NewOp;
5919 }
Eric Christopherfd179292009-08-27 18:07:15 +00005920
Dale Johannesen0488fb62010-09-30 23:57:10 +00005921 // Handle all 4 wide cases with a number of shuffles.
5922 if (NumElems == 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00005923 return LowerVECTOR_SHUFFLE_4wide(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005924
Dan Gohman475871a2008-07-27 21:46:04 +00005925 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005926}
5927
Dan Gohman475871a2008-07-27 21:46:04 +00005928SDValue
5929X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00005930 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005931 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005932 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005933 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005934 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005935 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00005936 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005937 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00005938 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005939 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00005940 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
5941 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
5942 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005943 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
5944 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005945 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005946 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00005947 Op.getOperand(0)),
5948 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00005949 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005950 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00005951 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005952 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00005953 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Anderson825b72b2009-08-11 20:47:22 +00005954 } else if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00005955 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
5956 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00005957 // result has a single use which is a store or a bitcast to i32. And in
5958 // the case of a store, it's not worth it if the index is a constant 0,
5959 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00005960 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00005961 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00005962 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00005963 if ((User->getOpcode() != ISD::STORE ||
5964 (isa<ConstantSDNode>(Op.getOperand(1)) &&
5965 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005966 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00005967 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00005968 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00005969 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005970 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00005971 Op.getOperand(0)),
5972 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005973 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Owen Anderson825b72b2009-08-11 20:47:22 +00005974 } else if (VT == MVT::i32) {
Mon P Wangf0fcdd82009-01-15 21:10:20 +00005975 // ExtractPS works with constant index.
5976 if (isa<ConstantSDNode>(Op.getOperand(1)))
5977 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00005978 }
Dan Gohman475871a2008-07-27 21:46:04 +00005979 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005980}
5981
5982
Dan Gohman475871a2008-07-27 21:46:04 +00005983SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005984X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
5985 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005986 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00005987 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005988
David Greene74a579d2011-02-10 16:57:36 +00005989 SDValue Vec = Op.getOperand(0);
5990 EVT VecVT = Vec.getValueType();
5991
5992 // If this is a 256-bit vector result, first extract the 128-bit
5993 // vector and then extract from the 128-bit vector.
5994 if (VecVT.getSizeInBits() > 128) {
5995 DebugLoc dl = Op.getNode()->getDebugLoc();
5996 unsigned NumElems = VecVT.getVectorNumElements();
5997 SDValue Idx = Op.getOperand(1);
5998
5999 if (!isa<ConstantSDNode>(Idx))
6000 return SDValue();
6001
6002 unsigned ExtractNumElems = NumElems / (VecVT.getSizeInBits() / 128);
6003 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
6004
6005 // Get the 128-bit vector.
6006 bool Upper = IdxVal >= ExtractNumElems;
6007 Vec = Extract128BitVector(Vec, Idx, DAG, dl);
6008
6009 // Extract from it.
6010 SDValue ScaledIdx = Idx;
6011 if (Upper)
6012 ScaledIdx = DAG.getNode(ISD::SUB, dl, Idx.getValueType(), Idx,
6013 DAG.getConstant(ExtractNumElems,
6014 Idx.getValueType()));
6015 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, Op.getValueType(), Vec,
6016 ScaledIdx);
6017 }
6018
6019 assert(Vec.getValueSizeInBits() <= 128 && "Unexpected vector length");
6020
Evan Cheng62a3f152008-03-24 21:52:23 +00006021 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006022 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006023 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00006024 return Res;
6025 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00006026
Owen Andersone50ed302009-08-10 22:56:29 +00006027 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006028 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006029 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006030 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00006031 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006032 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00006033 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006034 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6035 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006036 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006037 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00006038 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006039 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00006040 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00006041 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00006042 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00006043 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00006044 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006045 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006046 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006047 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006048 if (Idx == 0)
6049 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00006050
Evan Cheng0db9fe62006-04-25 20:13:52 +00006051 // SHUFPS the element to the lowest double word, then movss.
Nate Begeman9008ca62009-04-27 18:41:29 +00006052 int Mask[4] = { Idx, -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006053 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006054 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00006055 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00006056 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00006057 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00006058 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00006059 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
6060 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
6061 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006062 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006063 if (Idx == 0)
6064 return Op;
6065
6066 // UNPCKHPD the element to the lowest double word, then movsd.
6067 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
6068 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00006069 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006070 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006071 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00006072 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00006073 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00006074 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006075 }
6076
Dan Gohman475871a2008-07-27 21:46:04 +00006077 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006078}
6079
Dan Gohman475871a2008-07-27 21:46:04 +00006080SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006081X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
6082 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006083 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006084 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006085 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006086
Dan Gohman475871a2008-07-27 21:46:04 +00006087 SDValue N0 = Op.getOperand(0);
6088 SDValue N1 = Op.getOperand(1);
6089 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00006090
Dan Gohman8a55ce42009-09-23 21:02:20 +00006091 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00006092 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00006093 unsigned Opc;
6094 if (VT == MVT::v8i16)
6095 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00006096 else if (VT == MVT::v16i8)
6097 Opc = X86ISD::PINSRB;
6098 else
6099 Opc = X86ISD::PINSRB;
6100
Nate Begeman14d12ca2008-02-11 04:19:36 +00006101 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
6102 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00006103 if (N1.getValueType() != MVT::i32)
6104 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
6105 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006106 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00006107 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00006108 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00006109 // Bits [7:6] of the constant are the source select. This will always be
6110 // zero here. The DAG Combiner may combine an extract_elt index into these
6111 // bits. For example (insert (extract, 3), 2) could be matched by putting
6112 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00006113 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00006114 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00006115 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00006116 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006117 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00006118 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00006119 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00006120 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00006121 } else if (EltVT == MVT::i32 && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00006122 // PINSR* works with constant index.
6123 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00006124 }
Dan Gohman475871a2008-07-27 21:46:04 +00006125 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006126}
6127
Dan Gohman475871a2008-07-27 21:46:04 +00006128SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006129X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006130 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006131 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006132
David Greene6b381262011-02-09 15:32:06 +00006133 DebugLoc dl = Op.getDebugLoc();
6134 SDValue N0 = Op.getOperand(0);
6135 SDValue N1 = Op.getOperand(1);
6136 SDValue N2 = Op.getOperand(2);
6137
6138 // If this is a 256-bit vector result, first insert into a 128-bit
6139 // vector and then insert into the 256-bit vector.
6140 if (VT.getSizeInBits() > 128) {
6141 if (!isa<ConstantSDNode>(N2))
6142 return SDValue();
6143
6144 // Get the 128-bit vector.
6145 unsigned NumElems = VT.getVectorNumElements();
6146 unsigned IdxVal = cast<ConstantSDNode>(N2)->getZExtValue();
6147 bool Upper = IdxVal >= NumElems / 2;
6148
6149 SDValue SubN0 = Extract128BitVector(N0, N2, DAG, dl);
6150
6151 // Insert into it.
6152 SDValue ScaledN2 = N2;
6153 if (Upper)
6154 ScaledN2 = DAG.getNode(ISD::SUB, dl, N2.getValueType(), N2,
Owen Anderson95771af2011-02-25 21:41:48 +00006155 DAG.getConstant(NumElems /
David Greene6b381262011-02-09 15:32:06 +00006156 (VT.getSizeInBits() / 128),
6157 N2.getValueType()));
6158 Op = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, SubN0.getValueType(), SubN0,
6159 N1, ScaledN2);
6160
6161 // Insert the 128-bit vector
6162 // FIXME: Why UNDEF?
6163 return Insert128BitVector(N0, Op, N2, DAG, dl);
6164 }
6165
Nate Begeman14d12ca2008-02-11 04:19:36 +00006166 if (Subtarget->hasSSE41())
6167 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
6168
Dan Gohman8a55ce42009-09-23 21:02:20 +00006169 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00006170 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00006171
Dan Gohman8a55ce42009-09-23 21:02:20 +00006172 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00006173 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
6174 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00006175 if (N1.getValueType() != MVT::i32)
6176 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
6177 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006178 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00006179 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006180 }
Dan Gohman475871a2008-07-27 21:46:04 +00006181 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006182}
6183
Dan Gohman475871a2008-07-27 21:46:04 +00006184SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006185X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) const {
David Greene2fcdfb42011-02-10 23:11:29 +00006186 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006187 DebugLoc dl = Op.getDebugLoc();
David Greene2fcdfb42011-02-10 23:11:29 +00006188 EVT OpVT = Op.getValueType();
6189
6190 // If this is a 256-bit vector result, first insert into a 128-bit
6191 // vector and then insert into the 256-bit vector.
6192 if (OpVT.getSizeInBits() > 128) {
6193 // Insert into a 128-bit vector.
6194 EVT VT128 = EVT::getVectorVT(*Context,
6195 OpVT.getVectorElementType(),
6196 OpVT.getVectorNumElements() / 2);
6197
6198 Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT128, Op.getOperand(0));
6199
6200 // Insert the 128-bit vector.
6201 return Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, OpVT), Op,
6202 DAG.getConstant(0, MVT::i32),
6203 DAG, dl);
6204 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00006205
Chris Lattnerf172ecd2010-07-04 23:07:25 +00006206 if (Op.getValueType() == MVT::v1i64 &&
6207 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00006208 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00006209
Owen Anderson825b72b2009-08-11 20:47:22 +00006210 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Dale Johannesen0488fb62010-09-30 23:57:10 +00006211 assert(Op.getValueType().getSimpleVT().getSizeInBits() == 128 &&
6212 "Expected an SSE type!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006213 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(),
Dale Johannesen0488fb62010-09-30 23:57:10 +00006214 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006215}
6216
David Greene91585092011-01-26 15:38:49 +00006217// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
6218// a simple subregister reference or explicit instructions to grab
6219// upper bits of a vector.
6220SDValue
6221X86TargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
6222 if (Subtarget->hasAVX()) {
David Greenea5f26012011-02-07 19:36:54 +00006223 DebugLoc dl = Op.getNode()->getDebugLoc();
6224 SDValue Vec = Op.getNode()->getOperand(0);
6225 SDValue Idx = Op.getNode()->getOperand(1);
6226
6227 if (Op.getNode()->getValueType(0).getSizeInBits() == 128
6228 && Vec.getNode()->getValueType(0).getSizeInBits() == 256) {
6229 return Extract128BitVector(Vec, Idx, DAG, dl);
6230 }
David Greene91585092011-01-26 15:38:49 +00006231 }
6232 return SDValue();
6233}
6234
David Greenecfe33c42011-01-26 19:13:22 +00006235// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
6236// simple superregister reference or explicit instructions to insert
6237// the upper bits of a vector.
6238SDValue
6239X86TargetLowering::LowerINSERT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
6240 if (Subtarget->hasAVX()) {
6241 DebugLoc dl = Op.getNode()->getDebugLoc();
6242 SDValue Vec = Op.getNode()->getOperand(0);
6243 SDValue SubVec = Op.getNode()->getOperand(1);
6244 SDValue Idx = Op.getNode()->getOperand(2);
6245
6246 if (Op.getNode()->getValueType(0).getSizeInBits() == 256
6247 && SubVec.getNode()->getValueType(0).getSizeInBits() == 128) {
David Greenea5f26012011-02-07 19:36:54 +00006248 return Insert128BitVector(Vec, SubVec, Idx, DAG, dl);
David Greenecfe33c42011-01-26 19:13:22 +00006249 }
6250 }
6251 return SDValue();
6252}
6253
Bill Wendling056292f2008-09-16 21:48:12 +00006254// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
6255// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
6256// one of the above mentioned nodes. It has to be wrapped because otherwise
6257// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
6258// be used to form addressing mode. These wrapped nodes will be selected
6259// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00006260SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006261X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006262 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00006263
Chris Lattner41621a22009-06-26 19:22:52 +00006264 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6265 // global base reg.
6266 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006267 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006268 CodeModel::Model M = getTargetMachine().getCodeModel();
6269
Chris Lattner4f066492009-07-11 20:29:19 +00006270 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006271 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00006272 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00006273 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006274 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00006275 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006276 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00006277
Evan Cheng1606e8e2009-03-13 07:51:59 +00006278 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00006279 CP->getAlignment(),
6280 CP->getOffset(), OpFlag);
6281 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00006282 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006283 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00006284 if (OpFlag) {
6285 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00006286 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006287 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006288 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006289 }
6290
6291 return Result;
6292}
6293
Dan Gohmand858e902010-04-17 15:26:15 +00006294SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00006295 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00006296
Chris Lattner18c59872009-06-27 04:16:01 +00006297 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6298 // global base reg.
6299 unsigned char OpFlag = 0;
6300 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006301 CodeModel::Model M = getTargetMachine().getCodeModel();
6302
Chris Lattner4f066492009-07-11 20:29:19 +00006303 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006304 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00006305 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00006306 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006307 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00006308 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006309 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00006310
Chris Lattner18c59872009-06-27 04:16:01 +00006311 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
6312 OpFlag);
6313 DebugLoc DL = JT->getDebugLoc();
6314 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00006315
Chris Lattner18c59872009-06-27 04:16:01 +00006316 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00006317 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00006318 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6319 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006320 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00006321 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00006322
Chris Lattner18c59872009-06-27 04:16:01 +00006323 return Result;
6324}
6325
6326SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006327X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00006328 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00006329
Chris Lattner18c59872009-06-27 04:16:01 +00006330 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6331 // global base reg.
6332 unsigned char OpFlag = 0;
6333 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006334 CodeModel::Model M = getTargetMachine().getCodeModel();
6335
Chris Lattner4f066492009-07-11 20:29:19 +00006336 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006337 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00006338 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00006339 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006340 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00006341 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006342 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00006343
Chris Lattner18c59872009-06-27 04:16:01 +00006344 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00006345
Chris Lattner18c59872009-06-27 04:16:01 +00006346 DebugLoc DL = Op.getDebugLoc();
6347 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00006348
6349
Chris Lattner18c59872009-06-27 04:16:01 +00006350 // With PIC, the address is actually $g + Offset.
6351 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00006352 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00006353 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6354 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006355 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00006356 Result);
6357 }
Eric Christopherfd179292009-08-27 18:07:15 +00006358
Chris Lattner18c59872009-06-27 04:16:01 +00006359 return Result;
6360}
6361
Dan Gohman475871a2008-07-27 21:46:04 +00006362SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006363X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00006364 // Create the TargetBlockAddressAddress node.
6365 unsigned char OpFlags =
6366 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00006367 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00006368 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +00006369 DebugLoc dl = Op.getDebugLoc();
6370 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
6371 /*isTarget=*/true, OpFlags);
6372
Dan Gohmanf705adb2009-10-30 01:28:02 +00006373 if (Subtarget->isPICStyleRIPRel() &&
6374 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00006375 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
6376 else
6377 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00006378
Dan Gohman29cbade2009-11-20 23:18:13 +00006379 // With PIC, the address is actually $g + Offset.
6380 if (isGlobalRelativeToPICBase(OpFlags)) {
6381 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
6382 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
6383 Result);
6384 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00006385
6386 return Result;
6387}
6388
6389SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00006390X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00006391 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00006392 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00006393 // Create the TargetGlobalAddress node, folding in the constant
6394 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00006395 unsigned char OpFlags =
6396 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006397 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00006398 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006399 if (OpFlags == X86II::MO_NO_FLAG &&
6400 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00006401 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00006402 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00006403 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006404 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00006405 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00006406 }
Eric Christopherfd179292009-08-27 18:07:15 +00006407
Chris Lattner4f066492009-07-11 20:29:19 +00006408 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006409 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00006410 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
6411 else
6412 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00006413
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006414 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00006415 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00006416 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
6417 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006418 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006419 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006420
Chris Lattner36c25012009-07-10 07:34:39 +00006421 // For globals that require a load from a stub to get the address, emit the
6422 // load.
6423 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00006424 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00006425 MachinePointerInfo::getGOT(), false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006426
Dan Gohman6520e202008-10-18 02:06:02 +00006427 // If there was a non-zero offset that we didn't fold, create an explicit
6428 // addition for it.
6429 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00006430 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00006431 DAG.getConstant(Offset, getPointerTy()));
6432
Evan Cheng0db9fe62006-04-25 20:13:52 +00006433 return Result;
6434}
6435
Evan Chengda43bcf2008-09-24 00:05:32 +00006436SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006437X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00006438 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00006439 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006440 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00006441}
6442
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006443static SDValue
6444GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00006445 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerb903bed2009-06-26 21:20:29 +00006446 unsigned char OperandFlags) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006447 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006448 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006449 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00006450 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006451 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00006452 GA->getOffset(),
6453 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006454 if (InFlag) {
6455 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00006456 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006457 } else {
6458 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00006459 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006460 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006461
6462 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00006463 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006464
Rafael Espindola15f1b662009-04-24 12:59:40 +00006465 SDValue Flag = Chain.getValue(1);
6466 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006467}
6468
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006469// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00006470static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006471LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006472 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00006473 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00006474 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
6475 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006476 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006477 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006478 InFlag = Chain.getValue(1);
6479
Chris Lattnerb903bed2009-06-26 21:20:29 +00006480 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006481}
6482
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006483// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00006484static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006485LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006486 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00006487 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
6488 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006489}
6490
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006491// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
6492// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00006493static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006494 const EVT PtrVT, TLSModel::Model model,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00006495 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00006496 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00006497
Chris Lattnerf93b90c2010-09-22 04:39:11 +00006498 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
6499 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
6500 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00006501
Michael J. Spencerec38de22010-10-10 22:04:20 +00006502 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00006503 DAG.getIntPtrConstant(0),
6504 MachinePointerInfo(Ptr), false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00006505
Chris Lattnerb903bed2009-06-26 21:20:29 +00006506 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006507 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
6508 // initialexec.
6509 unsigned WrapperKind = X86ISD::Wrapper;
6510 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00006511 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00006512 } else if (is64Bit) {
6513 assert(model == TLSModel::InitialExec);
6514 OperandFlags = X86II::MO_GOTTPOFF;
6515 WrapperKind = X86ISD::WrapperRIP;
6516 } else {
6517 assert(model == TLSModel::InitialExec);
6518 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00006519 }
Eric Christopherfd179292009-08-27 18:07:15 +00006520
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006521 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
6522 // exec)
Michael J. Spencerec38de22010-10-10 22:04:20 +00006523 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00006524 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00006525 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00006526 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00006527
Rafael Espindola9a580232009-02-27 13:37:18 +00006528 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00006529 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00006530 MachinePointerInfo::getGOT(), false, false, 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00006531
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006532 // The address of the thread local variable is the add of the thread
6533 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00006534 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006535}
6536
Dan Gohman475871a2008-07-27 21:46:04 +00006537SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006538X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00006539
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006540 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00006541 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00006542
Eric Christopher30ef0e52010-06-03 04:07:48 +00006543 if (Subtarget->isTargetELF()) {
6544 // TODO: implement the "local dynamic" model
6545 // TODO: implement the "initial exec"model for pic executables
Michael J. Spencerec38de22010-10-10 22:04:20 +00006546
Eric Christopher30ef0e52010-06-03 04:07:48 +00006547 // If GV is an alias then use the aliasee for determining
6548 // thread-localness.
6549 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
6550 GV = GA->resolveAliasedGlobal(false);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006551
6552 TLSModel::Model model
Eric Christopher30ef0e52010-06-03 04:07:48 +00006553 = getTLSModel(GV, getTargetMachine().getRelocationModel());
Michael J. Spencerec38de22010-10-10 22:04:20 +00006554
Eric Christopher30ef0e52010-06-03 04:07:48 +00006555 switch (model) {
6556 case TLSModel::GeneralDynamic:
6557 case TLSModel::LocalDynamic: // not implemented
6558 if (Subtarget->is64Bit())
6559 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
6560 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Michael J. Spencerec38de22010-10-10 22:04:20 +00006561
Eric Christopher30ef0e52010-06-03 04:07:48 +00006562 case TLSModel::InitialExec:
6563 case TLSModel::LocalExec:
6564 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
6565 Subtarget->is64Bit());
6566 }
6567 } else if (Subtarget->isTargetDarwin()) {
6568 // Darwin only has one model of TLS. Lower to that.
6569 unsigned char OpFlag = 0;
6570 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
6571 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006572
Eric Christopher30ef0e52010-06-03 04:07:48 +00006573 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6574 // global base reg.
6575 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
6576 !Subtarget->is64Bit();
6577 if (PIC32)
6578 OpFlag = X86II::MO_TLVP_PIC_BASE;
6579 else
6580 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006581 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00006582 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00006583 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00006584 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00006585 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006586
Eric Christopher30ef0e52010-06-03 04:07:48 +00006587 // With PIC32, the address is actually $g + Offset.
6588 if (PIC32)
6589 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6590 DAG.getNode(X86ISD::GlobalBaseReg,
6591 DebugLoc(), getPointerTy()),
6592 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006593
Eric Christopher30ef0e52010-06-03 04:07:48 +00006594 // Lowering the machine isd will make sure everything is in the right
6595 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00006596 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006597 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00006598 SDValue Args[] = { Chain, Offset };
6599 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006600
Eric Christopher30ef0e52010-06-03 04:07:48 +00006601 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
6602 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
6603 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00006604
Eric Christopher30ef0e52010-06-03 04:07:48 +00006605 // And our return value (tls address) is in the standard call return value
6606 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00006607 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
6608 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy());
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006609 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00006610
Eric Christopher30ef0e52010-06-03 04:07:48 +00006611 assert(false &&
6612 "TLS not implemented for this target.");
Eric Christopherfd179292009-08-27 18:07:15 +00006613
Torok Edwinc23197a2009-07-14 16:55:14 +00006614 llvm_unreachable("Unreachable");
Chris Lattner5867de12009-04-01 22:14:45 +00006615 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006616}
6617
Evan Cheng0db9fe62006-04-25 20:13:52 +00006618
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006619/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00006620/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00006621SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman4c1fa612008-03-03 22:22:09 +00006622 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00006623 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006624 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006625 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006626 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00006627 SDValue ShOpLo = Op.getOperand(0);
6628 SDValue ShOpHi = Op.getOperand(1);
6629 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00006630 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00006631 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00006632 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00006633
Dan Gohman475871a2008-07-27 21:46:04 +00006634 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006635 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00006636 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
6637 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006638 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006639 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
6640 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006641 }
Evan Chenge3413162006-01-09 18:33:28 +00006642
Owen Anderson825b72b2009-08-11 20:47:22 +00006643 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
6644 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00006645 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00006646 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00006647
Dan Gohman475871a2008-07-27 21:46:04 +00006648 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00006649 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00006650 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
6651 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00006652
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006653 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00006654 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
6655 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006656 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006657 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
6658 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006659 }
6660
Dan Gohman475871a2008-07-27 21:46:04 +00006661 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00006662 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006663}
Evan Chenga3195e82006-01-12 22:54:21 +00006664
Dan Gohmand858e902010-04-17 15:26:15 +00006665SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
6666 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006667 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00006668
Dale Johannesen0488fb62010-09-30 23:57:10 +00006669 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00006670 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00006671
Owen Anderson825b72b2009-08-11 20:47:22 +00006672 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00006673 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00006674
Eli Friedman36df4992009-05-27 00:47:34 +00006675 // These are really Legal; return the operand so the caller accepts it as
6676 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00006677 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00006678 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00006679 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00006680 Subtarget->is64Bit()) {
6681 return Op;
6682 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006683
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006684 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006685 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006686 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00006687 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006688 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00006689 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00006690 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006691 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006692 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00006693 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
6694}
Evan Cheng0db9fe62006-04-25 20:13:52 +00006695
Owen Andersone50ed302009-08-10 22:56:29 +00006696SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00006697 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00006698 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006699 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00006700 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00006701 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00006702 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006703 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006704 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00006705 else
Owen Anderson825b72b2009-08-11 20:47:22 +00006706 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006707
Chris Lattner492a43e2010-09-22 01:28:21 +00006708 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006709
Chris Lattner492a43e2010-09-22 01:28:21 +00006710 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
6711 MachineMemOperand *MMO =
6712 DAG.getMachineFunction()
6713 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6714 MachineMemOperand::MOLoad, ByteSize, ByteSize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006715
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006716 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00006717 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
6718 X86ISD::FILD, DL,
6719 Tys, Ops, array_lengthof(Ops),
6720 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006721
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006722 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006723 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00006724 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006725
6726 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
6727 // shouldn't be necessary except that RFP cannot be live across
6728 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006729 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00006730 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
6731 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006732 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00006733 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006734 SDValue Ops[] = {
6735 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
6736 };
Chris Lattner492a43e2010-09-22 01:28:21 +00006737 MachineMemOperand *MMO =
6738 DAG.getMachineFunction()
6739 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00006740 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006741
Chris Lattner492a43e2010-09-22 01:28:21 +00006742 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
6743 Ops, array_lengthof(Ops),
6744 Op.getValueType(), MMO);
6745 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006746 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006747 false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006748 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006749
Evan Cheng0db9fe62006-04-25 20:13:52 +00006750 return Result;
6751}
6752
Bill Wendling8b8a6362009-01-17 03:56:04 +00006753// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00006754SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
6755 SelectionDAG &DAG) const {
Bill Wendling8b8a6362009-01-17 03:56:04 +00006756 // This algorithm is not obvious. Here it is in C code, more or less:
6757 /*
6758 double uint64_to_double( uint32_t hi, uint32_t lo ) {
6759 static const __m128i exp = { 0x4330000045300000ULL, 0 };
6760 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00006761
Bill Wendling8b8a6362009-01-17 03:56:04 +00006762 // Copy ints to xmm registers.
6763 __m128i xh = _mm_cvtsi32_si128( hi );
6764 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00006765
Bill Wendling8b8a6362009-01-17 03:56:04 +00006766 // Combine into low half of a single xmm register.
6767 __m128i x = _mm_unpacklo_epi32( xh, xl );
6768 __m128d d;
6769 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00006770
Bill Wendling8b8a6362009-01-17 03:56:04 +00006771 // Merge in appropriate exponents to give the integer bits the right
6772 // magnitude.
6773 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00006774
Bill Wendling8b8a6362009-01-17 03:56:04 +00006775 // Subtract away the biases to deal with the IEEE-754 double precision
6776 // implicit 1.
6777 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00006778
Bill Wendling8b8a6362009-01-17 03:56:04 +00006779 // All conversions up to here are exact. The correctly rounded result is
6780 // calculated using the current rounding mode using the following
6781 // horizontal add.
6782 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
6783 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
6784 // store doesn't really need to be here (except
6785 // maybe to zero the other double)
6786 return sd;
6787 }
6788 */
Dale Johannesen040225f2008-10-21 23:07:49 +00006789
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006790 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00006791 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00006792
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006793 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00006794 std::vector<Constant*> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00006795 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
6796 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
6797 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
6798 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00006799 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006800 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006801
Bill Wendling8b8a6362009-01-17 03:56:04 +00006802 std::vector<Constant*> CV1;
Owen Andersona90b3dc2009-07-15 21:51:10 +00006803 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006804 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
Owen Andersona90b3dc2009-07-15 21:51:10 +00006805 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006806 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00006807 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006808 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006809
Owen Anderson825b72b2009-08-11 20:47:22 +00006810 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6811 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00006812 Op.getOperand(0),
6813 DAG.getIntPtrConstant(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006814 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6815 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00006816 Op.getOperand(0),
6817 DAG.getIntPtrConstant(0)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006818 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
6819 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00006820 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006821 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00006822 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006823 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck2);
Owen Anderson825b72b2009-08-11 20:47:22 +00006824 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00006825 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006826 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00006827 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006828
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006829 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00006830 int ShufMask[2] = { 1, -1 };
Owen Anderson825b72b2009-08-11 20:47:22 +00006831 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
6832 DAG.getUNDEF(MVT::v2f64), ShufMask);
6833 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
6834 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006835 DAG.getIntPtrConstant(0));
6836}
6837
Bill Wendling8b8a6362009-01-17 03:56:04 +00006838// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00006839SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
6840 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006841 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00006842 // FP constant to bias correct the final result.
6843 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00006844 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006845
6846 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00006847 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6848 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Bill Wendling8b8a6362009-01-17 03:56:04 +00006849 Op.getOperand(0),
6850 DAG.getIntPtrConstant(0)));
6851
Owen Anderson825b72b2009-08-11 20:47:22 +00006852 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006853 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00006854 DAG.getIntPtrConstant(0));
6855
6856 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00006857 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006858 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00006859 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006860 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006861 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00006862 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006863 MVT::v2f64, Bias)));
6864 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006865 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00006866 DAG.getIntPtrConstant(0));
6867
6868 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00006869 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006870
6871 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00006872 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00006873
Owen Anderson825b72b2009-08-11 20:47:22 +00006874 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006875 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00006876 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00006877 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006878 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00006879 }
6880
6881 // Handle final rounding.
6882 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00006883}
6884
Dan Gohmand858e902010-04-17 15:26:15 +00006885SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
6886 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00006887 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006888 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00006889
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006890 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00006891 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
6892 // the optimization here.
6893 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00006894 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00006895
Owen Andersone50ed302009-08-10 22:56:29 +00006896 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006897 EVT DstVT = Op.getValueType();
6898 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00006899 return LowerUINT_TO_FP_i64(Op, DAG);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006900 else if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00006901 return LowerUINT_TO_FP_i32(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00006902
6903 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00006904 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006905 if (SrcVT == MVT::i32) {
6906 SDValue WordOff = DAG.getConstant(4, getPointerTy());
6907 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
6908 getPointerTy(), StackSlot, WordOff);
6909 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006910 StackSlot, MachinePointerInfo(),
6911 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006912 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006913 OffsetSlot, MachinePointerInfo(),
6914 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006915 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
6916 return Fild;
6917 }
6918
6919 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
6920 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006921 StackSlot, MachinePointerInfo(),
6922 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006923 // For i64 source, we need to add the appropriate power of 2 if the input
6924 // was negative. This is the same as the optimization in
6925 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
6926 // we must be careful to do the computation in x87 extended precision, not
6927 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00006928 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
6929 MachineMemOperand *MMO =
6930 DAG.getMachineFunction()
6931 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6932 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006933
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006934 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
6935 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00006936 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
6937 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006938
6939 APInt FF(32, 0x5F800000ULL);
6940
6941 // Check whether the sign bit is set.
6942 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
6943 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
6944 ISD::SETLT);
6945
6946 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
6947 SDValue FudgePtr = DAG.getConstantPool(
6948 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
6949 getPointerTy());
6950
6951 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
6952 SDValue Zero = DAG.getIntPtrConstant(0);
6953 SDValue Four = DAG.getIntPtrConstant(4);
6954 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
6955 Zero, Four);
6956 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
6957
6958 // Load the value out, extending it from f32 to f80.
6959 // FIXME: Avoid the extend by constructing the right constant pool?
Stuart Hastingsa9011292011-02-16 16:23:55 +00006960 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00006961 FudgePtr, MachinePointerInfo::getConstantPool(),
6962 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006963 // Extend everything to 80 bits to force it to be done on x87.
6964 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
6965 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00006966}
6967
Dan Gohman475871a2008-07-27 21:46:04 +00006968std::pair<SDValue,SDValue> X86TargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00006969FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) const {
Chris Lattner07290932010-09-22 01:05:16 +00006970 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00006971
Owen Andersone50ed302009-08-10 22:56:29 +00006972 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00006973
6974 if (!IsSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006975 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
6976 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00006977 }
6978
Owen Anderson825b72b2009-08-11 20:47:22 +00006979 assert(DstTy.getSimpleVT() <= MVT::i64 &&
6980 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00006981 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00006982
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006983 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00006984 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00006985 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00006986 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00006987 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00006988 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00006989 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00006990 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006991
Evan Cheng87c89352007-10-15 20:11:21 +00006992 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
6993 // stack slot.
6994 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00006995 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00006996 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006997 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00006998
Michael J. Spencerec38de22010-10-10 22:04:20 +00006999
7000
Evan Cheng0db9fe62006-04-25 20:13:52 +00007001 unsigned Opc;
Owen Anderson825b72b2009-08-11 20:47:22 +00007002 switch (DstTy.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007003 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson825b72b2009-08-11 20:47:22 +00007004 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
7005 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
7006 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007007 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007008
Dan Gohman475871a2008-07-27 21:46:04 +00007009 SDValue Chain = DAG.getEntryNode();
7010 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00007011 EVT TheVT = Op.getOperand(0).getValueType();
7012 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007013 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00007014 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007015 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007016 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007017 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00007018 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00007019 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00007020 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00007021
Chris Lattner492a43e2010-09-22 01:28:21 +00007022 MachineMemOperand *MMO =
7023 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7024 MachineMemOperand::MOLoad, MemSize, MemSize);
7025 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
7026 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007027 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00007028 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007029 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
7030 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007031
Chris Lattner07290932010-09-22 01:05:16 +00007032 MachineMemOperand *MMO =
7033 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7034 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007035
Evan Cheng0db9fe62006-04-25 20:13:52 +00007036 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00007037 SDValue Ops[] = { Chain, Value, StackSlot };
Chris Lattner07290932010-09-22 01:05:16 +00007038 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
7039 Ops, 3, DstTy, MMO);
Evan Chengd9558e02006-01-06 00:43:03 +00007040
Chris Lattner27a6c732007-11-24 07:07:01 +00007041 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007042}
7043
Dan Gohmand858e902010-04-17 15:26:15 +00007044SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
7045 SelectionDAG &DAG) const {
Dale Johannesen0488fb62010-09-30 23:57:10 +00007046 if (Op.getValueType().isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007047 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007048
Eli Friedman948e95a2009-05-23 09:59:16 +00007049 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00007050 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00007051 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
7052 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00007053
Chris Lattner27a6c732007-11-24 07:07:01 +00007054 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007055 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00007056 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00007057}
7058
Dan Gohmand858e902010-04-17 15:26:15 +00007059SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
7060 SelectionDAG &DAG) const {
Eli Friedman948e95a2009-05-23 09:59:16 +00007061 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
7062 SDValue FIST = Vals.first, StackSlot = Vals.second;
7063 assert(FIST.getNode() && "Unexpected failure");
7064
7065 // Load the result.
7066 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00007067 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007068}
7069
Dan Gohmand858e902010-04-17 15:26:15 +00007070SDValue X86TargetLowering::LowerFABS(SDValue Op,
7071 SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007072 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007073 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007074 EVT VT = Op.getValueType();
7075 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007076 if (VT.isVector())
7077 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007078 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007079 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007080 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00007081 CV.push_back(C);
7082 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007083 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007084 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00007085 CV.push_back(C);
7086 CV.push_back(C);
7087 CV.push_back(C);
7088 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007089 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007090 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007091 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007092 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007093 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007094 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007095 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007096}
7097
Dan Gohmand858e902010-04-17 15:26:15 +00007098SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007099 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007100 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007101 EVT VT = Op.getValueType();
7102 EVT EltVT = VT;
Duncan Sandsda9ad382009-09-06 19:29:07 +00007103 if (VT.isVector())
Duncan Sands83ec4b62008-06-06 12:08:01 +00007104 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007105 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007106 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007107 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00007108 CV.push_back(C);
7109 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007110 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007111 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00007112 CV.push_back(C);
7113 CV.push_back(C);
7114 CV.push_back(C);
7115 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007116 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007117 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007118 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007119 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007120 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007121 false, false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00007122 if (VT.isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007123 return DAG.getNode(ISD::BITCAST, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007124 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007125 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007126 Op.getOperand(0)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007127 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00007128 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007129 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00007130 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00007131}
7132
Dan Gohmand858e902010-04-17 15:26:15 +00007133SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007134 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00007135 SDValue Op0 = Op.getOperand(0);
7136 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007137 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007138 EVT VT = Op.getValueType();
7139 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00007140
7141 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007142 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007143 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00007144 SrcVT = VT;
7145 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007146 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007147 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007148 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007149 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007150 }
7151
7152 // At this point the operands and the result should have the same
7153 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00007154
Evan Cheng68c47cb2007-01-05 07:55:56 +00007155 // First get the sign bit of second operand.
7156 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007157 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007158 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
7159 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007160 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007161 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
7162 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7163 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7164 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007165 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007166 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007167 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007168 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007169 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007170 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007171 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00007172
7173 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007174 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007175 // Op0 is MVT::f32, Op1 is MVT::f64.
7176 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
7177 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
7178 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007179 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00007180 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00007181 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007182 }
7183
Evan Cheng73d6cf12007-01-05 21:37:56 +00007184 // Clear first operand sign bit.
7185 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00007186 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007187 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
7188 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00007189 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007190 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
7191 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7192 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7193 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00007194 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007195 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007196 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007197 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007198 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007199 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007200 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00007201
7202 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00007203 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00007204}
7205
Dan Gohman076aee32009-03-04 19:44:21 +00007206/// Emit nodes that will be selected as "test Op0,Op0", or something
7207/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00007208SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00007209 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00007210 DebugLoc dl = Op.getDebugLoc();
7211
Dan Gohman31125812009-03-07 01:58:32 +00007212 // CF and OF aren't always set the way we want. Determine which
7213 // of these we need.
7214 bool NeedCF = false;
7215 bool NeedOF = false;
7216 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007217 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00007218 case X86::COND_A: case X86::COND_AE:
7219 case X86::COND_B: case X86::COND_BE:
7220 NeedCF = true;
7221 break;
7222 case X86::COND_G: case X86::COND_GE:
7223 case X86::COND_L: case X86::COND_LE:
7224 case X86::COND_O: case X86::COND_NO:
7225 NeedOF = true;
7226 break;
Dan Gohman31125812009-03-07 01:58:32 +00007227 }
7228
Dan Gohman076aee32009-03-04 19:44:21 +00007229 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00007230 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
7231 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007232 if (Op.getResNo() != 0 || NeedOF || NeedCF)
7233 // Emit a CMP with 0, which is the TEST pattern.
7234 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
7235 DAG.getConstant(0, Op.getValueType()));
7236
7237 unsigned Opcode = 0;
7238 unsigned NumOperands = 0;
7239 switch (Op.getNode()->getOpcode()) {
7240 case ISD::ADD:
7241 // Due to an isel shortcoming, be conservative if this add is likely to be
7242 // selected as part of a load-modify-store instruction. When the root node
7243 // in a match is a store, isel doesn't know how to remap non-chain non-flag
7244 // uses of other nodes in the match, such as the ADD in this case. This
7245 // leads to the ADD being left around and reselected, with the result being
7246 // two adds in the output. Alas, even if none our users are stores, that
7247 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
7248 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
7249 // climbing the DAG back to the root, and it doesn't seem to be worth the
7250 // effort.
7251 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Dan Gohman076aee32009-03-04 19:44:21 +00007252 UE = Op.getNode()->use_end(); UI != UE; ++UI)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007253 if (UI->getOpcode() != ISD::CopyToReg && UI->getOpcode() != ISD::SETCC)
7254 goto default_case;
7255
7256 if (ConstantSDNode *C =
7257 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
7258 // An add of one will be selected as an INC.
7259 if (C->getAPIntValue() == 1) {
7260 Opcode = X86ISD::INC;
7261 NumOperands = 1;
7262 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00007263 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007264
7265 // An add of negative one (subtract of one) will be selected as a DEC.
7266 if (C->getAPIntValue().isAllOnesValue()) {
7267 Opcode = X86ISD::DEC;
7268 NumOperands = 1;
7269 break;
7270 }
Dan Gohman076aee32009-03-04 19:44:21 +00007271 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007272
7273 // Otherwise use a regular EFLAGS-setting add.
7274 Opcode = X86ISD::ADD;
7275 NumOperands = 2;
7276 break;
7277 case ISD::AND: {
7278 // If the primary and result isn't used, don't bother using X86ISD::AND,
7279 // because a TEST instruction will be better.
7280 bool NonFlagUse = false;
7281 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
7282 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
7283 SDNode *User = *UI;
7284 unsigned UOpNo = UI.getOperandNo();
7285 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
7286 // Look pass truncate.
7287 UOpNo = User->use_begin().getOperandNo();
7288 User = *User->use_begin();
7289 }
7290
7291 if (User->getOpcode() != ISD::BRCOND &&
7292 User->getOpcode() != ISD::SETCC &&
7293 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
7294 NonFlagUse = true;
7295 break;
7296 }
Dan Gohman076aee32009-03-04 19:44:21 +00007297 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007298
7299 if (!NonFlagUse)
7300 break;
7301 }
7302 // FALL THROUGH
7303 case ISD::SUB:
7304 case ISD::OR:
7305 case ISD::XOR:
7306 // Due to the ISEL shortcoming noted above, be conservative if this op is
7307 // likely to be selected as part of a load-modify-store instruction.
7308 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
7309 UE = Op.getNode()->use_end(); UI != UE; ++UI)
7310 if (UI->getOpcode() == ISD::STORE)
7311 goto default_case;
7312
7313 // Otherwise use a regular EFLAGS-setting instruction.
7314 switch (Op.getNode()->getOpcode()) {
7315 default: llvm_unreachable("unexpected operator!");
7316 case ISD::SUB: Opcode = X86ISD::SUB; break;
7317 case ISD::OR: Opcode = X86ISD::OR; break;
7318 case ISD::XOR: Opcode = X86ISD::XOR; break;
7319 case ISD::AND: Opcode = X86ISD::AND; break;
7320 }
7321
7322 NumOperands = 2;
7323 break;
7324 case X86ISD::ADD:
7325 case X86ISD::SUB:
7326 case X86ISD::INC:
7327 case X86ISD::DEC:
7328 case X86ISD::OR:
7329 case X86ISD::XOR:
7330 case X86ISD::AND:
7331 return SDValue(Op.getNode(), 1);
7332 default:
7333 default_case:
7334 break;
Dan Gohman076aee32009-03-04 19:44:21 +00007335 }
7336
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007337 if (Opcode == 0)
7338 // Emit a CMP with 0, which is the TEST pattern.
7339 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
7340 DAG.getConstant(0, Op.getValueType()));
7341
7342 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
7343 SmallVector<SDValue, 4> Ops;
7344 for (unsigned i = 0; i != NumOperands; ++i)
7345 Ops.push_back(Op.getOperand(i));
7346
7347 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
7348 DAG.ReplaceAllUsesWith(Op, New);
7349 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00007350}
7351
7352/// Emit nodes that will be selected as "cmp Op0,Op1", or something
7353/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00007354SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00007355 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00007356 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
7357 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00007358 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00007359
7360 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00007361 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00007362}
7363
Evan Chengd40d03e2010-01-06 19:38:29 +00007364/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
7365/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00007366SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
7367 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00007368 SDValue Op0 = And.getOperand(0);
7369 SDValue Op1 = And.getOperand(1);
7370 if (Op0.getOpcode() == ISD::TRUNCATE)
7371 Op0 = Op0.getOperand(0);
7372 if (Op1.getOpcode() == ISD::TRUNCATE)
7373 Op1 = Op1.getOperand(0);
7374
Evan Chengd40d03e2010-01-06 19:38:29 +00007375 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00007376 if (Op1.getOpcode() == ISD::SHL)
7377 std::swap(Op0, Op1);
7378 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00007379 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
7380 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00007381 // If we looked past a truncate, check that it's only truncating away
7382 // known zeros.
7383 unsigned BitWidth = Op0.getValueSizeInBits();
7384 unsigned AndBitWidth = And.getValueSizeInBits();
7385 if (BitWidth > AndBitWidth) {
7386 APInt Mask = APInt::getAllOnesValue(BitWidth), Zeros, Ones;
7387 DAG.ComputeMaskedBits(Op0, Mask, Zeros, Ones);
7388 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
7389 return SDValue();
7390 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007391 LHS = Op1;
7392 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00007393 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007394 } else if (Op1.getOpcode() == ISD::Constant) {
7395 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
7396 SDValue AndLHS = Op0;
Evan Chengd40d03e2010-01-06 19:38:29 +00007397 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
7398 LHS = AndLHS.getOperand(0);
7399 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00007400 }
Evan Chengd40d03e2010-01-06 19:38:29 +00007401 }
Evan Cheng0488db92007-09-25 01:57:46 +00007402
Evan Chengd40d03e2010-01-06 19:38:29 +00007403 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00007404 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00007405 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00007406 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00007407 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00007408 // Also promote i16 to i32 for performance / code size reason.
7409 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00007410 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00007411 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00007412
Evan Chengd40d03e2010-01-06 19:38:29 +00007413 // If the operand types disagree, extend the shift amount to match. Since
7414 // BT ignores high bits (like shifts) we can use anyextend.
7415 if (LHS.getValueType() != RHS.getValueType())
7416 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00007417
Evan Chengd40d03e2010-01-06 19:38:29 +00007418 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
7419 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
7420 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7421 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00007422 }
7423
Evan Cheng54de3ea2010-01-05 06:52:31 +00007424 return SDValue();
7425}
7426
Dan Gohmand858e902010-04-17 15:26:15 +00007427SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng54de3ea2010-01-05 06:52:31 +00007428 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
7429 SDValue Op0 = Op.getOperand(0);
7430 SDValue Op1 = Op.getOperand(1);
7431 DebugLoc dl = Op.getDebugLoc();
7432 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
7433
7434 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00007435 // Lower (X & (1 << N)) == 0 to BT(X, N).
7436 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
7437 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Andrew Trickf6c39412011-03-23 23:11:02 +00007438 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00007439 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00007440 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00007441 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
7442 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
7443 if (NewSetCC.getNode())
7444 return NewSetCC;
7445 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00007446
Chris Lattner481eebc2010-12-19 21:23:48 +00007447 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
7448 // these.
7449 if (Op1.getOpcode() == ISD::Constant &&
Andrew Trickf6c39412011-03-23 23:11:02 +00007450 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
Evan Cheng2c755ba2010-02-27 07:36:59 +00007451 cast<ConstantSDNode>(Op1)->isNullValue()) &&
7452 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007453
Chris Lattner481eebc2010-12-19 21:23:48 +00007454 // If the input is a setcc, then reuse the input setcc or use a new one with
7455 // the inverted condition.
7456 if (Op0.getOpcode() == X86ISD::SETCC) {
7457 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
7458 bool Invert = (CC == ISD::SETNE) ^
7459 cast<ConstantSDNode>(Op1)->isNullValue();
7460 if (!Invert) return Op0;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007461
Evan Cheng2c755ba2010-02-27 07:36:59 +00007462 CCode = X86::GetOppositeBranchCondition(CCode);
Chris Lattner481eebc2010-12-19 21:23:48 +00007463 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7464 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
7465 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007466 }
7467
Evan Chenge5b51ac2010-04-17 06:13:15 +00007468 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00007469 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00007470 if (X86CC == X86::COND_INVALID)
7471 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00007472
Chris Lattnerc19d1c32010-12-19 22:08:31 +00007473 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00007474 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnerc19d1c32010-12-19 22:08:31 +00007475 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
Evan Cheng0488db92007-09-25 01:57:46 +00007476}
7477
Dan Gohmand858e902010-04-17 15:26:15 +00007478SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00007479 SDValue Cond;
7480 SDValue Op0 = Op.getOperand(0);
7481 SDValue Op1 = Op.getOperand(1);
7482 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00007483 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00007484 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
7485 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007486 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00007487
7488 if (isFP) {
7489 unsigned SSECC = 8;
Owen Andersone50ed302009-08-10 22:56:29 +00007490 EVT VT0 = Op0.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00007491 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
7492 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00007493 bool Swap = false;
7494
7495 switch (SetCCOpcode) {
7496 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00007497 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00007498 case ISD::SETEQ: SSECC = 0; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00007499 case ISD::SETOGT:
Nate Begeman30a0de92008-07-17 16:51:19 +00007500 case ISD::SETGT: Swap = true; // Fallthrough
7501 case ISD::SETLT:
7502 case ISD::SETOLT: SSECC = 1; break;
7503 case ISD::SETOGE:
7504 case ISD::SETGE: Swap = true; // Fallthrough
7505 case ISD::SETLE:
7506 case ISD::SETOLE: SSECC = 2; break;
7507 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00007508 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00007509 case ISD::SETNE: SSECC = 4; break;
7510 case ISD::SETULE: Swap = true;
7511 case ISD::SETUGE: SSECC = 5; break;
7512 case ISD::SETULT: Swap = true;
7513 case ISD::SETUGT: SSECC = 6; break;
7514 case ISD::SETO: SSECC = 7; break;
7515 }
7516 if (Swap)
7517 std::swap(Op0, Op1);
7518
Nate Begemanfb8ead02008-07-25 19:05:58 +00007519 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00007520 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00007521 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00007522 SDValue UNORD, EQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00007523 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
7524 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00007525 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00007526 }
7527 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00007528 SDValue ORD, NEQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00007529 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
7530 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00007531 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00007532 }
Torok Edwinc23197a2009-07-14 16:55:14 +00007533 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00007534 }
7535 // Handle all other FP comparisons here.
Owen Anderson825b72b2009-08-11 20:47:22 +00007536 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00007537 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007538
Nate Begeman30a0de92008-07-17 16:51:19 +00007539 // We are handling one of the integer comparisons here. Since SSE only has
7540 // GT and EQ comparisons for integer, swapping operands and multiple
7541 // operations may be required for some comparisons.
7542 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
7543 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007544
Owen Anderson825b72b2009-08-11 20:47:22 +00007545 switch (VT.getSimpleVT().SimpleTy) {
Nate Begeman30a0de92008-07-17 16:51:19 +00007546 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007547 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007548 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007549 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
7550 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00007551 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007552
Nate Begeman30a0de92008-07-17 16:51:19 +00007553 switch (SetCCOpcode) {
7554 default: break;
7555 case ISD::SETNE: Invert = true;
7556 case ISD::SETEQ: Opc = EQOpc; break;
7557 case ISD::SETLT: Swap = true;
7558 case ISD::SETGT: Opc = GTOpc; break;
7559 case ISD::SETGE: Swap = true;
7560 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
7561 case ISD::SETULT: Swap = true;
7562 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
7563 case ISD::SETUGE: Swap = true;
7564 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
7565 }
7566 if (Swap)
7567 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007568
Nate Begeman30a0de92008-07-17 16:51:19 +00007569 // Since SSE has no unsigned integer comparisons, we need to flip the sign
7570 // bits of the inputs before performing those operations.
7571 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00007572 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00007573 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
7574 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00007575 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00007576 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
7577 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00007578 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
7579 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00007580 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007581
Dale Johannesenace16102009-02-03 19:33:06 +00007582 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00007583
7584 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00007585 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00007586 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00007587
Nate Begeman30a0de92008-07-17 16:51:19 +00007588 return Result;
7589}
Evan Cheng0488db92007-09-25 01:57:46 +00007590
Evan Cheng370e5342008-12-03 08:38:43 +00007591// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00007592static bool isX86LogicalCmp(SDValue Op) {
7593 unsigned Opc = Op.getNode()->getOpcode();
7594 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
7595 return true;
7596 if (Op.getResNo() == 1 &&
7597 (Opc == X86ISD::ADD ||
7598 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00007599 Opc == X86ISD::ADC ||
7600 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00007601 Opc == X86ISD::SMUL ||
7602 Opc == X86ISD::UMUL ||
7603 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00007604 Opc == X86ISD::DEC ||
7605 Opc == X86ISD::OR ||
7606 Opc == X86ISD::XOR ||
7607 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00007608 return true;
7609
Chris Lattner9637d5b2010-12-05 07:49:54 +00007610 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
7611 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007612
Dan Gohman076aee32009-03-04 19:44:21 +00007613 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00007614}
7615
Chris Lattnera2b56002010-12-05 01:23:24 +00007616static bool isZero(SDValue V) {
7617 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
7618 return C && C->isNullValue();
7619}
7620
Chris Lattner96908b12010-12-05 02:00:51 +00007621static bool isAllOnes(SDValue V) {
7622 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
7623 return C && C->isAllOnesValue();
7624}
7625
Dan Gohmand858e902010-04-17 15:26:15 +00007626SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00007627 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007628 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00007629 SDValue Op1 = Op.getOperand(1);
7630 SDValue Op2 = Op.getOperand(2);
7631 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00007632 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00007633
Dan Gohman1a492952009-10-20 16:22:37 +00007634 if (Cond.getOpcode() == ISD::SETCC) {
7635 SDValue NewCond = LowerSETCC(Cond, DAG);
7636 if (NewCond.getNode())
7637 Cond = NewCond;
7638 }
Evan Cheng734503b2006-09-11 02:19:56 +00007639
Chris Lattnera2b56002010-12-05 01:23:24 +00007640 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00007641 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00007642 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00007643 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007644 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00007645 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
7646 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007647 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007648
Chris Lattnera2b56002010-12-05 01:23:24 +00007649 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007650
7651 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00007652 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
7653 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00007654
7655 SDValue CmpOp0 = Cmp.getOperand(0);
7656 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
7657 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007658
Chris Lattner96908b12010-12-05 02:00:51 +00007659 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00007660 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
7661 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007662
Chris Lattner96908b12010-12-05 02:00:51 +00007663 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
7664 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007665
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007666 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00007667 if (N2C == 0 || !N2C->isNullValue())
7668 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
7669 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007670 }
7671 }
7672
Chris Lattnera2b56002010-12-05 01:23:24 +00007673 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00007674 if (Cond.getOpcode() == ISD::AND &&
7675 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
7676 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00007677 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00007678 Cond = Cond.getOperand(0);
7679 }
7680
Evan Cheng3f41d662007-10-08 22:16:29 +00007681 // If condition flag is set by a X86ISD::CMP, then use it as the condition
7682 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00007683 if (Cond.getOpcode() == X86ISD::SETCC ||
7684 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00007685 CC = Cond.getOperand(0);
7686
Dan Gohman475871a2008-07-27 21:46:04 +00007687 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00007688 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00007689 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00007690
Evan Cheng3f41d662007-10-08 22:16:29 +00007691 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007692 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00007693 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00007694 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00007695
Chris Lattnerd1980a52009-03-12 06:52:53 +00007696 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
7697 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00007698 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00007699 addTest = false;
7700 }
7701 }
7702
7703 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007704 // Look pass the truncate.
7705 if (Cond.getOpcode() == ISD::TRUNCATE)
7706 Cond = Cond.getOperand(0);
7707
7708 // We know the result of AND is compared against zero. Try to match
7709 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00007710 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00007711 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00007712 if (NewSetCC.getNode()) {
7713 CC = NewSetCC.getOperand(0);
7714 Cond = NewSetCC.getOperand(1);
7715 addTest = false;
7716 }
7717 }
7718 }
7719
7720 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007721 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00007722 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00007723 }
7724
Benjamin Kramere915ff32010-12-22 23:09:28 +00007725 // a < b ? -1 : 0 -> RES = ~setcc_carry
7726 // a < b ? 0 : -1 -> RES = setcc_carry
7727 // a >= b ? -1 : 0 -> RES = setcc_carry
7728 // a >= b ? 0 : -1 -> RES = ~setcc_carry
7729 if (Cond.getOpcode() == X86ISD::CMP) {
7730 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
7731
7732 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
7733 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
7734 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
7735 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
7736 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
7737 return DAG.getNOT(DL, Res, Res.getValueType());
7738 return Res;
7739 }
7740 }
7741
Evan Cheng0488db92007-09-25 01:57:46 +00007742 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
7743 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007744 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007745 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00007746 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00007747}
7748
Evan Cheng370e5342008-12-03 08:38:43 +00007749// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
7750// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
7751// from the AND / OR.
7752static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
7753 Opc = Op.getOpcode();
7754 if (Opc != ISD::OR && Opc != ISD::AND)
7755 return false;
7756 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
7757 Op.getOperand(0).hasOneUse() &&
7758 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
7759 Op.getOperand(1).hasOneUse());
7760}
7761
Evan Cheng961d6d42009-02-02 08:19:07 +00007762// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
7763// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00007764static bool isXor1OfSetCC(SDValue Op) {
7765 if (Op.getOpcode() != ISD::XOR)
7766 return false;
7767 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
7768 if (N1C && N1C->getAPIntValue() == 1) {
7769 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
7770 Op.getOperand(0).hasOneUse();
7771 }
7772 return false;
7773}
7774
Dan Gohmand858e902010-04-17 15:26:15 +00007775SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00007776 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007777 SDValue Chain = Op.getOperand(0);
7778 SDValue Cond = Op.getOperand(1);
7779 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007780 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00007781 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00007782
Dan Gohman1a492952009-10-20 16:22:37 +00007783 if (Cond.getOpcode() == ISD::SETCC) {
7784 SDValue NewCond = LowerSETCC(Cond, DAG);
7785 if (NewCond.getNode())
7786 Cond = NewCond;
7787 }
Chris Lattnere55484e2008-12-25 05:34:37 +00007788#if 0
7789 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00007790 else if (Cond.getOpcode() == X86ISD::ADD ||
7791 Cond.getOpcode() == X86ISD::SUB ||
7792 Cond.getOpcode() == X86ISD::SMUL ||
7793 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00007794 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00007795#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00007796
Evan Chengad9c0a32009-12-15 00:53:42 +00007797 // Look pass (and (setcc_carry (cmp ...)), 1).
7798 if (Cond.getOpcode() == ISD::AND &&
7799 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
7800 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00007801 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00007802 Cond = Cond.getOperand(0);
7803 }
7804
Evan Cheng3f41d662007-10-08 22:16:29 +00007805 // If condition flag is set by a X86ISD::CMP, then use it as the condition
7806 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00007807 if (Cond.getOpcode() == X86ISD::SETCC ||
7808 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00007809 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007810
Dan Gohman475871a2008-07-27 21:46:04 +00007811 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00007812 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00007813 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00007814 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00007815 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00007816 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00007817 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00007818 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00007819 default: break;
7820 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00007821 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00007822 // These can only come from an arithmetic instruction with overflow,
7823 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00007824 Cond = Cond.getNode()->getOperand(1);
7825 addTest = false;
7826 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00007827 }
Evan Cheng0488db92007-09-25 01:57:46 +00007828 }
Evan Cheng370e5342008-12-03 08:38:43 +00007829 } else {
7830 unsigned CondOpc;
7831 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
7832 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00007833 if (CondOpc == ISD::OR) {
7834 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
7835 // two branches instead of an explicit OR instruction with a
7836 // separate test.
7837 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00007838 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00007839 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007840 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00007841 Chain, Dest, CC, Cmp);
7842 CC = Cond.getOperand(1).getOperand(0);
7843 Cond = Cmp;
7844 addTest = false;
7845 }
7846 } else { // ISD::AND
7847 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
7848 // two branches instead of an explicit AND instruction with a
7849 // separate test. However, we only do this if this block doesn't
7850 // have a fall-through edge, because this requires an explicit
7851 // jmp when the condition is false.
7852 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00007853 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00007854 Op.getNode()->hasOneUse()) {
7855 X86::CondCode CCode =
7856 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
7857 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007858 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00007859 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00007860 // Look for an unconditional branch following this conditional branch.
7861 // We need this because we need to reverse the successors in order
7862 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00007863 if (User->getOpcode() == ISD::BR) {
7864 SDValue FalseBB = User->getOperand(1);
7865 SDNode *NewBR =
7866 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00007867 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00007868 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00007869 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00007870
Dale Johannesene4d209d2009-02-03 20:21:25 +00007871 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00007872 Chain, Dest, CC, Cmp);
7873 X86::CondCode CCode =
7874 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
7875 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007876 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00007877 Cond = Cmp;
7878 addTest = false;
7879 }
7880 }
Dan Gohman279c22e2008-10-21 03:29:32 +00007881 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00007882 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
7883 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
7884 // It should be transformed during dag combiner except when the condition
7885 // is set by a arithmetics with overflow node.
7886 X86::CondCode CCode =
7887 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
7888 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007889 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00007890 Cond = Cond.getOperand(0).getOperand(1);
7891 addTest = false;
Dan Gohman279c22e2008-10-21 03:29:32 +00007892 }
Evan Cheng0488db92007-09-25 01:57:46 +00007893 }
7894
7895 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007896 // Look pass the truncate.
7897 if (Cond.getOpcode() == ISD::TRUNCATE)
7898 Cond = Cond.getOperand(0);
7899
7900 // We know the result of AND is compared against zero. Try to match
7901 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00007902 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007903 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
7904 if (NewSetCC.getNode()) {
7905 CC = NewSetCC.getOperand(0);
7906 Cond = NewSetCC.getOperand(1);
7907 addTest = false;
7908 }
7909 }
7910 }
7911
7912 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007913 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00007914 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00007915 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00007916 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00007917 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00007918}
7919
Anton Korobeynikove060b532007-04-17 19:34:00 +00007920
7921// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
7922// Calls to _alloca is needed to probe the stack when allocating more than 4k
7923// bytes in one go. Touching the stack at 4K increments is necessary to ensure
7924// that the guard pages used by the OS virtual memory manager are allocated in
7925// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00007926SDValue
7927X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00007928 SelectionDAG &DAG) const {
Duncan Sands1e1ca0b2010-10-21 16:02:12 +00007929 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows()) &&
Michael J. Spencere9c253e2010-10-21 01:41:01 +00007930 "This should be used only on Windows targets");
NAKAMURA Takumia2e07622011-03-24 07:07:00 +00007931 assert(!Subtarget->isTargetEnvMacho());
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007932 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007933
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007934 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00007935 SDValue Chain = Op.getOperand(0);
7936 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007937 // FIXME: Ensure alignment here
7938
Dan Gohman475871a2008-07-27 21:46:04 +00007939 SDValue Flag;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007940
Owen Anderson825b72b2009-08-11 20:47:22 +00007941 EVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
NAKAMURA Takumia2e07622011-03-24 07:07:00 +00007942 unsigned Reg = (Subtarget->is64Bit() ? X86::RAX : X86::EAX);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007943
NAKAMURA Takumia2e07622011-03-24 07:07:00 +00007944 Chain = DAG.getCopyToReg(Chain, dl, Reg, Size, Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00007945 Flag = Chain.getValue(1);
7946
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007947 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00007948
Michael J. Spencere9c253e2010-10-21 01:41:01 +00007949 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00007950 Flag = Chain.getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007951
Dale Johannesendd64c412009-02-04 00:33:20 +00007952 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007953
Dan Gohman475871a2008-07-27 21:46:04 +00007954 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007955 return DAG.getMergeValues(Ops1, 2, dl);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007956}
7957
Dan Gohmand858e902010-04-17 15:26:15 +00007958SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00007959 MachineFunction &MF = DAG.getMachineFunction();
7960 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
7961
Dan Gohman69de1932008-02-06 22:27:42 +00007962 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00007963 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00007964
Anton Korobeynikove7beda12010-10-03 22:52:07 +00007965 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00007966 // vastart just stores the address of the VarArgsFrameIndex slot into the
7967 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00007968 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
7969 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00007970 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
7971 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007972 }
7973
7974 // __va_list_tag:
7975 // gp_offset (0 - 6 * 8)
7976 // fp_offset (48 - 48 + 8 * 16)
7977 // overflow_arg_area (point to parameters coming in memory).
7978 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00007979 SmallVector<SDValue, 8> MemOps;
7980 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00007981 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00007982 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00007983 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
7984 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007985 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007986 MemOps.push_back(Store);
7987
7988 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00007989 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007990 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +00007991 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00007992 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
7993 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007994 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007995 MemOps.push_back(Store);
7996
7997 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +00007998 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007999 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00008000 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
8001 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00008002 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
8003 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +00008004 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008005 MemOps.push_back(Store);
8006
8007 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +00008008 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008009 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00008010 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
8011 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00008012 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
8013 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008014 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +00008015 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00008016 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00008017}
8018
Dan Gohmand858e902010-04-17 15:26:15 +00008019SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +00008020 assert(Subtarget->is64Bit() &&
8021 "LowerVAARG only handles 64-bit va_arg!");
8022 assert((Subtarget->isTargetLinux() ||
8023 Subtarget->isTargetDarwin()) &&
8024 "Unhandled target in LowerVAARG");
8025 assert(Op.getNode()->getNumOperands() == 4);
8026 SDValue Chain = Op.getOperand(0);
8027 SDValue SrcPtr = Op.getOperand(1);
8028 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
8029 unsigned Align = Op.getConstantOperandVal(3);
8030 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +00008031
Dan Gohman320afb82010-10-12 18:00:49 +00008032 EVT ArgVT = Op.getNode()->getValueType(0);
8033 const Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
8034 uint32_t ArgSize = getTargetData()->getTypeAllocSize(ArgTy);
8035 uint8_t ArgMode;
8036
8037 // Decide which area this value should be read from.
8038 // TODO: Implement the AMD64 ABI in its entirety. This simple
8039 // selection mechanism works only for the basic types.
8040 if (ArgVT == MVT::f80) {
8041 llvm_unreachable("va_arg for f80 not yet implemented");
8042 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
8043 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
8044 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
8045 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
8046 } else {
8047 llvm_unreachable("Unhandled argument type in LowerVAARG");
8048 }
8049
8050 if (ArgMode == 2) {
8051 // Sanity Check: Make sure using fp_offset makes sense.
Michael J. Spencer87b86652010-10-19 07:32:42 +00008052 assert(!UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +00008053 !(DAG.getMachineFunction()
8054 .getFunction()->hasFnAttr(Attribute::NoImplicitFloat)) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00008055 Subtarget->hasXMM());
Dan Gohman320afb82010-10-12 18:00:49 +00008056 }
8057
8058 // Insert VAARG_64 node into the DAG
8059 // VAARG_64 returns two values: Variable Argument Address, Chain
8060 SmallVector<SDValue, 11> InstOps;
8061 InstOps.push_back(Chain);
8062 InstOps.push_back(SrcPtr);
8063 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
8064 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
8065 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
8066 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
8067 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
8068 VTs, &InstOps[0], InstOps.size(),
8069 MVT::i64,
8070 MachinePointerInfo(SV),
8071 /*Align=*/0,
8072 /*Volatile=*/false,
8073 /*ReadMem=*/true,
8074 /*WriteMem=*/true);
8075 Chain = VAARG.getValue(1);
8076
8077 // Load the next argument and return it
8078 return DAG.getLoad(ArgVT, dl,
8079 Chain,
8080 VAARG,
8081 MachinePointerInfo(),
8082 false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +00008083}
8084
Dan Gohmand858e902010-04-17 15:26:15 +00008085SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) const {
Evan Chengae642192007-03-02 23:16:35 +00008086 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00008087 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00008088 SDValue Chain = Op.getOperand(0);
8089 SDValue DstPtr = Op.getOperand(1);
8090 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00008091 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
8092 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +00008093 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00008094
Chris Lattnere72f2022010-09-21 05:40:29 +00008095 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00008096 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +00008097 false,
Chris Lattnere72f2022010-09-21 05:40:29 +00008098 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +00008099}
8100
Dan Gohman475871a2008-07-27 21:46:04 +00008101SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00008102X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008103 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00008104 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00008105 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00008106 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00008107 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00008108 case Intrinsic::x86_sse_comieq_ss:
8109 case Intrinsic::x86_sse_comilt_ss:
8110 case Intrinsic::x86_sse_comile_ss:
8111 case Intrinsic::x86_sse_comigt_ss:
8112 case Intrinsic::x86_sse_comige_ss:
8113 case Intrinsic::x86_sse_comineq_ss:
8114 case Intrinsic::x86_sse_ucomieq_ss:
8115 case Intrinsic::x86_sse_ucomilt_ss:
8116 case Intrinsic::x86_sse_ucomile_ss:
8117 case Intrinsic::x86_sse_ucomigt_ss:
8118 case Intrinsic::x86_sse_ucomige_ss:
8119 case Intrinsic::x86_sse_ucomineq_ss:
8120 case Intrinsic::x86_sse2_comieq_sd:
8121 case Intrinsic::x86_sse2_comilt_sd:
8122 case Intrinsic::x86_sse2_comile_sd:
8123 case Intrinsic::x86_sse2_comigt_sd:
8124 case Intrinsic::x86_sse2_comige_sd:
8125 case Intrinsic::x86_sse2_comineq_sd:
8126 case Intrinsic::x86_sse2_ucomieq_sd:
8127 case Intrinsic::x86_sse2_ucomilt_sd:
8128 case Intrinsic::x86_sse2_ucomile_sd:
8129 case Intrinsic::x86_sse2_ucomigt_sd:
8130 case Intrinsic::x86_sse2_ucomige_sd:
8131 case Intrinsic::x86_sse2_ucomineq_sd: {
8132 unsigned Opc = 0;
8133 ISD::CondCode CC = ISD::SETCC_INVALID;
8134 switch (IntNo) {
8135 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008136 case Intrinsic::x86_sse_comieq_ss:
8137 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008138 Opc = X86ISD::COMI;
8139 CC = ISD::SETEQ;
8140 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00008141 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008142 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008143 Opc = X86ISD::COMI;
8144 CC = ISD::SETLT;
8145 break;
8146 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008147 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008148 Opc = X86ISD::COMI;
8149 CC = ISD::SETLE;
8150 break;
8151 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008152 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008153 Opc = X86ISD::COMI;
8154 CC = ISD::SETGT;
8155 break;
8156 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008157 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008158 Opc = X86ISD::COMI;
8159 CC = ISD::SETGE;
8160 break;
8161 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008162 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008163 Opc = X86ISD::COMI;
8164 CC = ISD::SETNE;
8165 break;
8166 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008167 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008168 Opc = X86ISD::UCOMI;
8169 CC = ISD::SETEQ;
8170 break;
8171 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008172 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008173 Opc = X86ISD::UCOMI;
8174 CC = ISD::SETLT;
8175 break;
8176 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008177 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008178 Opc = X86ISD::UCOMI;
8179 CC = ISD::SETLE;
8180 break;
8181 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008182 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008183 Opc = X86ISD::UCOMI;
8184 CC = ISD::SETGT;
8185 break;
8186 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008187 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008188 Opc = X86ISD::UCOMI;
8189 CC = ISD::SETGE;
8190 break;
8191 case Intrinsic::x86_sse_ucomineq_ss:
8192 case Intrinsic::x86_sse2_ucomineq_sd:
8193 Opc = X86ISD::UCOMI;
8194 CC = ISD::SETNE;
8195 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00008196 }
Evan Cheng734503b2006-09-11 02:19:56 +00008197
Dan Gohman475871a2008-07-27 21:46:04 +00008198 SDValue LHS = Op.getOperand(1);
8199 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00008200 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00008201 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00008202 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
8203 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8204 DAG.getConstant(X86CC, MVT::i8), Cond);
8205 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00008206 }
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008207 // ptest and testp intrinsics. The intrinsic these come from are designed to
8208 // return an integer value, not just an instruction so lower it to the ptest
8209 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00008210 case Intrinsic::x86_sse41_ptestz:
8211 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008212 case Intrinsic::x86_sse41_ptestnzc:
8213 case Intrinsic::x86_avx_ptestz_256:
8214 case Intrinsic::x86_avx_ptestc_256:
8215 case Intrinsic::x86_avx_ptestnzc_256:
8216 case Intrinsic::x86_avx_vtestz_ps:
8217 case Intrinsic::x86_avx_vtestc_ps:
8218 case Intrinsic::x86_avx_vtestnzc_ps:
8219 case Intrinsic::x86_avx_vtestz_pd:
8220 case Intrinsic::x86_avx_vtestc_pd:
8221 case Intrinsic::x86_avx_vtestnzc_pd:
8222 case Intrinsic::x86_avx_vtestz_ps_256:
8223 case Intrinsic::x86_avx_vtestc_ps_256:
8224 case Intrinsic::x86_avx_vtestnzc_ps_256:
8225 case Intrinsic::x86_avx_vtestz_pd_256:
8226 case Intrinsic::x86_avx_vtestc_pd_256:
8227 case Intrinsic::x86_avx_vtestnzc_pd_256: {
8228 bool IsTestPacked = false;
Eric Christopher71c67532009-07-29 00:28:05 +00008229 unsigned X86CC = 0;
8230 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00008231 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008232 case Intrinsic::x86_avx_vtestz_ps:
8233 case Intrinsic::x86_avx_vtestz_pd:
8234 case Intrinsic::x86_avx_vtestz_ps_256:
8235 case Intrinsic::x86_avx_vtestz_pd_256:
8236 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00008237 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008238 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +00008239 // ZF = 1
8240 X86CC = X86::COND_E;
8241 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008242 case Intrinsic::x86_avx_vtestc_ps:
8243 case Intrinsic::x86_avx_vtestc_pd:
8244 case Intrinsic::x86_avx_vtestc_ps_256:
8245 case Intrinsic::x86_avx_vtestc_pd_256:
8246 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00008247 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008248 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00008249 // CF = 1
8250 X86CC = X86::COND_B;
8251 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008252 case Intrinsic::x86_avx_vtestnzc_ps:
8253 case Intrinsic::x86_avx_vtestnzc_pd:
8254 case Intrinsic::x86_avx_vtestnzc_ps_256:
8255 case Intrinsic::x86_avx_vtestnzc_pd_256:
8256 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +00008257 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008258 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00008259 // ZF and CF = 0
8260 X86CC = X86::COND_A;
8261 break;
8262 }
Eric Christopherfd179292009-08-27 18:07:15 +00008263
Eric Christopher71c67532009-07-29 00:28:05 +00008264 SDValue LHS = Op.getOperand(1);
8265 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008266 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
8267 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00008268 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
8269 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
8270 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00008271 }
Evan Cheng5759f972008-05-04 09:15:50 +00008272
8273 // Fix vector shift instructions where the last operand is a non-immediate
8274 // i32 value.
8275 case Intrinsic::x86_sse2_pslli_w:
8276 case Intrinsic::x86_sse2_pslli_d:
8277 case Intrinsic::x86_sse2_pslli_q:
8278 case Intrinsic::x86_sse2_psrli_w:
8279 case Intrinsic::x86_sse2_psrli_d:
8280 case Intrinsic::x86_sse2_psrli_q:
8281 case Intrinsic::x86_sse2_psrai_w:
8282 case Intrinsic::x86_sse2_psrai_d:
8283 case Intrinsic::x86_mmx_pslli_w:
8284 case Intrinsic::x86_mmx_pslli_d:
8285 case Intrinsic::x86_mmx_pslli_q:
8286 case Intrinsic::x86_mmx_psrli_w:
8287 case Intrinsic::x86_mmx_psrli_d:
8288 case Intrinsic::x86_mmx_psrli_q:
8289 case Intrinsic::x86_mmx_psrai_w:
8290 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00008291 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00008292 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00008293 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00008294
8295 unsigned NewIntNo = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00008296 EVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00008297 switch (IntNo) {
8298 case Intrinsic::x86_sse2_pslli_w:
8299 NewIntNo = Intrinsic::x86_sse2_psll_w;
8300 break;
8301 case Intrinsic::x86_sse2_pslli_d:
8302 NewIntNo = Intrinsic::x86_sse2_psll_d;
8303 break;
8304 case Intrinsic::x86_sse2_pslli_q:
8305 NewIntNo = Intrinsic::x86_sse2_psll_q;
8306 break;
8307 case Intrinsic::x86_sse2_psrli_w:
8308 NewIntNo = Intrinsic::x86_sse2_psrl_w;
8309 break;
8310 case Intrinsic::x86_sse2_psrli_d:
8311 NewIntNo = Intrinsic::x86_sse2_psrl_d;
8312 break;
8313 case Intrinsic::x86_sse2_psrli_q:
8314 NewIntNo = Intrinsic::x86_sse2_psrl_q;
8315 break;
8316 case Intrinsic::x86_sse2_psrai_w:
8317 NewIntNo = Intrinsic::x86_sse2_psra_w;
8318 break;
8319 case Intrinsic::x86_sse2_psrai_d:
8320 NewIntNo = Intrinsic::x86_sse2_psra_d;
8321 break;
8322 default: {
Owen Anderson825b72b2009-08-11 20:47:22 +00008323 ShAmtVT = MVT::v2i32;
Evan Cheng5759f972008-05-04 09:15:50 +00008324 switch (IntNo) {
8325 case Intrinsic::x86_mmx_pslli_w:
8326 NewIntNo = Intrinsic::x86_mmx_psll_w;
8327 break;
8328 case Intrinsic::x86_mmx_pslli_d:
8329 NewIntNo = Intrinsic::x86_mmx_psll_d;
8330 break;
8331 case Intrinsic::x86_mmx_pslli_q:
8332 NewIntNo = Intrinsic::x86_mmx_psll_q;
8333 break;
8334 case Intrinsic::x86_mmx_psrli_w:
8335 NewIntNo = Intrinsic::x86_mmx_psrl_w;
8336 break;
8337 case Intrinsic::x86_mmx_psrli_d:
8338 NewIntNo = Intrinsic::x86_mmx_psrl_d;
8339 break;
8340 case Intrinsic::x86_mmx_psrli_q:
8341 NewIntNo = Intrinsic::x86_mmx_psrl_q;
8342 break;
8343 case Intrinsic::x86_mmx_psrai_w:
8344 NewIntNo = Intrinsic::x86_mmx_psra_w;
8345 break;
8346 case Intrinsic::x86_mmx_psrai_d:
8347 NewIntNo = Intrinsic::x86_mmx_psra_d;
8348 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00008349 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00008350 }
8351 break;
8352 }
8353 }
Mon P Wangefa42202009-09-03 19:56:25 +00008354
8355 // The vector shift intrinsics with scalars uses 32b shift amounts but
8356 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
8357 // to be zero.
8358 SDValue ShOps[4];
8359 ShOps[0] = ShAmt;
8360 ShOps[1] = DAG.getConstant(0, MVT::i32);
8361 if (ShAmtVT == MVT::v4i32) {
8362 ShOps[2] = DAG.getUNDEF(MVT::i32);
8363 ShOps[3] = DAG.getUNDEF(MVT::i32);
8364 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
8365 } else {
8366 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
Dale Johannesen0488fb62010-09-30 23:57:10 +00008367// FIXME this must be lowered to get rid of the invalid type.
Mon P Wangefa42202009-09-03 19:56:25 +00008368 }
8369
Owen Andersone50ed302009-08-10 22:56:29 +00008370 EVT VT = Op.getValueType();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008371 ShAmt = DAG.getNode(ISD::BITCAST, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008372 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008373 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00008374 Op.getOperand(1), ShAmt);
8375 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00008376 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008377}
Evan Cheng72261582005-12-20 06:22:03 +00008378
Dan Gohmand858e902010-04-17 15:26:15 +00008379SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
8380 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +00008381 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
8382 MFI->setReturnAddressIsTaken(true);
8383
Bill Wendling64e87322009-01-16 19:25:27 +00008384 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008385 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00008386
8387 if (Depth > 0) {
8388 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
8389 SDValue Offset =
8390 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00008391 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008392 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00008393 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008394 FrameAddr, Offset),
Chris Lattner51abfe42010-09-21 06:02:19 +00008395 MachinePointerInfo(), false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +00008396 }
8397
8398 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00008399 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00008400 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Chris Lattner51abfe42010-09-21 06:02:19 +00008401 RetAddrFI, MachinePointerInfo(), false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00008402}
8403
Dan Gohmand858e902010-04-17 15:26:15 +00008404SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +00008405 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
8406 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00008407
Owen Andersone50ed302009-08-10 22:56:29 +00008408 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008409 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00008410 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
8411 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00008412 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00008413 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +00008414 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
8415 MachinePointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +00008416 false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00008417 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00008418}
8419
Dan Gohman475871a2008-07-27 21:46:04 +00008420SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008421 SelectionDAG &DAG) const {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00008422 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008423}
8424
Dan Gohmand858e902010-04-17 15:26:15 +00008425SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008426 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00008427 SDValue Chain = Op.getOperand(0);
8428 SDValue Offset = Op.getOperand(1);
8429 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008430 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008431
Dan Gohmand8816272010-08-11 18:14:00 +00008432 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
8433 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
8434 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008435 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008436
Dan Gohmand8816272010-08-11 18:14:00 +00008437 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
8438 DAG.getIntPtrConstant(TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008439 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +00008440 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
8441 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00008442 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008443 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008444
Dale Johannesene4d209d2009-02-03 20:21:25 +00008445 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008446 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008447 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008448}
8449
Dan Gohman475871a2008-07-27 21:46:04 +00008450SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008451 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008452 SDValue Root = Op.getOperand(0);
8453 SDValue Trmp = Op.getOperand(1); // trampoline
8454 SDValue FPtr = Op.getOperand(2); // nested function
8455 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008456 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008457
Dan Gohman69de1932008-02-06 22:27:42 +00008458 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008459
8460 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00008461 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00008462
8463 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +00008464 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
8465 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +00008466
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00008467 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
8468 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00008469
8470 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
8471
8472 // Load the pointer to the nested function into R11.
8473 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00008474 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00008475 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008476 Addr, MachinePointerInfo(TrmpAddr),
8477 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008478
Owen Anderson825b72b2009-08-11 20:47:22 +00008479 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8480 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008481 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
8482 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +00008483 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00008484
8485 // Load the 'nest' parameter value into R10.
8486 // R10 is specified in X86CallingConv.td
8487 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +00008488 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8489 DAG.getConstant(10, MVT::i64));
8490 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008491 Addr, MachinePointerInfo(TrmpAddr, 10),
8492 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008493
Owen Anderson825b72b2009-08-11 20:47:22 +00008494 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8495 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008496 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
8497 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +00008498 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00008499
8500 // Jump to the nested function.
8501 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +00008502 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8503 DAG.getConstant(20, MVT::i64));
8504 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008505 Addr, MachinePointerInfo(TrmpAddr, 20),
8506 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008507
8508 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +00008509 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8510 DAG.getConstant(22, MVT::i64));
8511 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00008512 MachinePointerInfo(TrmpAddr, 22),
8513 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008514
Dan Gohman475871a2008-07-27 21:46:04 +00008515 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00008516 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008517 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008518 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00008519 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00008520 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00008521 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00008522 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008523
8524 switch (CC) {
8525 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00008526 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00008527 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00008528 case CallingConv::X86_StdCall: {
8529 // Pass 'nest' parameter in ECX.
8530 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00008531 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008532
8533 // Check that ECX wasn't needed by an 'inreg' parameter.
8534 const FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00008535 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008536
Chris Lattner58d74912008-03-12 17:45:29 +00008537 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00008538 unsigned InRegCount = 0;
8539 unsigned Idx = 1;
8540
8541 for (FunctionType::param_iterator I = FTy->param_begin(),
8542 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00008543 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00008544 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00008545 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008546
8547 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +00008548 report_fatal_error("Nest register in use - reduce number of inreg"
8549 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00008550 }
8551 }
8552 break;
8553 }
8554 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +00008555 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00008556 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00008557 // Pass 'nest' parameter in EAX.
8558 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00008559 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008560 break;
8561 }
8562
Dan Gohman475871a2008-07-27 21:46:04 +00008563 SDValue OutChains[4];
8564 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008565
Owen Anderson825b72b2009-08-11 20:47:22 +00008566 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8567 DAG.getConstant(10, MVT::i32));
8568 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008569
Chris Lattnera62fe662010-02-05 19:20:30 +00008570 // This is storing the opcode for MOV32ri.
8571 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00008572 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00008573 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008574 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008575 Trmp, MachinePointerInfo(TrmpAddr),
8576 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008577
Owen Anderson825b72b2009-08-11 20:47:22 +00008578 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8579 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008580 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
8581 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +00008582 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008583
Chris Lattnera62fe662010-02-05 19:20:30 +00008584 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +00008585 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8586 DAG.getConstant(5, MVT::i32));
8587 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00008588 MachinePointerInfo(TrmpAddr, 5),
8589 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008590
Owen Anderson825b72b2009-08-11 20:47:22 +00008591 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8592 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008593 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
8594 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +00008595 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008596
Dan Gohman475871a2008-07-27 21:46:04 +00008597 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00008598 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008599 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008600 }
8601}
8602
Dan Gohmand858e902010-04-17 15:26:15 +00008603SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
8604 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008605 /*
8606 The rounding mode is in bits 11:10 of FPSR, and has the following
8607 settings:
8608 00 Round to nearest
8609 01 Round to -inf
8610 10 Round to +inf
8611 11 Round to 0
8612
8613 FLT_ROUNDS, on the other hand, expects the following:
8614 -1 Undefined
8615 0 Round to 0
8616 1 Round to nearest
8617 2 Round to +inf
8618 3 Round to -inf
8619
8620 To perform the conversion, we do:
8621 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
8622 */
8623
8624 MachineFunction &MF = DAG.getMachineFunction();
8625 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00008626 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008627 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00008628 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +00008629 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008630
8631 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00008632 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +00008633 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008634
Michael J. Spencerec38de22010-10-10 22:04:20 +00008635
Chris Lattner2156b792010-09-22 01:11:26 +00008636 MachineMemOperand *MMO =
8637 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8638 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008639
Chris Lattner2156b792010-09-22 01:11:26 +00008640 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
8641 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
8642 DAG.getVTList(MVT::Other),
8643 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008644
8645 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +00008646 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Chris Lattner51abfe42010-09-21 06:02:19 +00008647 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008648
8649 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00008650 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +00008651 DAG.getNode(ISD::SRL, DL, MVT::i16,
8652 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00008653 CWD, DAG.getConstant(0x800, MVT::i16)),
8654 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00008655 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +00008656 DAG.getNode(ISD::SRL, DL, MVT::i16,
8657 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00008658 CWD, DAG.getConstant(0x400, MVT::i16)),
8659 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008660
Dan Gohman475871a2008-07-27 21:46:04 +00008661 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +00008662 DAG.getNode(ISD::AND, DL, MVT::i16,
8663 DAG.getNode(ISD::ADD, DL, MVT::i16,
8664 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +00008665 DAG.getConstant(1, MVT::i16)),
8666 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008667
8668
Duncan Sands83ec4b62008-06-06 12:08:01 +00008669 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +00008670 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008671}
8672
Dan Gohmand858e902010-04-17 15:26:15 +00008673SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008674 EVT VT = Op.getValueType();
8675 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008676 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008677 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00008678
8679 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008680 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00008681 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +00008682 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00008683 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008684 }
Evan Cheng18efe262007-12-14 02:13:44 +00008685
Evan Cheng152804e2007-12-14 08:30:15 +00008686 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008687 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008688 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00008689
8690 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00008691 SDValue Ops[] = {
8692 Op,
8693 DAG.getConstant(NumBits+NumBits-1, OpVT),
8694 DAG.getConstant(X86::COND_E, MVT::i8),
8695 Op.getValue(1)
8696 };
8697 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00008698
8699 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00008700 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00008701
Owen Anderson825b72b2009-08-11 20:47:22 +00008702 if (VT == MVT::i8)
8703 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008704 return Op;
8705}
8706
Dan Gohmand858e902010-04-17 15:26:15 +00008707SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008708 EVT VT = Op.getValueType();
8709 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008710 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008711 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00008712
8713 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008714 if (VT == MVT::i8) {
8715 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00008716 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008717 }
Evan Cheng152804e2007-12-14 08:30:15 +00008718
8719 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008720 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008721 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00008722
8723 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00008724 SDValue Ops[] = {
8725 Op,
8726 DAG.getConstant(NumBits, OpVT),
8727 DAG.getConstant(X86::COND_E, MVT::i8),
8728 Op.getValue(1)
8729 };
8730 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00008731
Owen Anderson825b72b2009-08-11 20:47:22 +00008732 if (VT == MVT::i8)
8733 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008734 return Op;
8735}
8736
Dan Gohmand858e902010-04-17 15:26:15 +00008737SDValue X86TargetLowering::LowerMUL_V2I64(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008738 EVT VT = Op.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00008739 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008740 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00008741
Mon P Wangaf9b9522008-12-18 21:42:19 +00008742 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
8743 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
8744 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
8745 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
8746 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
8747 //
8748 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
8749 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
8750 // return AloBlo + AloBhi + AhiBlo;
8751
8752 SDValue A = Op.getOperand(0);
8753 SDValue B = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008754
Dale Johannesene4d209d2009-02-03 20:21:25 +00008755 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008756 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8757 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008758 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008759 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8760 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008761 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008762 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008763 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008764 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008765 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008766 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008767 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008768 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008769 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008770 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008771 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8772 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008773 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008774 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8775 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008776 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
8777 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +00008778 return Res;
8779}
8780
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008781SDValue X86TargetLowering::LowerSHL(SDValue Op, SelectionDAG &DAG) const {
8782 EVT VT = Op.getValueType();
8783 DebugLoc dl = Op.getDebugLoc();
8784 SDValue R = Op.getOperand(0);
8785
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008786 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008787
Nate Begeman51409212010-07-28 00:21:48 +00008788 assert(Subtarget->hasSSE41() && "Cannot lower SHL without SSE4.1 or later");
8789
8790 if (VT == MVT::v4i32) {
8791 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8792 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
8793 Op.getOperand(1), DAG.getConstant(23, MVT::i32));
8794
8795 ConstantInt *CI = ConstantInt::get(*Context, APInt(32, 0x3f800000U));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008796
Nate Begeman51409212010-07-28 00:21:48 +00008797 std::vector<Constant*> CV(4, CI);
8798 Constant *C = ConstantVector::get(CV);
8799 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8800 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008801 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00008802 false, false, 16);
8803
8804 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008805 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +00008806 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
8807 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
8808 }
8809 if (VT == MVT::v16i8) {
8810 // a = a << 5;
8811 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8812 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
8813 Op.getOperand(1), DAG.getConstant(5, MVT::i32));
8814
8815 ConstantInt *CM1 = ConstantInt::get(*Context, APInt(8, 15));
8816 ConstantInt *CM2 = ConstantInt::get(*Context, APInt(8, 63));
8817
8818 std::vector<Constant*> CVM1(16, CM1);
8819 std::vector<Constant*> CVM2(16, CM2);
8820 Constant *C = ConstantVector::get(CVM1);
8821 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8822 SDValue M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008823 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00008824 false, false, 16);
8825
8826 // r = pblendv(r, psllw(r & (char16)15, 4), a);
8827 M = DAG.getNode(ISD::AND, dl, VT, R, M);
8828 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8829 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
8830 DAG.getConstant(4, MVT::i32));
Nate Begeman672fb622010-12-20 22:04:24 +00008831 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT, R, M, Op);
Nate Begeman51409212010-07-28 00:21:48 +00008832 // a += a
8833 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008834
Nate Begeman51409212010-07-28 00:21:48 +00008835 C = ConstantVector::get(CVM2);
8836 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8837 M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008838 MachinePointerInfo::getConstantPool(),
Chris Lattner51abfe42010-09-21 06:02:19 +00008839 false, false, 16);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008840
Nate Begeman51409212010-07-28 00:21:48 +00008841 // r = pblendv(r, psllw(r & (char16)63, 2), a);
8842 M = DAG.getNode(ISD::AND, dl, VT, R, M);
8843 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8844 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
8845 DAG.getConstant(2, MVT::i32));
Nate Begeman672fb622010-12-20 22:04:24 +00008846 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT, R, M, Op);
Nate Begeman51409212010-07-28 00:21:48 +00008847 // a += a
8848 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008849
Nate Begeman51409212010-07-28 00:21:48 +00008850 // return pblendv(r, r+r, a);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008851 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT,
Nate Begeman51409212010-07-28 00:21:48 +00008852 R, DAG.getNode(ISD::ADD, dl, VT, R, R), Op);
8853 return R;
8854 }
8855 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008856}
Mon P Wangaf9b9522008-12-18 21:42:19 +00008857
Dan Gohmand858e902010-04-17 15:26:15 +00008858SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
Bill Wendling74c37652008-12-09 22:08:41 +00008859 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
8860 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +00008861 // looks for this combo and may remove the "setcc" instruction if the "setcc"
8862 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +00008863 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +00008864 SDValue LHS = N->getOperand(0);
8865 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +00008866 unsigned BaseOp = 0;
8867 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008868 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +00008869 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008870 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +00008871 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +00008872 // A subtract of one will be selected as a INC. Note that INC doesn't
8873 // set CF, so we can't do this for UADDO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +00008874 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
8875 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +00008876 BaseOp = X86ISD::INC;
8877 Cond = X86::COND_O;
8878 break;
8879 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008880 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +00008881 Cond = X86::COND_O;
8882 break;
8883 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008884 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +00008885 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00008886 break;
8887 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +00008888 // A subtract of one will be selected as a DEC. Note that DEC doesn't
8889 // set CF, so we can't do this for USUBO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +00008890 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
8891 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +00008892 BaseOp = X86ISD::DEC;
8893 Cond = X86::COND_O;
8894 break;
8895 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008896 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +00008897 Cond = X86::COND_O;
8898 break;
8899 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008900 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +00008901 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00008902 break;
8903 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00008904 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +00008905 Cond = X86::COND_O;
8906 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008907 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
8908 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
8909 MVT::i32);
8910 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008911
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008912 SDValue SetCC =
8913 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
8914 DAG.getConstant(X86::COND_O, MVT::i32),
8915 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008916
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008917 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
8918 return Sum;
8919 }
Bill Wendling74c37652008-12-09 22:08:41 +00008920 }
Bill Wendling3fafd932008-11-26 22:37:40 +00008921
Bill Wendling61edeb52008-12-02 01:06:39 +00008922 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008923 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008924 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +00008925
Bill Wendling61edeb52008-12-02 01:06:39 +00008926 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008927 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
8928 DAG.getConstant(Cond, MVT::i32),
8929 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +00008930
Bill Wendling61edeb52008-12-02 01:06:39 +00008931 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
8932 return Sum;
Bill Wendling41ea7e72008-11-24 19:21:46 +00008933}
8934
Eric Christopher9a9d2752010-07-22 02:48:34 +00008935SDValue X86TargetLowering::LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG) const{
8936 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00008937
Eric Christopherb6729dc2010-08-04 23:03:04 +00008938 if (!Subtarget->hasSSE2()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +00008939 SDValue Chain = Op.getOperand(0);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008940 SDValue Zero = DAG.getConstant(0,
Eric Christopherb6729dc2010-08-04 23:03:04 +00008941 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +00008942 SDValue Ops[] = {
8943 DAG.getRegister(X86::ESP, MVT::i32), // Base
8944 DAG.getTargetConstant(1, MVT::i8), // Scale
8945 DAG.getRegister(0, MVT::i32), // Index
8946 DAG.getTargetConstant(0, MVT::i32), // Disp
8947 DAG.getRegister(0, MVT::i32), // Segment.
8948 Zero,
8949 Chain
8950 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00008951 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +00008952 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
8953 array_lengthof(Ops));
8954 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +00008955 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00008956
Eric Christopher9a9d2752010-07-22 02:48:34 +00008957 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +00008958 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +00008959 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008960
Chris Lattner132929a2010-08-14 17:26:09 +00008961 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
8962 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
8963 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
8964 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +00008965
Chris Lattner132929a2010-08-14 17:26:09 +00008966 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
8967 if (!Op1 && !Op2 && !Op3 && Op4)
8968 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008969
Chris Lattner132929a2010-08-14 17:26:09 +00008970 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
8971 if (Op1 && !Op2 && !Op3 && !Op4)
8972 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008973
8974 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +00008975 // (MFENCE)>;
8976 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +00008977}
8978
Dan Gohmand858e902010-04-17 15:26:15 +00008979SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008980 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008981 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +00008982 unsigned Reg = 0;
8983 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00008984 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008985 default:
8986 assert(false && "Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +00008987 case MVT::i8: Reg = X86::AL; size = 1; break;
8988 case MVT::i16: Reg = X86::AX; size = 2; break;
8989 case MVT::i32: Reg = X86::EAX; size = 4; break;
8990 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +00008991 assert(Subtarget->is64Bit() && "Node not type legal!");
8992 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00008993 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00008994 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008995 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +00008996 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +00008997 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +00008998 Op.getOperand(1),
8999 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +00009000 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +00009001 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009002 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +00009003 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
9004 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
9005 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +00009006 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +00009007 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +00009008 return cpOut;
9009}
9010
Duncan Sands1607f052008-12-01 11:39:25 +00009011SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00009012 SelectionDAG &DAG) const {
Duncan Sands1607f052008-12-01 11:39:25 +00009013 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009014 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +00009015 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009016 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +00009017 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00009018 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
9019 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +00009020 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +00009021 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
9022 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +00009023 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +00009024 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +00009025 rdx.getValue(1)
9026 };
Dale Johannesene4d209d2009-02-03 20:21:25 +00009027 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009028}
9029
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009030SDValue X86TargetLowering::LowerBITCAST(SDValue Op,
Dale Johannesen7d07b482010-05-21 00:52:33 +00009031 SelectionDAG &DAG) const {
9032 EVT SrcVT = Op.getOperand(0).getValueType();
9033 EVT DstVT = Op.getValueType();
Chris Lattner2a786eb2010-12-19 20:19:20 +00009034 assert(Subtarget->is64Bit() && !Subtarget->hasSSE2() &&
9035 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +00009036 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +00009037 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009038 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +00009039 // i64 <=> MMX conversions are Legal.
9040 if (SrcVT==MVT::i64 && DstVT.isVector())
9041 return Op;
9042 if (DstVT==MVT::i64 && SrcVT.isVector())
9043 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +00009044 // MMX <=> MMX conversions are Legal.
9045 if (SrcVT.isVector() && DstVT.isVector())
9046 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +00009047 // All other conversions need to be expanded.
9048 return SDValue();
9049}
Chris Lattner5b856542010-12-20 00:59:46 +00009050
Dan Gohmand858e902010-04-17 15:26:15 +00009051SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen71d1bf52008-09-29 22:25:26 +00009052 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +00009053 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00009054 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009055 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +00009056 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009057 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009058 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +00009059 Node->getOperand(0),
9060 Node->getOperand(1), negOp,
9061 cast<AtomicSDNode>(Node)->getSrcValue(),
9062 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang63307c32008-05-05 19:05:59 +00009063}
9064
Chris Lattner5b856542010-12-20 00:59:46 +00009065static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
9066 EVT VT = Op.getNode()->getValueType(0);
9067
9068 // Let legalize expand this if it isn't a legal type yet.
9069 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
9070 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009071
Chris Lattner5b856542010-12-20 00:59:46 +00009072 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009073
Chris Lattner5b856542010-12-20 00:59:46 +00009074 unsigned Opc;
9075 bool ExtraOp = false;
9076 switch (Op.getOpcode()) {
9077 default: assert(0 && "Invalid code");
9078 case ISD::ADDC: Opc = X86ISD::ADD; break;
9079 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
9080 case ISD::SUBC: Opc = X86ISD::SUB; break;
9081 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
9082 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009083
Chris Lattner5b856542010-12-20 00:59:46 +00009084 if (!ExtraOp)
9085 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
9086 Op.getOperand(1));
9087 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
9088 Op.getOperand(1), Op.getOperand(2));
9089}
9090
Evan Cheng0db9fe62006-04-25 20:13:52 +00009091/// LowerOperation - Provide custom lowering hooks for some operations.
9092///
Dan Gohmand858e902010-04-17 15:26:15 +00009093SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00009094 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00009095 default: llvm_unreachable("Should not custom lower this!");
Eric Christopher9a9d2752010-07-22 02:48:34 +00009096 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op,DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009097 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
9098 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009099 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00009100 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009101 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
9102 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
9103 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
David Greene91585092011-01-26 15:38:49 +00009104 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
David Greenecfe33c42011-01-26 19:13:22 +00009105 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009106 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
9107 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
9108 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00009109 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +00009110 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +00009111 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009112 case ISD::SHL_PARTS:
9113 case ISD::SRA_PARTS:
9114 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
9115 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00009116 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009117 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00009118 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009119 case ISD::FABS: return LowerFABS(Op, DAG);
9120 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00009121 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00009122 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman30a0de92008-07-17 16:51:19 +00009123 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00009124 case ISD::SELECT: return LowerSELECT(Op, DAG);
9125 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009126 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009127 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +00009128 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +00009129 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009130 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00009131 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
9132 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009133 case ISD::FRAME_TO_ARGS_OFFSET:
9134 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009135 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009136 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009137 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00009138 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +00009139 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
9140 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Mon P Wangaf9b9522008-12-18 21:42:19 +00009141 case ISD::MUL: return LowerMUL_V2I64(Op, DAG);
Nate Begemanbdcb5af2010-07-27 22:37:06 +00009142 case ISD::SHL: return LowerSHL(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +00009143 case ISD::SADDO:
9144 case ISD::UADDO:
9145 case ISD::SSUBO:
9146 case ISD::USUBO:
9147 case ISD::SMULO:
9148 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00009149 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009150 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +00009151 case ISD::ADDC:
9152 case ISD::ADDE:
9153 case ISD::SUBC:
9154 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009155 }
Chris Lattner27a6c732007-11-24 07:07:01 +00009156}
9157
Duncan Sands1607f052008-12-01 11:39:25 +00009158void X86TargetLowering::
9159ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00009160 SelectionDAG &DAG, unsigned NewOp) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009161 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009162 DebugLoc dl = Node->getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00009163 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +00009164
9165 SDValue Chain = Node->getOperand(0);
9166 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00009167 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00009168 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00009169 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00009170 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +00009171 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +00009172 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +00009173 SDValue Result =
9174 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
9175 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +00009176 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +00009177 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00009178 Results.push_back(Result.getValue(2));
9179}
9180
Duncan Sands126d9072008-07-04 11:47:58 +00009181/// ReplaceNodeResults - Replace a node with an illegal result type
9182/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00009183void X86TargetLowering::ReplaceNodeResults(SDNode *N,
9184 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00009185 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +00009186 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +00009187 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +00009188 default:
Duncan Sands1607f052008-12-01 11:39:25 +00009189 assert(false && "Do not know how to custom type legalize this operation!");
9190 return;
Chris Lattner5b856542010-12-20 00:59:46 +00009191 case ISD::ADDC:
9192 case ISD::ADDE:
9193 case ISD::SUBC:
9194 case ISD::SUBE:
9195 // We don't want to expand or promote these.
9196 return;
Duncan Sands1607f052008-12-01 11:39:25 +00009197 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +00009198 std::pair<SDValue,SDValue> Vals =
9199 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +00009200 SDValue FIST = Vals.first, StackSlot = Vals.second;
9201 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +00009202 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +00009203 // Return a load from the stack slot.
Chris Lattner51abfe42010-09-21 06:02:19 +00009204 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
9205 MachinePointerInfo(), false, false, 0));
Duncan Sands1607f052008-12-01 11:39:25 +00009206 }
9207 return;
9208 }
9209 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009210 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +00009211 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009212 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00009213 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +00009214 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00009215 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00009216 eax.getValue(2));
9217 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
9218 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +00009219 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00009220 Results.push_back(edx.getValue(1));
9221 return;
9222 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009223 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +00009224 EVT T = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009225 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Duncan Sands1607f052008-12-01 11:39:25 +00009226 SDValue cpInL, cpInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00009227 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
9228 DAG.getConstant(0, MVT::i32));
9229 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
9230 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00009231 cpInL = DAG.getCopyToReg(N->getOperand(0), dl, X86::EAX, cpInL, SDValue());
9232 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl, X86::EDX, cpInH,
Duncan Sands1607f052008-12-01 11:39:25 +00009233 cpInL.getValue(1));
9234 SDValue swapInL, swapInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00009235 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
9236 DAG.getConstant(0, MVT::i32));
9237 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
9238 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00009239 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl, X86::EBX, swapInL,
Duncan Sands1607f052008-12-01 11:39:25 +00009240 cpInH.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00009241 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl, X86::ECX, swapInH,
Duncan Sands1607f052008-12-01 11:39:25 +00009242 swapInL.getValue(1));
9243 SDValue Ops[] = { swapInH.getValue(0),
9244 N->getOperand(1),
9245 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009246 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +00009247 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
9248 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG8_DAG, dl, Tys,
9249 Ops, 3, T, MMO);
Dale Johannesendd64c412009-02-04 00:33:20 +00009250 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl, X86::EAX,
Owen Anderson825b72b2009-08-11 20:47:22 +00009251 MVT::i32, Result.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00009252 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl, X86::EDX,
Owen Anderson825b72b2009-08-11 20:47:22 +00009253 MVT::i32, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +00009254 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Owen Anderson825b72b2009-08-11 20:47:22 +00009255 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00009256 Results.push_back(cpOutH.getValue(1));
9257 return;
9258 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009259 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +00009260 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
9261 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009262 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +00009263 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
9264 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009265 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +00009266 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
9267 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009268 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +00009269 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
9270 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009271 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +00009272 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
9273 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009274 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +00009275 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
9276 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009277 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +00009278 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
9279 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00009280 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00009281}
9282
Evan Cheng72261582005-12-20 06:22:03 +00009283const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
9284 switch (Opcode) {
9285 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +00009286 case X86ISD::BSF: return "X86ISD::BSF";
9287 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +00009288 case X86ISD::SHLD: return "X86ISD::SHLD";
9289 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00009290 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00009291 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00009292 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00009293 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00009294 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00009295 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00009296 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
9297 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
9298 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00009299 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00009300 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +00009301 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +00009302 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +00009303 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +00009304 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00009305 case X86ISD::COMI: return "X86ISD::COMI";
9306 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00009307 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +00009308 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Evan Cheng72261582005-12-20 06:22:03 +00009309 case X86ISD::CMOV: return "X86ISD::CMOV";
9310 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00009311 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00009312 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
9313 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +00009314 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00009315 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +00009316 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +00009317 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +00009318 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +00009319 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
9320 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +00009321 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +00009322 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Nate Begemanb65c1752010-12-17 22:55:37 +00009323 case X86ISD::PANDN: return "X86ISD::PANDN";
9324 case X86ISD::PSIGNB: return "X86ISD::PSIGNB";
9325 case X86ISD::PSIGNW: return "X86ISD::PSIGNW";
9326 case X86ISD::PSIGND: return "X86ISD::PSIGND";
Nate Begeman672fb622010-12-20 22:04:24 +00009327 case X86ISD::PBLENDVB: return "X86ISD::PBLENDVB";
Evan Cheng8ca29322006-11-10 21:43:37 +00009328 case X86ISD::FMAX: return "X86ISD::FMAX";
9329 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +00009330 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
9331 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00009332 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +00009333 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009334 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00009335 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009336 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +00009337 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
9338 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009339 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
9340 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
9341 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
9342 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
9343 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
9344 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +00009345 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
9346 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +00009347 case X86ISD::VSHL: return "X86ISD::VSHL";
9348 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +00009349 case X86ISD::CMPPD: return "X86ISD::CMPPD";
9350 case X86ISD::CMPPS: return "X86ISD::CMPPS";
9351 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
9352 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
9353 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
9354 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
9355 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
9356 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
9357 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
9358 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +00009359 case X86ISD::ADD: return "X86ISD::ADD";
9360 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +00009361 case X86ISD::ADC: return "X86ISD::ADC";
9362 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +00009363 case X86ISD::SMUL: return "X86ISD::SMUL";
9364 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +00009365 case X86ISD::INC: return "X86ISD::INC";
9366 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +00009367 case X86ISD::OR: return "X86ISD::OR";
9368 case X86ISD::XOR: return "X86ISD::XOR";
9369 case X86ISD::AND: return "X86ISD::AND";
Evan Cheng73f24c92009-03-30 21:36:47 +00009370 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +00009371 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009372 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009373 case X86ISD::PALIGN: return "X86ISD::PALIGN";
9374 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
9375 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
9376 case X86ISD::PSHUFHW_LD: return "X86ISD::PSHUFHW_LD";
9377 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
9378 case X86ISD::PSHUFLW_LD: return "X86ISD::PSHUFLW_LD";
9379 case X86ISD::SHUFPS: return "X86ISD::SHUFPS";
9380 case X86ISD::SHUFPD: return "X86ISD::SHUFPD";
9381 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009382 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00009383 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009384 case X86ISD::MOVHLPD: return "X86ISD::MOVHLPD";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00009385 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
9386 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009387 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
9388 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
9389 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
9390 case X86ISD::MOVSHDUP_LD: return "X86ISD::MOVSHDUP_LD";
9391 case X86ISD::MOVSLDUP_LD: return "X86ISD::MOVSLDUP_LD";
9392 case X86ISD::MOVSD: return "X86ISD::MOVSD";
9393 case X86ISD::MOVSS: return "X86ISD::MOVSS";
9394 case X86ISD::UNPCKLPS: return "X86ISD::UNPCKLPS";
9395 case X86ISD::UNPCKLPD: return "X86ISD::UNPCKLPD";
David Greenefbf05d32011-02-22 23:31:46 +00009396 case X86ISD::VUNPCKLPS: return "X86ISD::VUNPCKLPS";
9397 case X86ISD::VUNPCKLPD: return "X86ISD::VUNPCKLPD";
9398 case X86ISD::VUNPCKLPSY: return "X86ISD::VUNPCKLPSY";
9399 case X86ISD::VUNPCKLPDY: return "X86ISD::VUNPCKLPDY";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009400 case X86ISD::UNPCKHPS: return "X86ISD::UNPCKHPS";
9401 case X86ISD::UNPCKHPD: return "X86ISD::UNPCKHPD";
9402 case X86ISD::PUNPCKLBW: return "X86ISD::PUNPCKLBW";
9403 case X86ISD::PUNPCKLWD: return "X86ISD::PUNPCKLWD";
9404 case X86ISD::PUNPCKLDQ: return "X86ISD::PUNPCKLDQ";
9405 case X86ISD::PUNPCKLQDQ: return "X86ISD::PUNPCKLQDQ";
9406 case X86ISD::PUNPCKHBW: return "X86ISD::PUNPCKHBW";
9407 case X86ISD::PUNPCKHWD: return "X86ISD::PUNPCKHWD";
9408 case X86ISD::PUNPCKHDQ: return "X86ISD::PUNPCKHDQ";
9409 case X86ISD::PUNPCKHQDQ: return "X86ISD::PUNPCKHQDQ";
Dan Gohmand6708ea2009-08-15 01:38:56 +00009410 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +00009411 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +00009412 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Evan Cheng72261582005-12-20 06:22:03 +00009413 }
9414}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00009415
Chris Lattnerc9addb72007-03-30 23:15:24 +00009416// isLegalAddressingMode - Return true if the addressing mode represented
9417// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +00009418bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerc9addb72007-03-30 23:15:24 +00009419 const Type *Ty) const {
9420 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009421 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +00009422 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +00009423
Chris Lattnerc9addb72007-03-30 23:15:24 +00009424 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009425 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +00009426 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00009427
Chris Lattnerc9addb72007-03-30 23:15:24 +00009428 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +00009429 unsigned GVFlags =
9430 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009431
Chris Lattnerdfed4132009-07-10 07:38:24 +00009432 // If a reference to this global requires an extra load, we can't fold it.
9433 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +00009434 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009435
Chris Lattnerdfed4132009-07-10 07:38:24 +00009436 // If BaseGV requires a register for the PIC base, we cannot also have a
9437 // BaseReg specified.
9438 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +00009439 return false;
Evan Cheng52787842007-08-01 23:46:47 +00009440
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009441 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +00009442 if ((M != CodeModel::Small || R != Reloc::Static) &&
9443 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009444 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +00009445 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009446
Chris Lattnerc9addb72007-03-30 23:15:24 +00009447 switch (AM.Scale) {
9448 case 0:
9449 case 1:
9450 case 2:
9451 case 4:
9452 case 8:
9453 // These scales always work.
9454 break;
9455 case 3:
9456 case 5:
9457 case 9:
9458 // These scales are formed with basereg+scalereg. Only accept if there is
9459 // no basereg yet.
9460 if (AM.HasBaseReg)
9461 return false;
9462 break;
9463 default: // Other stuff never works.
9464 return false;
9465 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009466
Chris Lattnerc9addb72007-03-30 23:15:24 +00009467 return true;
9468}
9469
9470
Evan Cheng2bd122c2007-10-26 01:56:11 +00009471bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009472 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +00009473 return false;
Evan Chenge127a732007-10-29 07:57:50 +00009474 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
9475 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00009476 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +00009477 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00009478 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +00009479}
9480
Owen Andersone50ed302009-08-10 22:56:29 +00009481bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +00009482 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009483 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009484 unsigned NumBits1 = VT1.getSizeInBits();
9485 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00009486 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009487 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00009488 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009489}
Evan Cheng2bd122c2007-10-26 01:56:11 +00009490
Dan Gohman97121ba2009-04-08 00:15:30 +00009491bool X86TargetLowering::isZExtFree(const Type *Ty1, const Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00009492 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009493 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00009494}
9495
Owen Andersone50ed302009-08-10 22:56:29 +00009496bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00009497 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00009498 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00009499}
9500
Owen Andersone50ed302009-08-10 22:56:29 +00009501bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +00009502 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +00009503 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +00009504}
9505
Evan Cheng60c07e12006-07-05 22:17:51 +00009506/// isShuffleMaskLegal - Targets can use this to indicate that they only
9507/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
9508/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
9509/// are assumed to be legal.
9510bool
Eric Christopherfd179292009-08-27 18:07:15 +00009511X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +00009512 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +00009513 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +00009514 if (VT.getSizeInBits() == 64)
Eric Christophercff6f852010-04-15 01:40:20 +00009515 return isPALIGNRMask(M, VT, Subtarget->hasSSSE3());
Nate Begeman9008ca62009-04-27 18:41:29 +00009516
Nate Begemana09008b2009-10-19 02:17:23 +00009517 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +00009518 return (VT.getVectorNumElements() == 2 ||
9519 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
9520 isMOVLMask(M, VT) ||
9521 isSHUFPMask(M, VT) ||
9522 isPSHUFDMask(M, VT) ||
9523 isPSHUFHWMask(M, VT) ||
9524 isPSHUFLWMask(M, VT) ||
Nate Begemana09008b2009-10-19 02:17:23 +00009525 isPALIGNRMask(M, VT, Subtarget->hasSSSE3()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +00009526 isUNPCKLMask(M, VT) ||
9527 isUNPCKHMask(M, VT) ||
9528 isUNPCKL_v_undef_Mask(M, VT) ||
9529 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00009530}
9531
Dan Gohman7d8143f2008-04-09 20:09:42 +00009532bool
Nate Begeman5a5ca152009-04-29 05:20:52 +00009533X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +00009534 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00009535 unsigned NumElts = VT.getVectorNumElements();
9536 // FIXME: This collection of masks seems suspect.
9537 if (NumElts == 2)
9538 return true;
9539 if (NumElts == 4 && VT.getSizeInBits() == 128) {
9540 return (isMOVLMask(Mask, VT) ||
9541 isCommutedMOVLMask(Mask, VT, true) ||
9542 isSHUFPMask(Mask, VT) ||
9543 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00009544 }
9545 return false;
9546}
9547
9548//===----------------------------------------------------------------------===//
9549// X86 Scheduler Hooks
9550//===----------------------------------------------------------------------===//
9551
Mon P Wang63307c32008-05-05 19:05:59 +00009552// private utility function
9553MachineBasicBlock *
9554X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
9555 MachineBasicBlock *MBB,
9556 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009557 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009558 unsigned LoadOpc,
9559 unsigned CXchgOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009560 unsigned notOpc,
9561 unsigned EAXreg,
9562 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009563 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00009564 // For the atomic bitwise operator, we generate
9565 // thisMBB:
9566 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00009567 // ld t1 = [bitinstr.addr]
9568 // op t2 = t1, [bitinstr.val]
9569 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00009570 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
9571 // bz newMBB
9572 // fallthrough -->nextMBB
9573 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9574 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009575 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00009576 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009577
Mon P Wang63307c32008-05-05 19:05:59 +00009578 /// First build the CFG
9579 MachineFunction *F = MBB->getParent();
9580 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009581 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9582 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9583 F->insert(MBBIter, newMBB);
9584 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009585
Dan Gohman14152b42010-07-06 20:24:04 +00009586 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9587 nextMBB->splice(nextMBB->begin(), thisMBB,
9588 llvm::next(MachineBasicBlock::iterator(bInstr)),
9589 thisMBB->end());
9590 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009591
Mon P Wang63307c32008-05-05 19:05:59 +00009592 // Update thisMBB to fall through to newMBB
9593 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009594
Mon P Wang63307c32008-05-05 19:05:59 +00009595 // newMBB jumps to itself and fall through to nextMBB
9596 newMBB->addSuccessor(nextMBB);
9597 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009598
Mon P Wang63307c32008-05-05 19:05:59 +00009599 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009600 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009601 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +00009602 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00009603 MachineOperand& destOper = bInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009604 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00009605 int numArgs = bInstr->getNumOperands() - 1;
9606 for (int i=0; i < numArgs; ++i)
9607 argOpers[i] = &bInstr->getOperand(i+1);
9608
9609 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009610 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009611 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00009612
Dale Johannesen140be2d2008-08-19 18:47:28 +00009613 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009614 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00009615 for (int i=0; i <= lastAddrIndx; ++i)
9616 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009617
Dale Johannesen140be2d2008-08-19 18:47:28 +00009618 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009619 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00009620 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009621 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009622 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009623 tt = t1;
9624
Dale Johannesen140be2d2008-08-19 18:47:28 +00009625 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +00009626 assert((argOpers[valArgIndx]->isReg() ||
9627 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00009628 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +00009629 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009630 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00009631 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009632 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009633 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +00009634 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009635
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009636 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +00009637 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009638
Dale Johannesene4d209d2009-02-03 20:21:25 +00009639 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +00009640 for (int i=0; i <= lastAddrIndx; ++i)
9641 (*MIB).addOperand(*argOpers[i]);
9642 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +00009643 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009644 (*MIB).setMemRefs(bInstr->memoperands_begin(),
9645 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +00009646
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009647 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +00009648 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +00009649
Mon P Wang63307c32008-05-05 19:05:59 +00009650 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009651 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00009652
Dan Gohman14152b42010-07-06 20:24:04 +00009653 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00009654 return nextMBB;
9655}
9656
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00009657// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +00009658MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009659X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
9660 MachineBasicBlock *MBB,
9661 unsigned regOpcL,
9662 unsigned regOpcH,
9663 unsigned immOpcL,
9664 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009665 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009666 // For the atomic bitwise operator, we generate
9667 // thisMBB (instructions are in pairs, except cmpxchg8b)
9668 // ld t1,t2 = [bitinstr.addr]
9669 // newMBB:
9670 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
9671 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +00009672 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009673 // mov ECX, EBX <- t5, t6
9674 // mov EAX, EDX <- t1, t2
9675 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
9676 // mov t3, t4 <- EAX, EDX
9677 // bz newMBB
9678 // result in out1, out2
9679 // fallthrough -->nextMBB
9680
9681 const TargetRegisterClass *RC = X86::GR32RegisterClass;
9682 const unsigned LoadOpc = X86::MOV32rm;
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009683 const unsigned NotOpc = X86::NOT32r;
9684 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9685 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
9686 MachineFunction::iterator MBBIter = MBB;
9687 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009688
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009689 /// First build the CFG
9690 MachineFunction *F = MBB->getParent();
9691 MachineBasicBlock *thisMBB = MBB;
9692 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9693 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9694 F->insert(MBBIter, newMBB);
9695 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009696
Dan Gohman14152b42010-07-06 20:24:04 +00009697 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9698 nextMBB->splice(nextMBB->begin(), thisMBB,
9699 llvm::next(MachineBasicBlock::iterator(bInstr)),
9700 thisMBB->end());
9701 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009702
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009703 // Update thisMBB to fall through to newMBB
9704 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009705
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009706 // newMBB jumps to itself and fall through to nextMBB
9707 newMBB->addSuccessor(nextMBB);
9708 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009709
Dale Johannesene4d209d2009-02-03 20:21:25 +00009710 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009711 // Insert instructions into newMBB based on incoming instruction
9712 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009713 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009714 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009715 MachineOperand& dest1Oper = bInstr->getOperand(0);
9716 MachineOperand& dest2Oper = bInstr->getOperand(1);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009717 MachineOperand* argOpers[2 + X86::AddrNumOperands];
9718 for (int i=0; i < 2 + X86::AddrNumOperands; ++i) {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009719 argOpers[i] = &bInstr->getOperand(i+2);
9720
Dan Gohman71ea4e52010-05-14 21:01:44 +00009721 // We use some of the operands multiple times, so conservatively just
9722 // clear any kill flags that might be present.
9723 if (argOpers[i]->isReg() && argOpers[i]->isUse())
9724 argOpers[i]->setIsKill(false);
9725 }
9726
Evan Chengad5b52f2010-01-08 19:14:57 +00009727 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009728 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +00009729
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009730 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009731 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009732 for (int i=0; i <= lastAddrIndx; ++i)
9733 (*MIB).addOperand(*argOpers[i]);
9734 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009735 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +00009736 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +00009737 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009738 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +00009739 MachineOperand newOp3 = *(argOpers[3]);
9740 if (newOp3.isImm())
9741 newOp3.setImm(newOp3.getImm()+4);
9742 else
9743 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009744 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +00009745 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009746
9747 // t3/4 are defined later, at the bottom of the loop
9748 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
9749 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009750 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009751 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009752 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009753 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
9754
Evan Cheng306b4ca2010-01-08 23:41:50 +00009755 // The subsequent operations should be using the destination registers of
9756 //the PHI instructions.
Scott Michelfdc40a02009-02-17 22:15:04 +00009757 if (invSrc) {
Evan Cheng306b4ca2010-01-08 23:41:50 +00009758 t1 = F->getRegInfo().createVirtualRegister(RC);
9759 t2 = F->getRegInfo().createVirtualRegister(RC);
9760 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
9761 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009762 } else {
Evan Cheng306b4ca2010-01-08 23:41:50 +00009763 t1 = dest1Oper.getReg();
9764 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009765 }
9766
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009767 int valArgIndx = lastAddrIndx + 1;
9768 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +00009769 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009770 "invalid operand");
9771 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
9772 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009773 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009774 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009775 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009776 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +00009777 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00009778 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009779 (*MIB).addOperand(*argOpers[valArgIndx]);
9780 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00009781 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009782 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00009783 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009784 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009785 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009786 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009787 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +00009788 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00009789 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009790 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009791
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009792 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009793 MIB.addReg(t1);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009794 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009795 MIB.addReg(t2);
9796
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009797 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009798 MIB.addReg(t5);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009799 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009800 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +00009801
Dale Johannesene4d209d2009-02-03 20:21:25 +00009802 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009803 for (int i=0; i <= lastAddrIndx; ++i)
9804 (*MIB).addOperand(*argOpers[i]);
9805
9806 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009807 (*MIB).setMemRefs(bInstr->memoperands_begin(),
9808 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009809
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009810 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009811 MIB.addReg(X86::EAX);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009812 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009813 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +00009814
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009815 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009816 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009817
Dan Gohman14152b42010-07-06 20:24:04 +00009818 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009819 return nextMBB;
9820}
9821
9822// private utility function
9823MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +00009824X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
9825 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009826 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00009827 // For the atomic min/max operator, we generate
9828 // thisMBB:
9829 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00009830 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +00009831 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +00009832 // cmp t1, t2
9833 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +00009834 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00009835 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
9836 // bz newMBB
9837 // fallthrough -->nextMBB
9838 //
9839 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9840 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009841 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00009842 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009843
Mon P Wang63307c32008-05-05 19:05:59 +00009844 /// First build the CFG
9845 MachineFunction *F = MBB->getParent();
9846 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009847 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9848 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9849 F->insert(MBBIter, newMBB);
9850 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009851
Dan Gohman14152b42010-07-06 20:24:04 +00009852 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9853 nextMBB->splice(nextMBB->begin(), thisMBB,
9854 llvm::next(MachineBasicBlock::iterator(mInstr)),
9855 thisMBB->end());
9856 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009857
Mon P Wang63307c32008-05-05 19:05:59 +00009858 // Update thisMBB to fall through to newMBB
9859 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009860
Mon P Wang63307c32008-05-05 19:05:59 +00009861 // newMBB jumps to newMBB and fall through to nextMBB
9862 newMBB->addSuccessor(nextMBB);
9863 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009864
Dale Johannesene4d209d2009-02-03 20:21:25 +00009865 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00009866 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009867 assert(mInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009868 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +00009869 MachineOperand& destOper = mInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009870 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00009871 int numArgs = mInstr->getNumOperands() - 1;
9872 for (int i=0; i < numArgs; ++i)
9873 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009874
Mon P Wang63307c32008-05-05 19:05:59 +00009875 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009876 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009877 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00009878
Mon P Wangab3e7472008-05-05 22:56:23 +00009879 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009880 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00009881 for (int i=0; i <= lastAddrIndx; ++i)
9882 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +00009883
Mon P Wang63307c32008-05-05 19:05:59 +00009884 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +00009885 assert((argOpers[valArgIndx]->isReg() ||
9886 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00009887 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +00009888
9889 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +00009890 if (argOpers[valArgIndx]->isReg())
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009891 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +00009892 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009893 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00009894 (*MIB).addOperand(*argOpers[valArgIndx]);
9895
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009896 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +00009897 MIB.addReg(t1);
9898
Dale Johannesene4d209d2009-02-03 20:21:25 +00009899 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +00009900 MIB.addReg(t1);
9901 MIB.addReg(t2);
9902
9903 // Generate movc
9904 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009905 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +00009906 MIB.addReg(t2);
9907 MIB.addReg(t1);
9908
9909 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +00009910 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +00009911 for (int i=0; i <= lastAddrIndx; ++i)
9912 (*MIB).addOperand(*argOpers[i]);
9913 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +00009914 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009915 (*MIB).setMemRefs(mInstr->memoperands_begin(),
9916 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +00009917
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009918 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +00009919 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +00009920
Mon P Wang63307c32008-05-05 19:05:59 +00009921 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009922 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00009923
Dan Gohman14152b42010-07-06 20:24:04 +00009924 mInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00009925 return nextMBB;
9926}
9927
Eric Christopherf83a5de2009-08-27 18:08:16 +00009928// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009929// or XMM0_V32I8 in AVX all of this code can be replaced with that
9930// in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +00009931MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +00009932X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +00009933 unsigned numArgs, bool memArg) const {
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009934 assert((Subtarget->hasSSE42() || Subtarget->hasAVX()) &&
9935 "Target must have SSE4.2 or AVX features enabled");
9936
Eric Christopherb120ab42009-08-18 22:50:32 +00009937 DebugLoc dl = MI->getDebugLoc();
9938 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Eric Christopherb120ab42009-08-18 22:50:32 +00009939 unsigned Opc;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009940 if (!Subtarget->hasAVX()) {
9941 if (memArg)
9942 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
9943 else
9944 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
9945 } else {
9946 if (memArg)
9947 Opc = numArgs == 3 ? X86::VPCMPISTRM128rm : X86::VPCMPESTRM128rm;
9948 else
9949 Opc = numArgs == 3 ? X86::VPCMPISTRM128rr : X86::VPCMPESTRM128rr;
9950 }
Eric Christopherb120ab42009-08-18 22:50:32 +00009951
Eric Christopher41c902f2010-11-30 08:20:21 +00009952 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Eric Christopherb120ab42009-08-18 22:50:32 +00009953 for (unsigned i = 0; i < numArgs; ++i) {
9954 MachineOperand &Op = MI->getOperand(i+1);
Eric Christopherb120ab42009-08-18 22:50:32 +00009955 if (!(Op.isReg() && Op.isImplicit()))
9956 MIB.addOperand(Op);
9957 }
Eric Christopher41c902f2010-11-30 08:20:21 +00009958 BuildMI(*BB, MI, dl, TII->get(X86::MOVAPSrr), MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +00009959 .addReg(X86::XMM0);
9960
Dan Gohman14152b42010-07-06 20:24:04 +00009961 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +00009962 return BB;
9963}
9964
9965MachineBasicBlock *
Eric Christopher228232b2010-11-30 07:20:12 +00009966X86TargetLowering::EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +00009967 DebugLoc dl = MI->getDebugLoc();
9968 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009969
Eric Christopher228232b2010-11-30 07:20:12 +00009970 // Address into RAX/EAX, other two args into ECX, EDX.
9971 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
9972 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
9973 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
9974 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +00009975 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009976
Eric Christopher228232b2010-11-30 07:20:12 +00009977 unsigned ValOps = X86::AddrNumOperands;
9978 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
9979 .addReg(MI->getOperand(ValOps).getReg());
9980 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
9981 .addReg(MI->getOperand(ValOps+1).getReg());
9982
9983 // The instruction doesn't actually take any operands though.
9984 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009985
Eric Christopher228232b2010-11-30 07:20:12 +00009986 MI->eraseFromParent(); // The pseudo is gone now.
9987 return BB;
9988}
9989
9990MachineBasicBlock *
9991X86TargetLowering::EmitMwait(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +00009992 DebugLoc dl = MI->getDebugLoc();
9993 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009994
Eric Christopher228232b2010-11-30 07:20:12 +00009995 // First arg in ECX, the second in EAX.
9996 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
9997 .addReg(MI->getOperand(0).getReg());
9998 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EAX)
9999 .addReg(MI->getOperand(1).getReg());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010000
Eric Christopher228232b2010-11-30 07:20:12 +000010001 // The instruction doesn't actually take any operands though.
10002 BuildMI(*BB, MI, dl, TII->get(X86::MWAITrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010003
Eric Christopher228232b2010-11-30 07:20:12 +000010004 MI->eraseFromParent(); // The pseudo is gone now.
10005 return BB;
10006}
10007
10008MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +000010009X86TargetLowering::EmitVAARG64WithCustomInserter(
10010 MachineInstr *MI,
10011 MachineBasicBlock *MBB) const {
10012 // Emit va_arg instruction on X86-64.
10013
10014 // Operands to this pseudo-instruction:
10015 // 0 ) Output : destination address (reg)
10016 // 1-5) Input : va_list address (addr, i64mem)
10017 // 6 ) ArgSize : Size (in bytes) of vararg type
10018 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
10019 // 8 ) Align : Alignment of type
10020 // 9 ) EFLAGS (implicit-def)
10021
10022 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
10023 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
10024
10025 unsigned DestReg = MI->getOperand(0).getReg();
10026 MachineOperand &Base = MI->getOperand(1);
10027 MachineOperand &Scale = MI->getOperand(2);
10028 MachineOperand &Index = MI->getOperand(3);
10029 MachineOperand &Disp = MI->getOperand(4);
10030 MachineOperand &Segment = MI->getOperand(5);
10031 unsigned ArgSize = MI->getOperand(6).getImm();
10032 unsigned ArgMode = MI->getOperand(7).getImm();
10033 unsigned Align = MI->getOperand(8).getImm();
10034
10035 // Memory Reference
10036 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
10037 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
10038 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
10039
10040 // Machine Information
10041 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10042 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
10043 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
10044 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
10045 DebugLoc DL = MI->getDebugLoc();
10046
10047 // struct va_list {
10048 // i32 gp_offset
10049 // i32 fp_offset
10050 // i64 overflow_area (address)
10051 // i64 reg_save_area (address)
10052 // }
10053 // sizeof(va_list) = 24
10054 // alignment(va_list) = 8
10055
10056 unsigned TotalNumIntRegs = 6;
10057 unsigned TotalNumXMMRegs = 8;
10058 bool UseGPOffset = (ArgMode == 1);
10059 bool UseFPOffset = (ArgMode == 2);
10060 unsigned MaxOffset = TotalNumIntRegs * 8 +
10061 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
10062
10063 /* Align ArgSize to a multiple of 8 */
10064 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
10065 bool NeedsAlign = (Align > 8);
10066
10067 MachineBasicBlock *thisMBB = MBB;
10068 MachineBasicBlock *overflowMBB;
10069 MachineBasicBlock *offsetMBB;
10070 MachineBasicBlock *endMBB;
10071
10072 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
10073 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
10074 unsigned OffsetReg = 0;
10075
10076 if (!UseGPOffset && !UseFPOffset) {
10077 // If we only pull from the overflow region, we don't create a branch.
10078 // We don't need to alter control flow.
10079 OffsetDestReg = 0; // unused
10080 OverflowDestReg = DestReg;
10081
10082 offsetMBB = NULL;
10083 overflowMBB = thisMBB;
10084 endMBB = thisMBB;
10085 } else {
10086 // First emit code to check if gp_offset (or fp_offset) is below the bound.
10087 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
10088 // If not, pull from overflow_area. (branch to overflowMBB)
10089 //
10090 // thisMBB
10091 // | .
10092 // | .
10093 // offsetMBB overflowMBB
10094 // | .
10095 // | .
10096 // endMBB
10097
10098 // Registers for the PHI in endMBB
10099 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
10100 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
10101
10102 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
10103 MachineFunction *MF = MBB->getParent();
10104 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
10105 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
10106 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
10107
10108 MachineFunction::iterator MBBIter = MBB;
10109 ++MBBIter;
10110
10111 // Insert the new basic blocks
10112 MF->insert(MBBIter, offsetMBB);
10113 MF->insert(MBBIter, overflowMBB);
10114 MF->insert(MBBIter, endMBB);
10115
10116 // Transfer the remainder of MBB and its successor edges to endMBB.
10117 endMBB->splice(endMBB->begin(), thisMBB,
10118 llvm::next(MachineBasicBlock::iterator(MI)),
10119 thisMBB->end());
10120 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
10121
10122 // Make offsetMBB and overflowMBB successors of thisMBB
10123 thisMBB->addSuccessor(offsetMBB);
10124 thisMBB->addSuccessor(overflowMBB);
10125
10126 // endMBB is a successor of both offsetMBB and overflowMBB
10127 offsetMBB->addSuccessor(endMBB);
10128 overflowMBB->addSuccessor(endMBB);
10129
10130 // Load the offset value into a register
10131 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
10132 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
10133 .addOperand(Base)
10134 .addOperand(Scale)
10135 .addOperand(Index)
10136 .addDisp(Disp, UseFPOffset ? 4 : 0)
10137 .addOperand(Segment)
10138 .setMemRefs(MMOBegin, MMOEnd);
10139
10140 // Check if there is enough room left to pull this argument.
10141 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
10142 .addReg(OffsetReg)
10143 .addImm(MaxOffset + 8 - ArgSizeA8);
10144
10145 // Branch to "overflowMBB" if offset >= max
10146 // Fall through to "offsetMBB" otherwise
10147 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
10148 .addMBB(overflowMBB);
10149 }
10150
10151 // In offsetMBB, emit code to use the reg_save_area.
10152 if (offsetMBB) {
10153 assert(OffsetReg != 0);
10154
10155 // Read the reg_save_area address.
10156 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
10157 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
10158 .addOperand(Base)
10159 .addOperand(Scale)
10160 .addOperand(Index)
10161 .addDisp(Disp, 16)
10162 .addOperand(Segment)
10163 .setMemRefs(MMOBegin, MMOEnd);
10164
10165 // Zero-extend the offset
10166 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
10167 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
10168 .addImm(0)
10169 .addReg(OffsetReg)
10170 .addImm(X86::sub_32bit);
10171
10172 // Add the offset to the reg_save_area to get the final address.
10173 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
10174 .addReg(OffsetReg64)
10175 .addReg(RegSaveReg);
10176
10177 // Compute the offset for the next argument
10178 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
10179 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
10180 .addReg(OffsetReg)
10181 .addImm(UseFPOffset ? 16 : 8);
10182
10183 // Store it back into the va_list.
10184 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
10185 .addOperand(Base)
10186 .addOperand(Scale)
10187 .addOperand(Index)
10188 .addDisp(Disp, UseFPOffset ? 4 : 0)
10189 .addOperand(Segment)
10190 .addReg(NextOffsetReg)
10191 .setMemRefs(MMOBegin, MMOEnd);
10192
10193 // Jump to endMBB
10194 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
10195 .addMBB(endMBB);
10196 }
10197
10198 //
10199 // Emit code to use overflow area
10200 //
10201
10202 // Load the overflow_area address into a register.
10203 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
10204 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
10205 .addOperand(Base)
10206 .addOperand(Scale)
10207 .addOperand(Index)
10208 .addDisp(Disp, 8)
10209 .addOperand(Segment)
10210 .setMemRefs(MMOBegin, MMOEnd);
10211
10212 // If we need to align it, do so. Otherwise, just copy the address
10213 // to OverflowDestReg.
10214 if (NeedsAlign) {
10215 // Align the overflow address
10216 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
10217 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
10218
10219 // aligned_addr = (addr + (align-1)) & ~(align-1)
10220 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
10221 .addReg(OverflowAddrReg)
10222 .addImm(Align-1);
10223
10224 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
10225 .addReg(TmpReg)
10226 .addImm(~(uint64_t)(Align-1));
10227 } else {
10228 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
10229 .addReg(OverflowAddrReg);
10230 }
10231
10232 // Compute the next overflow address after this argument.
10233 // (the overflow address should be kept 8-byte aligned)
10234 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
10235 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
10236 .addReg(OverflowDestReg)
10237 .addImm(ArgSizeA8);
10238
10239 // Store the new overflow address.
10240 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
10241 .addOperand(Base)
10242 .addOperand(Scale)
10243 .addOperand(Index)
10244 .addDisp(Disp, 8)
10245 .addOperand(Segment)
10246 .addReg(NextAddrReg)
10247 .setMemRefs(MMOBegin, MMOEnd);
10248
10249 // If we branched, emit the PHI to the front of endMBB.
10250 if (offsetMBB) {
10251 BuildMI(*endMBB, endMBB->begin(), DL,
10252 TII->get(X86::PHI), DestReg)
10253 .addReg(OffsetDestReg).addMBB(offsetMBB)
10254 .addReg(OverflowDestReg).addMBB(overflowMBB);
10255 }
10256
10257 // Erase the pseudo instruction
10258 MI->eraseFromParent();
10259
10260 return endMBB;
10261}
10262
10263MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +000010264X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
10265 MachineInstr *MI,
10266 MachineBasicBlock *MBB) const {
10267 // Emit code to save XMM registers to the stack. The ABI says that the
10268 // number of registers to save is given in %al, so it's theoretically
10269 // possible to do an indirect jump trick to avoid saving all of them,
10270 // however this code takes a simpler approach and just executes all
10271 // of the stores if %al is non-zero. It's less code, and it's probably
10272 // easier on the hardware branch predictor, and stores aren't all that
10273 // expensive anyway.
10274
10275 // Create the new basic blocks. One block contains all the XMM stores,
10276 // and one block is the final destination regardless of whether any
10277 // stores were performed.
10278 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
10279 MachineFunction *F = MBB->getParent();
10280 MachineFunction::iterator MBBIter = MBB;
10281 ++MBBIter;
10282 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
10283 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
10284 F->insert(MBBIter, XMMSaveMBB);
10285 F->insert(MBBIter, EndMBB);
10286
Dan Gohman14152b42010-07-06 20:24:04 +000010287 // Transfer the remainder of MBB and its successor edges to EndMBB.
10288 EndMBB->splice(EndMBB->begin(), MBB,
10289 llvm::next(MachineBasicBlock::iterator(MI)),
10290 MBB->end());
10291 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
10292
Dan Gohmand6708ea2009-08-15 01:38:56 +000010293 // The original block will now fall through to the XMM save block.
10294 MBB->addSuccessor(XMMSaveMBB);
10295 // The XMMSaveMBB will fall through to the end block.
10296 XMMSaveMBB->addSuccessor(EndMBB);
10297
10298 // Now add the instructions.
10299 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10300 DebugLoc DL = MI->getDebugLoc();
10301
10302 unsigned CountReg = MI->getOperand(0).getReg();
10303 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
10304 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
10305
10306 if (!Subtarget->isTargetWin64()) {
10307 // If %al is 0, branch around the XMM save block.
10308 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +000010309 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010310 MBB->addSuccessor(EndMBB);
10311 }
10312
10313 // In the XMM save block, save all the XMM argument registers.
10314 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
10315 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +000010316 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +000010317 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +000010318 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +000010319 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +000010320 /*Size=*/16, /*Align=*/16);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010321 BuildMI(XMMSaveMBB, DL, TII->get(X86::MOVAPSmr))
10322 .addFrameIndex(RegSaveFrameIndex)
10323 .addImm(/*Scale=*/1)
10324 .addReg(/*IndexReg=*/0)
10325 .addImm(/*Disp=*/Offset)
10326 .addReg(/*Segment=*/0)
10327 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +000010328 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010329 }
10330
Dan Gohman14152b42010-07-06 20:24:04 +000010331 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +000010332
10333 return EndMBB;
10334}
Mon P Wang63307c32008-05-05 19:05:59 +000010335
Evan Cheng60c07e12006-07-05 22:17:51 +000010336MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +000010337X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010338 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +000010339 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10340 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +000010341
Chris Lattner52600972009-09-02 05:57:00 +000010342 // To "insert" a SELECT_CC instruction, we actually have to insert the
10343 // diamond control-flow pattern. The incoming instruction knows the
10344 // destination vreg to set, the condition code register to branch on, the
10345 // true/false values to select between, and a branch opcode to use.
10346 const BasicBlock *LLVM_BB = BB->getBasicBlock();
10347 MachineFunction::iterator It = BB;
10348 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +000010349
Chris Lattner52600972009-09-02 05:57:00 +000010350 // thisMBB:
10351 // ...
10352 // TrueVal = ...
10353 // cmpTY ccX, r1, r2
10354 // bCC copy1MBB
10355 // fallthrough --> copy0MBB
10356 MachineBasicBlock *thisMBB = BB;
10357 MachineFunction *F = BB->getParent();
10358 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
10359 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +000010360 F->insert(It, copy0MBB);
10361 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +000010362
Bill Wendling730c07e2010-06-25 20:48:10 +000010363 // If the EFLAGS register isn't dead in the terminator, then claim that it's
10364 // live into the sink and copy blocks.
10365 const MachineFunction *MF = BB->getParent();
10366 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
10367 BitVector ReservedRegs = TRI->getReservedRegs(*MF);
Bill Wendling730c07e2010-06-25 20:48:10 +000010368
Dan Gohman14152b42010-07-06 20:24:04 +000010369 for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) {
10370 const MachineOperand &MO = MI->getOperand(I);
10371 if (!MO.isReg() || !MO.isUse() || MO.isKill()) continue;
Bill Wendling730c07e2010-06-25 20:48:10 +000010372 unsigned Reg = MO.getReg();
10373 if (Reg != X86::EFLAGS) continue;
10374 copy0MBB->addLiveIn(Reg);
10375 sinkMBB->addLiveIn(Reg);
10376 }
10377
Dan Gohman14152b42010-07-06 20:24:04 +000010378 // Transfer the remainder of BB and its successor edges to sinkMBB.
10379 sinkMBB->splice(sinkMBB->begin(), BB,
10380 llvm::next(MachineBasicBlock::iterator(MI)),
10381 BB->end());
10382 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
10383
10384 // Add the true and fallthrough blocks as its successors.
10385 BB->addSuccessor(copy0MBB);
10386 BB->addSuccessor(sinkMBB);
10387
10388 // Create the conditional branch instruction.
10389 unsigned Opc =
10390 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
10391 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
10392
Chris Lattner52600972009-09-02 05:57:00 +000010393 // copy0MBB:
10394 // %FalseValue = ...
10395 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000010396 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000010397
Chris Lattner52600972009-09-02 05:57:00 +000010398 // sinkMBB:
10399 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
10400 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000010401 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
10402 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000010403 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
10404 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
10405
Dan Gohman14152b42010-07-06 20:24:04 +000010406 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000010407 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000010408}
10409
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010410MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010411X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010412 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010413 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10414 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010415
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000010416 assert(!Subtarget->isTargetEnvMacho());
10417
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010418 // The lowering is pretty easy: we're just emitting the call to _alloca. The
10419 // non-trivial part is impdef of ESP.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010420
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000010421 if (Subtarget->isTargetWin64()) {
10422 if (Subtarget->isTargetCygMing()) {
10423 // ___chkstk(Mingw64):
10424 // Clobbers R10, R11, RAX and EFLAGS.
10425 // Updates RSP.
10426 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
10427 .addExternalSymbol("___chkstk")
10428 .addReg(X86::RAX, RegState::Implicit)
10429 .addReg(X86::RSP, RegState::Implicit)
10430 .addReg(X86::RAX, RegState::Define | RegState::Implicit)
10431 .addReg(X86::RSP, RegState::Define | RegState::Implicit)
10432 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
10433 } else {
10434 // __chkstk(MSVCRT): does not update stack pointer.
10435 // Clobbers R10, R11 and EFLAGS.
10436 // FIXME: RAX(allocated size) might be reused and not killed.
10437 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
10438 .addExternalSymbol("__chkstk")
10439 .addReg(X86::RAX, RegState::Implicit)
10440 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
10441 // RAX has the offset to subtracted from RSP.
10442 BuildMI(*BB, MI, DL, TII->get(X86::SUB64rr), X86::RSP)
10443 .addReg(X86::RSP)
10444 .addReg(X86::RAX);
10445 }
10446 } else {
10447 const char *StackProbeSymbol =
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010448 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
10449
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000010450 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
10451 .addExternalSymbol(StackProbeSymbol)
10452 .addReg(X86::EAX, RegState::Implicit)
10453 .addReg(X86::ESP, RegState::Implicit)
10454 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
10455 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
10456 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
10457 }
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010458
Dan Gohman14152b42010-07-06 20:24:04 +000010459 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010460 return BB;
10461}
Chris Lattner52600972009-09-02 05:57:00 +000010462
10463MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000010464X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
10465 MachineBasicBlock *BB) const {
10466 // This is pretty easy. We're taking the value that we received from
10467 // our load from the relocation, sticking it in either RDI (x86-64)
10468 // or EAX and doing an indirect call. The return value will then
10469 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000010470 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000010471 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000010472 DebugLoc DL = MI->getDebugLoc();
10473 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000010474
10475 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000010476 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000010477
Eric Christopher30ef0e52010-06-03 04:07:48 +000010478 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000010479 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10480 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000010481 .addReg(X86::RIP)
10482 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010483 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000010484 MI->getOperand(3).getTargetFlags())
10485 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000010486 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000010487 addDirectMem(MIB, X86::RDI);
Eric Christopher61025492010-06-15 23:08:42 +000010488 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000010489 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10490 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000010491 .addReg(0)
10492 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010493 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000010494 MI->getOperand(3).getTargetFlags())
10495 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000010496 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000010497 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010498 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000010499 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10500 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000010501 .addReg(TII->getGlobalBaseReg(F))
10502 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010503 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000010504 MI->getOperand(3).getTargetFlags())
10505 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000010506 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000010507 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010508 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000010509
Dan Gohman14152b42010-07-06 20:24:04 +000010510 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000010511 return BB;
10512}
10513
10514MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000010515X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010516 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000010517 switch (MI->getOpcode()) {
10518 default: assert(false && "Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000010519 case X86::TAILJMPd64:
10520 case X86::TAILJMPr64:
10521 case X86::TAILJMPm64:
10522 assert(!"TAILJMP64 would not be touched here.");
10523 case X86::TCRETURNdi64:
10524 case X86::TCRETURNri64:
10525 case X86::TCRETURNmi64:
10526 // Defs of TCRETURNxx64 has Win64's callee-saved registers, as subset.
10527 // On AMD64, additional defs should be added before register allocation.
10528 if (!Subtarget->isTargetWin64()) {
10529 MI->addRegisterDefined(X86::RSI);
10530 MI->addRegisterDefined(X86::RDI);
10531 MI->addRegisterDefined(X86::XMM6);
10532 MI->addRegisterDefined(X86::XMM7);
10533 MI->addRegisterDefined(X86::XMM8);
10534 MI->addRegisterDefined(X86::XMM9);
10535 MI->addRegisterDefined(X86::XMM10);
10536 MI->addRegisterDefined(X86::XMM11);
10537 MI->addRegisterDefined(X86::XMM12);
10538 MI->addRegisterDefined(X86::XMM13);
10539 MI->addRegisterDefined(X86::XMM14);
10540 MI->addRegisterDefined(X86::XMM15);
10541 }
10542 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010543 case X86::WIN_ALLOCA:
10544 return EmitLoweredWinAlloca(MI, BB);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010545 case X86::TLSCall_32:
10546 case X86::TLSCall_64:
10547 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000010548 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000010549 case X86::CMOV_FR32:
10550 case X86::CMOV_FR64:
10551 case X86::CMOV_V4F32:
10552 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000010553 case X86::CMOV_V2I64:
Chris Lattner314a1132010-03-14 18:31:44 +000010554 case X86::CMOV_GR16:
10555 case X86::CMOV_GR32:
10556 case X86::CMOV_RFP32:
10557 case X86::CMOV_RFP64:
10558 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010559 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000010560
Dale Johannesen849f2142007-07-03 00:53:03 +000010561 case X86::FP32_TO_INT16_IN_MEM:
10562 case X86::FP32_TO_INT32_IN_MEM:
10563 case X86::FP32_TO_INT64_IN_MEM:
10564 case X86::FP64_TO_INT16_IN_MEM:
10565 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000010566 case X86::FP64_TO_INT64_IN_MEM:
10567 case X86::FP80_TO_INT16_IN_MEM:
10568 case X86::FP80_TO_INT32_IN_MEM:
10569 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000010570 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10571 DebugLoc DL = MI->getDebugLoc();
10572
Evan Cheng60c07e12006-07-05 22:17:51 +000010573 // Change the floating point control register to use "round towards zero"
10574 // mode when truncating to an integer value.
10575 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000010576 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000010577 addFrameReference(BuildMI(*BB, MI, DL,
10578 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010579
10580 // Load the old value of the high byte of the control word...
10581 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +000010582 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohman14152b42010-07-06 20:24:04 +000010583 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000010584 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010585
10586 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000010587 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000010588 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000010589
10590 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000010591 addFrameReference(BuildMI(*BB, MI, DL,
10592 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010593
10594 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000010595 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000010596 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000010597
10598 // Get the X86 opcode to use.
10599 unsigned Opc;
10600 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010601 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000010602 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
10603 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
10604 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
10605 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
10606 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
10607 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000010608 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
10609 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
10610 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000010611 }
10612
10613 X86AddressMode AM;
10614 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000010615 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000010616 AM.BaseType = X86AddressMode::RegBase;
10617 AM.Base.Reg = Op.getReg();
10618 } else {
10619 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000010620 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000010621 }
10622 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000010623 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000010624 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010625 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000010626 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000010627 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010628 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000010629 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000010630 AM.GV = Op.getGlobal();
10631 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000010632 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010633 }
Dan Gohman14152b42010-07-06 20:24:04 +000010634 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010635 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000010636
10637 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000010638 addFrameReference(BuildMI(*BB, MI, DL,
10639 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010640
Dan Gohman14152b42010-07-06 20:24:04 +000010641 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000010642 return BB;
10643 }
Eric Christopherb120ab42009-08-18 22:50:32 +000010644 // String/text processing lowering.
10645 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010646 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000010647 return EmitPCMP(MI, BB, 3, false /* in-mem */);
10648 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010649 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000010650 return EmitPCMP(MI, BB, 3, true /* in-mem */);
10651 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010652 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000010653 return EmitPCMP(MI, BB, 5, false /* in mem */);
10654 case X86::PCMPESTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010655 case X86::VPCMPESTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000010656 return EmitPCMP(MI, BB, 5, true /* in mem */);
10657
Eric Christopher228232b2010-11-30 07:20:12 +000010658 // Thread synchronization.
10659 case X86::MONITOR:
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010660 return EmitMonitor(MI, BB);
Eric Christopher228232b2010-11-30 07:20:12 +000010661 case X86::MWAIT:
10662 return EmitMwait(MI, BB);
10663
Eric Christopherb120ab42009-08-18 22:50:32 +000010664 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +000010665 case X86::ATOMAND32:
10666 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010667 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010668 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010669 X86::NOT32r, X86::EAX,
10670 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000010671 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +000010672 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
10673 X86::OR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010674 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010675 X86::NOT32r, X86::EAX,
10676 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000010677 case X86::ATOMXOR32:
10678 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010679 X86::XOR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010680 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010681 X86::NOT32r, X86::EAX,
10682 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010683 case X86::ATOMNAND32:
10684 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010685 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010686 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010687 X86::NOT32r, X86::EAX,
10688 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +000010689 case X86::ATOMMIN32:
10690 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
10691 case X86::ATOMMAX32:
10692 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
10693 case X86::ATOMUMIN32:
10694 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
10695 case X86::ATOMUMAX32:
10696 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +000010697
10698 case X86::ATOMAND16:
10699 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
10700 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010701 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010702 X86::NOT16r, X86::AX,
10703 X86::GR16RegisterClass);
10704 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +000010705 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010706 X86::OR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010707 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010708 X86::NOT16r, X86::AX,
10709 X86::GR16RegisterClass);
10710 case X86::ATOMXOR16:
10711 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
10712 X86::XOR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010713 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010714 X86::NOT16r, X86::AX,
10715 X86::GR16RegisterClass);
10716 case X86::ATOMNAND16:
10717 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
10718 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010719 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010720 X86::NOT16r, X86::AX,
10721 X86::GR16RegisterClass, true);
10722 case X86::ATOMMIN16:
10723 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
10724 case X86::ATOMMAX16:
10725 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
10726 case X86::ATOMUMIN16:
10727 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
10728 case X86::ATOMUMAX16:
10729 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
10730
10731 case X86::ATOMAND8:
10732 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
10733 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010734 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010735 X86::NOT8r, X86::AL,
10736 X86::GR8RegisterClass);
10737 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +000010738 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010739 X86::OR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010740 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010741 X86::NOT8r, X86::AL,
10742 X86::GR8RegisterClass);
10743 case X86::ATOMXOR8:
10744 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
10745 X86::XOR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010746 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010747 X86::NOT8r, X86::AL,
10748 X86::GR8RegisterClass);
10749 case X86::ATOMNAND8:
10750 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
10751 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010752 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010753 X86::NOT8r, X86::AL,
10754 X86::GR8RegisterClass, true);
10755 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010756 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +000010757 case X86::ATOMAND64:
10758 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010759 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010760 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010761 X86::NOT64r, X86::RAX,
10762 X86::GR64RegisterClass);
10763 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +000010764 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
10765 X86::OR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010766 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010767 X86::NOT64r, X86::RAX,
10768 X86::GR64RegisterClass);
10769 case X86::ATOMXOR64:
10770 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010771 X86::XOR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010772 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010773 X86::NOT64r, X86::RAX,
10774 X86::GR64RegisterClass);
10775 case X86::ATOMNAND64:
10776 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
10777 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010778 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010779 X86::NOT64r, X86::RAX,
10780 X86::GR64RegisterClass, true);
10781 case X86::ATOMMIN64:
10782 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
10783 case X86::ATOMMAX64:
10784 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
10785 case X86::ATOMUMIN64:
10786 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
10787 case X86::ATOMUMAX64:
10788 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010789
10790 // This group does 64-bit operations on a 32-bit host.
10791 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010792 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010793 X86::AND32rr, X86::AND32rr,
10794 X86::AND32ri, X86::AND32ri,
10795 false);
10796 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010797 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010798 X86::OR32rr, X86::OR32rr,
10799 X86::OR32ri, X86::OR32ri,
10800 false);
10801 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010802 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010803 X86::XOR32rr, X86::XOR32rr,
10804 X86::XOR32ri, X86::XOR32ri,
10805 false);
10806 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010807 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010808 X86::AND32rr, X86::AND32rr,
10809 X86::AND32ri, X86::AND32ri,
10810 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010811 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010812 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010813 X86::ADD32rr, X86::ADC32rr,
10814 X86::ADD32ri, X86::ADC32ri,
10815 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010816 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010817 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010818 X86::SUB32rr, X86::SBB32rr,
10819 X86::SUB32ri, X86::SBB32ri,
10820 false);
Dale Johannesen880ae362008-10-03 22:25:52 +000010821 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010822 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +000010823 X86::MOV32rr, X86::MOV32rr,
10824 X86::MOV32ri, X86::MOV32ri,
10825 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010826 case X86::VASTART_SAVE_XMM_REGS:
10827 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000010828
10829 case X86::VAARG_64:
10830 return EmitVAARG64WithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000010831 }
10832}
10833
10834//===----------------------------------------------------------------------===//
10835// X86 Optimization Hooks
10836//===----------------------------------------------------------------------===//
10837
Dan Gohman475871a2008-07-27 21:46:04 +000010838void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +000010839 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000010840 APInt &KnownZero,
10841 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000010842 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000010843 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010844 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000010845 assert((Opc >= ISD::BUILTIN_OP_END ||
10846 Opc == ISD::INTRINSIC_WO_CHAIN ||
10847 Opc == ISD::INTRINSIC_W_CHAIN ||
10848 Opc == ISD::INTRINSIC_VOID) &&
10849 "Should use MaskedValueIsZero if you don't know whether Op"
10850 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010851
Dan Gohmanf4f92f52008-02-13 23:07:24 +000010852 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010853 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000010854 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000010855 case X86ISD::ADD:
10856 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000010857 case X86ISD::ADC:
10858 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000010859 case X86ISD::SMUL:
10860 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000010861 case X86ISD::INC:
10862 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000010863 case X86ISD::OR:
10864 case X86ISD::XOR:
10865 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000010866 // These nodes' second result is a boolean.
10867 if (Op.getResNo() == 0)
10868 break;
10869 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010870 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000010871 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
10872 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000010873 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010874 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010875}
Chris Lattner259e97c2006-01-31 19:43:35 +000010876
Owen Andersonbc146b02010-09-21 20:42:50 +000010877unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
10878 unsigned Depth) const {
10879 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
10880 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
10881 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000010882
Owen Andersonbc146b02010-09-21 20:42:50 +000010883 // Fallback case.
10884 return 1;
10885}
10886
Evan Cheng206ee9d2006-07-07 08:33:52 +000010887/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000010888/// node is a GlobalAddress + offset.
10889bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000010890 const GlobalValue* &GA,
10891 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000010892 if (N->getOpcode() == X86ISD::Wrapper) {
10893 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000010894 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000010895 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000010896 return true;
10897 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000010898 }
Evan Chengad4196b2008-05-12 19:56:52 +000010899 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000010900}
10901
Evan Cheng206ee9d2006-07-07 08:33:52 +000010902/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
10903/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
10904/// if the load addresses are consecutive, non-overlapping, and in the right
Nate Begemanfdea31a2010-03-24 20:49:50 +000010905/// order.
Dan Gohman475871a2008-07-27 21:46:04 +000010906static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Mon P Wanga0fd0d52010-12-19 23:55:53 +000010907 TargetLowering::DAGCombinerInfo &DCI) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000010908 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000010909 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000010910
Eli Friedman7a5e5552009-06-07 06:52:44 +000010911 if (VT.getSizeInBits() != 128)
10912 return SDValue();
10913
Mon P Wanga0fd0d52010-12-19 23:55:53 +000010914 // Don't create instructions with illegal types after legalize types has run.
10915 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10916 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
10917 return SDValue();
10918
Nate Begemanfdea31a2010-03-24 20:49:50 +000010919 SmallVector<SDValue, 16> Elts;
10920 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000010921 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000010922
Nate Begemanfdea31a2010-03-24 20:49:50 +000010923 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000010924}
Evan Chengd880b972008-05-09 21:53:03 +000010925
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000010926/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
10927/// generation and convert it from being a bunch of shuffles and extracts
10928/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010929static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
10930 const TargetLowering &TLI) {
10931 SDValue InputVector = N->getOperand(0);
10932
10933 // Only operate on vectors of 4 elements, where the alternative shuffling
10934 // gets to be more expensive.
10935 if (InputVector.getValueType() != MVT::v4i32)
10936 return SDValue();
10937
10938 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
10939 // single use which is a sign-extend or zero-extend, and all elements are
10940 // used.
10941 SmallVector<SDNode *, 4> Uses;
10942 unsigned ExtractedElements = 0;
10943 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
10944 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
10945 if (UI.getUse().getResNo() != InputVector.getResNo())
10946 return SDValue();
10947
10948 SDNode *Extract = *UI;
10949 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
10950 return SDValue();
10951
10952 if (Extract->getValueType(0) != MVT::i32)
10953 return SDValue();
10954 if (!Extract->hasOneUse())
10955 return SDValue();
10956 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
10957 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
10958 return SDValue();
10959 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
10960 return SDValue();
10961
10962 // Record which element was extracted.
10963 ExtractedElements |=
10964 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
10965
10966 Uses.push_back(Extract);
10967 }
10968
10969 // If not all the elements were used, this may not be worthwhile.
10970 if (ExtractedElements != 15)
10971 return SDValue();
10972
10973 // Ok, we've now decided to do the transformation.
10974 DebugLoc dl = InputVector.getDebugLoc();
10975
10976 // Store the value to a temporary stack slot.
10977 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000010978 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
10979 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010980
10981 // Replace each use (extract) with a load of the appropriate element.
10982 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
10983 UE = Uses.end(); UI != UE; ++UI) {
10984 SDNode *Extract = *UI;
10985
10986 // Compute the element's address.
10987 SDValue Idx = Extract->getOperand(1);
10988 unsigned EltSize =
10989 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
10990 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
10991 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
10992
Eric Christopher90eb4022010-07-22 00:26:08 +000010993 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(),
Chris Lattner51abfe42010-09-21 06:02:19 +000010994 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010995
10996 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000010997 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000010998 ScalarAddr, MachinePointerInfo(),
10999 false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000011000
11001 // Replace the exact with the load.
11002 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
11003 }
11004
11005 // The replacement was made in place; don't return anything.
11006 return SDValue();
11007}
11008
Chris Lattner83e6c992006-10-04 06:57:07 +000011009/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000011010static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +000011011 const X86Subtarget *Subtarget) {
11012 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000011013 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000011014 // Get the LHS/RHS of the select.
11015 SDValue LHS = N->getOperand(1);
11016 SDValue RHS = N->getOperand(2);
Eric Christopherfd179292009-08-27 18:07:15 +000011017
Dan Gohman670e5392009-09-21 18:03:22 +000011018 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000011019 // instructions match the semantics of the common C idiom x<y?x:y but not
11020 // x<=y?x:y, because of how they handle negative zero (which can be
11021 // ignored in unsafe-math mode).
Chris Lattner83e6c992006-10-04 06:57:07 +000011022 if (Subtarget->hasSSE2() &&
Owen Anderson825b72b2009-08-11 20:47:22 +000011023 (LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64) &&
Chris Lattner47b4ce82009-03-11 05:48:52 +000011024 Cond.getOpcode() == ISD::SETCC) {
11025 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011026
Chris Lattner47b4ce82009-03-11 05:48:52 +000011027 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000011028 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000011029 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
11030 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000011031 switch (CC) {
11032 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000011033 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000011034 // Converting this to a min would handle NaNs incorrectly, and swapping
11035 // the operands would cause it to handle comparisons between positive
11036 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000011037 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000011038 if (!UnsafeFPMath &&
11039 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
11040 break;
11041 std::swap(LHS, RHS);
11042 }
Dan Gohman670e5392009-09-21 18:03:22 +000011043 Opcode = X86ISD::FMIN;
11044 break;
11045 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000011046 // Converting this to a min would handle comparisons between positive
11047 // and negative zero incorrectly.
11048 if (!UnsafeFPMath &&
11049 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
11050 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011051 Opcode = X86ISD::FMIN;
11052 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000011053 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000011054 // Converting this to a min would handle both negative zeros and NaNs
11055 // incorrectly, but we can swap the operands to fix both.
11056 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000011057 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011058 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000011059 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011060 Opcode = X86ISD::FMIN;
11061 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011062
Dan Gohman670e5392009-09-21 18:03:22 +000011063 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000011064 // Converting this to a max would handle comparisons between positive
11065 // and negative zero incorrectly.
11066 if (!UnsafeFPMath &&
11067 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(LHS))
11068 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011069 Opcode = X86ISD::FMAX;
11070 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000011071 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000011072 // Converting this to a max would handle NaNs incorrectly, and swapping
11073 // the operands would cause it to handle comparisons between positive
11074 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000011075 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000011076 if (!UnsafeFPMath &&
11077 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
11078 break;
11079 std::swap(LHS, RHS);
11080 }
Dan Gohman670e5392009-09-21 18:03:22 +000011081 Opcode = X86ISD::FMAX;
11082 break;
11083 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000011084 // Converting this to a max would handle both negative zeros and NaNs
11085 // incorrectly, but we can swap the operands to fix both.
11086 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000011087 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011088 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011089 case ISD::SETGE:
11090 Opcode = X86ISD::FMAX;
11091 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000011092 }
Dan Gohman670e5392009-09-21 18:03:22 +000011093 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000011094 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
11095 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000011096 switch (CC) {
11097 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000011098 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000011099 // Converting this to a min would handle comparisons between positive
11100 // and negative zero incorrectly, and swapping the operands would
11101 // cause it to handle NaNs incorrectly.
11102 if (!UnsafeFPMath &&
11103 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000011104 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000011105 break;
11106 std::swap(LHS, RHS);
11107 }
Dan Gohman670e5392009-09-21 18:03:22 +000011108 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000011109 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011110 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000011111 // Converting this to a min would handle NaNs incorrectly.
11112 if (!UnsafeFPMath &&
11113 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
11114 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011115 Opcode = X86ISD::FMIN;
11116 break;
11117 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000011118 // Converting this to a min would handle both negative zeros and NaNs
11119 // incorrectly, but we can swap the operands to fix both.
11120 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000011121 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011122 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011123 case ISD::SETGE:
11124 Opcode = X86ISD::FMIN;
11125 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011126
Dan Gohman670e5392009-09-21 18:03:22 +000011127 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000011128 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000011129 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000011130 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011131 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000011132 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011133 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000011134 // Converting this to a max would handle comparisons between positive
11135 // and negative zero incorrectly, and swapping the operands would
11136 // cause it to handle NaNs incorrectly.
11137 if (!UnsafeFPMath &&
11138 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000011139 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000011140 break;
11141 std::swap(LHS, RHS);
11142 }
Dan Gohman670e5392009-09-21 18:03:22 +000011143 Opcode = X86ISD::FMAX;
11144 break;
11145 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000011146 // Converting this to a max would handle both negative zeros and NaNs
11147 // incorrectly, but we can swap the operands to fix both.
11148 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000011149 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011150 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000011151 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011152 Opcode = X86ISD::FMAX;
11153 break;
11154 }
Chris Lattner83e6c992006-10-04 06:57:07 +000011155 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011156
Chris Lattner47b4ce82009-03-11 05:48:52 +000011157 if (Opcode)
11158 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000011159 }
Eric Christopherfd179292009-08-27 18:07:15 +000011160
Chris Lattnerd1980a52009-03-12 06:52:53 +000011161 // If this is a select between two integer constants, try to do some
11162 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000011163 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
11164 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000011165 // Don't do this for crazy integer types.
11166 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
11167 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000011168 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000011169 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000011170
Chris Lattnercee56e72009-03-13 05:53:31 +000011171 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000011172 // Efficiently invertible.
11173 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
11174 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
11175 isa<ConstantSDNode>(Cond.getOperand(1))))) {
11176 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000011177 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000011178 }
Eric Christopherfd179292009-08-27 18:07:15 +000011179
Chris Lattnerd1980a52009-03-12 06:52:53 +000011180 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000011181 if (FalseC->getAPIntValue() == 0 &&
11182 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000011183 if (NeedsCondInvert) // Invert the condition if needed.
11184 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
11185 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011186
Chris Lattnerd1980a52009-03-12 06:52:53 +000011187 // Zero extend the condition if needed.
11188 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011189
Chris Lattnercee56e72009-03-13 05:53:31 +000011190 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000011191 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000011192 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000011193 }
Eric Christopherfd179292009-08-27 18:07:15 +000011194
Chris Lattner97a29a52009-03-13 05:22:11 +000011195 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000011196 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000011197 if (NeedsCondInvert) // Invert the condition if needed.
11198 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
11199 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011200
Chris Lattner97a29a52009-03-13 05:22:11 +000011201 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000011202 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
11203 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000011204 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000011205 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000011206 }
Eric Christopherfd179292009-08-27 18:07:15 +000011207
Chris Lattnercee56e72009-03-13 05:53:31 +000011208 // Optimize cases that will turn into an LEA instruction. This requires
11209 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000011210 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000011211 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000011212 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000011213
Chris Lattnercee56e72009-03-13 05:53:31 +000011214 bool isFastMultiplier = false;
11215 if (Diff < 10) {
11216 switch ((unsigned char)Diff) {
11217 default: break;
11218 case 1: // result = add base, cond
11219 case 2: // result = lea base( , cond*2)
11220 case 3: // result = lea base(cond, cond*2)
11221 case 4: // result = lea base( , cond*4)
11222 case 5: // result = lea base(cond, cond*4)
11223 case 8: // result = lea base( , cond*8)
11224 case 9: // result = lea base(cond, cond*8)
11225 isFastMultiplier = true;
11226 break;
11227 }
11228 }
Eric Christopherfd179292009-08-27 18:07:15 +000011229
Chris Lattnercee56e72009-03-13 05:53:31 +000011230 if (isFastMultiplier) {
11231 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
11232 if (NeedsCondInvert) // Invert the condition if needed.
11233 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
11234 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011235
Chris Lattnercee56e72009-03-13 05:53:31 +000011236 // Zero extend the condition if needed.
11237 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
11238 Cond);
11239 // Scale the condition by the difference.
11240 if (Diff != 1)
11241 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
11242 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011243
Chris Lattnercee56e72009-03-13 05:53:31 +000011244 // Add the base if non-zero.
11245 if (FalseC->getAPIntValue() != 0)
11246 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
11247 SDValue(FalseC, 0));
11248 return Cond;
11249 }
Eric Christopherfd179292009-08-27 18:07:15 +000011250 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000011251 }
11252 }
Eric Christopherfd179292009-08-27 18:07:15 +000011253
Dan Gohman475871a2008-07-27 21:46:04 +000011254 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000011255}
11256
Chris Lattnerd1980a52009-03-12 06:52:53 +000011257/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
11258static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
11259 TargetLowering::DAGCombinerInfo &DCI) {
11260 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000011261
Chris Lattnerd1980a52009-03-12 06:52:53 +000011262 // If the flag operand isn't dead, don't touch this CMOV.
11263 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
11264 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000011265
Chris Lattnerd1980a52009-03-12 06:52:53 +000011266 // If this is a select between two integer constants, try to do some
11267 // optimizations. Note that the operands are ordered the opposite of SELECT
11268 // operands.
11269 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
11270 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
11271 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
11272 // larger than FalseC (the false value).
11273 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
Eric Christopherfd179292009-08-27 18:07:15 +000011274
Chris Lattnerd1980a52009-03-12 06:52:53 +000011275 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
11276 CC = X86::GetOppositeBranchCondition(CC);
11277 std::swap(TrueC, FalseC);
11278 }
Eric Christopherfd179292009-08-27 18:07:15 +000011279
Chris Lattnerd1980a52009-03-12 06:52:53 +000011280 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000011281 // This is efficient for any integer data type (including i8/i16) and
11282 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000011283 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
11284 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +000011285 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11286 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011287
Chris Lattnerd1980a52009-03-12 06:52:53 +000011288 // Zero extend the condition if needed.
11289 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011290
Chris Lattnerd1980a52009-03-12 06:52:53 +000011291 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
11292 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000011293 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000011294 if (N->getNumValues() == 2) // Dead flag value?
11295 return DCI.CombineTo(N, Cond, SDValue());
11296 return Cond;
11297 }
Eric Christopherfd179292009-08-27 18:07:15 +000011298
Chris Lattnercee56e72009-03-13 05:53:31 +000011299 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
11300 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000011301 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
11302 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +000011303 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11304 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011305
Chris Lattner97a29a52009-03-13 05:22:11 +000011306 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000011307 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
11308 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000011309 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
11310 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000011311
Chris Lattner97a29a52009-03-13 05:22:11 +000011312 if (N->getNumValues() == 2) // Dead flag value?
11313 return DCI.CombineTo(N, Cond, SDValue());
11314 return Cond;
11315 }
Eric Christopherfd179292009-08-27 18:07:15 +000011316
Chris Lattnercee56e72009-03-13 05:53:31 +000011317 // Optimize cases that will turn into an LEA instruction. This requires
11318 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000011319 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000011320 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000011321 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000011322
Chris Lattnercee56e72009-03-13 05:53:31 +000011323 bool isFastMultiplier = false;
11324 if (Diff < 10) {
11325 switch ((unsigned char)Diff) {
11326 default: break;
11327 case 1: // result = add base, cond
11328 case 2: // result = lea base( , cond*2)
11329 case 3: // result = lea base(cond, cond*2)
11330 case 4: // result = lea base( , cond*4)
11331 case 5: // result = lea base(cond, cond*4)
11332 case 8: // result = lea base( , cond*8)
11333 case 9: // result = lea base(cond, cond*8)
11334 isFastMultiplier = true;
11335 break;
11336 }
11337 }
Eric Christopherfd179292009-08-27 18:07:15 +000011338
Chris Lattnercee56e72009-03-13 05:53:31 +000011339 if (isFastMultiplier) {
11340 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
11341 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +000011342 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11343 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000011344 // Zero extend the condition if needed.
11345 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
11346 Cond);
11347 // Scale the condition by the difference.
11348 if (Diff != 1)
11349 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
11350 DAG.getConstant(Diff, Cond.getValueType()));
11351
11352 // Add the base if non-zero.
11353 if (FalseC->getAPIntValue() != 0)
11354 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
11355 SDValue(FalseC, 0));
11356 if (N->getNumValues() == 2) // Dead flag value?
11357 return DCI.CombineTo(N, Cond, SDValue());
11358 return Cond;
11359 }
Eric Christopherfd179292009-08-27 18:07:15 +000011360 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000011361 }
11362 }
11363 return SDValue();
11364}
11365
11366
Evan Cheng0b0cd912009-03-28 05:57:29 +000011367/// PerformMulCombine - Optimize a single multiply with constant into two
11368/// in order to implement it with two cheaper instructions, e.g.
11369/// LEA + SHL, LEA + LEA.
11370static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
11371 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000011372 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
11373 return SDValue();
11374
Owen Andersone50ed302009-08-10 22:56:29 +000011375 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000011376 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000011377 return SDValue();
11378
11379 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
11380 if (!C)
11381 return SDValue();
11382 uint64_t MulAmt = C->getZExtValue();
11383 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
11384 return SDValue();
11385
11386 uint64_t MulAmt1 = 0;
11387 uint64_t MulAmt2 = 0;
11388 if ((MulAmt % 9) == 0) {
11389 MulAmt1 = 9;
11390 MulAmt2 = MulAmt / 9;
11391 } else if ((MulAmt % 5) == 0) {
11392 MulAmt1 = 5;
11393 MulAmt2 = MulAmt / 5;
11394 } else if ((MulAmt % 3) == 0) {
11395 MulAmt1 = 3;
11396 MulAmt2 = MulAmt / 3;
11397 }
11398 if (MulAmt2 &&
11399 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
11400 DebugLoc DL = N->getDebugLoc();
11401
11402 if (isPowerOf2_64(MulAmt2) &&
11403 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
11404 // If second multiplifer is pow2, issue it first. We want the multiply by
11405 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
11406 // is an add.
11407 std::swap(MulAmt1, MulAmt2);
11408
11409 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000011410 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000011411 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000011412 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000011413 else
Evan Cheng73f24c92009-03-30 21:36:47 +000011414 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000011415 DAG.getConstant(MulAmt1, VT));
11416
Eric Christopherfd179292009-08-27 18:07:15 +000011417 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000011418 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000011419 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000011420 else
Evan Cheng73f24c92009-03-30 21:36:47 +000011421 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000011422 DAG.getConstant(MulAmt2, VT));
11423
11424 // Do not add new nodes to DAG combiner worklist.
11425 DCI.CombineTo(N, NewMul, false);
11426 }
11427 return SDValue();
11428}
11429
Evan Chengad9c0a32009-12-15 00:53:42 +000011430static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
11431 SDValue N0 = N->getOperand(0);
11432 SDValue N1 = N->getOperand(1);
11433 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
11434 EVT VT = N0.getValueType();
11435
11436 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
11437 // since the result of setcc_c is all zero's or all ones.
11438 if (N1C && N0.getOpcode() == ISD::AND &&
11439 N0.getOperand(1).getOpcode() == ISD::Constant) {
11440 SDValue N00 = N0.getOperand(0);
11441 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
11442 ((N00.getOpcode() == ISD::ANY_EXTEND ||
11443 N00.getOpcode() == ISD::ZERO_EXTEND) &&
11444 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
11445 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
11446 APInt ShAmt = N1C->getAPIntValue();
11447 Mask = Mask.shl(ShAmt);
11448 if (Mask != 0)
11449 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
11450 N00, DAG.getConstant(Mask, VT));
11451 }
11452 }
11453
11454 return SDValue();
11455}
Evan Cheng0b0cd912009-03-28 05:57:29 +000011456
Nate Begeman740ab032009-01-26 00:52:55 +000011457/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
11458/// when possible.
11459static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
11460 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000011461 EVT VT = N->getValueType(0);
11462 if (!VT.isVector() && VT.isInteger() &&
11463 N->getOpcode() == ISD::SHL)
11464 return PerformSHLCombine(N, DAG);
11465
Nate Begeman740ab032009-01-26 00:52:55 +000011466 // On X86 with SSE2 support, we can transform this to a vector shift if
11467 // all elements are shifted by the same amount. We can't do this in legalize
11468 // because the a constant vector is typically transformed to a constant pool
11469 // so we have no knowledge of the shift amount.
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011470 if (!Subtarget->hasSSE2())
11471 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000011472
Owen Anderson825b72b2009-08-11 20:47:22 +000011473 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011474 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000011475
Mon P Wang3becd092009-01-28 08:12:05 +000011476 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000011477 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000011478 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000011479 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000011480 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
11481 unsigned NumElts = VT.getVectorNumElements();
11482 unsigned i = 0;
11483 for (; i != NumElts; ++i) {
11484 SDValue Arg = ShAmtOp.getOperand(i);
11485 if (Arg.getOpcode() == ISD::UNDEF) continue;
11486 BaseShAmt = Arg;
11487 break;
11488 }
11489 for (; i != NumElts; ++i) {
11490 SDValue Arg = ShAmtOp.getOperand(i);
11491 if (Arg.getOpcode() == ISD::UNDEF) continue;
11492 if (Arg != BaseShAmt) {
11493 return SDValue();
11494 }
11495 }
11496 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000011497 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000011498 SDValue InVec = ShAmtOp.getOperand(0);
11499 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
11500 unsigned NumElts = InVec.getValueType().getVectorNumElements();
11501 unsigned i = 0;
11502 for (; i != NumElts; ++i) {
11503 SDValue Arg = InVec.getOperand(i);
11504 if (Arg.getOpcode() == ISD::UNDEF) continue;
11505 BaseShAmt = Arg;
11506 break;
11507 }
11508 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
11509 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000011510 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000011511 if (C->getZExtValue() == SplatIdx)
11512 BaseShAmt = InVec.getOperand(1);
11513 }
11514 }
11515 if (BaseShAmt.getNode() == 0)
11516 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
11517 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +000011518 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011519 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000011520
Mon P Wangefa42202009-09-03 19:56:25 +000011521 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000011522 if (EltVT.bitsGT(MVT::i32))
11523 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
11524 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000011525 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000011526
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011527 // The shift amount is identical so we can do a vector shift.
11528 SDValue ValOp = N->getOperand(0);
11529 switch (N->getOpcode()) {
11530 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000011531 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011532 break;
11533 case ISD::SHL:
Owen Anderson825b72b2009-08-11 20:47:22 +000011534 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011535 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011536 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011537 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011538 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011539 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011540 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011541 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011542 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011543 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011544 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011545 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011546 break;
11547 case ISD::SRA:
Owen Anderson825b72b2009-08-11 20:47:22 +000011548 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011549 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011550 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011551 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011552 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011553 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011554 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011555 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011556 break;
11557 case ISD::SRL:
Owen Anderson825b72b2009-08-11 20:47:22 +000011558 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011559 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011560 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011561 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011562 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011563 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011564 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011565 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011566 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011567 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011568 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011569 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011570 break;
Nate Begeman740ab032009-01-26 00:52:55 +000011571 }
11572 return SDValue();
11573}
11574
Nate Begemanb65c1752010-12-17 22:55:37 +000011575
11576static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
11577 TargetLowering::DAGCombinerInfo &DCI,
11578 const X86Subtarget *Subtarget) {
11579 if (DCI.isBeforeLegalizeOps())
11580 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011581
Nate Begemanb65c1752010-12-17 22:55:37 +000011582 // Want to form PANDN nodes, in the hopes of then easily combining them with
11583 // OR and AND nodes to form PBLEND/PSIGN.
11584 EVT VT = N->getValueType(0);
11585 if (VT != MVT::v2i64)
11586 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011587
Nate Begemanb65c1752010-12-17 22:55:37 +000011588 SDValue N0 = N->getOperand(0);
11589 SDValue N1 = N->getOperand(1);
11590 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011591
Nate Begemanb65c1752010-12-17 22:55:37 +000011592 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011593 if (N0.getOpcode() == ISD::XOR &&
Nate Begemanb65c1752010-12-17 22:55:37 +000011594 ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
11595 return DAG.getNode(X86ISD::PANDN, DL, VT, N0.getOperand(0), N1);
11596
11597 // Check RHS for vnot
11598 if (N1.getOpcode() == ISD::XOR &&
11599 ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
11600 return DAG.getNode(X86ISD::PANDN, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011601
Nate Begemanb65c1752010-12-17 22:55:37 +000011602 return SDValue();
11603}
11604
Evan Cheng760d1942010-01-04 21:22:48 +000011605static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000011606 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000011607 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +000011608 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000011609 return SDValue();
11610
Evan Cheng760d1942010-01-04 21:22:48 +000011611 EVT VT = N->getValueType(0);
Nate Begemanb65c1752010-12-17 22:55:37 +000011612 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64 && VT != MVT::v2i64)
Evan Cheng760d1942010-01-04 21:22:48 +000011613 return SDValue();
11614
Evan Cheng760d1942010-01-04 21:22:48 +000011615 SDValue N0 = N->getOperand(0);
11616 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011617
Nate Begemanb65c1752010-12-17 22:55:37 +000011618 // look for psign/blend
11619 if (Subtarget->hasSSSE3()) {
11620 if (VT == MVT::v2i64) {
11621 // Canonicalize pandn to RHS
11622 if (N0.getOpcode() == X86ISD::PANDN)
11623 std::swap(N0, N1);
11624 // or (and (m, x), (pandn m, y))
11625 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::PANDN) {
11626 SDValue Mask = N1.getOperand(0);
11627 SDValue X = N1.getOperand(1);
11628 SDValue Y;
11629 if (N0.getOperand(0) == Mask)
11630 Y = N0.getOperand(1);
11631 if (N0.getOperand(1) == Mask)
11632 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011633
Nate Begemanb65c1752010-12-17 22:55:37 +000011634 // Check to see if the mask appeared in both the AND and PANDN and
11635 if (!Y.getNode())
11636 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011637
Nate Begemanb65c1752010-12-17 22:55:37 +000011638 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
11639 if (Mask.getOpcode() != ISD::BITCAST ||
11640 X.getOpcode() != ISD::BITCAST ||
11641 Y.getOpcode() != ISD::BITCAST)
11642 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011643
Nate Begemanb65c1752010-12-17 22:55:37 +000011644 // Look through mask bitcast.
11645 Mask = Mask.getOperand(0);
11646 EVT MaskVT = Mask.getValueType();
11647
11648 // Validate that the Mask operand is a vector sra node. The sra node
11649 // will be an intrinsic.
11650 if (Mask.getOpcode() != ISD::INTRINSIC_WO_CHAIN)
11651 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011652
Nate Begemanb65c1752010-12-17 22:55:37 +000011653 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
11654 // there is no psrai.b
11655 switch (cast<ConstantSDNode>(Mask.getOperand(0))->getZExtValue()) {
11656 case Intrinsic::x86_sse2_psrai_w:
11657 case Intrinsic::x86_sse2_psrai_d:
11658 break;
11659 default: return SDValue();
11660 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011661
Nate Begemanb65c1752010-12-17 22:55:37 +000011662 // Check that the SRA is all signbits.
11663 SDValue SraC = Mask.getOperand(2);
11664 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
11665 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
11666 if ((SraAmt + 1) != EltBits)
11667 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011668
Nate Begemanb65c1752010-12-17 22:55:37 +000011669 DebugLoc DL = N->getDebugLoc();
11670
11671 // Now we know we at least have a plendvb with the mask val. See if
11672 // we can form a psignb/w/d.
11673 // psign = x.type == y.type == mask.type && y = sub(0, x);
11674 X = X.getOperand(0);
11675 Y = Y.getOperand(0);
11676 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
11677 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
11678 X.getValueType() == MaskVT && X.getValueType() == Y.getValueType()){
11679 unsigned Opc = 0;
11680 switch (EltBits) {
11681 case 8: Opc = X86ISD::PSIGNB; break;
11682 case 16: Opc = X86ISD::PSIGNW; break;
11683 case 32: Opc = X86ISD::PSIGND; break;
11684 default: break;
11685 }
11686 if (Opc) {
11687 SDValue Sign = DAG.getNode(Opc, DL, MaskVT, X, Mask.getOperand(1));
11688 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Sign);
11689 }
11690 }
11691 // PBLENDVB only available on SSE 4.1
11692 if (!Subtarget->hasSSE41())
11693 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011694
Nate Begemanb65c1752010-12-17 22:55:37 +000011695 X = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, X);
11696 Y = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Y);
11697 Mask = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Mask);
Nate Begeman672fb622010-12-20 22:04:24 +000011698 Mask = DAG.getNode(X86ISD::PBLENDVB, DL, MVT::v16i8, X, Y, Mask);
Nate Begemanb65c1752010-12-17 22:55:37 +000011699 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Mask);
11700 }
11701 }
11702 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011703
Nate Begemanb65c1752010-12-17 22:55:37 +000011704 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000011705 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
11706 std::swap(N0, N1);
11707 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
11708 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000011709 if (!N0.hasOneUse() || !N1.hasOneUse())
11710 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000011711
11712 SDValue ShAmt0 = N0.getOperand(1);
11713 if (ShAmt0.getValueType() != MVT::i8)
11714 return SDValue();
11715 SDValue ShAmt1 = N1.getOperand(1);
11716 if (ShAmt1.getValueType() != MVT::i8)
11717 return SDValue();
11718 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
11719 ShAmt0 = ShAmt0.getOperand(0);
11720 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
11721 ShAmt1 = ShAmt1.getOperand(0);
11722
11723 DebugLoc DL = N->getDebugLoc();
11724 unsigned Opc = X86ISD::SHLD;
11725 SDValue Op0 = N0.getOperand(0);
11726 SDValue Op1 = N1.getOperand(0);
11727 if (ShAmt0.getOpcode() == ISD::SUB) {
11728 Opc = X86ISD::SHRD;
11729 std::swap(Op0, Op1);
11730 std::swap(ShAmt0, ShAmt1);
11731 }
11732
Evan Cheng8b1190a2010-04-28 01:18:01 +000011733 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000011734 if (ShAmt1.getOpcode() == ISD::SUB) {
11735 SDValue Sum = ShAmt1.getOperand(0);
11736 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000011737 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
11738 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
11739 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
11740 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000011741 return DAG.getNode(Opc, DL, VT,
11742 Op0, Op1,
11743 DAG.getNode(ISD::TRUNCATE, DL,
11744 MVT::i8, ShAmt0));
11745 }
11746 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
11747 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
11748 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000011749 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000011750 return DAG.getNode(Opc, DL, VT,
11751 N0.getOperand(0), N1.getOperand(0),
11752 DAG.getNode(ISD::TRUNCATE, DL,
11753 MVT::i8, ShAmt0));
11754 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011755
Evan Cheng760d1942010-01-04 21:22:48 +000011756 return SDValue();
11757}
11758
Chris Lattner149a4e52008-02-22 02:09:43 +000011759/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000011760static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000011761 const X86Subtarget *Subtarget) {
Chris Lattner149a4e52008-02-22 02:09:43 +000011762 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
11763 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000011764 // A preferable solution to the general problem is to figure out the right
11765 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000011766
11767 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng7e2ff772008-05-08 00:57:18 +000011768 StoreSDNode *St = cast<StoreSDNode>(N);
Owen Andersone50ed302009-08-10 22:56:29 +000011769 EVT VT = St->getValue().getValueType();
Evan Cheng536e6672009-03-12 05:59:15 +000011770 if (VT.getSizeInBits() != 64)
11771 return SDValue();
11772
Devang Patel578efa92009-06-05 21:57:13 +000011773 const Function *F = DAG.getMachineFunction().getFunction();
11774 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Eric Christopherfd179292009-08-27 18:07:15 +000011775 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
Devang Patel578efa92009-06-05 21:57:13 +000011776 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +000011777 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000011778 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000011779 isa<LoadSDNode>(St->getValue()) &&
11780 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
11781 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000011782 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011783 LoadSDNode *Ld = 0;
11784 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000011785 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000011786 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011787 // Must be a store of a load. We currently handle two cases: the load
11788 // is a direct child, and it's under an intervening TokenFactor. It is
11789 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000011790 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000011791 Ld = cast<LoadSDNode>(St->getChain());
11792 else if (St->getValue().hasOneUse() &&
11793 ChainVal->getOpcode() == ISD::TokenFactor) {
11794 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000011795 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000011796 TokenFactorIndex = i;
11797 Ld = cast<LoadSDNode>(St->getValue());
11798 } else
11799 Ops.push_back(ChainVal->getOperand(i));
11800 }
11801 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000011802
Evan Cheng536e6672009-03-12 05:59:15 +000011803 if (!Ld || !ISD::isNormalLoad(Ld))
11804 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011805
Evan Cheng536e6672009-03-12 05:59:15 +000011806 // If this is not the MMX case, i.e. we are just turning i64 load/store
11807 // into f64 load/store, avoid the transformation if there are multiple
11808 // uses of the loaded value.
11809 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
11810 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011811
Evan Cheng536e6672009-03-12 05:59:15 +000011812 DebugLoc LdDL = Ld->getDebugLoc();
11813 DebugLoc StDL = N->getDebugLoc();
11814 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
11815 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
11816 // pair instead.
11817 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000011818 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000011819 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
11820 Ld->getPointerInfo(), Ld->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000011821 Ld->isNonTemporal(), Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000011822 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000011823 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000011824 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000011825 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000011826 Ops.size());
11827 }
Evan Cheng536e6672009-03-12 05:59:15 +000011828 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000011829 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000011830 St->isVolatile(), St->isNonTemporal(),
11831 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000011832 }
Evan Cheng536e6672009-03-12 05:59:15 +000011833
11834 // Otherwise, lower to two pairs of 32-bit loads / stores.
11835 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000011836 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
11837 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000011838
Owen Anderson825b72b2009-08-11 20:47:22 +000011839 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000011840 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000011841 Ld->isVolatile(), Ld->isNonTemporal(),
11842 Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000011843 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000011844 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000011845 Ld->isVolatile(), Ld->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000011846 MinAlign(Ld->getAlignment(), 4));
11847
11848 SDValue NewChain = LoLd.getValue(1);
11849 if (TokenFactorIndex != -1) {
11850 Ops.push_back(LoLd);
11851 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000011852 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000011853 Ops.size());
11854 }
11855
11856 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000011857 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
11858 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000011859
11860 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000011861 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000011862 St->isVolatile(), St->isNonTemporal(),
11863 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000011864 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000011865 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000011866 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000011867 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000011868 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000011869 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000011870 }
Dan Gohman475871a2008-07-27 21:46:04 +000011871 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000011872}
11873
Chris Lattner6cf73262008-01-25 06:14:17 +000011874/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
11875/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000011876static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000011877 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
11878 // F[X]OR(0.0, x) -> x
11879 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000011880 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
11881 if (C->getValueAPF().isPosZero())
11882 return N->getOperand(1);
11883 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
11884 if (C->getValueAPF().isPosZero())
11885 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000011886 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000011887}
11888
11889/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000011890static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000011891 // FAND(0.0, x) -> 0.0
11892 // FAND(x, 0.0) -> 0.0
11893 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
11894 if (C->getValueAPF().isPosZero())
11895 return N->getOperand(0);
11896 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
11897 if (C->getValueAPF().isPosZero())
11898 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000011899 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000011900}
11901
Dan Gohmane5af2d32009-01-29 01:59:02 +000011902static SDValue PerformBTCombine(SDNode *N,
11903 SelectionDAG &DAG,
11904 TargetLowering::DAGCombinerInfo &DCI) {
11905 // BT ignores high bits in the bit index operand.
11906 SDValue Op1 = N->getOperand(1);
11907 if (Op1.hasOneUse()) {
11908 unsigned BitWidth = Op1.getValueSizeInBits();
11909 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
11910 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000011911 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
11912 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000011913 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000011914 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
11915 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
11916 DCI.CommitTargetLoweringOpt(TLO);
11917 }
11918 return SDValue();
11919}
Chris Lattner83e6c992006-10-04 06:57:07 +000011920
Eli Friedman7a5e5552009-06-07 06:52:44 +000011921static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
11922 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011923 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000011924 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000011925 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000011926 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000011927 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000011928 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011929 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000011930 }
11931 return SDValue();
11932}
11933
Evan Cheng2e489c42009-12-16 00:53:11 +000011934static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
11935 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
11936 // (and (i32 x86isd::setcc_carry), 1)
11937 // This eliminates the zext. This transformation is necessary because
11938 // ISD::SETCC is always legalized to i8.
11939 DebugLoc dl = N->getDebugLoc();
11940 SDValue N0 = N->getOperand(0);
11941 EVT VT = N->getValueType(0);
11942 if (N0.getOpcode() == ISD::AND &&
11943 N0.hasOneUse() &&
11944 N0.getOperand(0).hasOneUse()) {
11945 SDValue N00 = N0.getOperand(0);
11946 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
11947 return SDValue();
11948 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
11949 if (!C || C->getZExtValue() != 1)
11950 return SDValue();
11951 return DAG.getNode(ISD::AND, dl, VT,
11952 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
11953 N00.getOperand(0), N00.getOperand(1)),
11954 DAG.getConstant(1, VT));
11955 }
11956
11957 return SDValue();
11958}
11959
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011960// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
11961static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG) {
11962 unsigned X86CC = N->getConstantOperandVal(0);
11963 SDValue EFLAG = N->getOperand(1);
11964 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011965
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011966 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
11967 // a zext and produces an all-ones bit which is more useful than 0/1 in some
11968 // cases.
11969 if (X86CC == X86::COND_B)
11970 return DAG.getNode(ISD::AND, DL, MVT::i8,
11971 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
11972 DAG.getConstant(X86CC, MVT::i8), EFLAG),
11973 DAG.getConstant(1, MVT::i8));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011974
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011975 return SDValue();
11976}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011977
Chris Lattner23a01992010-12-20 01:37:09 +000011978// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
11979static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
11980 X86TargetLowering::DAGCombinerInfo &DCI) {
11981 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
11982 // the result is either zero or one (depending on the input carry bit).
11983 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
11984 if (X86::isZeroNode(N->getOperand(0)) &&
11985 X86::isZeroNode(N->getOperand(1)) &&
11986 // We don't have a good way to replace an EFLAGS use, so only do this when
11987 // dead right now.
11988 SDValue(N, 1).use_empty()) {
11989 DebugLoc DL = N->getDebugLoc();
11990 EVT VT = N->getValueType(0);
11991 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
11992 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
11993 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
11994 DAG.getConstant(X86::COND_B,MVT::i8),
11995 N->getOperand(2)),
11996 DAG.getConstant(1, VT));
11997 return DCI.CombineTo(N, Res1, CarryOut);
11998 }
11999
12000 return SDValue();
12001}
12002
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000012003// fold (add Y, (sete X, 0)) -> adc 0, Y
12004// (add Y, (setne X, 0)) -> sbb -1, Y
12005// (sub (sete X, 0), Y) -> sbb 0, Y
12006// (sub (setne X, 0), Y) -> adc -1, Y
12007static SDValue OptimizeConditonalInDecrement(SDNode *N, SelectionDAG &DAG) {
12008 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012009
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000012010 // Look through ZExts.
12011 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
12012 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
12013 return SDValue();
12014
12015 SDValue SetCC = Ext.getOperand(0);
12016 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
12017 return SDValue();
12018
12019 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
12020 if (CC != X86::COND_E && CC != X86::COND_NE)
12021 return SDValue();
12022
12023 SDValue Cmp = SetCC.getOperand(1);
12024 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000012025 !X86::isZeroNode(Cmp.getOperand(1)) ||
12026 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000012027 return SDValue();
12028
12029 SDValue CmpOp0 = Cmp.getOperand(0);
12030 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
12031 DAG.getConstant(1, CmpOp0.getValueType()));
12032
12033 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
12034 if (CC == X86::COND_NE)
12035 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
12036 DL, OtherVal.getValueType(), OtherVal,
12037 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
12038 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
12039 DL, OtherVal.getValueType(), OtherVal,
12040 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
12041}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000012042
Dan Gohman475871a2008-07-27 21:46:04 +000012043SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000012044 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000012045 SelectionDAG &DAG = DCI.DAG;
12046 switch (N->getOpcode()) {
12047 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012048 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerc19d1c32010-12-19 22:08:31 +000012049 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, *this);
Chris Lattneraf723b92008-01-25 05:46:26 +000012050 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +000012051 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000012052 case ISD::ADD:
12053 case ISD::SUB: return OptimizeConditonalInDecrement(N, DAG);
Chris Lattner23a01992010-12-20 01:37:09 +000012054 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000012055 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000012056 case ISD::SHL:
12057 case ISD::SRA:
12058 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000012059 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000012060 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000012061 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +000012062 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000012063 case X86ISD::FOR: return PerformFORCombine(N, DAG);
12064 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000012065 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000012066 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Evan Cheng2e489c42009-12-16 00:53:11 +000012067 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG);
Chris Lattnerc19d1c32010-12-19 22:08:31 +000012068 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000012069 case X86ISD::SHUFPS: // Handle all target specific shuffles
12070 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000012071 case X86ISD::PALIGN:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000012072 case X86ISD::PUNPCKHBW:
12073 case X86ISD::PUNPCKHWD:
12074 case X86ISD::PUNPCKHDQ:
12075 case X86ISD::PUNPCKHQDQ:
12076 case X86ISD::UNPCKHPS:
12077 case X86ISD::UNPCKHPD:
12078 case X86ISD::PUNPCKLBW:
12079 case X86ISD::PUNPCKLWD:
12080 case X86ISD::PUNPCKLDQ:
12081 case X86ISD::PUNPCKLQDQ:
12082 case X86ISD::UNPCKLPS:
12083 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +000012084 case X86ISD::VUNPCKLPS:
12085 case X86ISD::VUNPCKLPD:
12086 case X86ISD::VUNPCKLPSY:
12087 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000012088 case X86ISD::MOVHLPS:
12089 case X86ISD::MOVLHPS:
12090 case X86ISD::PSHUFD:
12091 case X86ISD::PSHUFHW:
12092 case X86ISD::PSHUFLW:
12093 case X86ISD::MOVSS:
12094 case X86ISD::MOVSD:
Mon P Wanga0fd0d52010-12-19 23:55:53 +000012095 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI);
Evan Cheng206ee9d2006-07-07 08:33:52 +000012096 }
12097
Dan Gohman475871a2008-07-27 21:46:04 +000012098 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000012099}
12100
Evan Chenge5b51ac2010-04-17 06:13:15 +000012101/// isTypeDesirableForOp - Return true if the target has native support for
12102/// the specified value type and it is 'desirable' to use the type for the
12103/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
12104/// instruction encodings are longer and some i16 instructions are slow.
12105bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
12106 if (!isTypeLegal(VT))
12107 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000012108 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000012109 return true;
12110
12111 switch (Opc) {
12112 default:
12113 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000012114 case ISD::LOAD:
12115 case ISD::SIGN_EXTEND:
12116 case ISD::ZERO_EXTEND:
12117 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000012118 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000012119 case ISD::SRL:
12120 case ISD::SUB:
12121 case ISD::ADD:
12122 case ISD::MUL:
12123 case ISD::AND:
12124 case ISD::OR:
12125 case ISD::XOR:
12126 return false;
12127 }
12128}
12129
12130/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000012131/// beneficial for dag combiner to promote the specified node. If true, it
12132/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000012133bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000012134 EVT VT = Op.getValueType();
12135 if (VT != MVT::i16)
12136 return false;
12137
Evan Cheng4c26e932010-04-19 19:29:22 +000012138 bool Promote = false;
12139 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000012140 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000012141 default: break;
12142 case ISD::LOAD: {
12143 LoadSDNode *LD = cast<LoadSDNode>(Op);
12144 // If the non-extending load has a single use and it's not live out, then it
12145 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000012146 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
12147 Op.hasOneUse()*/) {
12148 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
12149 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
12150 // The only case where we'd want to promote LOAD (rather then it being
12151 // promoted as an operand is when it's only use is liveout.
12152 if (UI->getOpcode() != ISD::CopyToReg)
12153 return false;
12154 }
12155 }
Evan Cheng4c26e932010-04-19 19:29:22 +000012156 Promote = true;
12157 break;
12158 }
12159 case ISD::SIGN_EXTEND:
12160 case ISD::ZERO_EXTEND:
12161 case ISD::ANY_EXTEND:
12162 Promote = true;
12163 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000012164 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000012165 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000012166 SDValue N0 = Op.getOperand(0);
12167 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000012168 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000012169 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000012170 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000012171 break;
12172 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000012173 case ISD::ADD:
12174 case ISD::MUL:
12175 case ISD::AND:
12176 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000012177 case ISD::XOR:
12178 Commute = true;
12179 // fallthrough
12180 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000012181 SDValue N0 = Op.getOperand(0);
12182 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000012183 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000012184 return false;
12185 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000012186 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000012187 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000012188 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000012189 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000012190 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000012191 }
12192 }
12193
12194 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000012195 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000012196}
12197
Evan Cheng60c07e12006-07-05 22:17:51 +000012198//===----------------------------------------------------------------------===//
12199// X86 Inline Assembly Support
12200//===----------------------------------------------------------------------===//
12201
Chris Lattnerb8105652009-07-20 17:51:36 +000012202bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
12203 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000012204
12205 std::string AsmStr = IA->getAsmString();
12206
12207 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000012208 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000012209 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000012210
12211 switch (AsmPieces.size()) {
12212 default: return false;
12213 case 1:
12214 AsmStr = AsmPieces[0];
12215 AsmPieces.clear();
12216 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
12217
Chris Lattner7a2bdde2011-04-15 05:18:47 +000012218 // FIXME: this should verify that we are targeting a 486 or better. If not,
Evan Cheng55d42002011-01-08 01:24:27 +000012219 // we will turn this bswap into something that will be lowered to logical ops
12220 // instead of emitting the bswap asm. For now, we don't support 486 or lower
12221 // so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000012222 // bswap $0
12223 if (AsmPieces.size() == 2 &&
12224 (AsmPieces[0] == "bswap" ||
12225 AsmPieces[0] == "bswapq" ||
12226 AsmPieces[0] == "bswapl") &&
12227 (AsmPieces[1] == "$0" ||
12228 AsmPieces[1] == "${0:q}")) {
12229 // No need to check constraints, nothing other than the equivalent of
12230 // "=r,0" would be valid here.
Evan Cheng55d42002011-01-08 01:24:27 +000012231 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
12232 if (!Ty || Ty->getBitWidth() % 16 != 0)
12233 return false;
12234 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000012235 }
12236 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000012237 if (CI->getType()->isIntegerTy(16) &&
Chris Lattnerb8105652009-07-20 17:51:36 +000012238 AsmPieces.size() == 3 &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000012239 (AsmPieces[0] == "rorw" || AsmPieces[0] == "rolw") &&
Chris Lattnerb8105652009-07-20 17:51:36 +000012240 AsmPieces[1] == "$$8," &&
12241 AsmPieces[2] == "${0:w}" &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000012242 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
12243 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000012244 const std::string &ConstraintsStr = IA->getConstraintString();
12245 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000012246 std::sort(AsmPieces.begin(), AsmPieces.end());
12247 if (AsmPieces.size() == 4 &&
12248 AsmPieces[0] == "~{cc}" &&
12249 AsmPieces[1] == "~{dirflag}" &&
12250 AsmPieces[2] == "~{flags}" &&
12251 AsmPieces[3] == "~{fpsr}") {
Evan Cheng55d42002011-01-08 01:24:27 +000012252 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
12253 if (!Ty || Ty->getBitWidth() % 16 != 0)
12254 return false;
12255 return IntrinsicLowering::LowerToByteSwap(CI);
Dan Gohman0ef701e2010-03-04 19:58:08 +000012256 }
Chris Lattnerb8105652009-07-20 17:51:36 +000012257 }
12258 break;
12259 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000012260 if (CI->getType()->isIntegerTy(32) &&
12261 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
12262 SmallVector<StringRef, 4> Words;
12263 SplitString(AsmPieces[0], Words, " \t,");
12264 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
12265 Words[2] == "${0:w}") {
12266 Words.clear();
12267 SplitString(AsmPieces[1], Words, " \t,");
12268 if (Words.size() == 3 && Words[0] == "rorl" && Words[1] == "$$16" &&
12269 Words[2] == "$0") {
12270 Words.clear();
12271 SplitString(AsmPieces[2], Words, " \t,");
12272 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
12273 Words[2] == "${0:w}") {
12274 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000012275 const std::string &ConstraintsStr = IA->getConstraintString();
12276 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Peter Collingbourne948cf022010-11-13 19:54:30 +000012277 std::sort(AsmPieces.begin(), AsmPieces.end());
12278 if (AsmPieces.size() == 4 &&
12279 AsmPieces[0] == "~{cc}" &&
12280 AsmPieces[1] == "~{dirflag}" &&
12281 AsmPieces[2] == "~{flags}" &&
12282 AsmPieces[3] == "~{fpsr}") {
Evan Cheng55d42002011-01-08 01:24:27 +000012283 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
12284 if (!Ty || Ty->getBitWidth() % 16 != 0)
12285 return false;
12286 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000012287 }
12288 }
12289 }
12290 }
12291 }
Evan Cheng55d42002011-01-08 01:24:27 +000012292
12293 if (CI->getType()->isIntegerTy(64)) {
12294 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
12295 if (Constraints.size() >= 2 &&
12296 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
12297 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
12298 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
12299 SmallVector<StringRef, 4> Words;
12300 SplitString(AsmPieces[0], Words, " \t");
12301 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
Chris Lattnerb8105652009-07-20 17:51:36 +000012302 Words.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000012303 SplitString(AsmPieces[1], Words, " \t");
12304 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
12305 Words.clear();
12306 SplitString(AsmPieces[2], Words, " \t,");
12307 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
12308 Words[2] == "%edx") {
12309 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
12310 if (!Ty || Ty->getBitWidth() % 16 != 0)
12311 return false;
12312 return IntrinsicLowering::LowerToByteSwap(CI);
12313 }
Chris Lattnerb8105652009-07-20 17:51:36 +000012314 }
12315 }
12316 }
12317 }
12318 break;
12319 }
12320 return false;
12321}
12322
12323
12324
Chris Lattnerf4dff842006-07-11 02:54:03 +000012325/// getConstraintType - Given a constraint letter, return the type of
12326/// constraint it is for this target.
12327X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000012328X86TargetLowering::getConstraintType(const std::string &Constraint) const {
12329 if (Constraint.size() == 1) {
12330 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000012331 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000012332 case 'q':
12333 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000012334 case 'f':
12335 case 't':
12336 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000012337 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000012338 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000012339 case 'Y':
12340 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000012341 case 'a':
12342 case 'b':
12343 case 'c':
12344 case 'd':
12345 case 'S':
12346 case 'D':
12347 case 'A':
12348 return C_Register;
12349 case 'I':
12350 case 'J':
12351 case 'K':
12352 case 'L':
12353 case 'M':
12354 case 'N':
12355 case 'G':
12356 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000012357 case 'e':
12358 case 'Z':
12359 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000012360 default:
12361 break;
12362 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000012363 }
Chris Lattner4234f572007-03-25 02:14:49 +000012364 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000012365}
12366
John Thompson44ab89e2010-10-29 17:29:13 +000012367/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000012368/// This object must already have been set up with the operand type
12369/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000012370TargetLowering::ConstraintWeight
12371 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000012372 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000012373 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012374 Value *CallOperandVal = info.CallOperandVal;
12375 // If we don't have a value, we can't do a match,
12376 // but allow it at the lowest weight.
12377 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000012378 return CW_Default;
12379 const Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000012380 // Look at the constraint type.
12381 switch (*constraint) {
12382 default:
John Thompson44ab89e2010-10-29 17:29:13 +000012383 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
12384 case 'R':
12385 case 'q':
12386 case 'Q':
12387 case 'a':
12388 case 'b':
12389 case 'c':
12390 case 'd':
12391 case 'S':
12392 case 'D':
12393 case 'A':
12394 if (CallOperandVal->getType()->isIntegerTy())
12395 weight = CW_SpecificReg;
12396 break;
12397 case 'f':
12398 case 't':
12399 case 'u':
12400 if (type->isFloatingPointTy())
12401 weight = CW_SpecificReg;
12402 break;
12403 case 'y':
Chris Lattner2a786eb2010-12-19 20:19:20 +000012404 if (type->isX86_MMXTy() && Subtarget->hasMMX())
John Thompson44ab89e2010-10-29 17:29:13 +000012405 weight = CW_SpecificReg;
12406 break;
12407 case 'x':
12408 case 'Y':
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012409 if ((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasXMM())
John Thompson44ab89e2010-10-29 17:29:13 +000012410 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012411 break;
12412 case 'I':
12413 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
12414 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000012415 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012416 }
12417 break;
John Thompson44ab89e2010-10-29 17:29:13 +000012418 case 'J':
12419 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12420 if (C->getZExtValue() <= 63)
12421 weight = CW_Constant;
12422 }
12423 break;
12424 case 'K':
12425 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12426 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
12427 weight = CW_Constant;
12428 }
12429 break;
12430 case 'L':
12431 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12432 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
12433 weight = CW_Constant;
12434 }
12435 break;
12436 case 'M':
12437 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12438 if (C->getZExtValue() <= 3)
12439 weight = CW_Constant;
12440 }
12441 break;
12442 case 'N':
12443 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12444 if (C->getZExtValue() <= 0xff)
12445 weight = CW_Constant;
12446 }
12447 break;
12448 case 'G':
12449 case 'C':
12450 if (dyn_cast<ConstantFP>(CallOperandVal)) {
12451 weight = CW_Constant;
12452 }
12453 break;
12454 case 'e':
12455 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12456 if ((C->getSExtValue() >= -0x80000000LL) &&
12457 (C->getSExtValue() <= 0x7fffffffLL))
12458 weight = CW_Constant;
12459 }
12460 break;
12461 case 'Z':
12462 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12463 if (C->getZExtValue() <= 0xffffffff)
12464 weight = CW_Constant;
12465 }
12466 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012467 }
12468 return weight;
12469}
12470
Dale Johannesenba2a0b92008-01-29 02:21:21 +000012471/// LowerXConstraint - try to replace an X constraint, which matches anything,
12472/// with another that has more specific requirements based on the type of the
12473/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000012474const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000012475LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000012476 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
12477 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000012478 if (ConstraintVT.isFloatingPoint()) {
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012479 if (Subtarget->hasXMMInt())
Chris Lattner5e764232008-04-26 23:02:14 +000012480 return "Y";
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012481 if (Subtarget->hasXMM())
Chris Lattner5e764232008-04-26 23:02:14 +000012482 return "x";
12483 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012484
Chris Lattner5e764232008-04-26 23:02:14 +000012485 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000012486}
12487
Chris Lattner48884cd2007-08-25 00:47:38 +000012488/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
12489/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000012490void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattner48884cd2007-08-25 00:47:38 +000012491 char Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000012492 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000012493 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000012494 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000012495
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012496 switch (Constraint) {
12497 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000012498 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000012499 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000012500 if (C->getZExtValue() <= 31) {
12501 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000012502 break;
12503 }
Devang Patel84f7fd22007-03-17 00:13:28 +000012504 }
Chris Lattner48884cd2007-08-25 00:47:38 +000012505 return;
Evan Cheng364091e2008-09-22 23:57:37 +000012506 case 'J':
12507 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000012508 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000012509 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
12510 break;
12511 }
12512 }
12513 return;
12514 case 'K':
12515 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000012516 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000012517 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
12518 break;
12519 }
12520 }
12521 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000012522 case 'N':
12523 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000012524 if (C->getZExtValue() <= 255) {
12525 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000012526 break;
12527 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000012528 }
Chris Lattner48884cd2007-08-25 00:47:38 +000012529 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000012530 case 'e': {
12531 // 32-bit signed value
12532 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000012533 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
12534 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000012535 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000012536 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000012537 break;
12538 }
12539 // FIXME gcc accepts some relocatable values here too, but only in certain
12540 // memory models; it's complicated.
12541 }
12542 return;
12543 }
12544 case 'Z': {
12545 // 32-bit unsigned value
12546 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000012547 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
12548 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000012549 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
12550 break;
12551 }
12552 }
12553 // FIXME gcc accepts some relocatable values here too, but only in certain
12554 // memory models; it's complicated.
12555 return;
12556 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012557 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012558 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000012559 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000012560 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000012561 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000012562 break;
12563 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012564
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000012565 // In any sort of PIC mode addresses need to be computed at runtime by
12566 // adding in a register or some sort of table lookup. These can't
12567 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000012568 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000012569 return;
12570
Chris Lattnerdc43a882007-05-03 16:52:29 +000012571 // If we are in non-pic codegen mode, we allow the address of a global (with
12572 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000012573 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000012574 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000012575
Chris Lattner49921962009-05-08 18:23:14 +000012576 // Match either (GA), (GA+C), (GA+C1+C2), etc.
12577 while (1) {
12578 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
12579 Offset += GA->getOffset();
12580 break;
12581 } else if (Op.getOpcode() == ISD::ADD) {
12582 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
12583 Offset += C->getZExtValue();
12584 Op = Op.getOperand(0);
12585 continue;
12586 }
12587 } else if (Op.getOpcode() == ISD::SUB) {
12588 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
12589 Offset += -C->getZExtValue();
12590 Op = Op.getOperand(0);
12591 continue;
12592 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012593 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012594
Chris Lattner49921962009-05-08 18:23:14 +000012595 // Otherwise, this isn't something we can handle, reject it.
12596 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000012597 }
Eric Christopherfd179292009-08-27 18:07:15 +000012598
Dan Gohman46510a72010-04-15 01:51:59 +000012599 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012600 // If we require an extra load to get this address, as in PIC mode, we
12601 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000012602 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
12603 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012604 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000012605
Devang Patel0d881da2010-07-06 22:08:15 +000012606 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
12607 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000012608 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012609 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012610 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012611
Gabor Greifba36cb52008-08-28 21:40:38 +000012612 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000012613 Ops.push_back(Result);
12614 return;
12615 }
Dale Johannesen1784d162010-06-25 21:55:36 +000012616 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012617}
12618
Chris Lattner259e97c2006-01-31 19:43:35 +000012619std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +000012620getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000012621 EVT VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +000012622 if (Constraint.size() == 1) {
12623 // FIXME: not handling fp-stack yet!
Chris Lattner259e97c2006-01-31 19:43:35 +000012624 switch (Constraint[0]) { // GCC X86 Constraint Letters
Chris Lattnerf4dff842006-07-11 02:54:03 +000012625 default: break; // Unknown constraint letter
Evan Cheng47e9fab2009-07-17 22:13:25 +000012626 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
12627 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000012628 if (VT == MVT::i32)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012629 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
12630 X86::ESI, X86::EDI, X86::R8D, X86::R9D,
12631 X86::R10D,X86::R11D,X86::R12D,
12632 X86::R13D,X86::R14D,X86::R15D,
12633 X86::EBP, X86::ESP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012634 else if (VT == MVT::i16)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012635 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
12636 X86::SI, X86::DI, X86::R8W,X86::R9W,
12637 X86::R10W,X86::R11W,X86::R12W,
12638 X86::R13W,X86::R14W,X86::R15W,
12639 X86::BP, X86::SP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012640 else if (VT == MVT::i8)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012641 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL,
12642 X86::SIL, X86::DIL, X86::R8B,X86::R9B,
12643 X86::R10B,X86::R11B,X86::R12B,
12644 X86::R13B,X86::R14B,X86::R15B,
12645 X86::BPL, X86::SPL, 0);
12646
Owen Anderson825b72b2009-08-11 20:47:22 +000012647 else if (VT == MVT::i64)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012648 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX,
12649 X86::RSI, X86::RDI, X86::R8, X86::R9,
12650 X86::R10, X86::R11, X86::R12,
12651 X86::R13, X86::R14, X86::R15,
12652 X86::RBP, X86::RSP, 0);
12653
12654 break;
12655 }
Eric Christopherfd179292009-08-27 18:07:15 +000012656 // 32-bit fallthrough
Chris Lattner259e97c2006-01-31 19:43:35 +000012657 case 'Q': // Q_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000012658 if (VT == MVT::i32)
Chris Lattner80a7ecc2006-05-06 00:29:37 +000012659 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012660 else if (VT == MVT::i16)
Chris Lattner80a7ecc2006-05-06 00:29:37 +000012661 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012662 else if (VT == MVT::i8)
Evan Cheng12914382007-08-13 23:27:11 +000012663 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012664 else if (VT == MVT::i64)
Chris Lattner03e6c702007-11-04 06:51:12 +000012665 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
12666 break;
Chris Lattner259e97c2006-01-31 19:43:35 +000012667 }
12668 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012669
Chris Lattner1efa40f2006-02-22 00:56:39 +000012670 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +000012671}
Chris Lattnerf76d1802006-07-31 23:26:50 +000012672
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012673std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000012674X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000012675 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000012676 // First, see if this is a constraint that directly corresponds to an LLVM
12677 // register class.
12678 if (Constraint.size() == 1) {
12679 // GCC Constraint Letters
12680 switch (Constraint[0]) {
12681 default: break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000012682 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000012683 case 'l': // INDEX_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000012684 if (VT == MVT::i8)
Chris Lattner0f65cad2007-04-09 05:49:22 +000012685 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012686 if (VT == MVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +000012687 return std::make_pair(0U, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012688 if (VT == MVT::i32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +000012689 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +000012690 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000012691 case 'R': // LEGACY_REGS
12692 if (VT == MVT::i8)
12693 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
12694 if (VT == MVT::i16)
12695 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
12696 if (VT == MVT::i32 || !Subtarget->is64Bit())
12697 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
12698 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000012699 case 'f': // FP Stack registers.
12700 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
12701 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000012702 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000012703 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012704 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000012705 return std::make_pair(0U, X86::RFP64RegisterClass);
12706 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000012707 case 'y': // MMX_REGS if MMX allowed.
12708 if (!Subtarget->hasMMX()) break;
12709 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000012710 case 'Y': // SSE_REGS if SSE2 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012711 if (!Subtarget->hasXMMInt()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000012712 // FALL THROUGH.
12713 case 'x': // SSE_REGS if SSE1 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012714 if (!Subtarget->hasXMM()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000012715
Owen Anderson825b72b2009-08-11 20:47:22 +000012716 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000012717 default: break;
12718 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000012719 case MVT::f32:
12720 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +000012721 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012722 case MVT::f64:
12723 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +000012724 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000012725 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000012726 case MVT::v16i8:
12727 case MVT::v8i16:
12728 case MVT::v4i32:
12729 case MVT::v2i64:
12730 case MVT::v4f32:
12731 case MVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +000012732 return std::make_pair(0U, X86::VR128RegisterClass);
12733 }
Chris Lattnerad043e82007-04-09 05:11:28 +000012734 break;
12735 }
12736 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012737
Chris Lattnerf76d1802006-07-31 23:26:50 +000012738 // Use the default implementation in TargetLowering to convert the register
12739 // constraint into a member of a register class.
12740 std::pair<unsigned, const TargetRegisterClass*> Res;
12741 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000012742
12743 // Not found as a standard register?
12744 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000012745 // Map st(0) -> st(7) -> ST0
12746 if (Constraint.size() == 7 && Constraint[0] == '{' &&
12747 tolower(Constraint[1]) == 's' &&
12748 tolower(Constraint[2]) == 't' &&
12749 Constraint[3] == '(' &&
12750 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
12751 Constraint[5] == ')' &&
12752 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000012753
Chris Lattner56d77c72009-09-13 22:41:48 +000012754 Res.first = X86::ST0+Constraint[4]-'0';
12755 Res.second = X86::RFP80RegisterClass;
12756 return Res;
12757 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000012758
Chris Lattner56d77c72009-09-13 22:41:48 +000012759 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000012760 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000012761 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +000012762 Res.second = X86::RFP80RegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000012763 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000012764 }
Chris Lattner56d77c72009-09-13 22:41:48 +000012765
12766 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000012767 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000012768 Res.first = X86::EFLAGS;
12769 Res.second = X86::CCRRegisterClass;
12770 return Res;
12771 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000012772
Dale Johannesen330169f2008-11-13 21:52:36 +000012773 // 'A' means EAX + EDX.
12774 if (Constraint == "A") {
12775 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +000012776 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000012777 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000012778 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000012779 return Res;
12780 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012781
Chris Lattnerf76d1802006-07-31 23:26:50 +000012782 // Otherwise, check to see if this is a register class of the wrong value
12783 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
12784 // turn into {ax},{dx}.
12785 if (Res.second->hasType(VT))
12786 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012787
Chris Lattnerf76d1802006-07-31 23:26:50 +000012788 // All of the single-register GCC register classes map their values onto
12789 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
12790 // really want an 8-bit or 32-bit register, map to the appropriate register
12791 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +000012792 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000012793 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000012794 unsigned DestReg = 0;
12795 switch (Res.first) {
12796 default: break;
12797 case X86::AX: DestReg = X86::AL; break;
12798 case X86::DX: DestReg = X86::DL; break;
12799 case X86::CX: DestReg = X86::CL; break;
12800 case X86::BX: DestReg = X86::BL; break;
12801 }
12802 if (DestReg) {
12803 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000012804 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000012805 }
Owen Anderson825b72b2009-08-11 20:47:22 +000012806 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000012807 unsigned DestReg = 0;
12808 switch (Res.first) {
12809 default: break;
12810 case X86::AX: DestReg = X86::EAX; break;
12811 case X86::DX: DestReg = X86::EDX; break;
12812 case X86::CX: DestReg = X86::ECX; break;
12813 case X86::BX: DestReg = X86::EBX; break;
12814 case X86::SI: DestReg = X86::ESI; break;
12815 case X86::DI: DestReg = X86::EDI; break;
12816 case X86::BP: DestReg = X86::EBP; break;
12817 case X86::SP: DestReg = X86::ESP; break;
12818 }
12819 if (DestReg) {
12820 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000012821 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000012822 }
Owen Anderson825b72b2009-08-11 20:47:22 +000012823 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000012824 unsigned DestReg = 0;
12825 switch (Res.first) {
12826 default: break;
12827 case X86::AX: DestReg = X86::RAX; break;
12828 case X86::DX: DestReg = X86::RDX; break;
12829 case X86::CX: DestReg = X86::RCX; break;
12830 case X86::BX: DestReg = X86::RBX; break;
12831 case X86::SI: DestReg = X86::RSI; break;
12832 case X86::DI: DestReg = X86::RDI; break;
12833 case X86::BP: DestReg = X86::RBP; break;
12834 case X86::SP: DestReg = X86::RSP; break;
12835 }
12836 if (DestReg) {
12837 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000012838 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000012839 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000012840 }
Chris Lattner6ba50a92008-08-26 06:19:02 +000012841 } else if (Res.second == X86::FR32RegisterClass ||
12842 Res.second == X86::FR64RegisterClass ||
12843 Res.second == X86::VR128RegisterClass) {
12844 // Handle references to XMM physical registers that got mapped into the
12845 // wrong class. This can happen with constraints like {xmm0} where the
12846 // target independent register mapper will just pick the first match it can
12847 // find, ignoring the required type.
Owen Anderson825b72b2009-08-11 20:47:22 +000012848 if (VT == MVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +000012849 Res.second = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +000012850 else if (VT == MVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +000012851 Res.second = X86::FR64RegisterClass;
12852 else if (X86::VR128RegisterClass->hasType(VT))
12853 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000012854 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012855
Chris Lattnerf76d1802006-07-31 23:26:50 +000012856 return Res;
12857}