blob: 01cea3b0a784df3615f2f0e45276228fcc92dd5a [file] [log] [blame]
Arnold Schwaighofer92226dd2007-10-12 21:53:12 +00001//===-- X86ISelLowering.cpp - X86 DAG Lowering Implementation -------------===//
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that X86 uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
Evan Chengb1712452010-01-27 06:25:16 +000015#define DEBUG_TYPE "x86-isel"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000016#include "X86.h"
Evan Cheng0cc39452006-01-16 21:21:29 +000017#include "X86InstrBuilder.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000018#include "X86ISelLowering.h"
19#include "X86TargetMachine.h"
Chris Lattner8c6ed052009-09-16 01:46:41 +000020#include "X86TargetObjectFile.h"
David Greene583b68f2011-02-17 19:18:59 +000021#include "Utils/X86ShuffleDecode.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000022#include "llvm/CallingConv.h"
Evan Cheng223547a2006-01-31 22:28:30 +000023#include "llvm/Constants.h"
Evan Cheng347d5f72006-04-28 21:29:37 +000024#include "llvm/DerivedTypes.h"
Chris Lattnerb903bed2009-06-26 21:20:29 +000025#include "llvm/GlobalAlias.h"
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000026#include "llvm/GlobalVariable.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000027#include "llvm/Function.h"
Chris Lattnerb8105652009-07-20 17:51:36 +000028#include "llvm/Instructions.h"
Evan Cheng6be2c582006-04-05 23:38:46 +000029#include "llvm/Intrinsics.h"
Owen Andersona90b3dc2009-07-15 21:51:10 +000030#include "llvm/LLVMContext.h"
Evan Cheng55d42002011-01-08 01:24:27 +000031#include "llvm/CodeGen/IntrinsicLowering.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000032#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng4a460802006-01-11 00:33:36 +000033#include "llvm/CodeGen/MachineFunction.h"
34#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner5e1df8d2010-01-25 23:38:14 +000035#include "llvm/CodeGen/MachineJumpTableInfo.h"
Evan Chenga844bde2008-02-02 04:07:54 +000036#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000037#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman69de1932008-02-06 22:27:42 +000038#include "llvm/CodeGen/PseudoSourceValue.h"
Chris Lattner589c6f62010-01-26 06:28:43 +000039#include "llvm/MC/MCAsmInfo.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000040#include "llvm/MC/MCContext.h"
Daniel Dunbar4e815f82010-03-15 23:51:06 +000041#include "llvm/MC/MCExpr.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000042#include "llvm/MC/MCSymbol.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000043#include "llvm/ADT/BitVector.h"
Evan Cheng14b32e12007-12-11 01:46:18 +000044#include "llvm/ADT/SmallSet.h"
Evan Chengb1712452010-01-27 06:25:16 +000045#include "llvm/ADT/Statistic.h"
Chris Lattner1a60aa72006-10-31 19:42:44 +000046#include "llvm/ADT/StringExtras.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000047#include "llvm/ADT/VectorExtras.h"
Evan Cheng485fafc2011-03-21 01:19:09 +000048#include "llvm/Support/CallSite.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000049#include "llvm/Support/Debug.h"
Bill Wendlingec041eb2010-03-12 19:20:40 +000050#include "llvm/Support/Dwarf.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000051#include "llvm/Support/ErrorHandling.h"
52#include "llvm/Support/MathExtras.h"
Torok Edwindac237e2009-07-08 20:53:28 +000053#include "llvm/Support/raw_ostream.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000054using namespace llvm;
Bill Wendlingec041eb2010-03-12 19:20:40 +000055using namespace dwarf;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000056
Evan Chengb1712452010-01-27 06:25:16 +000057STATISTIC(NumTailCalls, "Number of tail calls");
58
Evan Cheng10e86422008-04-25 19:11:04 +000059// Forward declarations.
Owen Andersone50ed302009-08-10 22:56:29 +000060static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +000061 SDValue V2);
Evan Cheng10e86422008-04-25 19:11:04 +000062
David Greenea5f26012011-02-07 19:36:54 +000063static SDValue Insert128BitVector(SDValue Result,
64 SDValue Vec,
65 SDValue Idx,
66 SelectionDAG &DAG,
67 DebugLoc dl);
David Greenef125a292011-02-08 19:04:41 +000068
David Greenea5f26012011-02-07 19:36:54 +000069static SDValue Extract128BitVector(SDValue Vec,
70 SDValue Idx,
71 SelectionDAG &DAG,
72 DebugLoc dl);
73
David Greenef125a292011-02-08 19:04:41 +000074static SDValue ConcatVectors(SDValue Lower, SDValue Upper, SelectionDAG &DAG);
75
76
David Greenea5f26012011-02-07 19:36:54 +000077/// Generate a DAG to grab 128-bits from a vector > 128 bits. This
78/// sets things up to match to an AVX VEXTRACTF128 instruction or a
David Greene74a579d2011-02-10 16:57:36 +000079/// simple subregister reference. Idx is an index in the 128 bits we
80/// want. It need not be aligned to a 128-bit bounday. That makes
81/// lowering EXTRACT_VECTOR_ELT operations easier.
David Greenea5f26012011-02-07 19:36:54 +000082static SDValue Extract128BitVector(SDValue Vec,
83 SDValue Idx,
84 SelectionDAG &DAG,
85 DebugLoc dl) {
86 EVT VT = Vec.getValueType();
87 assert(VT.getSizeInBits() == 256 && "Unexpected vector size!");
88
89 EVT ElVT = VT.getVectorElementType();
90
91 int Factor = VT.getSizeInBits() / 128;
92
93 EVT ResultVT = EVT::getVectorVT(*DAG.getContext(),
94 ElVT,
95 VT.getVectorNumElements() / Factor);
96
97 // Extract from UNDEF is UNDEF.
98 if (Vec.getOpcode() == ISD::UNDEF)
99 return DAG.getNode(ISD::UNDEF, dl, ResultVT);
100
101 if (isa<ConstantSDNode>(Idx)) {
102 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
103
104 // Extract the relevant 128 bits. Generate an EXTRACT_SUBVECTOR
105 // we can match to VEXTRACTF128.
106 unsigned ElemsPerChunk = 128 / ElVT.getSizeInBits();
107
108 // This is the index of the first element of the 128-bit chunk
109 // we want.
110 unsigned NormalizedIdxVal = (((IdxVal * ElVT.getSizeInBits()) / 128)
111 * ElemsPerChunk);
112
113 SDValue VecIdx = DAG.getConstant(NormalizedIdxVal, MVT::i32);
114
115 SDValue Result = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, ResultVT, Vec,
116 VecIdx);
117
118 return Result;
119 }
120
121 return SDValue();
122}
123
124/// Generate a DAG to put 128-bits into a vector > 128 bits. This
125/// sets things up to match to an AVX VINSERTF128 instruction or a
David Greene6b381262011-02-09 15:32:06 +0000126/// simple superregister reference. Idx is an index in the 128 bits
127/// we want. It need not be aligned to a 128-bit bounday. That makes
128/// lowering INSERT_VECTOR_ELT operations easier.
David Greenea5f26012011-02-07 19:36:54 +0000129static SDValue Insert128BitVector(SDValue Result,
130 SDValue Vec,
131 SDValue Idx,
132 SelectionDAG &DAG,
133 DebugLoc dl) {
134 if (isa<ConstantSDNode>(Idx)) {
135 EVT VT = Vec.getValueType();
136 assert(VT.getSizeInBits() == 128 && "Unexpected vector size!");
137
138 EVT ElVT = VT.getVectorElementType();
139
140 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
141
142 EVT ResultVT = Result.getValueType();
143
144 // Insert the relevant 128 bits.
145 unsigned ElemsPerChunk = 128 / ElVT.getSizeInBits();
146
147 // This is the index of the first element of the 128-bit chunk
148 // we want.
149 unsigned NormalizedIdxVal = (((IdxVal * ElVT.getSizeInBits()) / 128)
150 * ElemsPerChunk);
151
152 SDValue VecIdx = DAG.getConstant(NormalizedIdxVal, MVT::i32);
153
154 Result = DAG.getNode(ISD::INSERT_SUBVECTOR, dl, ResultVT, Result, Vec,
155 VecIdx);
156 return Result;
157 }
158
159 return SDValue();
160}
161
David Greenef125a292011-02-08 19:04:41 +0000162/// Given two vectors, concat them.
163static SDValue ConcatVectors(SDValue Lower, SDValue Upper, SelectionDAG &DAG) {
164 DebugLoc dl = Lower.getDebugLoc();
165
166 assert(Lower.getValueType() == Upper.getValueType() && "Mismatched vectors!");
167
168 EVT VT = EVT::getVectorVT(*DAG.getContext(),
169 Lower.getValueType().getVectorElementType(),
170 Lower.getValueType().getVectorNumElements() * 2);
171
172 // TODO: Generalize to arbitrary vector length (this assumes 256-bit vectors).
173 assert(VT.getSizeInBits() == 256 && "Unsupported vector concat!");
174
175 // Insert the upper subvector.
176 SDValue Vec = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT), Upper,
177 DAG.getConstant(
178 // This is half the length of the result
179 // vector. Start inserting the upper 128
180 // bits here.
181 Lower.getValueType().getVectorNumElements(),
182 MVT::i32),
183 DAG, dl);
184
185 // Insert the lower subvector.
186 Vec = Insert128BitVector(Vec, Lower, DAG.getConstant(0, MVT::i32), DAG, dl);
187 return Vec;
188}
189
Chris Lattnerf0144122009-07-28 03:13:23 +0000190static TargetLoweringObjectFile *createTLOF(X86TargetMachine &TM) {
Evan Cheng2bffee22011-02-01 01:14:13 +0000191 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
192 bool is64Bit = Subtarget->is64Bit();
NAKAMURA Takumi27635382011-02-05 15:10:54 +0000193
Evan Cheng2bffee22011-02-01 01:14:13 +0000194 if (Subtarget->isTargetEnvMacho()) {
Chris Lattnere019ec12010-12-19 20:07:10 +0000195 if (is64Bit)
196 return new X8664_MachoTargetObjectFile();
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000197 return new TargetLoweringObjectFileMachO();
Michael J. Spencerec38de22010-10-10 22:04:20 +0000198 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000199
Evan Cheng2bffee22011-02-01 01:14:13 +0000200 if (Subtarget->isTargetELF()) {
Chris Lattnere019ec12010-12-19 20:07:10 +0000201 if (is64Bit)
202 return new X8664_ELFTargetObjectFile(TM);
203 return new X8632_ELFTargetObjectFile(TM);
204 }
Evan Cheng2bffee22011-02-01 01:14:13 +0000205 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
Chris Lattnere019ec12010-12-19 20:07:10 +0000206 return new TargetLoweringObjectFileCOFF();
Eric Christopher62f35a22010-07-05 19:26:33 +0000207 llvm_unreachable("unknown subtarget type");
Chris Lattnerf0144122009-07-28 03:13:23 +0000208}
209
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +0000210X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
Chris Lattnerf0144122009-07-28 03:13:23 +0000211 : TargetLowering(TM, createTLOF(TM)) {
Evan Cheng559806f2006-01-27 08:10:46 +0000212 Subtarget = &TM.getSubtarget<X86Subtarget>();
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000213 X86ScalarSSEf64 = Subtarget->hasXMMInt();
214 X86ScalarSSEf32 = Subtarget->hasXMM();
Evan Cheng25ab6902006-09-08 06:48:29 +0000215 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000216
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000217 RegInfo = TM.getRegisterInfo();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000218 TD = getTargetData();
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000219
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000220 // Set up the TargetLowering object.
Chris Lattnera34b3cf2010-12-19 20:03:11 +0000221 static MVT IntVTs[] = { MVT::i8, MVT::i16, MVT::i32, MVT::i64 };
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000222
223 // X86 is weird, it always uses i8 for shift amounts and setcc results.
Duncan Sands03228082008-11-23 15:47:28 +0000224 setBooleanContents(ZeroOrOneBooleanContent);
Eric Christopher471e4222011-06-08 23:55:35 +0000225
Eric Christopherde5e1012011-03-11 01:05:58 +0000226 // For 64-bit since we have so many registers use the ILP scheduler, for
227 // 32-bit code use the register pressure specific scheduling.
228 if (Subtarget->is64Bit())
229 setSchedulingPreference(Sched::ILP);
230 else
231 setSchedulingPreference(Sched::RegPressure);
Evan Cheng25ab6902006-09-08 06:48:29 +0000232 setStackPointerRegisterToSaveRestore(X86StackPtr);
Evan Cheng714554d2006-03-16 21:47:42 +0000233
Michael J. Spencer92bf38c2010-10-10 23:11:06 +0000234 if (Subtarget->isTargetWindows() && !Subtarget->isTargetCygMing()) {
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000235 // Setup Windows compiler runtime calls.
236 setLibcallName(RTLIB::SDIV_I64, "_alldiv");
Michael J. Spencer335b8062010-10-11 05:29:15 +0000237 setLibcallName(RTLIB::UDIV_I64, "_aulldiv");
238 setLibcallName(RTLIB::FPTOUINT_F64_I64, "_ftol2");
Michael J. Spencer94f7eeb2010-10-19 07:32:52 +0000239 setLibcallName(RTLIB::FPTOUINT_F32_I64, "_ftol2");
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000240 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::X86_StdCall);
Michael J. Spencer335b8062010-10-11 05:29:15 +0000241 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::X86_StdCall);
Michael J. Spencer6dad10e2010-10-27 18:52:38 +0000242 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I64, CallingConv::C);
243 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I64, CallingConv::C);
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000244 }
245
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000246 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +0000247 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000248 setUseUnderscoreSetJmp(false);
249 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000250 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000251 // MS runtime is weird: it exports _setjmp, but longjmp!
252 setUseUnderscoreSetJmp(true);
253 setUseUnderscoreLongJmp(false);
254 } else {
255 setUseUnderscoreSetJmp(true);
256 setUseUnderscoreLongJmp(true);
257 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000258
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000259 // Set up the register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000260 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
Dan Gohman71edb242010-04-30 18:30:26 +0000261 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000262 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
Evan Cheng25ab6902006-09-08 06:48:29 +0000263 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000264 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000265
Owen Anderson825b72b2009-08-11 20:47:22 +0000266 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Evan Chengc5484282006-10-04 00:56:09 +0000267
Scott Michelfdc40a02009-02-17 22:15:04 +0000268 // We don't accept any truncstore of integer registers.
Owen Anderson825b72b2009-08-11 20:47:22 +0000269 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000270 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000271 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000272 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000273 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
274 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
Evan Cheng7f042682008-10-15 02:05:31 +0000275
276 // SETOEQ and SETUNE require checking two conditions.
Owen Anderson825b72b2009-08-11 20:47:22 +0000277 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
278 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
279 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
280 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
281 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
282 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
Chris Lattnerddf89562008-01-17 19:59:44 +0000283
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000284 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
285 // operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000286 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
287 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
288 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +0000289
Evan Cheng25ab6902006-09-08 06:48:29 +0000290 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000291 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
292 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
Eli Friedman948e95a2009-05-23 09:59:16 +0000293 } else if (!UseSoftFloat) {
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000294 // We have an algorithm for SSE2->double, and we turn this into a
295 // 64-bit FILD followed by conditional FADD for other targets.
296 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Eli Friedman948e95a2009-05-23 09:59:16 +0000297 // We have an algorithm for SSE2, and we turn this into a 64-bit
298 // FILD for other targets.
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000299 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000300 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000301
302 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
303 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000304 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
305 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000306
Devang Patel6a784892009-06-05 18:48:29 +0000307 if (!UseSoftFloat) {
Bill Wendling105be5a2009-03-13 08:41:47 +0000308 // SSE has no i16 to fp conversion, only i32
309 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000310 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000311 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000312 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000313 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000314 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
315 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000316 }
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000317 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000318 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
319 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000320 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000321
Dale Johannesen73328d12007-09-19 23:55:34 +0000322 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
323 // are Legal, f80 is custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000324 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
325 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000326
Evan Cheng02568ff2006-01-30 22:13:22 +0000327 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
328 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000329 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
330 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
Evan Cheng02568ff2006-01-30 22:13:22 +0000331
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000332 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000333 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000334 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000335 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000336 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000337 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
338 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000339 }
340
341 // Handle FP_TO_UINT by promoting the destination to a larger signed
342 // conversion.
Owen Anderson825b72b2009-08-11 20:47:22 +0000343 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
344 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
345 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000346
Evan Cheng25ab6902006-09-08 06:48:29 +0000347 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000348 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
349 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Eli Friedman948e95a2009-05-23 09:59:16 +0000350 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000351 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000352 // Expand FP_TO_UINT into a select.
353 // FIXME: We would like to use a Custom expander here eventually to do
354 // the optimal thing for SSE vs. the default expansion in the legalizer.
Owen Anderson825b72b2009-08-11 20:47:22 +0000355 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000356 else
Eli Friedman948e95a2009-05-23 09:59:16 +0000357 // With SSE3 we can use fisttpll to convert to a signed i64; without
358 // SSE, we're stuck with a fistpll.
Owen Anderson825b72b2009-08-11 20:47:22 +0000359 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000360 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000361
Chris Lattner399610a2006-12-05 18:22:22 +0000362 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Michael J. Spencerec38de22010-10-10 22:04:20 +0000363 if (!X86ScalarSSEf64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000364 setOperationAction(ISD::BITCAST , MVT::f32 , Expand);
365 setOperationAction(ISD::BITCAST , MVT::i32 , Expand);
Dale Johannesene39859a2010-05-21 18:40:15 +0000366 if (Subtarget->is64Bit()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000367 setOperationAction(ISD::BITCAST , MVT::f64 , Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000368 // Without SSE, i64->f64 goes through memory.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000369 setOperationAction(ISD::BITCAST , MVT::i64 , Expand);
Dale Johannesen7d07b482010-05-21 00:52:33 +0000370 }
Chris Lattnerf3597a12006-12-05 18:45:06 +0000371 }
Chris Lattner21f66852005-12-23 05:15:23 +0000372
Dan Gohmanb00ee212008-02-18 19:34:53 +0000373 // Scalar integer divide and remainder are lowered to use operations that
374 // produce two results, to match the available instructions. This exposes
375 // the two-result form to trivial CSE, which is able to combine x/y and x%y
376 // into a single instruction.
377 //
378 // Scalar integer multiply-high is also lowered to use two-result
379 // operations, to match the available instructions. However, plain multiply
380 // (low) operations are left as Legal, as there are single-result
381 // instructions for this in x86. Using the two-result multiply instructions
382 // when both high and low results are needed must be arranged by dagcombine.
Chris Lattnere019ec12010-12-19 20:07:10 +0000383 for (unsigned i = 0, e = 4; i != e; ++i) {
384 MVT VT = IntVTs[i];
385 setOperationAction(ISD::MULHS, VT, Expand);
386 setOperationAction(ISD::MULHU, VT, Expand);
387 setOperationAction(ISD::SDIV, VT, Expand);
388 setOperationAction(ISD::UDIV, VT, Expand);
389 setOperationAction(ISD::SREM, VT, Expand);
390 setOperationAction(ISD::UREM, VT, Expand);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000391
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000392 // Add/Sub overflow ops with MVT::Glues are lowered to EFLAGS dependences.
Chris Lattnerd8ff7ec2010-12-20 01:03:27 +0000393 setOperationAction(ISD::ADDC, VT, Custom);
394 setOperationAction(ISD::ADDE, VT, Custom);
395 setOperationAction(ISD::SUBC, VT, Custom);
396 setOperationAction(ISD::SUBE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000397 }
Dan Gohmana37c9f72007-09-25 18:23:27 +0000398
Owen Anderson825b72b2009-08-11 20:47:22 +0000399 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
400 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
401 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
402 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000403 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000404 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
405 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
406 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
407 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
408 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
409 setOperationAction(ISD::FREM , MVT::f32 , Expand);
410 setOperationAction(ISD::FREM , MVT::f64 , Expand);
411 setOperationAction(ISD::FREM , MVT::f80 , Expand);
412 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000413
Owen Anderson825b72b2009-08-11 20:47:22 +0000414 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
415 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000416 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
417 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000418 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
419 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000420 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000421 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
422 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000423 }
424
Benjamin Kramer1292c222010-12-04 20:32:23 +0000425 if (Subtarget->hasPOPCNT()) {
426 setOperationAction(ISD::CTPOP , MVT::i8 , Promote);
427 } else {
428 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
429 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
430 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
431 if (Subtarget->is64Bit())
432 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
433 }
434
Owen Anderson825b72b2009-08-11 20:47:22 +0000435 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
436 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000437
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000438 // These should be promoted to a larger select which is supported.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000439 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000440 // X86 wants to expand cmov itself.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000441 setOperationAction(ISD::SELECT , MVT::i8 , Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000442 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000443 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
444 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
445 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
446 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
447 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000448 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000449 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
450 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
451 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
452 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000453 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000454 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
Andrew Trickf6c39412011-03-23 23:11:02 +0000455 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000456 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000457 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000458
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000459 // Darwin ABI issue.
Owen Anderson825b72b2009-08-11 20:47:22 +0000460 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
461 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
462 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
463 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000464 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000465 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
466 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000467 setOperationAction(ISD::BlockAddress , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000468 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000469 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
470 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
471 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
472 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000473 setOperationAction(ISD::BlockAddress , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000474 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000475 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Owen Anderson825b72b2009-08-11 20:47:22 +0000476 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
477 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
478 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000479 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000480 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
481 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
482 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000483 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000484
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000485 if (Subtarget->hasXMM())
Owen Anderson825b72b2009-08-11 20:47:22 +0000486 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000487
Eric Christopher9a9d2752010-07-22 02:48:34 +0000488 // We may not have a libcall for MEMBARRIER so we should lower this.
489 setOperationAction(ISD::MEMBARRIER , MVT::Other, Custom);
Michael J. Spencerec38de22010-10-10 22:04:20 +0000490
Jim Grosbachf1ab49e2010-06-23 16:25:07 +0000491 // On X86 and X86-64, atomic operations are lowered to locked instructions.
492 // Locked instructions, in turn, have implicit fence semantics (all memory
493 // operations are flushed before issuing the locked instruction, and they
494 // are not buffered), so we can fold away the common pattern of
495 // fence-atomic-fence.
496 setShouldFoldAtomicFences(true);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000497
Mon P Wang63307c32008-05-05 19:05:59 +0000498 // Expand certain atomics
Chris Lattnere019ec12010-12-19 20:07:10 +0000499 for (unsigned i = 0, e = 4; i != e; ++i) {
500 MVT VT = IntVTs[i];
501 setOperationAction(ISD::ATOMIC_CMP_SWAP, VT, Custom);
502 setOperationAction(ISD::ATOMIC_LOAD_SUB, VT, Custom);
503 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000504
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000505 if (!Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000506 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
507 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
508 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
509 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
510 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
511 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
512 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000513 }
514
Evan Cheng3c992d22006-03-07 02:02:57 +0000515 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000516 if (!Subtarget->isTargetDarwin() &&
517 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000518 !Subtarget->isTargetCygMing()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000519 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Dan Gohman44066042008-07-01 00:05:16 +0000520 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000521
Owen Anderson825b72b2009-08-11 20:47:22 +0000522 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
523 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
524 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
525 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000526 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000527 setExceptionPointerRegister(X86::RAX);
528 setExceptionSelectorRegister(X86::RDX);
529 } else {
530 setExceptionPointerRegister(X86::EAX);
531 setExceptionSelectorRegister(X86::EDX);
532 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000533 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
534 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000535
Owen Anderson825b72b2009-08-11 20:47:22 +0000536 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000537
Owen Anderson825b72b2009-08-11 20:47:22 +0000538 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000539
Nate Begemanacc398c2006-01-25 18:21:52 +0000540 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000541 setOperationAction(ISD::VASTART , MVT::Other, Custom);
542 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000543 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000544 setOperationAction(ISD::VAARG , MVT::Other, Custom);
545 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000546 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000547 setOperationAction(ISD::VAARG , MVT::Other, Expand);
548 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000549 }
Evan Chengae642192007-03-02 23:16:35 +0000550
Owen Anderson825b72b2009-08-11 20:47:22 +0000551 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
552 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
NAKAMURA Takumia2e07622011-03-24 07:07:00 +0000553 setOperationAction(ISD::DYNAMIC_STACKALLOC,
554 (Subtarget->is64Bit() ? MVT::i64 : MVT::i32),
555 (Subtarget->isTargetCOFF()
556 && !Subtarget->isTargetEnvMacho()
557 ? Custom : Expand));
Chris Lattnerb99329e2006-01-13 02:42:53 +0000558
Evan Chengc7ce29b2009-02-13 22:36:38 +0000559 if (!UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000560 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000561 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000562 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
563 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000564
Evan Cheng223547a2006-01-31 22:28:30 +0000565 // Use ANDPD to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000566 setOperationAction(ISD::FABS , MVT::f64, Custom);
567 setOperationAction(ISD::FABS , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000568
569 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000570 setOperationAction(ISD::FNEG , MVT::f64, Custom);
571 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000572
Evan Cheng68c47cb2007-01-05 07:55:56 +0000573 // Use ANDPD and ORPD to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000574 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
575 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000576
Stuart Hastings4fd0dee2011-06-01 04:39:42 +0000577 // Lower this to FGETSIGNx86 plus an AND.
578 setOperationAction(ISD::FGETSIGN, MVT::i64, Custom);
579 setOperationAction(ISD::FGETSIGN, MVT::i32, Custom);
580
Evan Chengd25e9e82006-02-02 00:28:23 +0000581 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000582 setOperationAction(ISD::FSIN , MVT::f64, Expand);
583 setOperationAction(ISD::FCOS , MVT::f64, Expand);
584 setOperationAction(ISD::FSIN , MVT::f32, Expand);
585 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000586
Chris Lattnera54aa942006-01-29 06:26:08 +0000587 // Expand FP immediates into loads from the stack, except for the special
588 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000589 addLegalFPImmediate(APFloat(+0.0)); // xorpd
590 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Evan Chengc7ce29b2009-02-13 22:36:38 +0000591 } else if (!UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000592 // Use SSE for f32, x87 for f64.
593 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000594 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
595 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000596
597 // Use ANDPS to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000598 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000599
600 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000601 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000602
Owen Anderson825b72b2009-08-11 20:47:22 +0000603 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000604
605 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000606 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
607 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000608
609 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000610 setOperationAction(ISD::FSIN , MVT::f32, Expand);
611 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000612
Nate Begemane1795842008-02-14 08:57:00 +0000613 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000614 addLegalFPImmediate(APFloat(+0.0f)); // xorps
615 addLegalFPImmediate(APFloat(+0.0)); // FLD0
616 addLegalFPImmediate(APFloat(+1.0)); // FLD1
617 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
618 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
619
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000620 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000621 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
622 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000623 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000624 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000625 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000626 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000627 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
628 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000629
Owen Anderson825b72b2009-08-11 20:47:22 +0000630 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
631 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
632 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
633 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000634
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000635 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000636 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
637 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000638 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000639 addLegalFPImmediate(APFloat(+0.0)); // FLD0
640 addLegalFPImmediate(APFloat(+1.0)); // FLD1
641 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
642 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000643 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
644 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
645 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
646 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000647 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000648
Dale Johannesen59a58732007-08-05 18:49:15 +0000649 // Long double always uses X87.
Evan Cheng92722532009-03-26 23:06:32 +0000650 if (!UseSoftFloat) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000651 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
652 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
653 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000654 {
Benjamin Kramer98383962010-12-04 14:22:24 +0000655 APFloat TmpFlt = APFloat::getZero(APFloat::x87DoubleExtended);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000656 addLegalFPImmediate(TmpFlt); // FLD0
657 TmpFlt.changeSign();
658 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
Benjamin Kramer98383962010-12-04 14:22:24 +0000659
660 bool ignored;
Evan Chengc7ce29b2009-02-13 22:36:38 +0000661 APFloat TmpFlt2(+1.0);
662 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
663 &ignored);
664 addLegalFPImmediate(TmpFlt2); // FLD1
665 TmpFlt2.changeSign();
666 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
667 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000668
Evan Chengc7ce29b2009-02-13 22:36:38 +0000669 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000670 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
671 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000672 }
Dale Johannesen2f429012007-09-26 21:10:55 +0000673 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000674
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000675 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000676 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
677 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
678 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000679
Owen Anderson825b72b2009-08-11 20:47:22 +0000680 setOperationAction(ISD::FLOG, MVT::f80, Expand);
681 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
682 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
683 setOperationAction(ISD::FEXP, MVT::f80, Expand);
684 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000685
Mon P Wangf007a8b2008-11-06 05:31:54 +0000686 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000687 // (for widening) or expand (for scalarization). Then we will selectively
688 // turn on ones that can be effectively codegen'd.
Owen Anderson825b72b2009-08-11 20:47:22 +0000689 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
690 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
691 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
692 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
693 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
694 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
695 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
696 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
697 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
698 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
699 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
700 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
701 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
702 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
703 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
704 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
705 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000706 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
David Greenecfe33c42011-01-26 19:13:22 +0000707 setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
708 setOperationAction(ISD::INSERT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000709 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
710 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
711 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
712 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
713 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
714 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
715 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
716 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
717 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
718 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
719 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
720 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
721 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
722 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
723 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
724 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
725 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
726 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
727 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
728 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
729 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
730 setOperationAction(ISD::VSETCC, (MVT::SimpleValueType)VT, Expand);
731 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
732 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
733 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
734 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
735 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
736 setOperationAction(ISD::FP_TO_UINT, (MVT::SimpleValueType)VT, Expand);
737 setOperationAction(ISD::FP_TO_SINT, (MVT::SimpleValueType)VT, Expand);
738 setOperationAction(ISD::UINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
739 setOperationAction(ISD::SINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
Dan Gohman87862e72009-12-11 21:31:27 +0000740 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000741 setOperationAction(ISD::TRUNCATE, (MVT::SimpleValueType)VT, Expand);
742 setOperationAction(ISD::SIGN_EXTEND, (MVT::SimpleValueType)VT, Expand);
743 setOperationAction(ISD::ZERO_EXTEND, (MVT::SimpleValueType)VT, Expand);
744 setOperationAction(ISD::ANY_EXTEND, (MVT::SimpleValueType)VT, Expand);
745 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
746 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
747 setTruncStoreAction((MVT::SimpleValueType)VT,
748 (MVT::SimpleValueType)InnerVT, Expand);
749 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
750 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
751 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000752 }
753
Evan Chengc7ce29b2009-02-13 22:36:38 +0000754 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
755 // with -msoft-float, disable use of MMX as well.
Chris Lattner2a786eb2010-12-19 20:19:20 +0000756 if (!UseSoftFloat && Subtarget->hasMMX()) {
Dale Johannesene93d99c2010-10-20 21:32:10 +0000757 addRegisterClass(MVT::x86mmx, X86::VR64RegisterClass);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000758 // No operations on x86mmx supported, everything uses intrinsics.
Evan Cheng470a6ad2006-02-22 02:26:30 +0000759 }
760
Dale Johannesen0488fb62010-09-30 23:57:10 +0000761 // MMX-sized vectors (other than x86mmx) are expected to be expanded
762 // into smaller operations.
763 setOperationAction(ISD::MULHS, MVT::v8i8, Expand);
764 setOperationAction(ISD::MULHS, MVT::v4i16, Expand);
765 setOperationAction(ISD::MULHS, MVT::v2i32, Expand);
766 setOperationAction(ISD::MULHS, MVT::v1i64, Expand);
767 setOperationAction(ISD::AND, MVT::v8i8, Expand);
768 setOperationAction(ISD::AND, MVT::v4i16, Expand);
769 setOperationAction(ISD::AND, MVT::v2i32, Expand);
770 setOperationAction(ISD::AND, MVT::v1i64, Expand);
771 setOperationAction(ISD::OR, MVT::v8i8, Expand);
772 setOperationAction(ISD::OR, MVT::v4i16, Expand);
773 setOperationAction(ISD::OR, MVT::v2i32, Expand);
774 setOperationAction(ISD::OR, MVT::v1i64, Expand);
775 setOperationAction(ISD::XOR, MVT::v8i8, Expand);
776 setOperationAction(ISD::XOR, MVT::v4i16, Expand);
777 setOperationAction(ISD::XOR, MVT::v2i32, Expand);
778 setOperationAction(ISD::XOR, MVT::v1i64, Expand);
779 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Expand);
780 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Expand);
781 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Expand);
782 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Expand);
783 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v1i64, Expand);
784 setOperationAction(ISD::SELECT, MVT::v8i8, Expand);
785 setOperationAction(ISD::SELECT, MVT::v4i16, Expand);
786 setOperationAction(ISD::SELECT, MVT::v2i32, Expand);
787 setOperationAction(ISD::SELECT, MVT::v1i64, Expand);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000788 setOperationAction(ISD::BITCAST, MVT::v8i8, Expand);
789 setOperationAction(ISD::BITCAST, MVT::v4i16, Expand);
790 setOperationAction(ISD::BITCAST, MVT::v2i32, Expand);
791 setOperationAction(ISD::BITCAST, MVT::v1i64, Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000792
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000793 if (!UseSoftFloat && Subtarget->hasXMM()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000794 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000795
Owen Anderson825b72b2009-08-11 20:47:22 +0000796 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
797 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
798 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
799 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
800 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
801 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
802 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
803 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
804 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
805 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
806 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
807 setOperationAction(ISD::VSETCC, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000808 }
809
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000810 if (!UseSoftFloat && Subtarget->hasXMMInt()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000811 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000812
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000813 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
814 // registers cannot be used even for integer operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000815 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
816 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
817 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
818 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000819
Owen Anderson825b72b2009-08-11 20:47:22 +0000820 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
821 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
822 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
823 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
824 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
825 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
826 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
827 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
828 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
829 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
830 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
831 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
832 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
833 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
834 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
835 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000836
Owen Anderson825b72b2009-08-11 20:47:22 +0000837 setOperationAction(ISD::VSETCC, MVT::v2f64, Custom);
838 setOperationAction(ISD::VSETCC, MVT::v16i8, Custom);
839 setOperationAction(ISD::VSETCC, MVT::v8i16, Custom);
840 setOperationAction(ISD::VSETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000841
Owen Anderson825b72b2009-08-11 20:47:22 +0000842 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
843 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
844 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
845 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
846 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000847
Mon P Wangeb38ebf2010-01-24 00:05:03 +0000848 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2f64, Custom);
849 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2i64, Custom);
850 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i8, Custom);
851 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i16, Custom);
852 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Custom);
853
Evan Cheng2c3ae372006-04-12 21:21:57 +0000854 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Owen Anderson825b72b2009-08-11 20:47:22 +0000855 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
856 EVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000857 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000858 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000859 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000860 // Do not attempt to custom lower non-128-bit vectors
861 if (!VT.is128BitVector())
862 continue;
Owen Anderson825b72b2009-08-11 20:47:22 +0000863 setOperationAction(ISD::BUILD_VECTOR,
864 VT.getSimpleVT().SimpleTy, Custom);
865 setOperationAction(ISD::VECTOR_SHUFFLE,
866 VT.getSimpleVT().SimpleTy, Custom);
867 setOperationAction(ISD::EXTRACT_VECTOR_ELT,
868 VT.getSimpleVT().SimpleTy, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000869 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000870
Owen Anderson825b72b2009-08-11 20:47:22 +0000871 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
872 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
873 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
874 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
875 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
876 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000877
Nate Begemancdd1eec2008-02-12 22:51:28 +0000878 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000879 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
880 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000881 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000882
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000883 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000884 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; i++) {
885 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
Owen Andersone50ed302009-08-10 22:56:29 +0000886 EVT VT = SVT;
David Greene9b9838d2009-06-29 16:47:10 +0000887
888 // Do not attempt to promote non-128-bit vectors
Chris Lattner32b4b5a2010-07-05 05:53:14 +0000889 if (!VT.is128BitVector())
David Greene9b9838d2009-06-29 16:47:10 +0000890 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +0000891
Owen Andersond6662ad2009-08-10 20:46:15 +0000892 setOperationAction(ISD::AND, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000893 AddPromotedToType (ISD::AND, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000894 setOperationAction(ISD::OR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000895 AddPromotedToType (ISD::OR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000896 setOperationAction(ISD::XOR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000897 AddPromotedToType (ISD::XOR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000898 setOperationAction(ISD::LOAD, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000899 AddPromotedToType (ISD::LOAD, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000900 setOperationAction(ISD::SELECT, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000901 AddPromotedToType (ISD::SELECT, SVT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000902 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000903
Owen Anderson825b72b2009-08-11 20:47:22 +0000904 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000905
Evan Cheng2c3ae372006-04-12 21:21:57 +0000906 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000907 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
908 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
909 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
910 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000911
Owen Anderson825b72b2009-08-11 20:47:22 +0000912 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
913 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000914 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000915
Nate Begeman14d12ca2008-02-11 04:19:36 +0000916 if (Subtarget->hasSSE41()) {
Dale Johannesen54feef22010-05-27 20:12:41 +0000917 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
918 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
919 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
920 setOperationAction(ISD::FRINT, MVT::f32, Legal);
921 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
922 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
923 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
924 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
925 setOperationAction(ISD::FRINT, MVT::f64, Legal);
926 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
927
Nate Begeman14d12ca2008-02-11 04:19:36 +0000928 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +0000929 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000930
Nate Begemanbdcb5af2010-07-27 22:37:06 +0000931 // Can turn SHL into an integer multiply.
932 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
Nate Begeman51409212010-07-28 00:21:48 +0000933 setOperationAction(ISD::SHL, MVT::v16i8, Custom);
Nate Begemanbdcb5af2010-07-27 22:37:06 +0000934
Nate Begeman14d12ca2008-02-11 04:19:36 +0000935 // i8 and i16 vectors are custom , because the source register and source
936 // source memory operand types are not the same width. f32 vectors are
937 // custom since the immediate controlling the insert encodes additional
938 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +0000939 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
940 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
941 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
942 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000943
Owen Anderson825b72b2009-08-11 20:47:22 +0000944 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
945 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
946 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
947 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000948
949 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000950 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
951 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000952 }
953 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000954
Nadav Rotem43012222011-05-11 08:12:09 +0000955 if (Subtarget->hasSSE2()) {
956 setOperationAction(ISD::SRL, MVT::v2i64, Custom);
957 setOperationAction(ISD::SRL, MVT::v4i32, Custom);
958 setOperationAction(ISD::SRL, MVT::v16i8, Custom);
959
960 setOperationAction(ISD::SHL, MVT::v2i64, Custom);
961 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
962 setOperationAction(ISD::SHL, MVT::v8i16, Custom);
963
964 setOperationAction(ISD::SRA, MVT::v4i32, Custom);
965 setOperationAction(ISD::SRA, MVT::v8i16, Custom);
966 }
967
Chris Lattnera34b3cf2010-12-19 20:03:11 +0000968 if (Subtarget->hasSSE42())
Owen Anderson825b72b2009-08-11 20:47:22 +0000969 setOperationAction(ISD::VSETCC, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000970
David Greene9b9838d2009-06-29 16:47:10 +0000971 if (!UseSoftFloat && Subtarget->hasAVX()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000972 addRegisterClass(MVT::v8f32, X86::VR256RegisterClass);
973 addRegisterClass(MVT::v4f64, X86::VR256RegisterClass);
974 addRegisterClass(MVT::v8i32, X86::VR256RegisterClass);
975 addRegisterClass(MVT::v4i64, X86::VR256RegisterClass);
Bruno Cardoso Lopes405f11b2010-08-10 01:43:16 +0000976 addRegisterClass(MVT::v32i8, X86::VR256RegisterClass);
David Greened94c1012009-06-29 22:50:51 +0000977
Owen Anderson825b72b2009-08-11 20:47:22 +0000978 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
979 setOperationAction(ISD::LOAD, MVT::v8i32, Legal);
980 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
981 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
David Greene54d8eba2011-01-27 22:38:56 +0000982
Owen Anderson825b72b2009-08-11 20:47:22 +0000983 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
984 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
985 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
986 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
987 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
988 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000989
Owen Anderson825b72b2009-08-11 20:47:22 +0000990 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
991 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
992 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
993 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
994 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
995 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000996
David Greene54d8eba2011-01-27 22:38:56 +0000997 // Custom lower build_vector, vector_shuffle, scalar_to_vector,
998 // insert_vector_elt extract_subvector and extract_vector_elt for
999 // 256-bit types.
1000 for (unsigned i = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
1001 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE;
1002 ++i) {
1003 MVT::SimpleValueType VT = (MVT::SimpleValueType)i;
1004 // Do not attempt to custom lower non-256-bit vectors
1005 if (!isPowerOf2_32(MVT(VT).getVectorNumElements())
1006 || (MVT(VT).getSizeInBits() < 256))
David Greene9b9838d2009-06-29 16:47:10 +00001007 continue;
David Greene9b9838d2009-06-29 16:47:10 +00001008 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
1009 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
David Greene54d8eba2011-01-27 22:38:56 +00001010 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001011 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
David Greene54d8eba2011-01-27 22:38:56 +00001012 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001013 }
David Greene54d8eba2011-01-27 22:38:56 +00001014 // Custom-lower insert_subvector and extract_subvector based on
1015 // the result type.
1016 for (unsigned i = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
1017 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE;
1018 ++i) {
1019 MVT::SimpleValueType VT = (MVT::SimpleValueType)i;
1020 // Do not attempt to custom lower non-256-bit vectors
1021 if (!isPowerOf2_32(MVT(VT).getVectorNumElements()))
David Greene9b9838d2009-06-29 16:47:10 +00001022 continue;
David Greene54d8eba2011-01-27 22:38:56 +00001023
1024 if (MVT(VT).getSizeInBits() == 128) {
1025 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001026 }
David Greene54d8eba2011-01-27 22:38:56 +00001027 else if (MVT(VT).getSizeInBits() == 256) {
1028 setOperationAction(ISD::INSERT_SUBVECTOR, VT, Custom);
1029 }
David Greene9b9838d2009-06-29 16:47:10 +00001030 }
1031
David Greene54d8eba2011-01-27 22:38:56 +00001032 // Promote v32i8, v16i16, v8i32 select, and, or, xor to v4i64.
1033 // Don't promote loads because we need them for VPERM vector index versions.
1034
1035 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
1036 VT != (unsigned)MVT::LAST_VECTOR_VALUETYPE;
1037 VT++) {
1038 if (!isPowerOf2_32(MVT((MVT::SimpleValueType)VT).getVectorNumElements())
1039 || (MVT((MVT::SimpleValueType)VT).getSizeInBits() < 256))
1040 continue;
1041 setOperationAction(ISD::AND, (MVT::SimpleValueType)VT, Promote);
1042 AddPromotedToType (ISD::AND, (MVT::SimpleValueType)VT, MVT::v4i64);
1043 setOperationAction(ISD::OR, (MVT::SimpleValueType)VT, Promote);
1044 AddPromotedToType (ISD::OR, (MVT::SimpleValueType)VT, MVT::v4i64);
1045 setOperationAction(ISD::XOR, (MVT::SimpleValueType)VT, Promote);
1046 AddPromotedToType (ISD::XOR, (MVT::SimpleValueType)VT, MVT::v4i64);
1047 //setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Promote);
1048 //AddPromotedToType (ISD::LOAD, (MVT::SimpleValueType)VT, MVT::v4i64);
1049 setOperationAction(ISD::SELECT, (MVT::SimpleValueType)VT, Promote);
1050 AddPromotedToType (ISD::SELECT, (MVT::SimpleValueType)VT, MVT::v4i64);
1051 }
David Greene9b9838d2009-06-29 16:47:10 +00001052 }
1053
Evan Cheng6be2c582006-04-05 23:38:46 +00001054 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +00001055 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +00001056
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001057
Eli Friedman962f5492010-06-02 19:35:46 +00001058 // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
1059 // handle type legalization for these operations here.
Dan Gohman71c62a22010-06-02 19:13:40 +00001060 //
Eli Friedman962f5492010-06-02 19:35:46 +00001061 // FIXME: We really should do custom legalization for addition and
1062 // subtraction on x86-32 once PR3203 is fixed. We really can't do much better
1063 // than generic legalization for 64-bit multiplication-with-overflow, though.
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001064 for (unsigned i = 0, e = 3+Subtarget->is64Bit(); i != e; ++i) {
1065 // Add/Sub/Mul with overflow operations are custom lowered.
1066 MVT VT = IntVTs[i];
1067 setOperationAction(ISD::SADDO, VT, Custom);
1068 setOperationAction(ISD::UADDO, VT, Custom);
1069 setOperationAction(ISD::SSUBO, VT, Custom);
1070 setOperationAction(ISD::USUBO, VT, Custom);
1071 setOperationAction(ISD::SMULO, VT, Custom);
1072 setOperationAction(ISD::UMULO, VT, Custom);
Eli Friedmana993f0a2010-06-02 00:27:18 +00001073 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001074
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001075 // There are no 8-bit 3-address imul/mul instructions
1076 setOperationAction(ISD::SMULO, MVT::i8, Expand);
1077 setOperationAction(ISD::UMULO, MVT::i8, Expand);
Bill Wendling41ea7e72008-11-24 19:21:46 +00001078
Evan Chengd54f2d52009-03-31 19:38:51 +00001079 if (!Subtarget->is64Bit()) {
1080 // These libcalls are not available in 32-bit.
1081 setLibcallName(RTLIB::SHL_I128, 0);
1082 setLibcallName(RTLIB::SRL_I128, 0);
1083 setLibcallName(RTLIB::SRA_I128, 0);
1084 }
1085
Evan Cheng206ee9d2006-07-07 08:33:52 +00001086 // We have target-specific dag combine patterns for the following nodes:
1087 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00001088 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Evan Chengd880b972008-05-09 21:53:03 +00001089 setTargetDAGCombine(ISD::BUILD_VECTOR);
Chris Lattner83e6c992006-10-04 06:57:07 +00001090 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +00001091 setTargetDAGCombine(ISD::SHL);
1092 setTargetDAGCombine(ISD::SRA);
1093 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +00001094 setTargetDAGCombine(ISD::OR);
Nate Begemanb65c1752010-12-17 22:55:37 +00001095 setTargetDAGCombine(ISD::AND);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001096 setTargetDAGCombine(ISD::ADD);
1097 setTargetDAGCombine(ISD::SUB);
Chris Lattner149a4e52008-02-22 02:09:43 +00001098 setTargetDAGCombine(ISD::STORE);
Evan Cheng2e489c42009-12-16 00:53:11 +00001099 setTargetDAGCombine(ISD::ZERO_EXTEND);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +00001100 setTargetDAGCombine(ISD::SINT_TO_FP);
Evan Cheng0b0cd912009-03-28 05:57:29 +00001101 if (Subtarget->is64Bit())
1102 setTargetDAGCombine(ISD::MUL);
Evan Cheng206ee9d2006-07-07 08:33:52 +00001103
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001104 computeRegisterProperties();
1105
Evan Cheng05219282011-01-06 06:52:41 +00001106 // On Darwin, -Os means optimize for size without hurting performance,
1107 // do not reduce the limit.
Dan Gohman87060f52008-06-30 21:00:56 +00001108 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001109 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 16 : 8;
Evan Cheng255f20f2010-04-01 06:04:33 +00001110 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001111 maxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
1112 maxStoresPerMemmove = 8; // For @llvm.memmove -> sequence of stores
1113 maxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Evan Chengfb8075d2008-02-28 00:43:03 +00001114 setPrefLoopAlignment(16);
Evan Cheng6ebf7bc2009-05-13 21:42:09 +00001115 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +00001116
1117 setPrefFunctionAlignment(4);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001118}
1119
Scott Michel5b8f82e2008-03-10 15:42:14 +00001120
Owen Anderson825b72b2009-08-11 20:47:22 +00001121MVT::SimpleValueType X86TargetLowering::getSetCCResultType(EVT VT) const {
1122 return MVT::i8;
Scott Michel5b8f82e2008-03-10 15:42:14 +00001123}
1124
1125
Evan Cheng29286502008-01-23 23:17:41 +00001126/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1127/// the desired ByVal argument alignment.
1128static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
1129 if (MaxAlign == 16)
1130 return;
1131 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
1132 if (VTy->getBitWidth() == 128)
1133 MaxAlign = 16;
Evan Cheng29286502008-01-23 23:17:41 +00001134 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
1135 unsigned EltAlign = 0;
1136 getMaxByValAlign(ATy->getElementType(), EltAlign);
1137 if (EltAlign > MaxAlign)
1138 MaxAlign = EltAlign;
1139 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
1140 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1141 unsigned EltAlign = 0;
1142 getMaxByValAlign(STy->getElementType(i), EltAlign);
1143 if (EltAlign > MaxAlign)
1144 MaxAlign = EltAlign;
1145 if (MaxAlign == 16)
1146 break;
1147 }
1148 }
1149 return;
1150}
1151
1152/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1153/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001154/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1155/// are at 4-byte boundaries.
Evan Cheng29286502008-01-23 23:17:41 +00001156unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001157 if (Subtarget->is64Bit()) {
1158 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001159 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001160 if (TyAlign > 8)
1161 return TyAlign;
1162 return 8;
1163 }
1164
Evan Cheng29286502008-01-23 23:17:41 +00001165 unsigned Align = 4;
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001166 if (Subtarget->hasXMM())
Dale Johannesen0c191872008-02-08 19:48:20 +00001167 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001168 return Align;
1169}
Chris Lattner2b02a442007-02-25 08:29:00 +00001170
Evan Chengf0df0312008-05-15 08:39:06 +00001171/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001172/// and store operations as a result of memset, memcpy, and memmove
1173/// lowering. If DstAlign is zero that means it's safe to destination
1174/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1175/// means there isn't a need to check it against alignment requirement,
1176/// probably because the source does not need to be loaded. If
1177/// 'NonScalarIntSafe' is true, that means it's safe to return a
1178/// non-scalar-integer type, e.g. empty string source, constant, or loaded
1179/// from memory. 'MemcpyStrSrc' indicates whether the memcpy source is
1180/// constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001181/// It returns EVT::Other if the type should be determined using generic
1182/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001183EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001184X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1185 unsigned DstAlign, unsigned SrcAlign,
Evan Chengf28f8bc2010-04-02 19:36:14 +00001186 bool NonScalarIntSafe,
Evan Chengc3b0c342010-04-08 07:37:57 +00001187 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001188 MachineFunction &MF) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001189 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1190 // linux. This is because the stack realignment code can't handle certain
1191 // cases like PR2962. This should be removed when PR2962 is fixed.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001192 const Function *F = MF.getFunction();
Evan Chenga5e13622011-01-07 19:35:30 +00001193 if (NonScalarIntSafe &&
1194 !F->hasFnAttr(Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001195 if (Size >= 16 &&
Evan Chenga5e13622011-01-07 19:35:30 +00001196 (Subtarget->isUnalignedMemAccessFast() ||
1197 ((DstAlign == 0 || DstAlign >= 16) &&
1198 (SrcAlign == 0 || SrcAlign >= 16))) &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001199 Subtarget->getStackAlignment() >= 16) {
1200 if (Subtarget->hasSSE2())
1201 return MVT::v4i32;
Evan Chengf28f8bc2010-04-02 19:36:14 +00001202 if (Subtarget->hasSSE1())
Evan Cheng255f20f2010-04-01 06:04:33 +00001203 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001204 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001205 !Subtarget->is64Bit() &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001206 Subtarget->getStackAlignment() >= 8 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001207 Subtarget->hasXMMInt()) {
Evan Chengc3b0c342010-04-08 07:37:57 +00001208 // Do not use f64 to lower memcpy if source is string constant. It's
1209 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001210 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001211 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001212 }
Evan Chengf0df0312008-05-15 08:39:06 +00001213 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001214 return MVT::i64;
1215 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001216}
1217
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001218/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1219/// current function. The returned value is a member of the
1220/// MachineJumpTableInfo::JTEntryKind enum.
1221unsigned X86TargetLowering::getJumpTableEncoding() const {
1222 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1223 // symbol.
1224 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1225 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001226 return MachineJumpTableInfo::EK_Custom32;
Michael J. Spencerec38de22010-10-10 22:04:20 +00001227
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001228 // Otherwise, use the normal jump table encoding heuristics.
1229 return TargetLowering::getJumpTableEncoding();
1230}
1231
Chris Lattnerc64daab2010-01-26 05:02:42 +00001232const MCExpr *
1233X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1234 const MachineBasicBlock *MBB,
1235 unsigned uid,MCContext &Ctx) const{
1236 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1237 Subtarget->isPICStyleGOT());
1238 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1239 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001240 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1241 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001242}
1243
Evan Chengcc415862007-11-09 01:32:10 +00001244/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1245/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001246SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001247 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001248 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001249 // This doesn't have DebugLoc associated with it, but is not really the
1250 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001251 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001252 return Table;
1253}
1254
Chris Lattner589c6f62010-01-26 06:28:43 +00001255/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1256/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1257/// MCExpr.
1258const MCExpr *X86TargetLowering::
1259getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1260 MCContext &Ctx) const {
1261 // X86-64 uses RIP relative addressing based on the jump table label.
1262 if (Subtarget->isPICStyleRIPRel())
1263 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1264
1265 // Otherwise, the reference is relative to the PIC base.
Chris Lattner142b5312010-11-14 22:48:15 +00001266 return MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
Chris Lattner589c6f62010-01-26 06:28:43 +00001267}
1268
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001269// FIXME: Why this routine is here? Move to RegInfo!
Evan Chengdee81012010-07-26 21:50:05 +00001270std::pair<const TargetRegisterClass*, uint8_t>
1271X86TargetLowering::findRepresentativeClass(EVT VT) const{
1272 const TargetRegisterClass *RRC = 0;
1273 uint8_t Cost = 1;
1274 switch (VT.getSimpleVT().SimpleTy) {
1275 default:
1276 return TargetLowering::findRepresentativeClass(VT);
1277 case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
1278 RRC = (Subtarget->is64Bit()
1279 ? X86::GR64RegisterClass : X86::GR32RegisterClass);
1280 break;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001281 case MVT::x86mmx:
Evan Chengdee81012010-07-26 21:50:05 +00001282 RRC = X86::VR64RegisterClass;
1283 break;
1284 case MVT::f32: case MVT::f64:
1285 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1286 case MVT::v4f32: case MVT::v2f64:
1287 case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1288 case MVT::v4f64:
1289 RRC = X86::VR128RegisterClass;
1290 break;
1291 }
1292 return std::make_pair(RRC, Cost);
1293}
1294
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001295bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1296 unsigned &Offset) const {
1297 if (!Subtarget->isTargetLinux())
1298 return false;
1299
1300 if (Subtarget->is64Bit()) {
1301 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1302 Offset = 0x28;
1303 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1304 AddressSpace = 256;
1305 else
1306 AddressSpace = 257;
1307 } else {
1308 // %gs:0x14 on i386
1309 Offset = 0x14;
1310 AddressSpace = 256;
1311 }
1312 return true;
1313}
1314
1315
Chris Lattner2b02a442007-02-25 08:29:00 +00001316//===----------------------------------------------------------------------===//
1317// Return Value Calling Convention Implementation
1318//===----------------------------------------------------------------------===//
1319
Chris Lattner59ed56b2007-02-28 04:55:35 +00001320#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001321
Michael J. Spencerec38de22010-10-10 22:04:20 +00001322bool
Eric Christopher471e4222011-06-08 23:55:35 +00001323X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv,
1324 MachineFunction &MF, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001325 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001326 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001327 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001328 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001329 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001330 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001331}
1332
Dan Gohman98ca4f22009-08-05 01:29:28 +00001333SDValue
1334X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001335 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001336 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001337 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001338 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001339 MachineFunction &MF = DAG.getMachineFunction();
1340 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001341
Chris Lattner9774c912007-02-27 05:28:59 +00001342 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001343 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001344 RVLocs, *DAG.getContext());
1345 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001346
Evan Chengdcea1632010-02-04 02:40:39 +00001347 // Add the regs to the liveout set for the function.
1348 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1349 for (unsigned i = 0; i != RVLocs.size(); ++i)
1350 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1351 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001352
Dan Gohman475871a2008-07-27 21:46:04 +00001353 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001354
Dan Gohman475871a2008-07-27 21:46:04 +00001355 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001356 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1357 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001358 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1359 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001360
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001361 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001362 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1363 CCValAssign &VA = RVLocs[i];
1364 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001365 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001366 EVT ValVT = ValToCopy.getValueType();
1367
Dale Johannesenc4510512010-09-24 19:05:48 +00001368 // If this is x86-64, and we disabled SSE, we can't return FP values,
1369 // or SSE or MMX vectors.
1370 if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
1371 VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001372 (Subtarget->is64Bit() && !Subtarget->hasXMM())) {
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001373 report_fatal_error("SSE register return with SSE disabled");
1374 }
1375 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1376 // llvm-gcc has never done it right and no one has noticed, so this
1377 // should be OK for now.
1378 if (ValVT == MVT::f64 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001379 (Subtarget->is64Bit() && !Subtarget->hasXMMInt()))
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001380 report_fatal_error("SSE2 register return with SSE2 disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001381
Chris Lattner447ff682008-03-11 03:23:40 +00001382 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1383 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001384 if (VA.getLocReg() == X86::ST0 ||
1385 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001386 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1387 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001388 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001389 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001390 RetOps.push_back(ValToCopy);
1391 // Don't emit a copytoreg.
1392 continue;
1393 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001394
Evan Cheng242b38b2009-02-23 09:03:22 +00001395 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1396 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001397 if (Subtarget->is64Bit()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001398 if (ValVT == MVT::x86mmx) {
Chris Lattner97a2a562010-08-26 05:24:29 +00001399 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001400 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ValToCopy);
Eric Christopher90eb4022010-07-22 00:26:08 +00001401 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1402 ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001403 // If we don't have SSE2 available, convert to v4f32 so the generated
1404 // register is legal.
1405 if (!Subtarget->hasSSE2())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001406 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32,ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001407 }
Evan Cheng242b38b2009-02-23 09:03:22 +00001408 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001409 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00001410
Dale Johannesendd64c412009-02-04 00:33:20 +00001411 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001412 Flag = Chain.getValue(1);
1413 }
Dan Gohman61a92132008-04-21 23:59:07 +00001414
1415 // The x86-64 ABI for returning structs by value requires that we copy
1416 // the sret argument into %rax for the return. We saved the argument into
1417 // a virtual register in the entry block, so now we copy the value out
1418 // and into %rax.
1419 if (Subtarget->is64Bit() &&
1420 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1421 MachineFunction &MF = DAG.getMachineFunction();
1422 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1423 unsigned Reg = FuncInfo->getSRetReturnReg();
Michael J. Spencerec38de22010-10-10 22:04:20 +00001424 assert(Reg &&
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001425 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001426 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001427
Dale Johannesendd64c412009-02-04 00:33:20 +00001428 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001429 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001430
1431 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001432 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001433 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001434
Chris Lattner447ff682008-03-11 03:23:40 +00001435 RetOps[0] = Chain; // Update chain.
1436
1437 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001438 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001439 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001440
1441 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001442 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001443}
1444
Evan Cheng3d2125c2010-11-30 23:55:39 +00001445bool X86TargetLowering::isUsedByReturnOnly(SDNode *N) const {
1446 if (N->getNumValues() != 1)
1447 return false;
1448 if (!N->hasNUsesOfValue(1, 0))
1449 return false;
1450
1451 SDNode *Copy = *N->use_begin();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001452 if (Copy->getOpcode() != ISD::CopyToReg &&
1453 Copy->getOpcode() != ISD::FP_EXTEND)
Evan Cheng3d2125c2010-11-30 23:55:39 +00001454 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001455
1456 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001457 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001458 UI != UE; ++UI) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001459 if (UI->getOpcode() != X86ISD::RET_FLAG)
1460 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001461 HasRet = true;
1462 }
Evan Cheng3d2125c2010-11-30 23:55:39 +00001463
Evan Cheng1bf891a2010-12-01 22:59:46 +00001464 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001465}
1466
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001467EVT
1468X86TargetLowering::getTypeForExtArgOrReturn(LLVMContext &Context, EVT VT,
Cameron Zwarich44579682011-03-17 14:21:56 +00001469 ISD::NodeType ExtendKind) const {
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001470 MVT ReturnMVT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001471 // TODO: Is this also valid on 32-bit?
1472 if (Subtarget->is64Bit() && VT == MVT::i1 && ExtendKind == ISD::ZERO_EXTEND)
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001473 ReturnMVT = MVT::i8;
1474 else
1475 ReturnMVT = MVT::i32;
1476
1477 EVT MinVT = getRegisterType(Context, ReturnMVT);
1478 return VT.bitsLT(MinVT) ? MinVT : VT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001479}
1480
Dan Gohman98ca4f22009-08-05 01:29:28 +00001481/// LowerCallResult - Lower the result values of a call into the
1482/// appropriate copies out of appropriate physical registers.
1483///
1484SDValue
1485X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001486 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001487 const SmallVectorImpl<ISD::InputArg> &Ins,
1488 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001489 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001490
Chris Lattnere32bbf62007-02-28 07:09:55 +00001491 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001492 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001493 bool Is64Bit = Subtarget->is64Bit();
Eric Christopher471e4222011-06-08 23:55:35 +00001494 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1495 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001496 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001497
Chris Lattner3085e152007-02-25 08:59:22 +00001498 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001499 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001500 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001501 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001502
Torok Edwin3f142c32009-02-01 18:15:56 +00001503 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001504 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001505 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasXMM())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001506 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001507 }
1508
Evan Cheng79fb3b42009-02-20 20:43:02 +00001509 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001510
1511 // If this is a call to a function that returns an fp value on the floating
1512 // point stack, we must guarantee the the value is popped from the stack, so
1513 // a CopyFromReg is not good enough - the copy instruction may be eliminated
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001514 // if the return value is not used. We use the FpPOP_RETVAL instruction
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001515 // instead.
1516 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1517 // If we prefer to use the value in xmm registers, copy it out as f80 and
1518 // use a truncate to move it from fp stack reg to xmm reg.
1519 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001520 SDValue Ops[] = { Chain, InFlag };
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001521 Chain = SDValue(DAG.getMachineNode(X86::FpPOP_RETVAL, dl, CopyVT,
1522 MVT::Other, MVT::Glue, Ops, 2), 1);
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001523 Val = Chain.getValue(0);
1524
1525 // Round the f80 to the right size, which also moves it to the appropriate
1526 // xmm register.
1527 if (CopyVT != VA.getValVT())
1528 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1529 // This truncation won't change the value.
1530 DAG.getIntPtrConstant(1));
Evan Cheng79fb3b42009-02-20 20:43:02 +00001531 } else {
1532 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1533 CopyVT, InFlag).getValue(1);
1534 Val = Chain.getValue(0);
1535 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001536 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001537 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001538 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001539
Dan Gohman98ca4f22009-08-05 01:29:28 +00001540 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001541}
1542
1543
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001544//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001545// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001546//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001547// StdCall calling convention seems to be standard for many Windows' API
1548// routines and around. It differs from C calling convention just a little:
1549// callee should clean up the stack, not caller. Symbols should be also
1550// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001551// For info on fast calling convention see Fast Calling Convention (tail call)
1552// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001553
Dan Gohman98ca4f22009-08-05 01:29:28 +00001554/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001555/// semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001556static bool CallIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
1557 if (Outs.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001558 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001559
Dan Gohman98ca4f22009-08-05 01:29:28 +00001560 return Outs[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001561}
1562
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001563/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001564/// return semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001565static bool
1566ArgsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
1567 if (Ins.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001568 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001569
Dan Gohman98ca4f22009-08-05 01:29:28 +00001570 return Ins[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001571}
1572
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001573/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1574/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001575/// the specific parameter attribute. The copy will be passed as a byval
1576/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001577static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001578CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001579 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1580 DebugLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00001581 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Michael J. Spencerec38de22010-10-10 22:04:20 +00001582
Dale Johannesendd64c412009-02-04 00:33:20 +00001583 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Stuart Hastings03d58262011-03-10 00:25:53 +00001584 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001585 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001586}
1587
Chris Lattner29689432010-03-11 00:22:57 +00001588/// IsTailCallConvention - Return true if the calling convention is one that
1589/// supports tail call optimization.
1590static bool IsTailCallConvention(CallingConv::ID CC) {
1591 return (CC == CallingConv::Fast || CC == CallingConv::GHC);
1592}
1593
Evan Cheng485fafc2011-03-21 01:19:09 +00001594bool X86TargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
1595 if (!CI->isTailCall())
1596 return false;
1597
1598 CallSite CS(CI);
1599 CallingConv::ID CalleeCC = CS.getCallingConv();
1600 if (!IsTailCallConvention(CalleeCC) && CalleeCC != CallingConv::C)
1601 return false;
1602
1603 return true;
1604}
1605
Evan Cheng0c439eb2010-01-27 00:07:07 +00001606/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1607/// a tailcall target by changing its ABI.
1608static bool FuncIsMadeTailCallSafe(CallingConv::ID CC) {
Chris Lattner29689432010-03-11 00:22:57 +00001609 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001610}
1611
Dan Gohman98ca4f22009-08-05 01:29:28 +00001612SDValue
1613X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001614 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001615 const SmallVectorImpl<ISD::InputArg> &Ins,
1616 DebugLoc dl, SelectionDAG &DAG,
1617 const CCValAssign &VA,
1618 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001619 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001620 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001621 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Evan Cheng0c439eb2010-01-27 00:07:07 +00001622 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001623 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001624 EVT ValVT;
1625
1626 // If value is passed by pointer we have address passed instead of the value
1627 // itself.
1628 if (VA.getLocInfo() == CCValAssign::Indirect)
1629 ValVT = VA.getLocVT();
1630 else
1631 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001632
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001633 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001634 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001635 // In case of tail call optimization mark all arguments mutable. Since they
1636 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001637 if (Flags.isByVal()) {
Evan Chengee2e0e32011-03-30 23:44:13 +00001638 unsigned Bytes = Flags.getByValSize();
1639 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
1640 int FI = MFI->CreateFixedObject(Bytes, VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001641 return DAG.getFrameIndex(FI, getPointerTy());
1642 } else {
1643 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001644 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001645 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1646 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001647 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00001648 false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001649 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001650}
1651
Dan Gohman475871a2008-07-27 21:46:04 +00001652SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001653X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001654 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001655 bool isVarArg,
1656 const SmallVectorImpl<ISD::InputArg> &Ins,
1657 DebugLoc dl,
1658 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001659 SmallVectorImpl<SDValue> &InVals)
1660 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001661 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001662 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001663
Gordon Henriksen86737662008-01-05 16:56:59 +00001664 const Function* Fn = MF.getFunction();
1665 if (Fn->hasExternalLinkage() &&
1666 Subtarget->isTargetCygMing() &&
1667 Fn->getName() == "main")
1668 FuncInfo->setForceFramePointer(true);
1669
Evan Cheng1bc78042006-04-26 01:20:17 +00001670 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001671 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001672 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001673
Chris Lattner29689432010-03-11 00:22:57 +00001674 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1675 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001676
Chris Lattner638402b2007-02-28 07:00:42 +00001677 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001678 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001679 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001680 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001681
1682 // Allocate shadow area for Win64
1683 if (IsWin64) {
1684 CCInfo.AllocateStack(32, 8);
1685 }
1686
Duncan Sands45907662010-10-31 13:21:44 +00001687 CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001688
Chris Lattnerf39f7712007-02-28 05:46:49 +00001689 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001690 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001691 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1692 CCValAssign &VA = ArgLocs[i];
1693 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1694 // places.
1695 assert(VA.getValNo() != LastVal &&
1696 "Don't support value assigned to multiple locs yet");
1697 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001698
Chris Lattnerf39f7712007-02-28 05:46:49 +00001699 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001700 EVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001701 TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001702 if (RegVT == MVT::i32)
Chris Lattnerf39f7712007-02-28 05:46:49 +00001703 RC = X86::GR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001704 else if (Is64Bit && RegVT == MVT::i64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001705 RC = X86::GR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001706 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001707 RC = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001708 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001709 RC = X86::FR64RegisterClass;
Bruno Cardoso Lopesac098352010-08-05 23:35:51 +00001710 else if (RegVT.isVector() && RegVT.getSizeInBits() == 256)
1711 RC = X86::VR256RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001712 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001713 RC = X86::VR128RegisterClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001714 else if (RegVT == MVT::x86mmx)
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001715 RC = X86::VR64RegisterClass;
1716 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001717 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001718
Devang Patel68e6bee2011-02-21 23:21:26 +00001719 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001720 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001721
Chris Lattnerf39f7712007-02-28 05:46:49 +00001722 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1723 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1724 // right size.
1725 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001726 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001727 DAG.getValueType(VA.getValVT()));
1728 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001729 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001730 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001731 else if (VA.getLocInfo() == CCValAssign::BCvt)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001732 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001733
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001734 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001735 // Handle MMX values passed in XMM regs.
1736 if (RegVT.isVector()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001737 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(),
1738 ArgValue);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001739 } else
1740 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001741 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001742 } else {
1743 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001744 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001745 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001746
1747 // If value is passed via pointer - do a load.
1748 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00001749 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
1750 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001751
Dan Gohman98ca4f22009-08-05 01:29:28 +00001752 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001753 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001754
Dan Gohman61a92132008-04-21 23:59:07 +00001755 // The x86-64 ABI for returning structs by value requires that we copy
1756 // the sret argument into %rax for the return. Save the argument into
1757 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001758 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001759 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1760 unsigned Reg = FuncInfo->getSRetReturnReg();
1761 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001762 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001763 FuncInfo->setSRetReturnReg(Reg);
1764 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001765 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001766 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001767 }
1768
Chris Lattnerf39f7712007-02-28 05:46:49 +00001769 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001770 // Align stack specially for tail calls.
1771 if (FuncIsMadeTailCallSafe(CallConv))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001772 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001773
Evan Cheng1bc78042006-04-26 01:20:17 +00001774 // If the function takes variable number of arguments, make a frame index for
1775 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001776 if (isVarArg) {
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001777 if (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
1778 CallConv != CallingConv::X86_ThisCall)) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00001779 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00001780 }
1781 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001782 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1783
1784 // FIXME: We should really autogenerate these arrays
1785 static const unsigned GPR64ArgRegsWin64[] = {
1786 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001787 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001788 static const unsigned GPR64ArgRegs64Bit[] = {
1789 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1790 };
1791 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001792 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1793 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1794 };
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001795 const unsigned *GPR64ArgRegs;
1796 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001797
1798 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001799 // The XMM registers which might contain var arg parameters are shadowed
1800 // in their paired GPR. So we only need to save the GPR to their home
1801 // slots.
1802 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001803 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001804 } else {
1805 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1806 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001807
1808 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit, TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001809 }
1810 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1811 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001812
Devang Patel578efa92009-06-05 21:57:13 +00001813 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001814 assert(!(NumXMMRegs && !Subtarget->hasXMM()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001815 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001816 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001817 "SSE register cannot be used when SSE is disabled!");
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001818 if (UseSoftFloat || NoImplicitFloatOps || !Subtarget->hasXMM())
Torok Edwin3f142c32009-02-01 18:15:56 +00001819 // Kernel mode asks for SSE to be disabled, so don't push them
1820 // on the stack.
1821 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001822
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001823 if (IsWin64) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001824 const TargetFrameLowering &TFI = *getTargetMachine().getFrameLowering();
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001825 // Get to the caller-allocated home save location. Add 8 to account
1826 // for the return address.
1827 int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001828 FuncInfo->setRegSaveFrameIndex(
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001829 MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001830 // Fixup to set vararg frame on shadow area (4 x i64).
1831 if (NumIntRegs < 4)
1832 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001833 } else {
1834 // For X86-64, if there are vararg parameters that are passed via
1835 // registers, then we must store them to their spots on the stack so they
1836 // may be loaded by deferencing the result of va_next.
1837 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
1838 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
1839 FuncInfo->setRegSaveFrameIndex(
1840 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00001841 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001842 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001843
Gordon Henriksen86737662008-01-05 16:56:59 +00001844 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001845 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00001846 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
1847 getPointerTy());
1848 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001849 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00001850 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
1851 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00001852 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00001853 X86::GR64RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00001854 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001855 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001856 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001857 MachinePointerInfo::getFixedStack(
1858 FuncInfo->getRegSaveFrameIndex(), Offset),
1859 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001860 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001861 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001862 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001863
Dan Gohmanface41a2009-08-16 21:24:25 +00001864 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
1865 // Now store the XMM (fp + vector) parameter registers.
1866 SmallVector<SDValue, 11> SaveXMMOps;
1867 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001868
Devang Patel68e6bee2011-02-21 23:21:26 +00001869 unsigned AL = MF.addLiveIn(X86::AL, X86::GR8RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00001870 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
1871 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001872
Dan Gohman1e93df62010-04-17 14:41:14 +00001873 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1874 FuncInfo->getRegSaveFrameIndex()));
1875 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1876 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00001877
Dan Gohmanface41a2009-08-16 21:24:25 +00001878 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001879 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00001880 X86::VR128RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00001881 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
1882 SaveXMMOps.push_back(Val);
1883 }
1884 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
1885 MVT::Other,
1886 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00001887 }
Dan Gohmanface41a2009-08-16 21:24:25 +00001888
1889 if (!MemOps.empty())
1890 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1891 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001892 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001893 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001894
Gordon Henriksen86737662008-01-05 16:56:59 +00001895 // Some CCs need callee pop.
Evan Chengef41ff62011-06-23 17:54:54 +00001896 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg, GuaranteedTailCallOpt)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001897 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001898 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00001899 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001900 // If this is an sret function, the return should pop the hidden pointer.
Chris Lattner29689432010-03-11 00:22:57 +00001901 if (!Is64Bit && !IsTailCallConvention(CallConv) && ArgsAreStructReturn(Ins))
Dan Gohman1e93df62010-04-17 14:41:14 +00001902 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001903 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001904
Gordon Henriksen86737662008-01-05 16:56:59 +00001905 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001906 // RegSaveFrameIndex is X86-64 only.
1907 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001908 if (CallConv == CallingConv::X86_FastCall ||
1909 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00001910 // fastcc functions can't have varargs.
1911 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00001912 }
Evan Cheng25caf632006-05-23 21:06:34 +00001913
Dan Gohman98ca4f22009-08-05 01:29:28 +00001914 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001915}
1916
Dan Gohman475871a2008-07-27 21:46:04 +00001917SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001918X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
1919 SDValue StackPtr, SDValue Arg,
1920 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00001921 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001922 ISD::ArgFlagsTy Flags) const {
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001923 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00001924 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00001925 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001926 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00001927 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001928
1929 return DAG.getStore(Chain, dl, Arg, PtrOff,
1930 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00001931 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00001932}
1933
Bill Wendling64e87322009-01-16 19:25:27 +00001934/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001935/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00001936SDValue
1937X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00001938 SDValue &OutRetAddr, SDValue Chain,
1939 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00001940 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001941 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00001942 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001943 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00001944
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001945 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00001946 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
1947 false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001948 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001949}
1950
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001951/// EmitTailCallStoreRetAddr - Emit a store of the return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001952/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00001953static SDValue
1954EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00001955 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00001956 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001957 // Store the return address to the appropriate stack slot.
1958 if (!FPDiff) return Chain;
1959 // Calculate the new stack slot for the return address.
1960 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00001961 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00001962 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00001963 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00001964 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001965 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00001966 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00001967 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001968 return Chain;
1969}
1970
Dan Gohman98ca4f22009-08-05 01:29:28 +00001971SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001972X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001973 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001974 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001975 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001976 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001977 const SmallVectorImpl<ISD::InputArg> &Ins,
1978 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001979 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001980 MachineFunction &MF = DAG.getMachineFunction();
1981 bool Is64Bit = Subtarget->is64Bit();
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00001982 bool IsWin64 = Subtarget->isTargetWin64();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001983 bool IsStructRet = CallIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00001984 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001985
Evan Cheng5f941932010-02-05 02:21:12 +00001986 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00001987 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00001988 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1989 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001990 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00001991
1992 // Sibcalls are automatically detected tailcalls which do not require
1993 // ABI changes.
Dan Gohman1797ed52010-02-08 20:27:50 +00001994 if (!GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00001995 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00001996
1997 if (isTailCall)
1998 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00001999 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00002000
Chris Lattner29689432010-03-11 00:22:57 +00002001 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
2002 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00002003
Chris Lattner638402b2007-02-28 07:00:42 +00002004 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00002005 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002006 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00002007 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002008
2009 // Allocate shadow area for Win64
2010 if (IsWin64) {
2011 CCInfo.AllocateStack(32, 8);
2012 }
2013
Duncan Sands45907662010-10-31 13:21:44 +00002014 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00002015
Chris Lattner423c5f42007-02-28 05:31:48 +00002016 // Get a count of how many bytes are to be pushed on the stack.
2017 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00002018 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00002019 // This is a sibcall. The memory operands are available in caller's
2020 // own caller's stack.
2021 NumBytes = 0;
Chris Lattner29689432010-03-11 00:22:57 +00002022 else if (GuaranteedTailCallOpt && IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00002023 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002024
Gordon Henriksen86737662008-01-05 16:56:59 +00002025 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00002026 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002027 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00002028 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00002029 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
2030 FPDiff = NumBytesCallerPushed - NumBytes;
2031
2032 // Set the delta of movement of the returnaddr stackslot.
2033 // But only set if delta is greater than previous delta.
2034 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
2035 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
2036 }
2037
Evan Chengf22f9b32010-02-06 03:28:46 +00002038 if (!IsSibcall)
2039 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002040
Dan Gohman475871a2008-07-27 21:46:04 +00002041 SDValue RetAddrFrIdx;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002042 // Load return address for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00002043 if (isTailCall && FPDiff)
2044 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
2045 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002046
Dan Gohman475871a2008-07-27 21:46:04 +00002047 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
2048 SmallVector<SDValue, 8> MemOpChains;
2049 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00002050
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002051 // Walk the register/memloc assignments, inserting copies/loads. In the case
2052 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00002053 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2054 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00002055 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00002056 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002057 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00002058 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00002059
Chris Lattner423c5f42007-02-28 05:31:48 +00002060 // Promote the value if needed.
2061 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002062 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00002063 case CCValAssign::Full: break;
2064 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002065 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002066 break;
2067 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002068 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002069 break;
2070 case CCValAssign::AExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002071 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
2072 // Special case: passing MMX values in XMM registers.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002073 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00002074 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
2075 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002076 } else
2077 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
2078 break;
2079 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002080 Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002081 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002082 case CCValAssign::Indirect: {
2083 // Store the argument.
2084 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00002085 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002086 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00002087 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002088 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002089 Arg = SpillSlot;
2090 break;
2091 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00002092 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002093
Chris Lattner423c5f42007-02-28 05:31:48 +00002094 if (VA.isRegLoc()) {
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002095 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
2096 if (isVarArg && IsWin64) {
2097 // Win64 ABI requires argument XMM reg to be copied to the corresponding
2098 // shadow reg if callee is a varargs function.
2099 unsigned ShadowReg = 0;
2100 switch (VA.getLocReg()) {
2101 case X86::XMM0: ShadowReg = X86::RCX; break;
2102 case X86::XMM1: ShadowReg = X86::RDX; break;
2103 case X86::XMM2: ShadowReg = X86::R8; break;
2104 case X86::XMM3: ShadowReg = X86::R9; break;
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002105 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002106 if (ShadowReg)
2107 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002108 }
Evan Chengf22f9b32010-02-06 03:28:46 +00002109 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00002110 assert(VA.isMemLoc());
2111 if (StackPtr.getNode() == 0)
2112 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
2113 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
2114 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002115 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002116 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002117
Evan Cheng32fe1032006-05-25 00:59:30 +00002118 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002119 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002120 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002121
Evan Cheng347d5f72006-04-28 21:29:37 +00002122 // Build a sequence of copy-to-reg nodes chained together with token chain
2123 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002124 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002125 // Tail call byval lowering might overwrite argument registers so in case of
2126 // tail call optimization the copies to registers are lowered later.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002127 if (!isTailCall)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002128 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002129 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002130 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002131 InFlag = Chain.getValue(1);
2132 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002133
Chris Lattner88e1fd52009-07-09 04:24:46 +00002134 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002135 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2136 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002137 if (!isTailCall) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002138 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
2139 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00002140 DebugLoc(), getPointerTy()),
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002141 InFlag);
2142 InFlag = Chain.getValue(1);
2143 } else {
2144 // If we are tail calling and generating PIC/GOT style code load the
2145 // address of the callee into ECX. The value in ecx is used as target of
2146 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2147 // for tail calls on PIC/GOT architectures. Normally we would just put the
2148 // address of GOT into ebx and then call target@PLT. But for tail calls
2149 // ebx would be restored (since ebx is callee saved) before jumping to the
2150 // target@PLT.
2151
2152 // Note: The actual moving to ECX is done further down.
2153 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2154 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2155 !G->getGlobal()->hasProtectedVisibility())
2156 Callee = LowerGlobalAddress(Callee, DAG);
2157 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002158 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002159 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002160 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002161
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002162 if (Is64Bit && isVarArg && !IsWin64) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002163 // From AMD64 ABI document:
2164 // For calls that may call functions that use varargs or stdargs
2165 // (prototype-less calls or calls to functions containing ellipsis (...) in
2166 // the declaration) %al is used as hidden argument to specify the number
2167 // of SSE registers used. The contents of %al do not need to match exactly
2168 // the number of registers, but must be an ubound on the number of SSE
2169 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002170
Gordon Henriksen86737662008-01-05 16:56:59 +00002171 // Count the number of XMM registers allocated.
2172 static const unsigned XMMArgRegs[] = {
2173 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2174 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2175 };
2176 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00002177 assert((Subtarget->hasXMM() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002178 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002179
Dale Johannesendd64c412009-02-04 00:33:20 +00002180 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Owen Anderson825b72b2009-08-11 20:47:22 +00002181 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002182 InFlag = Chain.getValue(1);
2183 }
2184
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002185
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002186 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002187 if (isTailCall) {
2188 // Force all the incoming stack arguments to be loaded from the stack
2189 // before any new outgoing arguments are stored to the stack, because the
2190 // outgoing stack slots may alias the incoming argument stack slots, and
2191 // the alias isn't otherwise explicit. This is slightly more conservative
2192 // than necessary, because it means that each store effectively depends
2193 // on every argument instead of just those arguments it would clobber.
2194 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2195
Dan Gohman475871a2008-07-27 21:46:04 +00002196 SmallVector<SDValue, 8> MemOpChains2;
2197 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002198 int FI = 0;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002199 // Do not flag preceding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00002200 InFlag = SDValue();
Dan Gohman1797ed52010-02-08 20:27:50 +00002201 if (GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002202 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2203 CCValAssign &VA = ArgLocs[i];
2204 if (VA.isRegLoc())
2205 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002206 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002207 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002208 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002209 // Create frame index.
2210 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002211 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002212 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002213 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002214
Duncan Sands276dcbd2008-03-21 09:14:45 +00002215 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002216 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002217 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002218 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002219 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00002220 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002221 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002222
Dan Gohman98ca4f22009-08-05 01:29:28 +00002223 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2224 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002225 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002226 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002227 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002228 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002229 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002230 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002231 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002232 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002233 }
2234 }
2235
2236 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002237 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002238 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002239
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002240 // Copy arguments to their registers.
2241 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002242 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002243 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002244 InFlag = Chain.getValue(1);
2245 }
Dan Gohman475871a2008-07-27 21:46:04 +00002246 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002247
Gordon Henriksen86737662008-01-05 16:56:59 +00002248 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002249 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002250 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002251 }
2252
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002253 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2254 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2255 // In the 64-bit large code model, we have to make all calls
2256 // through a register, since the call instruction's 32-bit
2257 // pc-relative offset may not be large enough to hold the whole
2258 // address.
2259 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002260 // If the callee is a GlobalAddress node (quite common, every direct call
2261 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2262 // it.
2263
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002264 // We should use extra load for direct calls to dllimported functions in
2265 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002266 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002267 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002268 unsigned char OpFlags = 0;
John McCall3a3465b2011-06-15 20:36:13 +00002269 bool ExtraLoad = false;
2270 unsigned WrapperKind = ISD::DELETED_NODE;
Eric Christopherfd179292009-08-27 18:07:15 +00002271
Chris Lattner48a7d022009-07-09 05:02:21 +00002272 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2273 // external symbols most go through the PLT in PIC mode. If the symbol
2274 // has hidden or protected visibility, or if it is static or local, then
2275 // we don't need to use the PLT - we can directly call it.
2276 if (Subtarget->isTargetELF() &&
2277 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002278 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002279 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002280 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002281 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002282 (!Subtarget->getTargetTriple().isMacOSX() ||
2283 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002284 // PC-relative references to external symbols should go through $stub,
2285 // unless we're building with the leopard linker or later, which
2286 // automatically synthesizes these stubs.
2287 OpFlags = X86II::MO_DARWIN_STUB;
John McCall3a3465b2011-06-15 20:36:13 +00002288 } else if (Subtarget->isPICStyleRIPRel() &&
2289 isa<Function>(GV) &&
2290 cast<Function>(GV)->hasFnAttr(Attribute::NonLazyBind)) {
2291 // If the function is marked as non-lazy, generate an indirect call
2292 // which loads from the GOT directly. This avoids runtime overhead
2293 // at the cost of eager binding (and one extra byte of encoding).
2294 OpFlags = X86II::MO_GOTPCREL;
2295 WrapperKind = X86ISD::WrapperRIP;
2296 ExtraLoad = true;
Chris Lattner74e726e2009-07-09 05:27:35 +00002297 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002298
Devang Patel0d881da2010-07-06 22:08:15 +00002299 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002300 G->getOffset(), OpFlags);
John McCall3a3465b2011-06-15 20:36:13 +00002301
2302 // Add a wrapper if needed.
2303 if (WrapperKind != ISD::DELETED_NODE)
2304 Callee = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Callee);
2305 // Add extra indirection if needed.
2306 if (ExtraLoad)
2307 Callee = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Callee,
2308 MachinePointerInfo::getGOT(),
2309 false, false, 0);
Chris Lattner48a7d022009-07-09 05:02:21 +00002310 }
Bill Wendling056292f2008-09-16 21:48:12 +00002311 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002312 unsigned char OpFlags = 0;
2313
Evan Cheng1bf891a2010-12-01 22:59:46 +00002314 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2315 // external symbols should go through the PLT.
2316 if (Subtarget->isTargetELF() &&
2317 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2318 OpFlags = X86II::MO_PLT;
2319 } else if (Subtarget->isPICStyleStubAny() &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002320 (!Subtarget->getTargetTriple().isMacOSX() ||
2321 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Evan Cheng1bf891a2010-12-01 22:59:46 +00002322 // PC-relative references to external symbols should go through $stub,
2323 // unless we're building with the leopard linker or later, which
2324 // automatically synthesizes these stubs.
2325 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002326 }
Eric Christopherfd179292009-08-27 18:07:15 +00002327
Chris Lattner48a7d022009-07-09 05:02:21 +00002328 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2329 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002330 }
2331
Chris Lattnerd96d0722007-02-25 06:40:16 +00002332 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002333 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002334 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002335
Evan Chengf22f9b32010-02-06 03:28:46 +00002336 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002337 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2338 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002339 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002340 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002341
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002342 Ops.push_back(Chain);
2343 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002344
Dan Gohman98ca4f22009-08-05 01:29:28 +00002345 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002346 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002347
Gordon Henriksen86737662008-01-05 16:56:59 +00002348 // Add argument registers to the end of the list so that they are known live
2349 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002350 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2351 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2352 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002353
Evan Cheng586ccac2008-03-18 23:36:35 +00002354 // Add an implicit use GOT pointer in EBX.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002355 if (!isTailCall && Subtarget->isPICStyleGOT())
Evan Cheng586ccac2008-03-18 23:36:35 +00002356 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
2357
Anton Korobeynikov3a1e54a2010-08-17 21:06:07 +00002358 // Add an implicit use of AL for non-Windows x86 64-bit vararg functions.
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002359 if (Is64Bit && isVarArg && !IsWin64)
Owen Anderson825b72b2009-08-11 20:47:22 +00002360 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
Evan Cheng586ccac2008-03-18 23:36:35 +00002361
Gabor Greifba36cb52008-08-28 21:40:38 +00002362 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002363 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002364
Dan Gohman98ca4f22009-08-05 01:29:28 +00002365 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002366 // We used to do:
2367 //// If this is the first return lowered for this function, add the regs
2368 //// to the liveout set for the function.
2369 // This isn't right, although it's probably harmless on x86; liveouts
2370 // should be computed from returns not tail calls. Consider a void
2371 // function making a tail call to a function returning int.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002372 return DAG.getNode(X86ISD::TC_RETURN, dl,
2373 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002374 }
2375
Dale Johannesenace16102009-02-03 19:33:06 +00002376 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002377 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002378
Chris Lattner2d297092006-05-23 18:50:38 +00002379 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002380 unsigned NumBytesForCalleeToPush;
Evan Chengef41ff62011-06-23 17:54:54 +00002381 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg, GuaranteedTailCallOpt))
Gordon Henriksen86737662008-01-05 16:56:59 +00002382 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Chris Lattner29689432010-03-11 00:22:57 +00002383 else if (!Is64Bit && !IsTailCallConvention(CallConv) && IsStructRet)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002384 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002385 // pops the hidden struct pointer, so we have to push it back.
2386 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002387 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002388 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002389 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002390
Gordon Henriksenae636f82008-01-03 16:47:34 +00002391 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002392 if (!IsSibcall) {
2393 Chain = DAG.getCALLSEQ_END(Chain,
2394 DAG.getIntPtrConstant(NumBytes, true),
2395 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2396 true),
2397 InFlag);
2398 InFlag = Chain.getValue(1);
2399 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002400
Chris Lattner3085e152007-02-25 08:59:22 +00002401 // Handle result values, copying them out of physregs into vregs that we
2402 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002403 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2404 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002405}
2406
Evan Cheng25ab6902006-09-08 06:48:29 +00002407
2408//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002409// Fast Calling Convention (tail call) implementation
2410//===----------------------------------------------------------------------===//
2411
2412// Like std call, callee cleans arguments, convention except that ECX is
2413// reserved for storing the tail called function address. Only 2 registers are
2414// free for argument passing (inreg). Tail call optimization is performed
2415// provided:
2416// * tailcallopt is enabled
2417// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002418// On X86_64 architecture with GOT-style position independent code only local
2419// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002420// To keep the stack aligned according to platform abi the function
2421// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2422// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002423// If a tail called function callee has more arguments than the caller the
2424// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002425// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002426// original REtADDR, but before the saved framepointer or the spilled registers
2427// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2428// stack layout:
2429// arg1
2430// arg2
2431// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002432// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002433// move area ]
2434// (possible EBP)
2435// ESI
2436// EDI
2437// local1 ..
2438
2439/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2440/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002441unsigned
2442X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2443 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002444 MachineFunction &MF = DAG.getMachineFunction();
2445 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002446 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002447 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002448 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002449 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002450 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002451 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2452 // Number smaller than 12 so just add the difference.
2453 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2454 } else {
2455 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002456 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002457 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002458 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002459 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002460}
2461
Evan Cheng5f941932010-02-05 02:21:12 +00002462/// MatchingStackOffset - Return true if the given stack call argument is
2463/// already available in the same position (relatively) of the caller's
2464/// incoming argument stack.
2465static
2466bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2467 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2468 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002469 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2470 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002471 if (Arg.getOpcode() == ISD::CopyFromReg) {
2472 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00002473 if (!TargetRegisterInfo::isVirtualRegister(VR))
Evan Cheng5f941932010-02-05 02:21:12 +00002474 return false;
2475 MachineInstr *Def = MRI->getVRegDef(VR);
2476 if (!Def)
2477 return false;
2478 if (!Flags.isByVal()) {
2479 if (!TII->isLoadFromStackSlot(Def, FI))
2480 return false;
2481 } else {
2482 unsigned Opcode = Def->getOpcode();
2483 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2484 Def->getOperand(1).isFI()) {
2485 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002486 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002487 } else
2488 return false;
2489 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002490 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2491 if (Flags.isByVal())
2492 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002493 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002494 // define @foo(%struct.X* %A) {
2495 // tail call @bar(%struct.X* byval %A)
2496 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002497 return false;
2498 SDValue Ptr = Ld->getBasePtr();
2499 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2500 if (!FINode)
2501 return false;
2502 FI = FINode->getIndex();
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002503 } else if (Arg.getOpcode() == ISD::FrameIndex && Flags.isByVal()) {
Chad Rosier14d71aa2011-06-25 18:51:28 +00002504 FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Arg);
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002505 FI = FINode->getIndex();
2506 Bytes = Flags.getByValSize();
Evan Cheng4cae1332010-03-05 08:38:04 +00002507 } else
2508 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002509
Evan Cheng4cae1332010-03-05 08:38:04 +00002510 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002511 if (!MFI->isFixedObjectIndex(FI))
2512 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002513 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002514}
2515
Dan Gohman98ca4f22009-08-05 01:29:28 +00002516/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2517/// for tail call optimization. Targets which want to do tail call
2518/// optimization should implement this function.
2519bool
2520X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002521 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002522 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002523 bool isCalleeStructRet,
2524 bool isCallerStructRet,
Evan Chengb1712452010-01-27 06:25:16 +00002525 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002526 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002527 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002528 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002529 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002530 CalleeCC != CallingConv::C)
2531 return false;
2532
Evan Cheng7096ae42010-01-29 06:45:59 +00002533 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002534 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002535 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Cheng13617962010-04-30 01:12:32 +00002536 CallingConv::ID CallerCC = CallerF->getCallingConv();
2537 bool CCMatch = CallerCC == CalleeCC;
2538
Dan Gohman1797ed52010-02-08 20:27:50 +00002539 if (GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002540 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002541 return true;
2542 return false;
2543 }
2544
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002545 // Look for obvious safe cases to perform tail call optimization that do not
2546 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002547
Evan Cheng2c12cb42010-03-26 16:26:03 +00002548 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2549 // emit a special epilogue.
2550 if (RegInfo->needsStackRealignment(MF))
2551 return false;
2552
Evan Chenga375d472010-03-15 18:54:48 +00002553 // Also avoid sibcall optimization if either caller or callee uses struct
2554 // return semantics.
2555 if (isCalleeStructRet || isCallerStructRet)
2556 return false;
2557
Chad Rosier2416da32011-06-24 21:15:36 +00002558 // An stdcall caller is expected to clean up its arguments; the callee
2559 // isn't going to do that.
2560 if (!CCMatch && CallerCC==CallingConv::X86_StdCall)
2561 return false;
2562
Chad Rosier871f6642011-05-18 19:59:50 +00002563 // Do not sibcall optimize vararg calls unless all arguments are passed via
Chad Rosiera1660892011-05-20 00:59:28 +00002564 // registers.
Chad Rosier871f6642011-05-18 19:59:50 +00002565 if (isVarArg && !Outs.empty()) {
Chad Rosiera1660892011-05-20 00:59:28 +00002566
2567 // Optimizing for varargs on Win64 is unlikely to be safe without
2568 // additional testing.
2569 if (Subtarget->isTargetWin64())
2570 return false;
2571
Chad Rosier871f6642011-05-18 19:59:50 +00002572 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002573 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
2574 getTargetMachine(), ArgLocs, *DAG.getContext());
Chad Rosier871f6642011-05-18 19:59:50 +00002575
Chad Rosier871f6642011-05-18 19:59:50 +00002576 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
2577 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i)
2578 if (!ArgLocs[i].isRegLoc())
2579 return false;
2580 }
2581
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002582 // If the call result is in ST0 / ST1, it needs to be popped off the x87 stack.
2583 // Therefore if it's not used by the call it is not safe to optimize this into
2584 // a sibcall.
2585 bool Unused = false;
2586 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2587 if (!Ins[i].Used) {
2588 Unused = true;
2589 break;
2590 }
2591 }
2592 if (Unused) {
2593 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002594 CCState CCInfo(CalleeCC, false, DAG.getMachineFunction(),
2595 getTargetMachine(), RVLocs, *DAG.getContext());
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002596 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002597 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002598 CCValAssign &VA = RVLocs[i];
2599 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2600 return false;
2601 }
2602 }
2603
Evan Cheng13617962010-04-30 01:12:32 +00002604 // If the calling conventions do not match, then we'd better make sure the
2605 // results are returned in the same way as what the caller expects.
2606 if (!CCMatch) {
2607 SmallVector<CCValAssign, 16> RVLocs1;
Eric Christopher471e4222011-06-08 23:55:35 +00002608 CCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
2609 getTargetMachine(), RVLocs1, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002610 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2611
2612 SmallVector<CCValAssign, 16> RVLocs2;
Eric Christopher471e4222011-06-08 23:55:35 +00002613 CCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
2614 getTargetMachine(), RVLocs2, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002615 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2616
2617 if (RVLocs1.size() != RVLocs2.size())
2618 return false;
2619 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2620 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2621 return false;
2622 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2623 return false;
2624 if (RVLocs1[i].isRegLoc()) {
2625 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2626 return false;
2627 } else {
2628 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2629 return false;
2630 }
2631 }
2632 }
2633
Evan Chenga6bff982010-01-30 01:22:00 +00002634 // If the callee takes no arguments then go on to check the results of the
2635 // call.
2636 if (!Outs.empty()) {
2637 // Check if stack adjustment is needed. For now, do not do this if any
2638 // argument is passed on the stack.
2639 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002640 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
2641 getTargetMachine(), ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002642
2643 // Allocate shadow area for Win64
2644 if (Subtarget->isTargetWin64()) {
2645 CCInfo.AllocateStack(32, 8);
2646 }
2647
Duncan Sands45907662010-10-31 13:21:44 +00002648 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Stuart Hastings6db2c2f2011-05-17 16:59:46 +00002649 if (CCInfo.getNextStackOffset()) {
Evan Chengb2c92902010-02-02 02:22:50 +00002650 MachineFunction &MF = DAG.getMachineFunction();
2651 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2652 return false;
Evan Chengb2c92902010-02-02 02:22:50 +00002653
2654 // Check if the arguments are already laid out in the right way as
2655 // the caller's fixed stack objects.
2656 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002657 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2658 const X86InstrInfo *TII =
2659 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002660 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2661 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002662 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002663 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002664 if (VA.getLocInfo() == CCValAssign::Indirect)
2665 return false;
2666 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002667 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2668 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002669 return false;
2670 }
2671 }
2672 }
Evan Cheng9c044672010-05-29 01:35:22 +00002673
2674 // If the tailcall address may be in a register, then make sure it's
2675 // possible to register allocate for it. In 32-bit, the call address can
2676 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002677 // callee-saved registers are restored. These happen to be the same
2678 // registers used to pass 'inreg' arguments so watch out for those.
2679 if (!Subtarget->is64Bit() &&
2680 !isa<GlobalAddressSDNode>(Callee) &&
Evan Cheng9c044672010-05-29 01:35:22 +00002681 !isa<ExternalSymbolSDNode>(Callee)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002682 unsigned NumInRegs = 0;
2683 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2684 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002685 if (!VA.isRegLoc())
2686 continue;
2687 unsigned Reg = VA.getLocReg();
2688 switch (Reg) {
2689 default: break;
2690 case X86::EAX: case X86::EDX: case X86::ECX:
2691 if (++NumInRegs == 3)
Evan Cheng9c044672010-05-29 01:35:22 +00002692 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002693 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002694 }
2695 }
2696 }
Evan Chenga6bff982010-01-30 01:22:00 +00002697 }
Evan Chengb1712452010-01-27 06:25:16 +00002698
Evan Cheng86809cc2010-02-03 03:28:02 +00002699 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002700}
2701
Dan Gohman3df24e62008-09-03 23:12:08 +00002702FastISel *
Dan Gohmana4160c32010-07-07 16:29:44 +00002703X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
2704 return X86::createFastISel(funcInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00002705}
2706
2707
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002708//===----------------------------------------------------------------------===//
2709// Other Lowering Hooks
2710//===----------------------------------------------------------------------===//
2711
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00002712static bool MayFoldLoad(SDValue Op) {
2713 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
2714}
2715
2716static bool MayFoldIntoStore(SDValue Op) {
2717 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
2718}
2719
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002720static bool isTargetShuffle(unsigned Opcode) {
2721 switch(Opcode) {
2722 default: return false;
2723 case X86ISD::PSHUFD:
2724 case X86ISD::PSHUFHW:
2725 case X86ISD::PSHUFLW:
2726 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002727 case X86ISD::PALIGN:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002728 case X86ISD::SHUFPS:
2729 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002730 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002731 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002732 case X86ISD::MOVLPS:
2733 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002734 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002735 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002736 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002737 case X86ISD::MOVSS:
2738 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002739 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002740 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +00002741 case X86ISD::VUNPCKLPS:
2742 case X86ISD::VUNPCKLPD:
2743 case X86ISD::VUNPCKLPSY:
2744 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002745 case X86ISD::PUNPCKLWD:
2746 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002747 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002748 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002749 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002750 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002751 case X86ISD::PUNPCKHWD:
2752 case X86ISD::PUNPCKHBW:
2753 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002754 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002755 return true;
2756 }
2757 return false;
2758}
2759
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002760static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002761 SDValue V1, SelectionDAG &DAG) {
2762 switch(Opc) {
2763 default: llvm_unreachable("Unknown x86 shuffle node");
2764 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002765 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002766 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002767 return DAG.getNode(Opc, dl, VT, V1);
2768 }
2769
2770 return SDValue();
2771}
2772
2773static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00002774 SDValue V1, unsigned TargetMask, SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002775 switch(Opc) {
2776 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002777 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002778 case X86ISD::PSHUFHW:
2779 case X86ISD::PSHUFLW:
2780 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
2781 }
2782
2783 return SDValue();
2784}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002785
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002786static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2787 SDValue V1, SDValue V2, unsigned TargetMask, SelectionDAG &DAG) {
2788 switch(Opc) {
2789 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002790 case X86ISD::PALIGN:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002791 case X86ISD::SHUFPD:
2792 case X86ISD::SHUFPS:
2793 return DAG.getNode(Opc, dl, VT, V1, V2,
2794 DAG.getConstant(TargetMask, MVT::i8));
2795 }
2796 return SDValue();
2797}
2798
2799static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2800 SDValue V1, SDValue V2, SelectionDAG &DAG) {
2801 switch(Opc) {
2802 default: llvm_unreachable("Unknown x86 shuffle node");
2803 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00002804 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002805 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002806 case X86ISD::MOVLPS:
2807 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002808 case X86ISD::MOVSS:
2809 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002810 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002811 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +00002812 case X86ISD::VUNPCKLPS:
2813 case X86ISD::VUNPCKLPD:
2814 case X86ISD::VUNPCKLPSY:
2815 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002816 case X86ISD::PUNPCKLWD:
2817 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002818 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002819 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002820 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002821 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002822 case X86ISD::PUNPCKHWD:
2823 case X86ISD::PUNPCKHBW:
2824 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002825 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002826 return DAG.getNode(Opc, dl, VT, V1, V2);
2827 }
2828 return SDValue();
2829}
2830
Dan Gohmand858e902010-04-17 15:26:15 +00002831SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002832 MachineFunction &MF = DAG.getMachineFunction();
2833 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2834 int ReturnAddrIndex = FuncInfo->getRAIndex();
2835
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002836 if (ReturnAddrIndex == 0) {
2837 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002838 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002839 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00002840 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002841 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002842 }
2843
Evan Cheng25ab6902006-09-08 06:48:29 +00002844 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002845}
2846
2847
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002848bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2849 bool hasSymbolicDisplacement) {
2850 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00002851 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002852 return false;
2853
2854 // If we don't have a symbolic displacement - we don't have any extra
2855 // restrictions.
2856 if (!hasSymbolicDisplacement)
2857 return true;
2858
2859 // FIXME: Some tweaks might be needed for medium code model.
2860 if (M != CodeModel::Small && M != CodeModel::Kernel)
2861 return false;
2862
2863 // For small code model we assume that latest object is 16MB before end of 31
2864 // bits boundary. We may also accept pretty large negative constants knowing
2865 // that all objects are in the positive half of address space.
2866 if (M == CodeModel::Small && Offset < 16*1024*1024)
2867 return true;
2868
2869 // For kernel code model we know that all object resist in the negative half
2870 // of 32bits address space. We may not accept negative offsets, since they may
2871 // be just off and we may accept pretty large positive ones.
2872 if (M == CodeModel::Kernel && Offset > 0)
2873 return true;
2874
2875 return false;
2876}
2877
Evan Chengef41ff62011-06-23 17:54:54 +00002878/// isCalleePop - Determines whether the callee is required to pop its
2879/// own arguments. Callee pop is necessary to support tail calls.
2880bool X86::isCalleePop(CallingConv::ID CallingConv,
2881 bool is64Bit, bool IsVarArg, bool TailCallOpt) {
2882 if (IsVarArg)
2883 return false;
2884
2885 switch (CallingConv) {
2886 default:
2887 return false;
2888 case CallingConv::X86_StdCall:
2889 return !is64Bit;
2890 case CallingConv::X86_FastCall:
2891 return !is64Bit;
2892 case CallingConv::X86_ThisCall:
2893 return !is64Bit;
2894 case CallingConv::Fast:
2895 return TailCallOpt;
2896 case CallingConv::GHC:
2897 return TailCallOpt;
2898 }
2899}
2900
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002901/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2902/// specific condition code, returning the condition code and the LHS/RHS of the
2903/// comparison to make.
2904static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2905 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00002906 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002907 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2908 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2909 // X > -1 -> X == 0, jump !sign.
2910 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002911 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002912 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2913 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002914 return X86::COND_S;
Andrew Trickf6c39412011-03-23 23:11:02 +00002915 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00002916 // X < 1 -> X <= 0
2917 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002918 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002919 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002920 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002921
Evan Chengd9558e02006-01-06 00:43:03 +00002922 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002923 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002924 case ISD::SETEQ: return X86::COND_E;
2925 case ISD::SETGT: return X86::COND_G;
2926 case ISD::SETGE: return X86::COND_GE;
2927 case ISD::SETLT: return X86::COND_L;
2928 case ISD::SETLE: return X86::COND_LE;
2929 case ISD::SETNE: return X86::COND_NE;
2930 case ISD::SETULT: return X86::COND_B;
2931 case ISD::SETUGT: return X86::COND_A;
2932 case ISD::SETULE: return X86::COND_BE;
2933 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00002934 }
Chris Lattner4c78e022008-12-23 23:42:27 +00002935 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002936
Chris Lattner4c78e022008-12-23 23:42:27 +00002937 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00002938
Chris Lattner4c78e022008-12-23 23:42:27 +00002939 // If LHS is a foldable load, but RHS is not, flip the condition.
Rafael Espindolaf297c932011-02-03 03:58:05 +00002940 if (ISD::isNON_EXTLoad(LHS.getNode()) &&
2941 !ISD::isNON_EXTLoad(RHS.getNode())) {
Chris Lattner4c78e022008-12-23 23:42:27 +00002942 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
2943 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00002944 }
2945
Chris Lattner4c78e022008-12-23 23:42:27 +00002946 switch (SetCCOpcode) {
2947 default: break;
2948 case ISD::SETOLT:
2949 case ISD::SETOLE:
2950 case ISD::SETUGT:
2951 case ISD::SETUGE:
2952 std::swap(LHS, RHS);
2953 break;
2954 }
2955
2956 // On a floating point condition, the flags are set as follows:
2957 // ZF PF CF op
2958 // 0 | 0 | 0 | X > Y
2959 // 0 | 0 | 1 | X < Y
2960 // 1 | 0 | 0 | X == Y
2961 // 1 | 1 | 1 | unordered
2962 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002963 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00002964 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002965 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00002966 case ISD::SETOLT: // flipped
2967 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002968 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00002969 case ISD::SETOLE: // flipped
2970 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002971 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002972 case ISD::SETUGT: // flipped
2973 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002974 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00002975 case ISD::SETUGE: // flipped
2976 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002977 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002978 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002979 case ISD::SETNE: return X86::COND_NE;
2980 case ISD::SETUO: return X86::COND_P;
2981 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00002982 case ISD::SETOEQ:
2983 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00002984 }
Evan Chengd9558e02006-01-06 00:43:03 +00002985}
2986
Evan Cheng4a460802006-01-11 00:33:36 +00002987/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2988/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00002989/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00002990static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00002991 switch (X86CC) {
2992 default:
2993 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00002994 case X86::COND_B:
2995 case X86::COND_BE:
2996 case X86::COND_E:
2997 case X86::COND_P:
2998 case X86::COND_A:
2999 case X86::COND_AE:
3000 case X86::COND_NE:
3001 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00003002 return true;
3003 }
3004}
3005
Evan Chengeb2f9692009-10-27 19:56:55 +00003006/// isFPImmLegal - Returns true if the target can instruction select the
3007/// specified FP immediate natively. If false, the legalizer will
3008/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00003009bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00003010 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
3011 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
3012 return true;
3013 }
3014 return false;
3015}
3016
Nate Begeman9008ca62009-04-27 18:41:29 +00003017/// isUndefOrInRange - Return true if Val is undef or if its value falls within
3018/// the specified range (L, H].
3019static bool isUndefOrInRange(int Val, int Low, int Hi) {
3020 return (Val < 0) || (Val >= Low && Val < Hi);
3021}
3022
3023/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
3024/// specified value.
3025static bool isUndefOrEqual(int Val, int CmpVal) {
3026 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003027 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00003028 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00003029}
3030
Nate Begeman9008ca62009-04-27 18:41:29 +00003031/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
3032/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
3033/// the second operand.
Owen Andersone50ed302009-08-10 22:56:29 +00003034static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00003035 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00003036 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00003037 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00003038 return (Mask[0] < 2 && Mask[1] < 2);
3039 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003040}
3041
Nate Begeman9008ca62009-04-27 18:41:29 +00003042bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003043 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003044 N->getMask(M);
3045 return ::isPSHUFDMask(M, N->getValueType(0));
3046}
Evan Cheng0188ecb2006-03-22 18:59:22 +00003047
Nate Begeman9008ca62009-04-27 18:41:29 +00003048/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
3049/// is suitable for input to PSHUFHW.
Owen Andersone50ed302009-08-10 22:56:29 +00003050static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003051 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00003052 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003053
Nate Begeman9008ca62009-04-27 18:41:29 +00003054 // Lower quadword copied in order or undef.
3055 for (int i = 0; i != 4; ++i)
3056 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00003057 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003058
Evan Cheng506d3df2006-03-29 23:07:14 +00003059 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00003060 for (int i = 4; i != 8; ++i)
3061 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00003062 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003063
Evan Cheng506d3df2006-03-29 23:07:14 +00003064 return true;
3065}
3066
Nate Begeman9008ca62009-04-27 18:41:29 +00003067bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003068 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003069 N->getMask(M);
3070 return ::isPSHUFHWMask(M, N->getValueType(0));
3071}
Evan Cheng506d3df2006-03-29 23:07:14 +00003072
Nate Begeman9008ca62009-04-27 18:41:29 +00003073/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
3074/// is suitable for input to PSHUFLW.
Owen Andersone50ed302009-08-10 22:56:29 +00003075static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003076 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00003077 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003078
Rafael Espindola15684b22009-04-24 12:40:33 +00003079 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00003080 for (int i = 4; i != 8; ++i)
3081 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00003082 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003083
Rafael Espindola15684b22009-04-24 12:40:33 +00003084 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00003085 for (int i = 0; i != 4; ++i)
3086 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00003087 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003088
Rafael Espindola15684b22009-04-24 12:40:33 +00003089 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003090}
3091
Nate Begeman9008ca62009-04-27 18:41:29 +00003092bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003093 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003094 N->getMask(M);
3095 return ::isPSHUFLWMask(M, N->getValueType(0));
3096}
3097
Nate Begemana09008b2009-10-19 02:17:23 +00003098/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
3099/// is suitable for input to PALIGNR.
3100static bool isPALIGNRMask(const SmallVectorImpl<int> &Mask, EVT VT,
3101 bool hasSSSE3) {
3102 int i, e = VT.getVectorNumElements();
Michael J. Spencerec38de22010-10-10 22:04:20 +00003103
Nate Begemana09008b2009-10-19 02:17:23 +00003104 // Do not handle v2i64 / v2f64 shuffles with palignr.
3105 if (e < 4 || !hasSSSE3)
3106 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003107
Nate Begemana09008b2009-10-19 02:17:23 +00003108 for (i = 0; i != e; ++i)
3109 if (Mask[i] >= 0)
3110 break;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003111
Nate Begemana09008b2009-10-19 02:17:23 +00003112 // All undef, not a palignr.
3113 if (i == e)
3114 return false;
3115
3116 // Determine if it's ok to perform a palignr with only the LHS, since we
3117 // don't have access to the actual shuffle elements to see if RHS is undef.
3118 bool Unary = Mask[i] < (int)e;
3119 bool NeedsUnary = false;
3120
3121 int s = Mask[i] - i;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003122
Nate Begemana09008b2009-10-19 02:17:23 +00003123 // Check the rest of the elements to see if they are consecutive.
3124 for (++i; i != e; ++i) {
3125 int m = Mask[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00003126 if (m < 0)
Nate Begemana09008b2009-10-19 02:17:23 +00003127 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003128
Nate Begemana09008b2009-10-19 02:17:23 +00003129 Unary = Unary && (m < (int)e);
3130 NeedsUnary = NeedsUnary || (m < s);
3131
3132 if (NeedsUnary && !Unary)
3133 return false;
3134 if (Unary && m != ((s+i) & (e-1)))
3135 return false;
3136 if (!Unary && m != (s+i))
3137 return false;
3138 }
3139 return true;
3140}
3141
3142bool X86::isPALIGNRMask(ShuffleVectorSDNode *N) {
3143 SmallVector<int, 8> M;
3144 N->getMask(M);
3145 return ::isPALIGNRMask(M, N->getValueType(0), true);
3146}
3147
Evan Cheng14aed5e2006-03-24 01:18:28 +00003148/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
3149/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Owen Andersone50ed302009-08-10 22:56:29 +00003150static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003151 int NumElems = VT.getVectorNumElements();
3152 if (NumElems != 2 && NumElems != 4)
3153 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003154
Nate Begeman9008ca62009-04-27 18:41:29 +00003155 int Half = NumElems / 2;
3156 for (int i = 0; i < Half; ++i)
3157 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003158 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003159 for (int i = Half; i < NumElems; ++i)
3160 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00003161 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003162
Evan Cheng14aed5e2006-03-24 01:18:28 +00003163 return true;
3164}
3165
Nate Begeman9008ca62009-04-27 18:41:29 +00003166bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
3167 SmallVector<int, 8> M;
3168 N->getMask(M);
3169 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003170}
3171
Evan Cheng213d2cf2007-05-17 18:45:50 +00003172/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00003173/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
3174/// half elements to come from vector 1 (which would equal the dest.) and
3175/// the upper half to come from vector 2.
Owen Andersone50ed302009-08-10 22:56:29 +00003176static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003177 int NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003178
3179 if (NumElems != 2 && NumElems != 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00003180 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003181
Nate Begeman9008ca62009-04-27 18:41:29 +00003182 int Half = NumElems / 2;
3183 for (int i = 0; i < Half; ++i)
3184 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00003185 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003186 for (int i = Half; i < NumElems; ++i)
3187 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003188 return false;
3189 return true;
3190}
3191
Nate Begeman9008ca62009-04-27 18:41:29 +00003192static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
3193 SmallVector<int, 8> M;
3194 N->getMask(M);
3195 return isCommutedSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003196}
3197
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003198/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
3199/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00003200bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
3201 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003202 return false;
3203
Evan Cheng2064a2b2006-03-28 06:50:32 +00003204 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00003205 return isUndefOrEqual(N->getMaskElt(0), 6) &&
3206 isUndefOrEqual(N->getMaskElt(1), 7) &&
3207 isUndefOrEqual(N->getMaskElt(2), 2) &&
3208 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003209}
3210
Nate Begeman0b10b912009-11-07 23:17:15 +00003211/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3212/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3213/// <2, 3, 2, 3>
3214bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
3215 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Michael J. Spencerec38de22010-10-10 22:04:20 +00003216
Nate Begeman0b10b912009-11-07 23:17:15 +00003217 if (NumElems != 4)
3218 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003219
Nate Begeman0b10b912009-11-07 23:17:15 +00003220 return isUndefOrEqual(N->getMaskElt(0), 2) &&
3221 isUndefOrEqual(N->getMaskElt(1), 3) &&
3222 isUndefOrEqual(N->getMaskElt(2), 2) &&
3223 isUndefOrEqual(N->getMaskElt(3), 3);
3224}
3225
Evan Cheng5ced1d82006-04-06 23:23:56 +00003226/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3227/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00003228bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
3229 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003230
Evan Cheng5ced1d82006-04-06 23:23:56 +00003231 if (NumElems != 2 && NumElems != 4)
3232 return false;
3233
Evan Chengc5cdff22006-04-07 21:53:05 +00003234 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003235 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003236 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003237
Evan Chengc5cdff22006-04-07 21:53:05 +00003238 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003239 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003240 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003241
3242 return true;
3243}
3244
Nate Begeman0b10b912009-11-07 23:17:15 +00003245/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3246/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
3247bool X86::isMOVLHPSMask(ShuffleVectorSDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003248 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003249
David Greenea20244d2011-03-02 17:23:43 +00003250 if ((NumElems != 2 && NumElems != 4)
3251 || N->getValueType(0).getSizeInBits() > 128)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003252 return false;
3253
Evan Chengc5cdff22006-04-07 21:53:05 +00003254 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003255 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003256 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003257
Nate Begeman9008ca62009-04-27 18:41:29 +00003258 for (unsigned i = 0; i < NumElems/2; ++i)
3259 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003260 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003261
3262 return true;
3263}
3264
Evan Cheng0038e592006-03-28 00:39:58 +00003265/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3266/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Owen Andersone50ed302009-08-10 22:56:29 +00003267static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003268 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003269 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003270 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng0038e592006-03-28 00:39:58 +00003271 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003272
David Greenea20244d2011-03-02 17:23:43 +00003273 // Handle vector lengths > 128 bits. Define a "section" as a set of
3274 // 128 bits. AVX defines UNPCK* to operate independently on 128-bit
3275 // sections.
3276 unsigned NumSections = VT.getSizeInBits() / 128;
3277 if (NumSections == 0 ) NumSections = 1; // Handle MMX
3278 unsigned NumSectionElts = NumElts / NumSections;
3279
3280 unsigned Start = 0;
3281 unsigned End = NumSectionElts;
3282 for (unsigned s = 0; s < NumSections; ++s) {
3283 for (unsigned i = Start, j = s * NumSectionElts;
3284 i != End;
3285 i += 2, ++j) {
3286 int BitI = Mask[i];
3287 int BitI1 = Mask[i+1];
3288 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003289 return false;
David Greenea20244d2011-03-02 17:23:43 +00003290 if (V2IsSplat) {
3291 if (!isUndefOrEqual(BitI1, NumElts))
3292 return false;
3293 } else {
3294 if (!isUndefOrEqual(BitI1, j + NumElts))
3295 return false;
3296 }
Evan Cheng39623da2006-04-20 08:58:49 +00003297 }
David Greenea20244d2011-03-02 17:23:43 +00003298 // Process the next 128 bits.
3299 Start += NumSectionElts;
3300 End += NumSectionElts;
Evan Cheng0038e592006-03-28 00:39:58 +00003301 }
David Greenea20244d2011-03-02 17:23:43 +00003302
Evan Cheng0038e592006-03-28 00:39:58 +00003303 return true;
3304}
3305
Nate Begeman9008ca62009-04-27 18:41:29 +00003306bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3307 SmallVector<int, 8> M;
3308 N->getMask(M);
3309 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003310}
3311
Evan Cheng4fcb9222006-03-28 02:43:26 +00003312/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3313/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Eric Christopherfd179292009-08-27 18:07:15 +00003314static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003315 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003316 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003317 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng4fcb9222006-03-28 02:43:26 +00003318 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003319
Nate Begeman9008ca62009-04-27 18:41:29 +00003320 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
3321 int BitI = Mask[i];
3322 int BitI1 = Mask[i+1];
Chris Lattner5a88b832007-02-25 07:10:00 +00003323 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengc5cdff22006-04-07 21:53:05 +00003324 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00003325 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00003326 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003327 return false;
3328 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00003329 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003330 return false;
3331 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003332 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003333 return true;
3334}
3335
Nate Begeman9008ca62009-04-27 18:41:29 +00003336bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3337 SmallVector<int, 8> M;
3338 N->getMask(M);
3339 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003340}
3341
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003342/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3343/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3344/// <0, 0, 1, 1>
Owen Andersone50ed302009-08-10 22:56:29 +00003345static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003346 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003347 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003348 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003349
David Greenea20244d2011-03-02 17:23:43 +00003350 // Handle vector lengths > 128 bits. Define a "section" as a set of
3351 // 128 bits. AVX defines UNPCK* to operate independently on 128-bit
3352 // sections.
3353 unsigned NumSections = VT.getSizeInBits() / 128;
3354 if (NumSections == 0 ) NumSections = 1; // Handle MMX
3355 unsigned NumSectionElts = NumElems / NumSections;
3356
3357 for (unsigned s = 0; s < NumSections; ++s) {
3358 for (unsigned i = s * NumSectionElts, j = s * NumSectionElts;
3359 i != NumSectionElts * (s + 1);
3360 i += 2, ++j) {
3361 int BitI = Mask[i];
3362 int BitI1 = Mask[i+1];
3363
3364 if (!isUndefOrEqual(BitI, j))
3365 return false;
3366 if (!isUndefOrEqual(BitI1, j))
3367 return false;
3368 }
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003369 }
David Greenea20244d2011-03-02 17:23:43 +00003370
Rafael Espindola15684b22009-04-24 12:40:33 +00003371 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003372}
3373
Nate Begeman9008ca62009-04-27 18:41:29 +00003374bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
3375 SmallVector<int, 8> M;
3376 N->getMask(M);
3377 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
3378}
3379
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003380/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3381/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3382/// <2, 2, 3, 3>
Owen Andersone50ed302009-08-10 22:56:29 +00003383static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003384 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003385 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
3386 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003387
Nate Begeman9008ca62009-04-27 18:41:29 +00003388 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
3389 int BitI = Mask[i];
3390 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003391 if (!isUndefOrEqual(BitI, j))
3392 return false;
3393 if (!isUndefOrEqual(BitI1, j))
3394 return false;
3395 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003396 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003397}
3398
Nate Begeman9008ca62009-04-27 18:41:29 +00003399bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
3400 SmallVector<int, 8> M;
3401 N->getMask(M);
3402 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
3403}
3404
Evan Cheng017dcc62006-04-21 01:05:10 +00003405/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3406/// specifies a shuffle of elements that is suitable for input to MOVSS,
3407/// MOVSD, and MOVD, i.e. setting the lowest element.
Owen Andersone50ed302009-08-10 22:56:29 +00003408static bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003409 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003410 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003411
3412 int NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003413
Nate Begeman9008ca62009-04-27 18:41:29 +00003414 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003415 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003416
Nate Begeman9008ca62009-04-27 18:41:29 +00003417 for (int i = 1; i < NumElts; ++i)
3418 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003419 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003420
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003421 return true;
3422}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003423
Nate Begeman9008ca62009-04-27 18:41:29 +00003424bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
3425 SmallVector<int, 8> M;
3426 N->getMask(M);
3427 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003428}
3429
Evan Cheng017dcc62006-04-21 01:05:10 +00003430/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
3431/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003432/// element of vector 2 and the other elements to come from vector 1 in order.
Owen Andersone50ed302009-08-10 22:56:29 +00003433static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003434 bool V2IsSplat = false, bool V2IsUndef = false) {
3435 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003436 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003437 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003438
Nate Begeman9008ca62009-04-27 18:41:29 +00003439 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003440 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003441
Nate Begeman9008ca62009-04-27 18:41:29 +00003442 for (int i = 1; i < NumOps; ++i)
3443 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3444 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3445 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003446 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003447
Evan Cheng39623da2006-04-20 08:58:49 +00003448 return true;
3449}
3450
Nate Begeman9008ca62009-04-27 18:41:29 +00003451static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00003452 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003453 SmallVector<int, 8> M;
3454 N->getMask(M);
3455 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00003456}
3457
Evan Chengd9539472006-04-14 21:59:03 +00003458/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3459/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003460bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N) {
3461 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003462 return false;
3463
3464 // Expect 1, 1, 3, 3
Rafael Espindola15684b22009-04-24 12:40:33 +00003465 for (unsigned i = 0; i < 2; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003466 int Elt = N->getMaskElt(i);
3467 if (Elt >= 0 && Elt != 1)
3468 return false;
Rafael Espindola15684b22009-04-24 12:40:33 +00003469 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003470
3471 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003472 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003473 int Elt = N->getMaskElt(i);
3474 if (Elt >= 0 && Elt != 3)
3475 return false;
3476 if (Elt == 3)
3477 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003478 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003479 // Don't use movshdup if it can be done with a shufps.
Nate Begeman9008ca62009-04-27 18:41:29 +00003480 // FIXME: verify that matching u, u, 3, 3 is what we want.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003481 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003482}
3483
3484/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3485/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003486bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N) {
3487 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003488 return false;
3489
3490 // Expect 0, 0, 2, 2
Nate Begeman9008ca62009-04-27 18:41:29 +00003491 for (unsigned i = 0; i < 2; ++i)
3492 if (N->getMaskElt(i) > 0)
3493 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003494
3495 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003496 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003497 int Elt = N->getMaskElt(i);
3498 if (Elt >= 0 && Elt != 2)
3499 return false;
3500 if (Elt == 2)
3501 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003502 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003503 // Don't use movsldup if it can be done with a shufps.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003504 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003505}
3506
Evan Cheng0b457f02008-09-25 20:50:48 +00003507/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3508/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003509bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
3510 int e = N->getValueType(0).getVectorNumElements() / 2;
Eric Christopherfd179292009-08-27 18:07:15 +00003511
Nate Begeman9008ca62009-04-27 18:41:29 +00003512 for (int i = 0; i < e; ++i)
3513 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003514 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003515 for (int i = 0; i < e; ++i)
3516 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003517 return false;
3518 return true;
3519}
3520
David Greenec38a03e2011-02-03 15:50:00 +00003521/// isVEXTRACTF128Index - Return true if the specified
3522/// EXTRACT_SUBVECTOR operand specifies a vector extract that is
3523/// suitable for input to VEXTRACTF128.
3524bool X86::isVEXTRACTF128Index(SDNode *N) {
3525 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3526 return false;
3527
3528 // The index should be aligned on a 128-bit boundary.
3529 uint64_t Index =
3530 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
3531
3532 unsigned VL = N->getValueType(0).getVectorNumElements();
3533 unsigned VBits = N->getValueType(0).getSizeInBits();
3534 unsigned ElSize = VBits / VL;
3535 bool Result = (Index * ElSize) % 128 == 0;
3536
3537 return Result;
3538}
3539
David Greeneccacdc12011-02-04 16:08:29 +00003540/// isVINSERTF128Index - Return true if the specified INSERT_SUBVECTOR
3541/// operand specifies a subvector insert that is suitable for input to
3542/// VINSERTF128.
3543bool X86::isVINSERTF128Index(SDNode *N) {
3544 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
3545 return false;
3546
3547 // The index should be aligned on a 128-bit boundary.
3548 uint64_t Index =
3549 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
3550
3551 unsigned VL = N->getValueType(0).getVectorNumElements();
3552 unsigned VBits = N->getValueType(0).getSizeInBits();
3553 unsigned ElSize = VBits / VL;
3554 bool Result = (Index * ElSize) % 128 == 0;
3555
3556 return Result;
3557}
3558
Evan Cheng63d33002006-03-22 08:01:21 +00003559/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003560/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Evan Cheng63d33002006-03-22 08:01:21 +00003561unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003562 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3563 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
3564
Evan Chengb9df0ca2006-03-22 02:53:00 +00003565 unsigned Shift = (NumOperands == 4) ? 2 : 1;
3566 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00003567 for (int i = 0; i < NumOperands; ++i) {
3568 int Val = SVOp->getMaskElt(NumOperands-i-1);
3569 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00003570 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00003571 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00003572 if (i != NumOperands - 1)
3573 Mask <<= Shift;
3574 }
Evan Cheng63d33002006-03-22 08:01:21 +00003575 return Mask;
3576}
3577
Evan Cheng506d3df2006-03-29 23:07:14 +00003578/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003579/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003580unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003581 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003582 unsigned Mask = 0;
3583 // 8 nodes, but we only care about the last 4.
3584 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003585 int Val = SVOp->getMaskElt(i);
3586 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00003587 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00003588 if (i != 4)
3589 Mask <<= 2;
3590 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003591 return Mask;
3592}
3593
3594/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003595/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003596unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003597 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003598 unsigned Mask = 0;
3599 // 8 nodes, but we only care about the first 4.
3600 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003601 int Val = SVOp->getMaskElt(i);
3602 if (Val >= 0)
3603 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00003604 if (i != 0)
3605 Mask <<= 2;
3606 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003607 return Mask;
3608}
3609
Nate Begemana09008b2009-10-19 02:17:23 +00003610/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
3611/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
3612unsigned X86::getShufflePALIGNRImmediate(SDNode *N) {
3613 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3614 EVT VVT = N->getValueType(0);
3615 unsigned EltSize = VVT.getVectorElementType().getSizeInBits() >> 3;
3616 int Val = 0;
3617
3618 unsigned i, e;
3619 for (i = 0, e = VVT.getVectorNumElements(); i != e; ++i) {
3620 Val = SVOp->getMaskElt(i);
3621 if (Val >= 0)
3622 break;
3623 }
3624 return (Val - i) * EltSize;
3625}
3626
David Greenec38a03e2011-02-03 15:50:00 +00003627/// getExtractVEXTRACTF128Immediate - Return the appropriate immediate
3628/// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF128
3629/// instructions.
3630unsigned X86::getExtractVEXTRACTF128Immediate(SDNode *N) {
3631 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3632 llvm_unreachable("Illegal extract subvector for VEXTRACTF128");
3633
3634 uint64_t Index =
3635 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
3636
3637 EVT VecVT = N->getOperand(0).getValueType();
3638 EVT ElVT = VecVT.getVectorElementType();
3639
3640 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
3641
3642 return Index / NumElemsPerChunk;
3643}
3644
David Greeneccacdc12011-02-04 16:08:29 +00003645/// getInsertVINSERTF128Immediate - Return the appropriate immediate
3646/// to insert at the specified INSERT_SUBVECTOR index with VINSERTF128
3647/// instructions.
3648unsigned X86::getInsertVINSERTF128Immediate(SDNode *N) {
3649 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
3650 llvm_unreachable("Illegal insert subvector for VINSERTF128");
3651
3652 uint64_t Index =
NAKAMURA Takumi27635382011-02-05 15:10:54 +00003653 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
David Greeneccacdc12011-02-04 16:08:29 +00003654
3655 EVT VecVT = N->getValueType(0);
3656 EVT ElVT = VecVT.getVectorElementType();
3657
3658 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
3659
3660 return Index / NumElemsPerChunk;
3661}
3662
Evan Cheng37b73872009-07-30 08:33:02 +00003663/// isZeroNode - Returns true if Elt is a constant zero or a floating point
3664/// constant +0.0.
3665bool X86::isZeroNode(SDValue Elt) {
3666 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00003667 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00003668 (isa<ConstantFPSDNode>(Elt) &&
3669 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
3670}
3671
Nate Begeman9008ca62009-04-27 18:41:29 +00003672/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
3673/// their permute mask.
3674static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
3675 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003676 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003677 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00003678 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00003679
Nate Begeman5a5ca152009-04-29 05:20:52 +00003680 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003681 int idx = SVOp->getMaskElt(i);
3682 if (idx < 0)
3683 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003684 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003685 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003686 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003687 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003688 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003689 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
3690 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003691}
3692
Evan Cheng779ccea2007-12-07 21:30:01 +00003693/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3694/// the two vector operands have swapped position.
Owen Andersone50ed302009-08-10 22:56:29 +00003695static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00003696 unsigned NumElems = VT.getVectorNumElements();
3697 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003698 int idx = Mask[i];
3699 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003700 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003701 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003702 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003703 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003704 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003705 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003706}
3707
Evan Cheng533a0aa2006-04-19 20:35:22 +00003708/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
3709/// match movhlps. The lower half elements should come from upper half of
3710/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003711/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00003712static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
3713 if (Op->getValueType(0).getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00003714 return false;
3715 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003716 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003717 return false;
3718 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003719 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003720 return false;
3721 return true;
3722}
3723
Evan Cheng5ced1d82006-04-06 23:23:56 +00003724/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00003725/// is promoted to a vector. It also returns the LoadSDNode by reference if
3726/// required.
3727static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00003728 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
3729 return false;
3730 N = N->getOperand(0).getNode();
3731 if (!ISD::isNON_EXTLoad(N))
3732 return false;
3733 if (LD)
3734 *LD = cast<LoadSDNode>(N);
3735 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003736}
3737
Evan Cheng533a0aa2006-04-19 20:35:22 +00003738/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
3739/// match movlp{s|d}. The lower half elements should come from lower half of
3740/// V1 (and in order), and the upper half elements should come from the upper
3741/// half of V2 (and in order). And since V1 will become the source of the
3742/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003743static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
3744 ShuffleVectorSDNode *Op) {
Evan Cheng466685d2006-10-09 20:57:25 +00003745 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003746 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00003747 // Is V2 is a vector load, don't do this transformation. We will try to use
3748 // load folding shufps op.
3749 if (ISD::isNON_EXTLoad(V2))
3750 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003751
Nate Begeman5a5ca152009-04-29 05:20:52 +00003752 unsigned NumElems = Op->getValueType(0).getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003753
Evan Cheng533a0aa2006-04-19 20:35:22 +00003754 if (NumElems != 2 && NumElems != 4)
3755 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003756 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003757 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003758 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003759 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003760 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003761 return false;
3762 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003763}
3764
Evan Cheng39623da2006-04-20 08:58:49 +00003765/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
3766/// all the same.
3767static bool isSplatVector(SDNode *N) {
3768 if (N->getOpcode() != ISD::BUILD_VECTOR)
3769 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003770
Dan Gohman475871a2008-07-27 21:46:04 +00003771 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00003772 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
3773 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003774 return false;
3775 return true;
3776}
3777
Evan Cheng213d2cf2007-05-17 18:45:50 +00003778/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00003779/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00003780/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00003781static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00003782 SDValue V1 = N->getOperand(0);
3783 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003784 unsigned NumElems = N->getValueType(0).getVectorNumElements();
3785 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003786 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003787 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003788 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00003789 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
3790 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003791 if (Opc != ISD::BUILD_VECTOR ||
3792 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00003793 return false;
3794 } else if (Idx >= 0) {
3795 unsigned Opc = V1.getOpcode();
3796 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
3797 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003798 if (Opc != ISD::BUILD_VECTOR ||
3799 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00003800 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00003801 }
3802 }
3803 return true;
3804}
3805
3806/// getZeroVector - Returns a vector of specified type with all zero elements.
3807///
Owen Andersone50ed302009-08-10 22:56:29 +00003808static SDValue getZeroVector(EVT VT, bool HasSSE2, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00003809 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003810 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003811
Dale Johannesen0488fb62010-09-30 23:57:10 +00003812 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003813 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00003814 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00003815 if (VT.getSizeInBits() == 128) { // SSE
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003816 if (HasSSE2) { // SSE2
3817 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3818 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
3819 } else { // SSE1
3820 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
3821 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
3822 }
3823 } else if (VT.getSizeInBits() == 256) { // AVX
3824 // 256-bit logic and arithmetic instructions in AVX are
3825 // all floating-point, no support for integer ops. Default
3826 // to emitting fp zeroed vectors then.
Owen Anderson825b72b2009-08-11 20:47:22 +00003827 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003828 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
3829 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
Evan Chengf0df0312008-05-15 08:39:06 +00003830 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003831 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00003832}
3833
Chris Lattner8a594482007-11-25 00:24:49 +00003834/// getOnesVector - Returns a vector of specified type with all bits set.
3835///
Owen Andersone50ed302009-08-10 22:56:29 +00003836static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003837 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003838
Chris Lattner8a594482007-11-25 00:24:49 +00003839 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
3840 // type. This ensures they get CSE'd.
Owen Anderson825b72b2009-08-11 20:47:22 +00003841 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00003842 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00003843 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003844 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00003845}
3846
3847
Evan Cheng39623da2006-04-20 08:58:49 +00003848/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
3849/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00003850static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003851 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003852 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003853
Evan Cheng39623da2006-04-20 08:58:49 +00003854 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003855 SmallVector<int, 8> MaskVec;
3856 SVOp->getMask(MaskVec);
Eric Christopherfd179292009-08-27 18:07:15 +00003857
Nate Begeman5a5ca152009-04-29 05:20:52 +00003858 for (unsigned i = 0; i != NumElems; ++i) {
3859 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003860 MaskVec[i] = NumElems;
3861 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00003862 }
Evan Cheng39623da2006-04-20 08:58:49 +00003863 }
Evan Cheng39623da2006-04-20 08:58:49 +00003864 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00003865 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
3866 SVOp->getOperand(1), &MaskVec[0]);
3867 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00003868}
3869
Evan Cheng017dcc62006-04-21 01:05:10 +00003870/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
3871/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00003872static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003873 SDValue V2) {
3874 unsigned NumElems = VT.getVectorNumElements();
3875 SmallVector<int, 8> Mask;
3876 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00003877 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003878 Mask.push_back(i);
3879 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00003880}
3881
Nate Begeman9008ca62009-04-27 18:41:29 +00003882/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003883static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003884 SDValue V2) {
3885 unsigned NumElems = VT.getVectorNumElements();
3886 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00003887 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003888 Mask.push_back(i);
3889 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00003890 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003891 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00003892}
3893
Nate Begeman9008ca62009-04-27 18:41:29 +00003894/// getUnpackhMask - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003895static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003896 SDValue V2) {
3897 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00003898 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00003899 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00003900 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003901 Mask.push_back(i + Half);
3902 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00003903 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003904 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003905}
3906
Bruno Cardoso Lopesbb0a9482010-08-13 17:50:47 +00003907/// PromoteSplat - Promote a splat of v4i32, v8i16 or v16i8 to v4f32.
3908static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003909 EVT PVT = MVT::v4f32;
Owen Andersone50ed302009-08-10 22:56:29 +00003910 EVT VT = SV->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00003911 DebugLoc dl = SV->getDebugLoc();
3912 SDValue V1 = SV->getOperand(0);
3913 int NumElems = VT.getVectorNumElements();
3914 int EltNo = SV->getSplatIndex();
Rafael Espindola15684b22009-04-24 12:40:33 +00003915
Nate Begeman9008ca62009-04-27 18:41:29 +00003916 // unpack elements to the correct location
3917 while (NumElems > 4) {
3918 if (EltNo < NumElems/2) {
3919 V1 = getUnpackl(DAG, dl, VT, V1, V1);
3920 } else {
3921 V1 = getUnpackh(DAG, dl, VT, V1, V1);
3922 EltNo -= NumElems/2;
3923 }
3924 NumElems >>= 1;
3925 }
Eric Christopherfd179292009-08-27 18:07:15 +00003926
Nate Begeman9008ca62009-04-27 18:41:29 +00003927 // Perform the splat.
3928 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003929 V1 = DAG.getNode(ISD::BITCAST, dl, PVT, V1);
Nate Begeman9008ca62009-04-27 18:41:29 +00003930 V1 = DAG.getVectorShuffle(PVT, dl, V1, DAG.getUNDEF(PVT), &SplatMask[0]);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003931 return DAG.getNode(ISD::BITCAST, dl, VT, V1);
Evan Chengc575ca22006-04-17 20:43:08 +00003932}
3933
Evan Chengba05f722006-04-21 23:03:30 +00003934/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00003935/// vector of zero or undef vector. This produces a shuffle where the low
3936/// element of V2 is swizzled into the zero/undef vector, landing at element
3937/// 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 +00003938static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Chengf0df0312008-05-15 08:39:06 +00003939 bool isZero, bool HasSSE2,
3940 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003941 EVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00003942 SDValue V1 = isZero
Nate Begeman9008ca62009-04-27 18:41:29 +00003943 ? getZeroVector(VT, HasSSE2, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
3944 unsigned NumElems = VT.getVectorNumElements();
3945 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00003946 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003947 // If this is the insertion idx, put the low elt of V2 here.
3948 MaskVec.push_back(i == Idx ? NumElems : i);
3949 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00003950}
3951
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003952/// getShuffleScalarElt - Returns the scalar element that will make up the ith
3953/// element of the result of the vector shuffle.
Benjamin Kramer050db522011-03-26 12:38:19 +00003954static SDValue getShuffleScalarElt(SDNode *N, int Index, SelectionDAG &DAG,
3955 unsigned Depth) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003956 if (Depth == 6)
3957 return SDValue(); // Limit search depth.
3958
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003959 SDValue V = SDValue(N, 0);
3960 EVT VT = V.getValueType();
3961 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003962
3963 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
3964 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
3965 Index = SV->getMaskElt(Index);
3966
3967 if (Index < 0)
3968 return DAG.getUNDEF(VT.getVectorElementType());
3969
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003970 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003971 SDValue NewV = (Index < NumElems) ? SV->getOperand(0) : SV->getOperand(1);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003972 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00003973 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003974
3975 // Recurse into target specific vector shuffles to find scalars.
3976 if (isTargetShuffle(Opcode)) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003977 int NumElems = VT.getVectorNumElements();
3978 SmallVector<unsigned, 16> ShuffleMask;
3979 SDValue ImmN;
3980
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003981 switch(Opcode) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003982 case X86ISD::SHUFPS:
3983 case X86ISD::SHUFPD:
3984 ImmN = N->getOperand(N->getNumOperands()-1);
3985 DecodeSHUFPSMask(NumElems,
3986 cast<ConstantSDNode>(ImmN)->getZExtValue(),
3987 ShuffleMask);
3988 break;
3989 case X86ISD::PUNPCKHBW:
3990 case X86ISD::PUNPCKHWD:
3991 case X86ISD::PUNPCKHDQ:
3992 case X86ISD::PUNPCKHQDQ:
3993 DecodePUNPCKHMask(NumElems, ShuffleMask);
3994 break;
3995 case X86ISD::UNPCKHPS:
3996 case X86ISD::UNPCKHPD:
3997 DecodeUNPCKHPMask(NumElems, ShuffleMask);
3998 break;
3999 case X86ISD::PUNPCKLBW:
4000 case X86ISD::PUNPCKLWD:
4001 case X86ISD::PUNPCKLDQ:
4002 case X86ISD::PUNPCKLQDQ:
David Greenec4db4e52011-02-28 19:06:56 +00004003 DecodePUNPCKLMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004004 break;
4005 case X86ISD::UNPCKLPS:
4006 case X86ISD::UNPCKLPD:
David Greenec4db4e52011-02-28 19:06:56 +00004007 case X86ISD::VUNPCKLPS:
4008 case X86ISD::VUNPCKLPD:
4009 case X86ISD::VUNPCKLPSY:
4010 case X86ISD::VUNPCKLPDY:
4011 DecodeUNPCKLPMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004012 break;
4013 case X86ISD::MOVHLPS:
4014 DecodeMOVHLPSMask(NumElems, ShuffleMask);
4015 break;
4016 case X86ISD::MOVLHPS:
4017 DecodeMOVLHPSMask(NumElems, ShuffleMask);
4018 break;
4019 case X86ISD::PSHUFD:
4020 ImmN = N->getOperand(N->getNumOperands()-1);
4021 DecodePSHUFMask(NumElems,
4022 cast<ConstantSDNode>(ImmN)->getZExtValue(),
4023 ShuffleMask);
4024 break;
4025 case X86ISD::PSHUFHW:
4026 ImmN = N->getOperand(N->getNumOperands()-1);
4027 DecodePSHUFHWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
4028 ShuffleMask);
4029 break;
4030 case X86ISD::PSHUFLW:
4031 ImmN = N->getOperand(N->getNumOperands()-1);
4032 DecodePSHUFLWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
4033 ShuffleMask);
4034 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004035 case X86ISD::MOVSS:
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00004036 case X86ISD::MOVSD: {
4037 // The index 0 always comes from the first element of the second source,
4038 // this is why MOVSS and MOVSD are used in the first place. The other
4039 // elements come from the other positions of the first source vector.
4040 unsigned OpNum = (Index == 0) ? 1 : 0;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004041 return getShuffleScalarElt(V.getOperand(OpNum).getNode(), Index, DAG,
4042 Depth+1);
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00004043 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004044 default:
4045 assert("not implemented for target shuffle node");
4046 return SDValue();
4047 }
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004048
4049 Index = ShuffleMask[Index];
4050 if (Index < 0)
4051 return DAG.getUNDEF(VT.getVectorElementType());
4052
4053 SDValue NewV = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
4054 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG,
4055 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004056 }
4057
4058 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004059 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004060 V = V.getOperand(0);
4061 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004062 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004063
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004064 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004065 return SDValue();
4066 }
4067
4068 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
4069 return (Index == 0) ? V.getOperand(0)
4070 : DAG.getUNDEF(VT.getVectorElementType());
4071
4072 if (V.getOpcode() == ISD::BUILD_VECTOR)
4073 return V.getOperand(Index);
4074
4075 return SDValue();
4076}
4077
4078/// getNumOfConsecutiveZeros - Return the number of elements of a vector
4079/// shuffle operation which come from a consecutively from a zero. The
Chris Lattner7a2bdde2011-04-15 05:18:47 +00004080/// search can start in two different directions, from left or right.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004081static
4082unsigned getNumOfConsecutiveZeros(SDNode *N, int NumElems,
4083 bool ZerosFromLeft, SelectionDAG &DAG) {
4084 int i = 0;
4085
4086 while (i < NumElems) {
4087 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004088 SDValue Elt = getShuffleScalarElt(N, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004089 if (!(Elt.getNode() &&
4090 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
4091 break;
4092 ++i;
4093 }
4094
4095 return i;
4096}
4097
4098/// isShuffleMaskConsecutive - Check if the shuffle mask indicies from MaskI to
4099/// MaskE correspond consecutively to elements from one of the vector operands,
4100/// starting from its index OpIdx. Also tell OpNum which source vector operand.
4101static
4102bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp, int MaskI, int MaskE,
4103 int OpIdx, int NumElems, unsigned &OpNum) {
4104 bool SeenV1 = false;
4105 bool SeenV2 = false;
4106
4107 for (int i = MaskI; i <= MaskE; ++i, ++OpIdx) {
4108 int Idx = SVOp->getMaskElt(i);
4109 // Ignore undef indicies
4110 if (Idx < 0)
4111 continue;
4112
4113 if (Idx < NumElems)
4114 SeenV1 = true;
4115 else
4116 SeenV2 = true;
4117
4118 // Only accept consecutive elements from the same vector
4119 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
4120 return false;
4121 }
4122
4123 OpNum = SeenV1 ? 0 : 1;
4124 return true;
4125}
4126
4127/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
4128/// logical left shift of a vector.
4129static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4130 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4131 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4132 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4133 false /* check zeros from right */, DAG);
4134 unsigned OpSrc;
4135
4136 if (!NumZeros)
4137 return false;
4138
4139 // Considering the elements in the mask that are not consecutive zeros,
4140 // check if they consecutively come from only one of the source vectors.
4141 //
4142 // V1 = {X, A, B, C} 0
4143 // \ \ \ /
4144 // vector_shuffle V1, V2 <1, 2, 3, X>
4145 //
4146 if (!isShuffleMaskConsecutive(SVOp,
4147 0, // Mask Start Index
4148 NumElems-NumZeros-1, // Mask End Index
4149 NumZeros, // Where to start looking in the src vector
4150 NumElems, // Number of elements in vector
4151 OpSrc)) // Which source operand ?
4152 return false;
4153
4154 isLeft = false;
4155 ShAmt = NumZeros;
4156 ShVal = SVOp->getOperand(OpSrc);
4157 return true;
4158}
4159
4160/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
4161/// logical left shift of a vector.
4162static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4163 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4164 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4165 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4166 true /* check zeros from left */, DAG);
4167 unsigned OpSrc;
4168
4169 if (!NumZeros)
4170 return false;
4171
4172 // Considering the elements in the mask that are not consecutive zeros,
4173 // check if they consecutively come from only one of the source vectors.
4174 //
4175 // 0 { A, B, X, X } = V2
4176 // / \ / /
4177 // vector_shuffle V1, V2 <X, X, 4, 5>
4178 //
4179 if (!isShuffleMaskConsecutive(SVOp,
4180 NumZeros, // Mask Start Index
4181 NumElems-1, // Mask End Index
4182 0, // Where to start looking in the src vector
4183 NumElems, // Number of elements in vector
4184 OpSrc)) // Which source operand ?
4185 return false;
4186
4187 isLeft = true;
4188 ShAmt = NumZeros;
4189 ShVal = SVOp->getOperand(OpSrc);
4190 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004191}
4192
4193/// isVectorShift - Returns true if the shuffle can be implemented as a
4194/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004195static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00004196 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004197 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
4198 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
4199 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004200
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004201 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00004202}
4203
Evan Chengc78d3b42006-04-24 18:01:45 +00004204/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
4205///
Dan Gohman475871a2008-07-27 21:46:04 +00004206static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00004207 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00004208 SelectionDAG &DAG,
4209 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004210 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00004211 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004212
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004213 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004214 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004215 bool First = true;
4216 for (unsigned i = 0; i < 16; ++i) {
4217 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
4218 if (ThisIsNonZero && First) {
4219 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004220 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004221 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004222 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004223 First = false;
4224 }
4225
4226 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00004227 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004228 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
4229 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004230 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004231 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00004232 }
4233 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004234 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
4235 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
4236 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00004237 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004238 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00004239 } else
4240 ThisElt = LastElt;
4241
Gabor Greifba36cb52008-08-28 21:40:38 +00004242 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00004243 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00004244 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00004245 }
4246 }
4247
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004248 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00004249}
4250
Bill Wendlinga348c562007-03-22 18:42:45 +00004251/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00004252///
Dan Gohman475871a2008-07-27 21:46:04 +00004253static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00004254 unsigned NumNonZero, unsigned NumZero,
4255 SelectionDAG &DAG,
4256 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004257 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00004258 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004259
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004260 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004261 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004262 bool First = true;
4263 for (unsigned i = 0; i < 8; ++i) {
4264 bool isNonZero = (NonZeros & (1 << i)) != 0;
4265 if (isNonZero) {
4266 if (First) {
4267 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004268 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004269 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004270 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004271 First = false;
4272 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004273 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004274 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00004275 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00004276 }
4277 }
4278
4279 return V;
4280}
4281
Evan Chengf26ffe92008-05-29 08:22:04 +00004282/// getVShift - Return a vector logical shift node.
4283///
Owen Andersone50ed302009-08-10 22:56:29 +00004284static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00004285 unsigned NumBits, SelectionDAG &DAG,
4286 const TargetLowering &TLI, DebugLoc dl) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004287 EVT ShVT = MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00004288 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004289 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
4290 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004291 DAG.getNode(Opc, dl, ShVT, SrcOp,
Owen Anderson95771af2011-02-25 21:41:48 +00004292 DAG.getConstant(NumBits,
4293 TLI.getShiftAmountTy(SrcOp.getValueType()))));
Evan Chengf26ffe92008-05-29 08:22:04 +00004294}
4295
Dan Gohman475871a2008-07-27 21:46:04 +00004296SDValue
Evan Chengc3630942009-12-09 21:00:30 +00004297X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00004298 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00004299
Evan Chengc3630942009-12-09 21:00:30 +00004300 // Check if the scalar load can be widened into a vector load. And if
4301 // the address is "base + cst" see if the cst can be "absorbed" into
4302 // the shuffle mask.
4303 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
4304 SDValue Ptr = LD->getBasePtr();
4305 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
4306 return SDValue();
4307 EVT PVT = LD->getValueType(0);
4308 if (PVT != MVT::i32 && PVT != MVT::f32)
4309 return SDValue();
4310
4311 int FI = -1;
4312 int64_t Offset = 0;
4313 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
4314 FI = FINode->getIndex();
4315 Offset = 0;
Chris Lattner0a9481f2011-02-13 22:25:43 +00004316 } else if (DAG.isBaseWithConstantOffset(Ptr) &&
Evan Chengc3630942009-12-09 21:00:30 +00004317 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4318 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4319 Offset = Ptr.getConstantOperandVal(1);
4320 Ptr = Ptr.getOperand(0);
4321 } else {
4322 return SDValue();
4323 }
4324
4325 SDValue Chain = LD->getChain();
4326 // Make sure the stack object alignment is at least 16.
4327 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
4328 if (DAG.InferPtrAlignment(Ptr) < 16) {
4329 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00004330 // Can't change the alignment. FIXME: It's possible to compute
4331 // the exact stack offset and reference FI + adjust offset instead.
4332 // If someone *really* cares about this. That's the way to implement it.
4333 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004334 } else {
4335 MFI->setObjectAlignment(FI, 16);
4336 }
4337 }
4338
4339 // (Offset % 16) must be multiple of 4. Then address is then
4340 // Ptr + (Offset & ~15).
4341 if (Offset < 0)
4342 return SDValue();
4343 if ((Offset % 16) & 3)
4344 return SDValue();
4345 int64_t StartOffset = Offset & ~15;
4346 if (StartOffset)
4347 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
4348 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
4349
4350 int EltNo = (Offset - StartOffset) >> 2;
4351 int Mask[4] = { EltNo, EltNo, EltNo, EltNo };
4352 EVT VT = (PVT == MVT::i32) ? MVT::v4i32 : MVT::v4f32;
Chris Lattner51abfe42010-09-21 06:02:19 +00004353 SDValue V1 = DAG.getLoad(VT, dl, Chain, Ptr,
4354 LD->getPointerInfo().getWithOffset(StartOffset),
David Greene67c9d422010-02-15 16:53:33 +00004355 false, false, 0);
Evan Chengc3630942009-12-09 21:00:30 +00004356 // Canonicalize it to a v4i32 shuffle.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004357 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, V1);
4358 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Chengc3630942009-12-09 21:00:30 +00004359 DAG.getVectorShuffle(MVT::v4i32, dl, V1,
Chris Lattner51abfe42010-09-21 06:02:19 +00004360 DAG.getUNDEF(MVT::v4i32),&Mask[0]));
Evan Chengc3630942009-12-09 21:00:30 +00004361 }
4362
4363 return SDValue();
4364}
4365
Michael J. Spencerec38de22010-10-10 22:04:20 +00004366/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
4367/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00004368/// load which has the same value as a build_vector whose operands are 'elts'.
4369///
4370/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00004371///
Nate Begeman1449f292010-03-24 22:19:06 +00004372/// FIXME: we'd also like to handle the case where the last elements are zero
4373/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
4374/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004375static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00004376 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004377 EVT EltVT = VT.getVectorElementType();
4378 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00004379
Nate Begemanfdea31a2010-03-24 20:49:50 +00004380 LoadSDNode *LDBase = NULL;
4381 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004382
Nate Begeman1449f292010-03-24 22:19:06 +00004383 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00004384 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00004385 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004386 for (unsigned i = 0; i < NumElems; ++i) {
4387 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00004388
Nate Begemanfdea31a2010-03-24 20:49:50 +00004389 if (!Elt.getNode() ||
4390 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
4391 return SDValue();
4392 if (!LDBase) {
4393 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
4394 return SDValue();
4395 LDBase = cast<LoadSDNode>(Elt.getNode());
4396 LastLoadedElt = i;
4397 continue;
4398 }
4399 if (Elt.getOpcode() == ISD::UNDEF)
4400 continue;
4401
4402 LoadSDNode *LD = cast<LoadSDNode>(Elt);
4403 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
4404 return SDValue();
4405 LastLoadedElt = i;
4406 }
Nate Begeman1449f292010-03-24 22:19:06 +00004407
4408 // If we have found an entire vector of loads and undefs, then return a large
4409 // load of the entire vector width starting at the base pointer. If we found
4410 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004411 if (LastLoadedElt == NumElems - 1) {
4412 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00004413 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004414 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004415 LDBase->isVolatile(), LDBase->isNonTemporal(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00004416 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004417 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004418 LDBase->isVolatile(), LDBase->isNonTemporal(),
4419 LDBase->getAlignment());
4420 } else if (NumElems == 4 && LastLoadedElt == 1) {
4421 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
4422 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Chris Lattner88641552010-09-22 00:34:38 +00004423 SDValue ResNode = DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys,
4424 Ops, 2, MVT::i32,
4425 LDBase->getMemOperand());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004426 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00004427 }
4428 return SDValue();
4429}
4430
Evan Chengc3630942009-12-09 21:00:30 +00004431SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004432X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004433 DebugLoc dl = Op.getDebugLoc();
David Greenea5f26012011-02-07 19:36:54 +00004434
David Greenef125a292011-02-08 19:04:41 +00004435 EVT VT = Op.getValueType();
4436 EVT ExtVT = VT.getVectorElementType();
4437
4438 unsigned NumElems = Op.getNumOperands();
4439
4440 // For AVX-length vectors, build the individual 128-bit pieces and
4441 // use shuffles to put them in place.
Owen Anderson95771af2011-02-25 21:41:48 +00004442 if (VT.getSizeInBits() > 256 &&
4443 Subtarget->hasAVX() &&
David Greenef125a292011-02-08 19:04:41 +00004444 !ISD::isBuildVectorAllZeros(Op.getNode())) {
4445 SmallVector<SDValue, 8> V;
4446 V.resize(NumElems);
4447 for (unsigned i = 0; i < NumElems; ++i) {
4448 V[i] = Op.getOperand(i);
4449 }
Owen Anderson95771af2011-02-25 21:41:48 +00004450
David Greenef125a292011-02-08 19:04:41 +00004451 EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElems/2);
4452
4453 // Build the lower subvector.
4454 SDValue Lower = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[0], NumElems/2);
4455 // Build the upper subvector.
4456 SDValue Upper = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[NumElems / 2],
4457 NumElems/2);
4458
4459 return ConcatVectors(Lower, Upper, DAG);
4460 }
4461
Chris Lattner6e80e442010-08-28 17:15:43 +00004462 // All zero's are handled with pxor in SSE2 and above, xorps in SSE1.
4463 // All one's are handled with pcmpeqd. In AVX, zero's are handled with
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004464 // vpxor in 128-bit and xor{pd,ps} in 256-bit, but no 256 version of pcmpeqd
4465 // is present, so AllOnes is ignored.
4466 if (ISD::isBuildVectorAllZeros(Op.getNode()) ||
4467 (Op.getValueType().getSizeInBits() != 256 &&
4468 ISD::isBuildVectorAllOnes(Op.getNode()))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004469 // Canonicalize this to <4 x i32> (SSE) to
Chris Lattner8a594482007-11-25 00:24:49 +00004470 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
4471 // eliminated on x86-32 hosts.
Dale Johannesen0488fb62010-09-30 23:57:10 +00004472 if (Op.getValueType() == MVT::v4i32)
Chris Lattner8a594482007-11-25 00:24:49 +00004473 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004474
Gabor Greifba36cb52008-08-28 21:40:38 +00004475 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00004476 return getOnesVector(Op.getValueType(), DAG, dl);
4477 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00004478 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004479
Owen Andersone50ed302009-08-10 22:56:29 +00004480 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004481
Evan Cheng0db9fe62006-04-25 20:13:52 +00004482 unsigned NumZero = 0;
4483 unsigned NumNonZero = 0;
4484 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004485 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00004486 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004487 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00004488 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00004489 if (Elt.getOpcode() == ISD::UNDEF)
4490 continue;
4491 Values.insert(Elt);
4492 if (Elt.getOpcode() != ISD::Constant &&
4493 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004494 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00004495 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00004496 NumZero++;
4497 else {
4498 NonZeros |= (1 << i);
4499 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004500 }
4501 }
4502
Chris Lattner97a2a562010-08-26 05:24:29 +00004503 // All undef vector. Return an UNDEF. All zero vectors were handled above.
4504 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00004505 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004506
Chris Lattner67f453a2008-03-09 05:42:06 +00004507 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00004508 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004509 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00004510 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00004511
Chris Lattner62098042008-03-09 01:05:04 +00004512 // If this is an insertion of an i64 value on x86-32, and if the top bits of
4513 // the value are obviously zero, truncate the value to i32 and do the
4514 // insertion that way. Only do this if the value is non-constant or if the
4515 // value is a constant being inserted into element 0. It is cheaper to do
4516 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00004517 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00004518 (!IsAllConstants || Idx == 0)) {
4519 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004520 // Handle SSE only.
4521 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
4522 EVT VecVT = MVT::v4i32;
4523 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00004524
Chris Lattner62098042008-03-09 01:05:04 +00004525 // Truncate the value (which may itself be a constant) to i32, and
4526 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00004527 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00004528 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00004529 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
4530 Subtarget->hasSSE2(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00004531
Chris Lattner62098042008-03-09 01:05:04 +00004532 // Now we have our 32-bit value zero extended in the low element of
4533 // a vector. If Idx != 0, swizzle it into place.
4534 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004535 SmallVector<int, 4> Mask;
4536 Mask.push_back(Idx);
4537 for (unsigned i = 1; i != VecElts; ++i)
4538 Mask.push_back(i);
4539 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopherfd179292009-08-27 18:07:15 +00004540 DAG.getUNDEF(Item.getValueType()),
Nate Begeman9008ca62009-04-27 18:41:29 +00004541 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004542 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004543 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00004544 }
4545 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004546
Chris Lattner19f79692008-03-08 22:59:52 +00004547 // If we have a constant or non-constant insertion into the low element of
4548 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
4549 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00004550 // depending on what the source datatype is.
4551 if (Idx == 0) {
4552 if (NumZero == 0) {
4553 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Owen Anderson825b72b2009-08-11 20:47:22 +00004554 } else if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
4555 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Eli Friedman10415532009-06-06 06:05:10 +00004556 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
4557 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
4558 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget->hasSSE2(),
4559 DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00004560 } else if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
4561 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Dale Johannesen0488fb62010-09-30 23:57:10 +00004562 assert(VT.getSizeInBits() == 128 && "Expected an SSE value type!");
4563 EVT MiddleVT = MVT::v4i32;
Eli Friedman10415532009-06-06 06:05:10 +00004564 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
4565 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
4566 Subtarget->hasSSE2(), DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004567 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00004568 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004569 }
Evan Chengf26ffe92008-05-29 08:22:04 +00004570
4571 // Is it a vector logical left shift?
4572 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00004573 X86::isZeroNode(Op.getOperand(0)) &&
4574 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004575 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00004576 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00004577 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004578 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00004579 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004580 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004581
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004582 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00004583 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004584
Chris Lattner19f79692008-03-08 22:59:52 +00004585 // Otherwise, if this is a vector with i32 or f32 elements, and the element
4586 // is a non-constant being inserted into an element other than the low one,
4587 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
4588 // movd/movss) to move this into the low element, then shuffle it into
4589 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004590 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00004591 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00004592
Evan Cheng0db9fe62006-04-25 20:13:52 +00004593 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00004594 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
4595 Subtarget->hasSSE2(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00004596 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004597 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00004598 MaskVec.push_back(i == Idx ? 0 : 1);
4599 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004600 }
4601 }
4602
Chris Lattner67f453a2008-03-09 05:42:06 +00004603 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00004604 if (Values.size() == 1) {
4605 if (EVTBits == 32) {
4606 // Instead of a shuffle like this:
4607 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
4608 // Check if it's possible to issue this instead.
4609 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
4610 unsigned Idx = CountTrailingZeros_32(NonZeros);
4611 SDValue Item = Op.getOperand(Idx);
4612 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
4613 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
4614 }
Dan Gohman475871a2008-07-27 21:46:04 +00004615 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004616 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004617
Dan Gohmana3941172007-07-24 22:55:08 +00004618 // A vector full of immediates; various special cases are already
4619 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004620 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00004621 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00004622
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00004623 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00004624 if (EVTBits == 64) {
4625 if (NumNonZero == 1) {
4626 // One half is zero or undef.
4627 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00004628 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00004629 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00004630 return getShuffleVectorZeroOrUndef(V2, Idx, true,
4631 Subtarget->hasSSE2(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00004632 }
Dan Gohman475871a2008-07-27 21:46:04 +00004633 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00004634 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004635
4636 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00004637 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00004638 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00004639 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004640 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004641 }
4642
Bill Wendling826f36f2007-03-28 00:57:11 +00004643 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00004644 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Chris Lattner97a2a562010-08-26 05:24:29 +00004645 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004646 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004647 }
4648
4649 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00004650 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00004651 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004652 if (NumElems == 4 && NumZero > 0) {
4653 for (unsigned i = 0; i < 4; ++i) {
4654 bool isZero = !(NonZeros & (1 << i));
4655 if (isZero)
Dale Johannesenace16102009-02-03 19:33:06 +00004656 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004657 else
Dale Johannesenace16102009-02-03 19:33:06 +00004658 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004659 }
4660
4661 for (unsigned i = 0; i < 2; ++i) {
4662 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
4663 default: break;
4664 case 0:
4665 V[i] = V[i*2]; // Must be a zero vector.
4666 break;
4667 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00004668 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004669 break;
4670 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00004671 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004672 break;
4673 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00004674 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004675 break;
4676 }
4677 }
4678
Nate Begeman9008ca62009-04-27 18:41:29 +00004679 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004680 bool Reverse = (NonZeros & 0x3) == 2;
4681 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004682 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004683 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
4684 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004685 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
4686 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004687 }
4688
Nate Begemanfdea31a2010-03-24 20:49:50 +00004689 if (Values.size() > 1 && VT.getSizeInBits() == 128) {
4690 // Check for a build vector of consecutive loads.
4691 for (unsigned i = 0; i < NumElems; ++i)
4692 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00004693
Nate Begemanfdea31a2010-03-24 20:49:50 +00004694 // Check for elements which are consecutive loads.
4695 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
4696 if (LD.getNode())
4697 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004698
4699 // For SSE 4.1, use insertps to put the high elements into the low element.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004700 if (getSubtarget()->hasSSE41()) {
Chris Lattner24faf612010-08-28 17:59:08 +00004701 SDValue Result;
4702 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
4703 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
4704 else
4705 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00004706
Chris Lattner24faf612010-08-28 17:59:08 +00004707 for (unsigned i = 1; i < NumElems; ++i) {
4708 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
4709 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00004710 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00004711 }
4712 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00004713 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00004714
Chris Lattner6e80e442010-08-28 17:15:43 +00004715 // Otherwise, expand into a number of unpckl*, start by extending each of
4716 // our (non-undef) elements to the full vector width with the element in the
4717 // bottom slot of the vector (which generates no code for SSE).
4718 for (unsigned i = 0; i < NumElems; ++i) {
4719 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
4720 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
4721 else
4722 V[i] = DAG.getUNDEF(VT);
4723 }
4724
4725 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004726 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
4727 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
4728 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00004729 unsigned EltStride = NumElems >> 1;
4730 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00004731 for (unsigned i = 0; i < EltStride; ++i) {
4732 // If V[i+EltStride] is undef and this is the first round of mixing,
4733 // then it is safe to just drop this shuffle: V[i] is already in the
4734 // right place, the one element (since it's the first round) being
4735 // inserted as undef can be dropped. This isn't safe for successive
4736 // rounds because they will permute elements within both vectors.
4737 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
4738 EltStride == NumElems/2)
4739 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004740
Chris Lattner6e80e442010-08-28 17:15:43 +00004741 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00004742 }
Chris Lattner6e80e442010-08-28 17:15:43 +00004743 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004744 }
4745 return V[0];
4746 }
Dan Gohman475871a2008-07-27 21:46:04 +00004747 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004748}
4749
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004750SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004751X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const {
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004752 // We support concatenate two MMX registers and place them in a MMX
4753 // register. This is better than doing a stack convert.
4754 DebugLoc dl = Op.getDebugLoc();
4755 EVT ResVT = Op.getValueType();
4756 assert(Op.getNumOperands() == 2);
4757 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
4758 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
4759 int Mask[2];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004760 SDValue InVec = DAG.getNode(ISD::BITCAST,dl, MVT::v1i64, Op.getOperand(0));
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004761 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4762 InVec = Op.getOperand(1);
4763 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4764 unsigned NumElts = ResVT.getVectorNumElements();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004765 VecOp = DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004766 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
4767 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
4768 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004769 InVec = DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, InVec);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004770 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4771 Mask[0] = 0; Mask[1] = 2;
4772 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
4773 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004774 return DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004775}
4776
Nate Begemanb9a47b82009-02-23 08:49:38 +00004777// v8i16 shuffles - Prefer shuffles in the following order:
4778// 1. [all] pshuflw, pshufhw, optional move
4779// 2. [ssse3] 1 x pshufb
4780// 3. [ssse3] 2 x pshufb + 1 x por
4781// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004782SDValue
4783X86TargetLowering::LowerVECTOR_SHUFFLEv8i16(SDValue Op,
4784 SelectionDAG &DAG) const {
4785 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00004786 SDValue V1 = SVOp->getOperand(0);
4787 SDValue V2 = SVOp->getOperand(1);
4788 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004789 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00004790
Nate Begemanb9a47b82009-02-23 08:49:38 +00004791 // Determine if more than 1 of the words in each of the low and high quadwords
4792 // of the result come from the same quadword of one of the two inputs. Undef
4793 // mask values count as coming from any quadword, for better codegen.
4794 SmallVector<unsigned, 4> LoQuad(4);
4795 SmallVector<unsigned, 4> HiQuad(4);
4796 BitVector InputQuads(4);
4797 for (unsigned i = 0; i < 8; ++i) {
4798 SmallVectorImpl<unsigned> &Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00004799 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004800 MaskVals.push_back(EltIdx);
4801 if (EltIdx < 0) {
4802 ++Quad[0];
4803 ++Quad[1];
4804 ++Quad[2];
4805 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00004806 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004807 }
4808 ++Quad[EltIdx / 4];
4809 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00004810 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004811
Nate Begemanb9a47b82009-02-23 08:49:38 +00004812 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004813 unsigned MaxQuad = 1;
4814 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004815 if (LoQuad[i] > MaxQuad) {
4816 BestLoQuad = i;
4817 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004818 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004819 }
4820
Nate Begemanb9a47b82009-02-23 08:49:38 +00004821 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004822 MaxQuad = 1;
4823 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004824 if (HiQuad[i] > MaxQuad) {
4825 BestHiQuad = i;
4826 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004827 }
4828 }
4829
Nate Begemanb9a47b82009-02-23 08:49:38 +00004830 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00004831 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00004832 // single pshufb instruction is necessary. If There are more than 2 input
4833 // quads, disable the next transformation since it does not help SSSE3.
4834 bool V1Used = InputQuads[0] || InputQuads[1];
4835 bool V2Used = InputQuads[2] || InputQuads[3];
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004836 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004837 if (InputQuads.count() == 2 && V1Used && V2Used) {
4838 BestLoQuad = InputQuads.find_first();
4839 BestHiQuad = InputQuads.find_next(BestLoQuad);
4840 }
4841 if (InputQuads.count() > 2) {
4842 BestLoQuad = -1;
4843 BestHiQuad = -1;
4844 }
4845 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004846
Nate Begemanb9a47b82009-02-23 08:49:38 +00004847 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
4848 // the shuffle mask. If a quad is scored as -1, that means that it contains
4849 // words from all 4 input quadwords.
4850 SDValue NewV;
4851 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004852 SmallVector<int, 8> MaskV;
4853 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
4854 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Eric Christopherfd179292009-08-27 18:07:15 +00004855 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004856 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
4857 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
4858 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00004859
Nate Begemanb9a47b82009-02-23 08:49:38 +00004860 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
4861 // source words for the shuffle, to aid later transformations.
4862 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00004863 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00004864 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004865 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00004866 if (idx != (int)i)
4867 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004868 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00004869 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004870 AllWordsInNewV = false;
4871 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00004872 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004873
Nate Begemanb9a47b82009-02-23 08:49:38 +00004874 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
4875 if (AllWordsInNewV) {
4876 for (int i = 0; i != 8; ++i) {
4877 int idx = MaskVals[i];
4878 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00004879 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004880 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004881 if ((idx != i) && idx < 4)
4882 pshufhw = false;
4883 if ((idx != i) && idx > 3)
4884 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00004885 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00004886 V1 = NewV;
4887 V2Used = false;
4888 BestLoQuad = 0;
4889 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004890 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004891
Nate Begemanb9a47b82009-02-23 08:49:38 +00004892 // If we've eliminated the use of V2, and the new mask is a pshuflw or
4893 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00004894 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00004895 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
4896 unsigned TargetMask = 0;
4897 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00004898 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00004899 TargetMask = pshufhw ? X86::getShufflePSHUFHWImmediate(NewV.getNode()):
4900 X86::getShufflePSHUFLWImmediate(NewV.getNode());
4901 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004902 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00004903 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004904 }
Eric Christopherfd179292009-08-27 18:07:15 +00004905
Nate Begemanb9a47b82009-02-23 08:49:38 +00004906 // If we have SSSE3, and all words of the result are from 1 input vector,
4907 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
4908 // is present, fall back to case 4.
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004909 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004910 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004911
Nate Begemanb9a47b82009-02-23 08:49:38 +00004912 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00004913 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00004914 // mask, and elements that come from V1 in the V2 mask, so that the two
4915 // results can be OR'd together.
4916 bool TwoInputs = V1Used && V2Used;
4917 for (unsigned i = 0; i != 8; ++i) {
4918 int EltIdx = MaskVals[i] * 2;
4919 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004920 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4921 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004922 continue;
4923 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004924 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
4925 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004926 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004927 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004928 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004929 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004930 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004931 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004932 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004933
Nate Begemanb9a47b82009-02-23 08:49:38 +00004934 // Calculate the shuffle mask for the second input, shuffle it, and
4935 // OR it with the first shuffled input.
4936 pshufbMask.clear();
4937 for (unsigned i = 0; i != 8; ++i) {
4938 int EltIdx = MaskVals[i] * 2;
4939 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004940 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4941 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004942 continue;
4943 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004944 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
4945 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004946 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004947 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00004948 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00004949 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004950 MVT::v16i8, &pshufbMask[0], 16));
4951 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004952 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004953 }
4954
4955 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
4956 // and update MaskVals with new element order.
4957 BitVector InOrder(8);
4958 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004959 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004960 for (int i = 0; i != 4; ++i) {
4961 int idx = MaskVals[i];
4962 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004963 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004964 InOrder.set(i);
4965 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004966 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004967 InOrder.set(i);
4968 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004969 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004970 }
4971 }
4972 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004973 MaskV.push_back(i);
Owen Anderson825b72b2009-08-11 20:47:22 +00004974 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004975 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004976
4977 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
4978 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
4979 NewV.getOperand(0),
4980 X86::getShufflePSHUFLWImmediate(NewV.getNode()),
4981 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004982 }
Eric Christopherfd179292009-08-27 18:07:15 +00004983
Nate Begemanb9a47b82009-02-23 08:49:38 +00004984 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
4985 // and update MaskVals with the new element order.
4986 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004987 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004988 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004989 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004990 for (unsigned i = 4; i != 8; ++i) {
4991 int idx = MaskVals[i];
4992 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004993 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004994 InOrder.set(i);
4995 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004996 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004997 InOrder.set(i);
4998 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004999 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005000 }
5001 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005002 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005003 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005004
5005 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
5006 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
5007 NewV.getOperand(0),
5008 X86::getShufflePSHUFHWImmediate(NewV.getNode()),
5009 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005010 }
Eric Christopherfd179292009-08-27 18:07:15 +00005011
Nate Begemanb9a47b82009-02-23 08:49:38 +00005012 // In case BestHi & BestLo were both -1, which means each quadword has a word
5013 // from each of the four input quadwords, calculate the InOrder bitvector now
5014 // before falling through to the insert/extract cleanup.
5015 if (BestLoQuad == -1 && BestHiQuad == -1) {
5016 NewV = V1;
5017 for (int i = 0; i != 8; ++i)
5018 if (MaskVals[i] < 0 || MaskVals[i] == i)
5019 InOrder.set(i);
5020 }
Eric Christopherfd179292009-08-27 18:07:15 +00005021
Nate Begemanb9a47b82009-02-23 08:49:38 +00005022 // The other elements are put in the right place using pextrw and pinsrw.
5023 for (unsigned i = 0; i != 8; ++i) {
5024 if (InOrder[i])
5025 continue;
5026 int EltIdx = MaskVals[i];
5027 if (EltIdx < 0)
5028 continue;
5029 SDValue ExtOp = (EltIdx < 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00005030 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005031 DAG.getIntPtrConstant(EltIdx))
Owen Anderson825b72b2009-08-11 20:47:22 +00005032 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005033 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00005034 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005035 DAG.getIntPtrConstant(i));
5036 }
5037 return NewV;
5038}
5039
5040// v16i8 shuffles - Prefer shuffles in the following order:
5041// 1. [ssse3] 1 x pshufb
5042// 2. [ssse3] 2 x pshufb + 1 x por
5043// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
5044static
Nate Begeman9008ca62009-04-27 18:41:29 +00005045SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00005046 SelectionDAG &DAG,
5047 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005048 SDValue V1 = SVOp->getOperand(0);
5049 SDValue V2 = SVOp->getOperand(1);
5050 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005051 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00005052 SVOp->getMask(MaskVals);
Eric Christopherfd179292009-08-27 18:07:15 +00005053
Nate Begemanb9a47b82009-02-23 08:49:38 +00005054 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00005055 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00005056 // present, fall back to case 3.
5057 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
5058 bool V1Only = true;
5059 bool V2Only = true;
5060 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005061 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00005062 if (EltIdx < 0)
5063 continue;
5064 if (EltIdx < 16)
5065 V2Only = false;
5066 else
5067 V1Only = false;
5068 }
Eric Christopherfd179292009-08-27 18:07:15 +00005069
Nate Begemanb9a47b82009-02-23 08:49:38 +00005070 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
5071 if (TLI.getSubtarget()->hasSSSE3()) {
5072 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005073
Nate Begemanb9a47b82009-02-23 08:49:38 +00005074 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00005075 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005076 //
5077 // Otherwise, we have elements from both input vectors, and must zero out
5078 // elements that come from V2 in the first mask, and V1 in the second mask
5079 // so that we can OR them together.
5080 bool TwoInputs = !(V1Only || V2Only);
5081 for (unsigned i = 0; i != 16; ++i) {
5082 int EltIdx = MaskVals[i];
5083 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005084 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005085 continue;
5086 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005087 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005088 }
5089 // If all the elements are from V2, assign it to V1 and return after
5090 // building the first pshufb.
5091 if (V2Only)
5092 V1 = V2;
Owen Anderson825b72b2009-08-11 20:47:22 +00005093 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005094 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005095 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005096 if (!TwoInputs)
5097 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00005098
Nate Begemanb9a47b82009-02-23 08:49:38 +00005099 // Calculate the shuffle mask for the second input, shuffle it, and
5100 // OR it with the first shuffled input.
5101 pshufbMask.clear();
5102 for (unsigned i = 0; i != 16; ++i) {
5103 int EltIdx = MaskVals[i];
5104 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005105 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005106 continue;
5107 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005108 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005109 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005110 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005111 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005112 MVT::v16i8, &pshufbMask[0], 16));
5113 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005114 }
Eric Christopherfd179292009-08-27 18:07:15 +00005115
Nate Begemanb9a47b82009-02-23 08:49:38 +00005116 // No SSSE3 - Calculate in place words and then fix all out of place words
5117 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
5118 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005119 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
5120 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005121 SDValue NewV = V2Only ? V2 : V1;
5122 for (int i = 0; i != 8; ++i) {
5123 int Elt0 = MaskVals[i*2];
5124 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00005125
Nate Begemanb9a47b82009-02-23 08:49:38 +00005126 // This word of the result is all undef, skip it.
5127 if (Elt0 < 0 && Elt1 < 0)
5128 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005129
Nate Begemanb9a47b82009-02-23 08:49:38 +00005130 // This word of the result is already in the correct place, skip it.
5131 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
5132 continue;
5133 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
5134 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005135
Nate Begemanb9a47b82009-02-23 08:49:38 +00005136 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
5137 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
5138 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00005139
5140 // If Elt0 and Elt1 are defined, are consecutive, and can be load
5141 // using a single extract together, load it and store it.
5142 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005143 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005144 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00005145 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005146 DAG.getIntPtrConstant(i));
5147 continue;
5148 }
5149
Nate Begemanb9a47b82009-02-23 08:49:38 +00005150 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00005151 // source byte is not also odd, shift the extracted word left 8 bits
5152 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005153 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005154 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005155 DAG.getIntPtrConstant(Elt1 / 2));
5156 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005157 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Owen Anderson95771af2011-02-25 21:41:48 +00005158 DAG.getConstant(8,
5159 TLI.getShiftAmountTy(InsElt.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005160 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005161 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
5162 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005163 }
5164 // If Elt0 is defined, extract it from the appropriate source. If the
5165 // source byte is not also even, shift the extracted word right 8 bits. If
5166 // Elt1 was also defined, OR the extracted values together before
5167 // inserting them in the result.
5168 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005169 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005170 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
5171 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005172 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Owen Anderson95771af2011-02-25 21:41:48 +00005173 DAG.getConstant(8,
5174 TLI.getShiftAmountTy(InsElt0.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005175 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005176 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
5177 DAG.getConstant(0x00FF, MVT::i16));
5178 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00005179 : InsElt0;
5180 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005181 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005182 DAG.getIntPtrConstant(i));
5183 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005184 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005185}
5186
Evan Cheng7a831ce2007-12-15 03:00:47 +00005187/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005188/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00005189/// done when every pair / quad of shuffle mask elements point to elements in
5190/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005191/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00005192static
Nate Begeman9008ca62009-04-27 18:41:29 +00005193SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005194 SelectionDAG &DAG, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00005195 EVT VT = SVOp->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00005196 SDValue V1 = SVOp->getOperand(0);
5197 SDValue V2 = SVOp->getOperand(1);
5198 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00005199 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005200 EVT NewVT;
Owen Anderson825b72b2009-08-11 20:47:22 +00005201 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005202 default: assert(false && "Unexpected!");
Owen Anderson825b72b2009-08-11 20:47:22 +00005203 case MVT::v4f32: NewVT = MVT::v2f64; break;
5204 case MVT::v4i32: NewVT = MVT::v2i64; break;
5205 case MVT::v8i16: NewVT = MVT::v4i32; break;
5206 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00005207 }
5208
Nate Begeman9008ca62009-04-27 18:41:29 +00005209 int Scale = NumElems / NewWidth;
5210 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00005211 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005212 int StartIdx = -1;
5213 for (int j = 0; j < Scale; ++j) {
5214 int EltIdx = SVOp->getMaskElt(i+j);
5215 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005216 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00005217 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00005218 StartIdx = EltIdx - (EltIdx % Scale);
5219 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00005220 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00005221 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005222 if (StartIdx == -1)
5223 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00005224 else
Nate Begeman9008ca62009-04-27 18:41:29 +00005225 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005226 }
5227
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005228 V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, V1);
5229 V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00005230 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005231}
5232
Evan Chengd880b972008-05-09 21:53:03 +00005233/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005234///
Owen Andersone50ed302009-08-10 22:56:29 +00005235static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00005236 SDValue SrcOp, SelectionDAG &DAG,
5237 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005238 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005239 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00005240 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00005241 LD = dyn_cast<LoadSDNode>(SrcOp);
5242 if (!LD) {
5243 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
5244 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00005245 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00005246 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00005247 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005248 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00005249 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005250 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00005251 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005252 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005253 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
5254 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
5255 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00005256 SrcOp.getOperand(0)
5257 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005258 }
5259 }
5260 }
5261
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005262 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005263 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005264 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00005265 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005266}
5267
Evan Chengace3c172008-07-22 21:13:36 +00005268/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
5269/// shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00005270static SDValue
Nate Begeman9008ca62009-04-27 18:41:29 +00005271LowerVECTOR_SHUFFLE_4wide(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
5272 SDValue V1 = SVOp->getOperand(0);
5273 SDValue V2 = SVOp->getOperand(1);
5274 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005275 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00005276
Evan Chengace3c172008-07-22 21:13:36 +00005277 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00005278 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00005279 SmallVector<int, 8> Mask1(4U, -1);
5280 SmallVector<int, 8> PermMask;
5281 SVOp->getMask(PermMask);
5282
Evan Chengace3c172008-07-22 21:13:36 +00005283 unsigned NumHi = 0;
5284 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00005285 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005286 int Idx = PermMask[i];
5287 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00005288 Locs[i] = std::make_pair(-1, -1);
5289 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005290 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
5291 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00005292 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00005293 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005294 NumLo++;
5295 } else {
5296 Locs[i] = std::make_pair(1, NumHi);
5297 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00005298 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005299 NumHi++;
5300 }
5301 }
5302 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005303
Evan Chengace3c172008-07-22 21:13:36 +00005304 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005305 // If no more than two elements come from either vector. This can be
5306 // implemented with two shuffles. First shuffle gather the elements.
5307 // The second shuffle, which takes the first shuffle as both of its
5308 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00005309 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005310
Nate Begeman9008ca62009-04-27 18:41:29 +00005311 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopherfd179292009-08-27 18:07:15 +00005312
Evan Chengace3c172008-07-22 21:13:36 +00005313 for (unsigned i = 0; i != 4; ++i) {
5314 if (Locs[i].first == -1)
5315 continue;
5316 else {
5317 unsigned Idx = (i < 2) ? 0 : 4;
5318 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00005319 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005320 }
5321 }
5322
Nate Begeman9008ca62009-04-27 18:41:29 +00005323 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005324 } else if (NumLo == 3 || NumHi == 3) {
5325 // Otherwise, we must have three elements from one vector, call it X, and
5326 // one element from the other, call it Y. First, use a shufps to build an
5327 // intermediate vector with the one element from Y and the element from X
5328 // that will be in the same half in the final destination (the indexes don't
5329 // matter). Then, use a shufps to build the final vector, taking the half
5330 // containing the element from Y from the intermediate, and the other half
5331 // from X.
5332 if (NumHi == 3) {
5333 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00005334 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005335 std::swap(V1, V2);
5336 }
5337
5338 // Find the element from V2.
5339 unsigned HiIndex;
5340 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005341 int Val = PermMask[HiIndex];
5342 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005343 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005344 if (Val >= 4)
5345 break;
5346 }
5347
Nate Begeman9008ca62009-04-27 18:41:29 +00005348 Mask1[0] = PermMask[HiIndex];
5349 Mask1[1] = -1;
5350 Mask1[2] = PermMask[HiIndex^1];
5351 Mask1[3] = -1;
5352 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005353
5354 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005355 Mask1[0] = PermMask[0];
5356 Mask1[1] = PermMask[1];
5357 Mask1[2] = HiIndex & 1 ? 6 : 4;
5358 Mask1[3] = HiIndex & 1 ? 4 : 6;
5359 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005360 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005361 Mask1[0] = HiIndex & 1 ? 2 : 0;
5362 Mask1[1] = HiIndex & 1 ? 0 : 2;
5363 Mask1[2] = PermMask[2];
5364 Mask1[3] = PermMask[3];
5365 if (Mask1[2] >= 0)
5366 Mask1[2] += 4;
5367 if (Mask1[3] >= 0)
5368 Mask1[3] += 4;
5369 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005370 }
Evan Chengace3c172008-07-22 21:13:36 +00005371 }
5372
5373 // Break it into (shuffle shuffle_hi, shuffle_lo).
5374 Locs.clear();
David Greenec4db4e52011-02-28 19:06:56 +00005375 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00005376 SmallVector<int,8> LoMask(4U, -1);
5377 SmallVector<int,8> HiMask(4U, -1);
5378
5379 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00005380 unsigned MaskIdx = 0;
5381 unsigned LoIdx = 0;
5382 unsigned HiIdx = 2;
5383 for (unsigned i = 0; i != 4; ++i) {
5384 if (i == 2) {
5385 MaskPtr = &HiMask;
5386 MaskIdx = 1;
5387 LoIdx = 0;
5388 HiIdx = 2;
5389 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005390 int Idx = PermMask[i];
5391 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00005392 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00005393 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00005394 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00005395 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005396 LoIdx++;
5397 } else {
5398 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00005399 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005400 HiIdx++;
5401 }
5402 }
5403
Nate Begeman9008ca62009-04-27 18:41:29 +00005404 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
5405 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
5406 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00005407 for (unsigned i = 0; i != 4; ++i) {
5408 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005409 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00005410 } else {
5411 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00005412 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00005413 }
5414 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005415 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00005416}
5417
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005418static bool MayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005419 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005420 V = V.getOperand(0);
5421 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
5422 V = V.getOperand(0);
5423 if (MayFoldLoad(V))
5424 return true;
5425 return false;
5426}
5427
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005428// FIXME: the version above should always be used. Since there's
5429// a bug where several vector shuffles can't be folded because the
5430// DAG is not updated during lowering and a node claims to have two
5431// uses while it only has one, use this version, and let isel match
5432// another instruction if the load really happens to have more than
5433// one use. Remove this version after this bug get fixed.
Evan Cheng835580f2010-10-07 20:50:20 +00005434// rdar://8434668, PR8156
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005435static bool RelaxedMayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005436 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005437 V = V.getOperand(0);
5438 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
5439 V = V.getOperand(0);
5440 if (ISD::isNormalLoad(V.getNode()))
5441 return true;
5442 return false;
5443}
5444
5445/// CanFoldShuffleIntoVExtract - Check if the current shuffle is used by
5446/// a vector extract, and if both can be later optimized into a single load.
5447/// This is done in visitEXTRACT_VECTOR_ELT and the conditions are checked
5448/// here because otherwise a target specific shuffle node is going to be
5449/// emitted for this shuffle, and the optimization not done.
5450/// FIXME: This is probably not the best approach, but fix the problem
5451/// until the right path is decided.
5452static
5453bool CanXFormVExtractWithShuffleIntoLoad(SDValue V, SelectionDAG &DAG,
5454 const TargetLowering &TLI) {
5455 EVT VT = V.getValueType();
5456 ShuffleVectorSDNode *SVOp = dyn_cast<ShuffleVectorSDNode>(V);
5457
5458 // Be sure that the vector shuffle is present in a pattern like this:
5459 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), c) -> (f32 load $addr)
5460 if (!V.hasOneUse())
5461 return false;
5462
5463 SDNode *N = *V.getNode()->use_begin();
5464 if (N->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
5465 return false;
5466
5467 SDValue EltNo = N->getOperand(1);
5468 if (!isa<ConstantSDNode>(EltNo))
5469 return false;
5470
5471 // If the bit convert changed the number of elements, it is unsafe
5472 // to examine the mask.
5473 bool HasShuffleIntoBitcast = false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005474 if (V.getOpcode() == ISD::BITCAST) {
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005475 EVT SrcVT = V.getOperand(0).getValueType();
5476 if (SrcVT.getVectorNumElements() != VT.getVectorNumElements())
5477 return false;
5478 V = V.getOperand(0);
5479 HasShuffleIntoBitcast = true;
5480 }
5481
5482 // Select the input vector, guarding against out of range extract vector.
5483 unsigned NumElems = VT.getVectorNumElements();
5484 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
5485 int Idx = (Elt > NumElems) ? -1 : SVOp->getMaskElt(Elt);
5486 V = (Idx < (int)NumElems) ? V.getOperand(0) : V.getOperand(1);
5487
5488 // Skip one more bit_convert if necessary
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005489 if (V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005490 V = V.getOperand(0);
5491
5492 if (ISD::isNormalLoad(V.getNode())) {
5493 // Is the original load suitable?
5494 LoadSDNode *LN0 = cast<LoadSDNode>(V);
5495
5496 // FIXME: avoid the multi-use bug that is preventing lots of
5497 // of foldings to be detected, this is still wrong of course, but
5498 // give the temporary desired behavior, and if it happens that
5499 // the load has real more uses, during isel it will not fold, and
5500 // will generate poor code.
5501 if (!LN0 || LN0->isVolatile()) // || !LN0->hasOneUse()
5502 return false;
5503
5504 if (!HasShuffleIntoBitcast)
5505 return true;
5506
5507 // If there's a bitcast before the shuffle, check if the load type and
5508 // alignment is valid.
5509 unsigned Align = LN0->getAlignment();
5510 unsigned NewAlign =
5511 TLI.getTargetData()->getABITypeAlignment(
5512 VT.getTypeForEVT(*DAG.getContext()));
5513
5514 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
5515 return false;
5516 }
5517
5518 return true;
5519}
5520
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005521static
Evan Cheng835580f2010-10-07 20:50:20 +00005522SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
5523 EVT VT = Op.getValueType();
5524
5525 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005526 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
5527 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00005528 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
5529 V1, DAG));
5530}
5531
5532static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005533SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
5534 bool HasSSE2) {
5535 SDValue V1 = Op.getOperand(0);
5536 SDValue V2 = Op.getOperand(1);
5537 EVT VT = Op.getValueType();
5538
5539 assert(VT != MVT::v2i64 && "unsupported shuffle type");
5540
5541 if (HasSSE2 && VT == MVT::v2f64)
5542 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
5543
5544 // v4f32 or v4i32
5545 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V2, DAG);
5546}
5547
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00005548static
5549SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
5550 SDValue V1 = Op.getOperand(0);
5551 SDValue V2 = Op.getOperand(1);
5552 EVT VT = Op.getValueType();
5553
5554 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
5555 "unsupported shuffle type");
5556
5557 if (V2.getOpcode() == ISD::UNDEF)
5558 V2 = V1;
5559
5560 // v4i32 or v4f32
5561 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
5562}
5563
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005564static
5565SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasSSE2) {
5566 SDValue V1 = Op.getOperand(0);
5567 SDValue V2 = Op.getOperand(1);
5568 EVT VT = Op.getValueType();
5569 unsigned NumElems = VT.getVectorNumElements();
5570
5571 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
5572 // operand of these instructions is only memory, so check if there's a
5573 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
5574 // same masks.
5575 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005576
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00005577 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005578 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005579 CanFoldLoad = true;
5580
5581 // When V1 is a load, it can be folded later into a store in isel, example:
5582 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
5583 // turns into:
5584 // (MOVLPSmr addr:$src1, VR128:$src2)
5585 // So, recognize this potential and also use MOVLPS or MOVLPD
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005586 if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005587 CanFoldLoad = true;
5588
Eric Christopher893a8822011-02-20 05:04:42 +00005589 // Both of them can't be memory operations though.
5590 if (MayFoldVectorLoad(V1) && MayFoldVectorLoad(V2))
5591 CanFoldLoad = false;
Owen Anderson95771af2011-02-25 21:41:48 +00005592
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005593 if (CanFoldLoad) {
5594 if (HasSSE2 && NumElems == 2)
5595 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
5596
5597 if (NumElems == 4)
5598 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
5599 }
5600
5601 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5602 // movl and movlp will both match v2i64, but v2i64 is never matched by
5603 // movl earlier because we make it strict to avoid messing with the movlp load
5604 // folding logic (see the code above getMOVLP call). Match it here then,
5605 // this is horrible, but will stay like this until we move all shuffle
5606 // matching to x86 specific nodes. Note that for the 1st condition all
5607 // types are matched with movsd.
5608 if ((HasSSE2 && NumElems == 2) || !X86::isMOVLMask(SVOp))
5609 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
5610 else if (HasSSE2)
5611 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
5612
5613
5614 assert(VT != MVT::v4i32 && "unsupported shuffle type");
5615
5616 // Invert the operand order and use SHUFPS to match it.
5617 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V2, V1,
5618 X86::getShuffleSHUFImmediate(SVOp), DAG);
5619}
5620
David Greenec4db4e52011-02-28 19:06:56 +00005621static inline unsigned getUNPCKLOpcode(EVT VT, const X86Subtarget *Subtarget) {
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005622 switch(VT.getSimpleVT().SimpleTy) {
5623 case MVT::v4i32: return X86ISD::PUNPCKLDQ;
5624 case MVT::v2i64: return X86ISD::PUNPCKLQDQ;
David Greenec4db4e52011-02-28 19:06:56 +00005625 case MVT::v4f32:
5626 return Subtarget->hasAVX() ? X86ISD::VUNPCKLPS : X86ISD::UNPCKLPS;
5627 case MVT::v2f64:
5628 return Subtarget->hasAVX() ? X86ISD::VUNPCKLPD : X86ISD::UNPCKLPD;
5629 case MVT::v8f32: return X86ISD::VUNPCKLPSY;
5630 case MVT::v4f64: return X86ISD::VUNPCKLPDY;
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005631 case MVT::v16i8: return X86ISD::PUNPCKLBW;
5632 case MVT::v8i16: return X86ISD::PUNPCKLWD;
5633 default:
Eric Christopherdd6e40a2011-02-19 03:19:09 +00005634 llvm_unreachable("Unknown type for unpckl");
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005635 }
5636 return 0;
5637}
5638
5639static inline unsigned getUNPCKHOpcode(EVT VT) {
5640 switch(VT.getSimpleVT().SimpleTy) {
5641 case MVT::v4i32: return X86ISD::PUNPCKHDQ;
5642 case MVT::v2i64: return X86ISD::PUNPCKHQDQ;
5643 case MVT::v4f32: return X86ISD::UNPCKHPS;
5644 case MVT::v2f64: return X86ISD::UNPCKHPD;
5645 case MVT::v16i8: return X86ISD::PUNPCKHBW;
5646 case MVT::v8i16: return X86ISD::PUNPCKHWD;
5647 default:
Eric Christopherdd6e40a2011-02-19 03:19:09 +00005648 llvm_unreachable("Unknown type for unpckh");
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005649 }
5650 return 0;
5651}
5652
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005653static
5654SDValue NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG,
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005655 const TargetLowering &TLI,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005656 const X86Subtarget *Subtarget) {
5657 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5658 EVT VT = Op.getValueType();
5659 DebugLoc dl = Op.getDebugLoc();
5660 SDValue V1 = Op.getOperand(0);
5661 SDValue V2 = Op.getOperand(1);
5662
5663 if (isZeroShuffle(SVOp))
5664 return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
5665
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005666 // Handle splat operations
5667 if (SVOp->isSplat()) {
5668 // Special case, this is the only place now where it's
5669 // allowed to return a vector_shuffle operation without
5670 // using a target specific node, because *hopefully* it
5671 // will be optimized away by the dag combiner.
5672 if (VT.getVectorNumElements() <= 4 &&
5673 CanXFormVExtractWithShuffleIntoLoad(Op, DAG, TLI))
5674 return Op;
5675
5676 // Handle splats by matching through known masks
5677 if (VT.getVectorNumElements() <= 4)
5678 return SDValue();
5679
Evan Cheng835580f2010-10-07 20:50:20 +00005680 // Canonicalize all of the remaining to v4f32.
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005681 return PromoteSplat(SVOp, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005682 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005683
5684 // If the shuffle can be profitably rewritten as a narrower shuffle, then
5685 // do it!
5686 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
5687 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5688 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005689 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005690 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
5691 // FIXME: Figure out a cleaner way to do this.
5692 // Try to make use of movq to zero out the top part.
5693 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
5694 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5695 if (NewOp.getNode()) {
5696 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
5697 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
5698 DAG, Subtarget, dl);
5699 }
5700 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
5701 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5702 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
5703 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
5704 DAG, Subtarget, dl);
5705 }
5706 }
5707 return SDValue();
5708}
5709
Dan Gohman475871a2008-07-27 21:46:04 +00005710SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005711X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00005712 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00005713 SDValue V1 = Op.getOperand(0);
5714 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00005715 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005716 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00005717 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005718 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005719 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
5720 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00005721 bool V1IsSplat = false;
5722 bool V2IsSplat = false;
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005723 bool HasSSE2 = Subtarget->hasSSE2() || Subtarget->hasAVX();
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00005724 bool HasSSE3 = Subtarget->hasSSE3() || Subtarget->hasAVX();
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00005725 bool HasSSSE3 = Subtarget->hasSSSE3() || Subtarget->hasAVX();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005726 MachineFunction &MF = DAG.getMachineFunction();
5727 bool OptForSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005728
Dale Johannesen0488fb62010-09-30 23:57:10 +00005729 // Shuffle operations on MMX not supported.
5730 if (isMMX)
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00005731 return Op;
5732
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005733 // Vector shuffle lowering takes 3 steps:
5734 //
5735 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
5736 // narrowing and commutation of operands should be handled.
5737 // 2) Matching of shuffles with known shuffle masks to x86 target specific
5738 // shuffle nodes.
5739 // 3) Rewriting of unmatched masks into new generic shuffle operations,
5740 // so the shuffle can be broken into other shuffles and the legalizer can
5741 // try the lowering again.
5742 //
5743 // The general ideia is that no vector_shuffle operation should be left to
5744 // be matched during isel, all of them must be converted to a target specific
5745 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00005746
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005747 // Normalize the input vectors. Here splats, zeroed vectors, profitable
5748 // narrowing and commutation of operands should be handled. The actual code
5749 // doesn't include all of those, work in progress...
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005750 SDValue NewOp = NormalizeVectorShuffle(Op, DAG, *this, Subtarget);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005751 if (NewOp.getNode())
5752 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00005753
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005754 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
5755 // unpckh_undef). Only use pshufd if speed is more important than size.
5756 if (OptForSize && X86::isUNPCKL_v_undef_Mask(SVOp))
5757 if (VT != MVT::v2i64 && VT != MVT::v2f64)
David Greenec4db4e52011-02-28 19:06:56 +00005758 return getTargetShuffleNode(getUNPCKLOpcode(VT, getSubtarget()), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005759 if (OptForSize && X86::isUNPCKH_v_undef_Mask(SVOp))
5760 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5761 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00005762
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005763 if (X86::isMOVDDUPMask(SVOp) && HasSSE3 && V2IsUndef &&
Dale Johannesen0488fb62010-09-30 23:57:10 +00005764 RelaxedMayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00005765 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005766
Dale Johannesen0488fb62010-09-30 23:57:10 +00005767 if (X86::isMOVHLPS_v_undef_Mask(SVOp))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005768 return getMOVHighToLow(Op, dl, DAG);
5769
5770 // Use to match splats
5771 if (HasSSE2 && X86::isUNPCKHMask(SVOp) && V2IsUndef &&
5772 (VT == MVT::v2f64 || VT == MVT::v2i64))
5773 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
5774
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005775 if (X86::isPSHUFDMask(SVOp)) {
5776 // The actual implementation will match the mask in the if above and then
5777 // during isel it can match several different instructions, not only pshufd
5778 // as its name says, sad but true, emulate the behavior for now...
5779 if (X86::isMOVDDUPMask(SVOp) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
5780 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
5781
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005782 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
5783
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005784 if (HasSSE2 && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005785 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
5786
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005787 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005788 return getTargetShuffleNode(X86ISD::SHUFPD, dl, VT, V1, V1,
5789 TargetMask, DAG);
5790
5791 if (VT == MVT::v4f32)
5792 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V1, V1,
5793 TargetMask, DAG);
5794 }
Eric Christopherfd179292009-08-27 18:07:15 +00005795
Evan Chengf26ffe92008-05-29 08:22:04 +00005796 // Check if this can be converted into a logical shift.
5797 bool isLeft = false;
5798 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00005799 SDValue ShVal;
Nate Begeman9008ca62009-04-27 18:41:29 +00005800 bool isShift = getSubtarget()->hasSSE2() &&
Evan Chengc3630942009-12-09 21:00:30 +00005801 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00005802 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00005803 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00005804 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00005805 EVT EltVT = VT.getVectorElementType();
5806 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00005807 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005808 }
Eric Christopherfd179292009-08-27 18:07:15 +00005809
Nate Begeman9008ca62009-04-27 18:41:29 +00005810 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005811 if (V1IsUndef)
5812 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00005813 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00005814 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Dale Johannesen0488fb62010-09-30 23:57:10 +00005815 if (!X86::isMOVLPMask(SVOp)) {
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005816 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00005817 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
5818
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005819 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00005820 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
5821 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00005822 }
Eric Christopherfd179292009-08-27 18:07:15 +00005823
Nate Begeman9008ca62009-04-27 18:41:29 +00005824 // FIXME: fold these into legal mask.
Dale Johannesen0488fb62010-09-30 23:57:10 +00005825 if (X86::isMOVLHPSMask(SVOp) && !X86::isUNPCKLMask(SVOp))
5826 return getMOVLowToHigh(Op, dl, DAG, HasSSE2);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005827
Dale Johannesen0488fb62010-09-30 23:57:10 +00005828 if (X86::isMOVHLPSMask(SVOp))
5829 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00005830
Dale Johannesen0488fb62010-09-30 23:57:10 +00005831 if (X86::isMOVSHDUPMask(SVOp) && HasSSE3 && V2IsUndef && NumElems == 4)
5832 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00005833
Dale Johannesen0488fb62010-09-30 23:57:10 +00005834 if (X86::isMOVSLDUPMask(SVOp) && HasSSE3 && V2IsUndef && NumElems == 4)
5835 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00005836
Dale Johannesen0488fb62010-09-30 23:57:10 +00005837 if (X86::isMOVLPMask(SVOp))
5838 return getMOVLP(Op, dl, DAG, HasSSE2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005839
Nate Begeman9008ca62009-04-27 18:41:29 +00005840 if (ShouldXformToMOVHLPS(SVOp) ||
5841 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
5842 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005843
Evan Chengf26ffe92008-05-29 08:22:04 +00005844 if (isShift) {
5845 // No better options. Use a vshl / vsrl.
Dan Gohman8a55ce42009-09-23 21:02:20 +00005846 EVT EltVT = VT.getVectorElementType();
5847 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00005848 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005849 }
Eric Christopherfd179292009-08-27 18:07:15 +00005850
Evan Cheng9eca5e82006-10-25 21:49:50 +00005851 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00005852 // FIXME: This should also accept a bitcast of a splat? Be careful, not
5853 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00005854 V1IsSplat = isSplatVector(V1.getNode());
5855 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00005856
Chris Lattner8a594482007-11-25 00:24:49 +00005857 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00005858 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005859 Op = CommuteVectorShuffle(SVOp, DAG);
5860 SVOp = cast<ShuffleVectorSDNode>(Op);
5861 V1 = SVOp->getOperand(0);
5862 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00005863 std::swap(V1IsSplat, V2IsSplat);
5864 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00005865 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00005866 }
5867
Nate Begeman9008ca62009-04-27 18:41:29 +00005868 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
5869 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00005870 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00005871 return V1;
5872 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
5873 // the instruction selector will not match, so get a canonical MOVL with
5874 // swapped operands to undo the commute.
5875 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00005876 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005877
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005878 if (X86::isUNPCKLMask(SVOp))
David Greenec4db4e52011-02-28 19:06:56 +00005879 return getTargetShuffleNode(getUNPCKLOpcode(VT, getSubtarget()),
5880 dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005881
5882 if (X86::isUNPCKHMask(SVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005883 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00005884
Evan Cheng9bbbb982006-10-25 20:48:19 +00005885 if (V2IsSplat) {
5886 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005887 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00005888 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00005889 SDValue NewMask = NormalizeMask(SVOp, DAG);
5890 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
5891 if (NSVOp != SVOp) {
5892 if (X86::isUNPCKLMask(NSVOp, true)) {
5893 return NewMask;
5894 } else if (X86::isUNPCKHMask(NSVOp, true)) {
5895 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005896 }
5897 }
5898 }
5899
Evan Cheng9eca5e82006-10-25 21:49:50 +00005900 if (Commuted) {
5901 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00005902 // FIXME: this seems wrong.
5903 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
5904 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005905
5906 if (X86::isUNPCKLMask(NewSVOp))
David Greenec4db4e52011-02-28 19:06:56 +00005907 return getTargetShuffleNode(getUNPCKLOpcode(VT, getSubtarget()),
5908 dl, VT, V2, V1, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005909
5910 if (X86::isUNPCKHMask(NewSVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005911 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V2, V1, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00005912 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005913
Nate Begeman9008ca62009-04-27 18:41:29 +00005914 // Normalize the node to match x86 shuffle ops if needed
Dale Johannesen0488fb62010-09-30 23:57:10 +00005915 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
Nate Begeman9008ca62009-04-27 18:41:29 +00005916 return CommuteVectorShuffle(SVOp, DAG);
5917
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00005918 // The checks below are all present in isShuffleMaskLegal, but they are
5919 // inlined here right now to enable us to directly emit target specific
5920 // nodes, and remove one by one until they don't return Op anymore.
5921 SmallVector<int, 16> M;
5922 SVOp->getMask(M);
5923
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00005924 if (isPALIGNRMask(M, VT, HasSSSE3))
5925 return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
5926 X86::getShufflePALIGNRImmediate(SVOp),
5927 DAG);
5928
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00005929 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
5930 SVOp->getSplatIndex() == 0 && V2IsUndef) {
David Greenec4db4e52011-02-28 19:06:56 +00005931 if (VT == MVT::v2f64) {
5932 X86ISD::NodeType Opcode =
5933 getSubtarget()->hasAVX() ? X86ISD::VUNPCKLPD : X86ISD::UNPCKLPD;
5934 return getTargetShuffleNode(Opcode, dl, VT, V1, V1, DAG);
5935 }
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00005936 if (VT == MVT::v2i64)
5937 return getTargetShuffleNode(X86ISD::PUNPCKLQDQ, dl, VT, V1, V1, DAG);
5938 }
5939
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00005940 if (isPSHUFHWMask(M, VT))
5941 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
5942 X86::getShufflePSHUFHWImmediate(SVOp),
5943 DAG);
5944
5945 if (isPSHUFLWMask(M, VT))
5946 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
5947 X86::getShufflePSHUFLWImmediate(SVOp),
5948 DAG);
5949
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00005950 if (isSHUFPMask(M, VT)) {
5951 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
5952 if (VT == MVT::v4f32 || VT == MVT::v4i32)
5953 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V1, V2,
5954 TargetMask, DAG);
5955 if (VT == MVT::v2f64 || VT == MVT::v2i64)
5956 return getTargetShuffleNode(X86ISD::SHUFPD, dl, VT, V1, V2,
5957 TargetMask, DAG);
5958 }
5959
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005960 if (X86::isUNPCKL_v_undef_Mask(SVOp))
5961 if (VT != MVT::v2i64 && VT != MVT::v2f64)
David Greenec4db4e52011-02-28 19:06:56 +00005962 return getTargetShuffleNode(getUNPCKLOpcode(VT, getSubtarget()),
5963 dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005964 if (X86::isUNPCKH_v_undef_Mask(SVOp))
5965 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5966 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
5967
Evan Cheng14b32e12007-12-11 01:46:18 +00005968 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
Owen Anderson825b72b2009-08-11 20:47:22 +00005969 if (VT == MVT::v8i16) {
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005970 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00005971 if (NewOp.getNode())
Evan Cheng14b32e12007-12-11 01:46:18 +00005972 return NewOp;
5973 }
5974
Owen Anderson825b72b2009-08-11 20:47:22 +00005975 if (VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005976 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005977 if (NewOp.getNode())
5978 return NewOp;
5979 }
Eric Christopherfd179292009-08-27 18:07:15 +00005980
Dale Johannesen0488fb62010-09-30 23:57:10 +00005981 // Handle all 4 wide cases with a number of shuffles.
5982 if (NumElems == 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00005983 return LowerVECTOR_SHUFFLE_4wide(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005984
Dan Gohman475871a2008-07-27 21:46:04 +00005985 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005986}
5987
Dan Gohman475871a2008-07-27 21:46:04 +00005988SDValue
5989X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00005990 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005991 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005992 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005993 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005994 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005995 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00005996 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005997 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00005998 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005999 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00006000 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
6001 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
6002 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006003 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6004 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006005 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006006 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00006007 Op.getOperand(0)),
6008 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006009 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006010 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006011 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006012 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006013 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Anderson825b72b2009-08-11 20:47:22 +00006014 } else if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00006015 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
6016 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006017 // result has a single use which is a store or a bitcast to i32. And in
6018 // the case of a store, it's not worth it if the index is a constant 0,
6019 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00006020 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00006021 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00006022 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006023 if ((User->getOpcode() != ISD::STORE ||
6024 (isa<ConstantSDNode>(Op.getOperand(1)) &&
6025 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006026 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00006027 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00006028 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00006029 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006030 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00006031 Op.getOperand(0)),
6032 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006033 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Owen Anderson825b72b2009-08-11 20:47:22 +00006034 } else if (VT == MVT::i32) {
Mon P Wangf0fcdd82009-01-15 21:10:20 +00006035 // ExtractPS works with constant index.
6036 if (isa<ConstantSDNode>(Op.getOperand(1)))
6037 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00006038 }
Dan Gohman475871a2008-07-27 21:46:04 +00006039 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006040}
6041
6042
Dan Gohman475871a2008-07-27 21:46:04 +00006043SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006044X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
6045 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006046 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00006047 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006048
David Greene74a579d2011-02-10 16:57:36 +00006049 SDValue Vec = Op.getOperand(0);
6050 EVT VecVT = Vec.getValueType();
6051
6052 // If this is a 256-bit vector result, first extract the 128-bit
6053 // vector and then extract from the 128-bit vector.
6054 if (VecVT.getSizeInBits() > 128) {
6055 DebugLoc dl = Op.getNode()->getDebugLoc();
6056 unsigned NumElems = VecVT.getVectorNumElements();
6057 SDValue Idx = Op.getOperand(1);
6058
6059 if (!isa<ConstantSDNode>(Idx))
6060 return SDValue();
6061
6062 unsigned ExtractNumElems = NumElems / (VecVT.getSizeInBits() / 128);
6063 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
6064
6065 // Get the 128-bit vector.
6066 bool Upper = IdxVal >= ExtractNumElems;
6067 Vec = Extract128BitVector(Vec, Idx, DAG, dl);
6068
6069 // Extract from it.
6070 SDValue ScaledIdx = Idx;
6071 if (Upper)
6072 ScaledIdx = DAG.getNode(ISD::SUB, dl, Idx.getValueType(), Idx,
6073 DAG.getConstant(ExtractNumElems,
6074 Idx.getValueType()));
6075 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, Op.getValueType(), Vec,
6076 ScaledIdx);
6077 }
6078
6079 assert(Vec.getValueSizeInBits() <= 128 && "Unexpected vector length");
6080
Evan Cheng62a3f152008-03-24 21:52:23 +00006081 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006082 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006083 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00006084 return Res;
6085 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00006086
Owen Andersone50ed302009-08-10 22:56:29 +00006087 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006088 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006089 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006090 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00006091 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006092 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00006093 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006094 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6095 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006096 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006097 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00006098 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006099 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00006100 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00006101 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00006102 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00006103 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00006104 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006105 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006106 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006107 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006108 if (Idx == 0)
6109 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00006110
Evan Cheng0db9fe62006-04-25 20:13:52 +00006111 // SHUFPS the element to the lowest double word, then movss.
Nate Begeman9008ca62009-04-27 18:41:29 +00006112 int Mask[4] = { Idx, -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006113 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006114 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00006115 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00006116 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00006117 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00006118 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00006119 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
6120 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
6121 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006122 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006123 if (Idx == 0)
6124 return Op;
6125
6126 // UNPCKHPD the element to the lowest double word, then movsd.
6127 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
6128 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00006129 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006130 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006131 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00006132 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00006133 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00006134 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006135 }
6136
Dan Gohman475871a2008-07-27 21:46:04 +00006137 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006138}
6139
Dan Gohman475871a2008-07-27 21:46:04 +00006140SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006141X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
6142 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006143 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006144 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006145 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006146
Dan Gohman475871a2008-07-27 21:46:04 +00006147 SDValue N0 = Op.getOperand(0);
6148 SDValue N1 = Op.getOperand(1);
6149 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00006150
Dan Gohman8a55ce42009-09-23 21:02:20 +00006151 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00006152 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00006153 unsigned Opc;
6154 if (VT == MVT::v8i16)
6155 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00006156 else if (VT == MVT::v16i8)
6157 Opc = X86ISD::PINSRB;
6158 else
6159 Opc = X86ISD::PINSRB;
6160
Nate Begeman14d12ca2008-02-11 04:19:36 +00006161 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
6162 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00006163 if (N1.getValueType() != MVT::i32)
6164 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
6165 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006166 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00006167 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00006168 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00006169 // Bits [7:6] of the constant are the source select. This will always be
6170 // zero here. The DAG Combiner may combine an extract_elt index into these
6171 // bits. For example (insert (extract, 3), 2) could be matched by putting
6172 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00006173 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00006174 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00006175 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00006176 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006177 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00006178 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00006179 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00006180 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00006181 } else if (EltVT == MVT::i32 && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00006182 // PINSR* works with constant index.
6183 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00006184 }
Dan Gohman475871a2008-07-27 21:46:04 +00006185 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006186}
6187
Dan Gohman475871a2008-07-27 21:46:04 +00006188SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006189X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006190 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006191 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006192
David Greene6b381262011-02-09 15:32:06 +00006193 DebugLoc dl = Op.getDebugLoc();
6194 SDValue N0 = Op.getOperand(0);
6195 SDValue N1 = Op.getOperand(1);
6196 SDValue N2 = Op.getOperand(2);
6197
6198 // If this is a 256-bit vector result, first insert into a 128-bit
6199 // vector and then insert into the 256-bit vector.
6200 if (VT.getSizeInBits() > 128) {
6201 if (!isa<ConstantSDNode>(N2))
6202 return SDValue();
6203
6204 // Get the 128-bit vector.
6205 unsigned NumElems = VT.getVectorNumElements();
6206 unsigned IdxVal = cast<ConstantSDNode>(N2)->getZExtValue();
6207 bool Upper = IdxVal >= NumElems / 2;
6208
6209 SDValue SubN0 = Extract128BitVector(N0, N2, DAG, dl);
6210
6211 // Insert into it.
6212 SDValue ScaledN2 = N2;
6213 if (Upper)
6214 ScaledN2 = DAG.getNode(ISD::SUB, dl, N2.getValueType(), N2,
Owen Anderson95771af2011-02-25 21:41:48 +00006215 DAG.getConstant(NumElems /
David Greene6b381262011-02-09 15:32:06 +00006216 (VT.getSizeInBits() / 128),
6217 N2.getValueType()));
6218 Op = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, SubN0.getValueType(), SubN0,
6219 N1, ScaledN2);
6220
6221 // Insert the 128-bit vector
6222 // FIXME: Why UNDEF?
6223 return Insert128BitVector(N0, Op, N2, DAG, dl);
6224 }
6225
Nate Begeman14d12ca2008-02-11 04:19:36 +00006226 if (Subtarget->hasSSE41())
6227 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
6228
Dan Gohman8a55ce42009-09-23 21:02:20 +00006229 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00006230 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00006231
Dan Gohman8a55ce42009-09-23 21:02:20 +00006232 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00006233 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
6234 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00006235 if (N1.getValueType() != MVT::i32)
6236 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
6237 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006238 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00006239 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006240 }
Dan Gohman475871a2008-07-27 21:46:04 +00006241 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006242}
6243
Dan Gohman475871a2008-07-27 21:46:04 +00006244SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006245X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) const {
David Greene2fcdfb42011-02-10 23:11:29 +00006246 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006247 DebugLoc dl = Op.getDebugLoc();
David Greene2fcdfb42011-02-10 23:11:29 +00006248 EVT OpVT = Op.getValueType();
6249
6250 // If this is a 256-bit vector result, first insert into a 128-bit
6251 // vector and then insert into the 256-bit vector.
6252 if (OpVT.getSizeInBits() > 128) {
6253 // Insert into a 128-bit vector.
6254 EVT VT128 = EVT::getVectorVT(*Context,
6255 OpVT.getVectorElementType(),
6256 OpVT.getVectorNumElements() / 2);
6257
6258 Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT128, Op.getOperand(0));
6259
6260 // Insert the 128-bit vector.
6261 return Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, OpVT), Op,
6262 DAG.getConstant(0, MVT::i32),
6263 DAG, dl);
6264 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00006265
Chris Lattnerf172ecd2010-07-04 23:07:25 +00006266 if (Op.getValueType() == MVT::v1i64 &&
6267 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00006268 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00006269
Owen Anderson825b72b2009-08-11 20:47:22 +00006270 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Dale Johannesen0488fb62010-09-30 23:57:10 +00006271 assert(Op.getValueType().getSimpleVT().getSizeInBits() == 128 &&
6272 "Expected an SSE type!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006273 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(),
Dale Johannesen0488fb62010-09-30 23:57:10 +00006274 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006275}
6276
David Greene91585092011-01-26 15:38:49 +00006277// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
6278// a simple subregister reference or explicit instructions to grab
6279// upper bits of a vector.
6280SDValue
6281X86TargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
6282 if (Subtarget->hasAVX()) {
David Greenea5f26012011-02-07 19:36:54 +00006283 DebugLoc dl = Op.getNode()->getDebugLoc();
6284 SDValue Vec = Op.getNode()->getOperand(0);
6285 SDValue Idx = Op.getNode()->getOperand(1);
6286
6287 if (Op.getNode()->getValueType(0).getSizeInBits() == 128
6288 && Vec.getNode()->getValueType(0).getSizeInBits() == 256) {
6289 return Extract128BitVector(Vec, Idx, DAG, dl);
6290 }
David Greene91585092011-01-26 15:38:49 +00006291 }
6292 return SDValue();
6293}
6294
David Greenecfe33c42011-01-26 19:13:22 +00006295// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
6296// simple superregister reference or explicit instructions to insert
6297// the upper bits of a vector.
6298SDValue
6299X86TargetLowering::LowerINSERT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
6300 if (Subtarget->hasAVX()) {
6301 DebugLoc dl = Op.getNode()->getDebugLoc();
6302 SDValue Vec = Op.getNode()->getOperand(0);
6303 SDValue SubVec = Op.getNode()->getOperand(1);
6304 SDValue Idx = Op.getNode()->getOperand(2);
6305
6306 if (Op.getNode()->getValueType(0).getSizeInBits() == 256
6307 && SubVec.getNode()->getValueType(0).getSizeInBits() == 128) {
David Greenea5f26012011-02-07 19:36:54 +00006308 return Insert128BitVector(Vec, SubVec, Idx, DAG, dl);
David Greenecfe33c42011-01-26 19:13:22 +00006309 }
6310 }
6311 return SDValue();
6312}
6313
Bill Wendling056292f2008-09-16 21:48:12 +00006314// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
6315// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
6316// one of the above mentioned nodes. It has to be wrapped because otherwise
6317// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
6318// be used to form addressing mode. These wrapped nodes will be selected
6319// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00006320SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006321X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006322 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00006323
Chris Lattner41621a22009-06-26 19:22:52 +00006324 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6325 // global base reg.
6326 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006327 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006328 CodeModel::Model M = getTargetMachine().getCodeModel();
6329
Chris Lattner4f066492009-07-11 20:29:19 +00006330 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006331 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00006332 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00006333 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006334 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00006335 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006336 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00006337
Evan Cheng1606e8e2009-03-13 07:51:59 +00006338 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00006339 CP->getAlignment(),
6340 CP->getOffset(), OpFlag);
6341 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00006342 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006343 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00006344 if (OpFlag) {
6345 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00006346 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006347 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006348 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006349 }
6350
6351 return Result;
6352}
6353
Dan Gohmand858e902010-04-17 15:26:15 +00006354SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00006355 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00006356
Chris Lattner18c59872009-06-27 04:16:01 +00006357 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6358 // global base reg.
6359 unsigned char OpFlag = 0;
6360 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006361 CodeModel::Model M = getTargetMachine().getCodeModel();
6362
Chris Lattner4f066492009-07-11 20:29:19 +00006363 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006364 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00006365 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00006366 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006367 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00006368 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006369 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00006370
Chris Lattner18c59872009-06-27 04:16:01 +00006371 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
6372 OpFlag);
6373 DebugLoc DL = JT->getDebugLoc();
6374 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00006375
Chris Lattner18c59872009-06-27 04:16:01 +00006376 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00006377 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00006378 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6379 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006380 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00006381 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00006382
Chris Lattner18c59872009-06-27 04:16:01 +00006383 return Result;
6384}
6385
6386SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006387X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00006388 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00006389
Chris Lattner18c59872009-06-27 04:16:01 +00006390 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6391 // global base reg.
6392 unsigned char OpFlag = 0;
6393 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006394 CodeModel::Model M = getTargetMachine().getCodeModel();
6395
Chris Lattner4f066492009-07-11 20:29:19 +00006396 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006397 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00006398 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00006399 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006400 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00006401 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006402 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00006403
Chris Lattner18c59872009-06-27 04:16:01 +00006404 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00006405
Chris Lattner18c59872009-06-27 04:16:01 +00006406 DebugLoc DL = Op.getDebugLoc();
6407 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00006408
6409
Chris Lattner18c59872009-06-27 04:16:01 +00006410 // With PIC, the address is actually $g + Offset.
6411 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00006412 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00006413 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6414 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006415 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00006416 Result);
6417 }
Eric Christopherfd179292009-08-27 18:07:15 +00006418
Chris Lattner18c59872009-06-27 04:16:01 +00006419 return Result;
6420}
6421
Dan Gohman475871a2008-07-27 21:46:04 +00006422SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006423X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00006424 // Create the TargetBlockAddressAddress node.
6425 unsigned char OpFlags =
6426 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00006427 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00006428 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +00006429 DebugLoc dl = Op.getDebugLoc();
6430 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
6431 /*isTarget=*/true, OpFlags);
6432
Dan Gohmanf705adb2009-10-30 01:28:02 +00006433 if (Subtarget->isPICStyleRIPRel() &&
6434 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00006435 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
6436 else
6437 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00006438
Dan Gohman29cbade2009-11-20 23:18:13 +00006439 // With PIC, the address is actually $g + Offset.
6440 if (isGlobalRelativeToPICBase(OpFlags)) {
6441 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
6442 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
6443 Result);
6444 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00006445
6446 return Result;
6447}
6448
6449SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00006450X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00006451 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00006452 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00006453 // Create the TargetGlobalAddress node, folding in the constant
6454 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00006455 unsigned char OpFlags =
6456 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006457 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00006458 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006459 if (OpFlags == X86II::MO_NO_FLAG &&
6460 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00006461 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00006462 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00006463 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006464 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00006465 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00006466 }
Eric Christopherfd179292009-08-27 18:07:15 +00006467
Chris Lattner4f066492009-07-11 20:29:19 +00006468 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006469 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00006470 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
6471 else
6472 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00006473
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006474 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00006475 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00006476 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
6477 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006478 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006479 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006480
Chris Lattner36c25012009-07-10 07:34:39 +00006481 // For globals that require a load from a stub to get the address, emit the
6482 // load.
6483 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00006484 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00006485 MachinePointerInfo::getGOT(), false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006486
Dan Gohman6520e202008-10-18 02:06:02 +00006487 // If there was a non-zero offset that we didn't fold, create an explicit
6488 // addition for it.
6489 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00006490 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00006491 DAG.getConstant(Offset, getPointerTy()));
6492
Evan Cheng0db9fe62006-04-25 20:13:52 +00006493 return Result;
6494}
6495
Evan Chengda43bcf2008-09-24 00:05:32 +00006496SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006497X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00006498 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00006499 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006500 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00006501}
6502
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006503static SDValue
6504GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00006505 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerb903bed2009-06-26 21:20:29 +00006506 unsigned char OperandFlags) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006507 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006508 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006509 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00006510 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006511 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00006512 GA->getOffset(),
6513 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006514 if (InFlag) {
6515 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00006516 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006517 } else {
6518 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00006519 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006520 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006521
6522 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00006523 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006524
Rafael Espindola15f1b662009-04-24 12:59:40 +00006525 SDValue Flag = Chain.getValue(1);
6526 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006527}
6528
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006529// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00006530static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006531LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006532 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00006533 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00006534 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
6535 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006536 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006537 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006538 InFlag = Chain.getValue(1);
6539
Chris Lattnerb903bed2009-06-26 21:20:29 +00006540 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006541}
6542
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006543// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00006544static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006545LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006546 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00006547 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
6548 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006549}
6550
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006551// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
6552// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00006553static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006554 const EVT PtrVT, TLSModel::Model model,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00006555 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00006556 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00006557
Chris Lattnerf93b90c2010-09-22 04:39:11 +00006558 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
6559 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
6560 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00006561
Michael J. Spencerec38de22010-10-10 22:04:20 +00006562 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00006563 DAG.getIntPtrConstant(0),
6564 MachinePointerInfo(Ptr), false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00006565
Chris Lattnerb903bed2009-06-26 21:20:29 +00006566 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006567 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
6568 // initialexec.
6569 unsigned WrapperKind = X86ISD::Wrapper;
6570 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00006571 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00006572 } else if (is64Bit) {
6573 assert(model == TLSModel::InitialExec);
6574 OperandFlags = X86II::MO_GOTTPOFF;
6575 WrapperKind = X86ISD::WrapperRIP;
6576 } else {
6577 assert(model == TLSModel::InitialExec);
6578 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00006579 }
Eric Christopherfd179292009-08-27 18:07:15 +00006580
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006581 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
6582 // exec)
Michael J. Spencerec38de22010-10-10 22:04:20 +00006583 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00006584 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00006585 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00006586 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00006587
Rafael Espindola9a580232009-02-27 13:37:18 +00006588 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00006589 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00006590 MachinePointerInfo::getGOT(), false, false, 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00006591
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006592 // The address of the thread local variable is the add of the thread
6593 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00006594 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006595}
6596
Dan Gohman475871a2008-07-27 21:46:04 +00006597SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006598X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00006599
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006600 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00006601 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00006602
Eric Christopher30ef0e52010-06-03 04:07:48 +00006603 if (Subtarget->isTargetELF()) {
6604 // TODO: implement the "local dynamic" model
6605 // TODO: implement the "initial exec"model for pic executables
Michael J. Spencerec38de22010-10-10 22:04:20 +00006606
Eric Christopher30ef0e52010-06-03 04:07:48 +00006607 // If GV is an alias then use the aliasee for determining
6608 // thread-localness.
6609 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
6610 GV = GA->resolveAliasedGlobal(false);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006611
6612 TLSModel::Model model
Eric Christopher30ef0e52010-06-03 04:07:48 +00006613 = getTLSModel(GV, getTargetMachine().getRelocationModel());
Michael J. Spencerec38de22010-10-10 22:04:20 +00006614
Eric Christopher30ef0e52010-06-03 04:07:48 +00006615 switch (model) {
6616 case TLSModel::GeneralDynamic:
6617 case TLSModel::LocalDynamic: // not implemented
6618 if (Subtarget->is64Bit())
6619 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
6620 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Michael J. Spencerec38de22010-10-10 22:04:20 +00006621
Eric Christopher30ef0e52010-06-03 04:07:48 +00006622 case TLSModel::InitialExec:
6623 case TLSModel::LocalExec:
6624 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
6625 Subtarget->is64Bit());
6626 }
6627 } else if (Subtarget->isTargetDarwin()) {
6628 // Darwin only has one model of TLS. Lower to that.
6629 unsigned char OpFlag = 0;
6630 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
6631 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006632
Eric Christopher30ef0e52010-06-03 04:07:48 +00006633 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6634 // global base reg.
6635 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
6636 !Subtarget->is64Bit();
6637 if (PIC32)
6638 OpFlag = X86II::MO_TLVP_PIC_BASE;
6639 else
6640 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006641 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00006642 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00006643 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00006644 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00006645 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006646
Eric Christopher30ef0e52010-06-03 04:07:48 +00006647 // With PIC32, the address is actually $g + Offset.
6648 if (PIC32)
6649 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6650 DAG.getNode(X86ISD::GlobalBaseReg,
6651 DebugLoc(), getPointerTy()),
6652 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006653
Eric Christopher30ef0e52010-06-03 04:07:48 +00006654 // Lowering the machine isd will make sure everything is in the right
6655 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00006656 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006657 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00006658 SDValue Args[] = { Chain, Offset };
6659 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006660
Eric Christopher30ef0e52010-06-03 04:07:48 +00006661 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
6662 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
6663 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00006664
Eric Christopher30ef0e52010-06-03 04:07:48 +00006665 // And our return value (tls address) is in the standard call return value
6666 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00006667 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
6668 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy());
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006669 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00006670
Eric Christopher30ef0e52010-06-03 04:07:48 +00006671 assert(false &&
6672 "TLS not implemented for this target.");
Eric Christopherfd179292009-08-27 18:07:15 +00006673
Torok Edwinc23197a2009-07-14 16:55:14 +00006674 llvm_unreachable("Unreachable");
Chris Lattner5867de12009-04-01 22:14:45 +00006675 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006676}
6677
Evan Cheng0db9fe62006-04-25 20:13:52 +00006678
Nadav Rotem43012222011-05-11 08:12:09 +00006679/// LowerShiftParts - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00006680/// take a 2 x i32 value to shift plus a shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +00006681SDValue X86TargetLowering::LowerShiftParts(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman4c1fa612008-03-03 22:22:09 +00006682 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00006683 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006684 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006685 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006686 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00006687 SDValue ShOpLo = Op.getOperand(0);
6688 SDValue ShOpHi = Op.getOperand(1);
6689 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00006690 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00006691 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00006692 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00006693
Dan Gohman475871a2008-07-27 21:46:04 +00006694 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006695 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00006696 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
6697 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006698 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006699 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
6700 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006701 }
Evan Chenge3413162006-01-09 18:33:28 +00006702
Owen Anderson825b72b2009-08-11 20:47:22 +00006703 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
6704 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00006705 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00006706 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00006707
Dan Gohman475871a2008-07-27 21:46:04 +00006708 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00006709 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00006710 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
6711 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00006712
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006713 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00006714 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
6715 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006716 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006717 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
6718 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006719 }
6720
Dan Gohman475871a2008-07-27 21:46:04 +00006721 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00006722 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006723}
Evan Chenga3195e82006-01-12 22:54:21 +00006724
Dan Gohmand858e902010-04-17 15:26:15 +00006725SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
6726 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006727 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00006728
Dale Johannesen0488fb62010-09-30 23:57:10 +00006729 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00006730 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00006731
Owen Anderson825b72b2009-08-11 20:47:22 +00006732 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00006733 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00006734
Eli Friedman36df4992009-05-27 00:47:34 +00006735 // These are really Legal; return the operand so the caller accepts it as
6736 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00006737 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00006738 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00006739 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00006740 Subtarget->is64Bit()) {
6741 return Op;
6742 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006743
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006744 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006745 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006746 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00006747 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006748 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00006749 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00006750 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006751 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006752 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00006753 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
6754}
Evan Cheng0db9fe62006-04-25 20:13:52 +00006755
Owen Andersone50ed302009-08-10 22:56:29 +00006756SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00006757 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00006758 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006759 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00006760 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00006761 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00006762 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006763 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006764 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00006765 else
Owen Anderson825b72b2009-08-11 20:47:22 +00006766 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006767
Chris Lattner492a43e2010-09-22 01:28:21 +00006768 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006769
Stuart Hastings84be9582011-06-02 15:57:11 +00006770 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(StackSlot);
6771 MachineMemOperand *MMO;
6772 if (FI) {
6773 int SSFI = FI->getIndex();
6774 MMO =
6775 DAG.getMachineFunction()
6776 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6777 MachineMemOperand::MOLoad, ByteSize, ByteSize);
6778 } else {
6779 MMO = cast<LoadSDNode>(StackSlot)->getMemOperand();
6780 StackSlot = StackSlot.getOperand(1);
6781 }
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006782 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00006783 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
6784 X86ISD::FILD, DL,
6785 Tys, Ops, array_lengthof(Ops),
6786 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006787
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006788 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006789 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00006790 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006791
6792 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
6793 // shouldn't be necessary except that RFP cannot be live across
6794 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006795 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00006796 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
6797 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006798 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00006799 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006800 SDValue Ops[] = {
6801 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
6802 };
Chris Lattner492a43e2010-09-22 01:28:21 +00006803 MachineMemOperand *MMO =
6804 DAG.getMachineFunction()
6805 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00006806 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006807
Chris Lattner492a43e2010-09-22 01:28:21 +00006808 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
6809 Ops, array_lengthof(Ops),
6810 Op.getValueType(), MMO);
6811 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006812 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006813 false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006814 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006815
Evan Cheng0db9fe62006-04-25 20:13:52 +00006816 return Result;
6817}
6818
Bill Wendling8b8a6362009-01-17 03:56:04 +00006819// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00006820SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
6821 SelectionDAG &DAG) const {
Bill Wendling8b8a6362009-01-17 03:56:04 +00006822 // This algorithm is not obvious. Here it is in C code, more or less:
6823 /*
6824 double uint64_to_double( uint32_t hi, uint32_t lo ) {
6825 static const __m128i exp = { 0x4330000045300000ULL, 0 };
6826 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00006827
Bill Wendling8b8a6362009-01-17 03:56:04 +00006828 // Copy ints to xmm registers.
6829 __m128i xh = _mm_cvtsi32_si128( hi );
6830 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00006831
Bill Wendling8b8a6362009-01-17 03:56:04 +00006832 // Combine into low half of a single xmm register.
6833 __m128i x = _mm_unpacklo_epi32( xh, xl );
6834 __m128d d;
6835 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00006836
Bill Wendling8b8a6362009-01-17 03:56:04 +00006837 // Merge in appropriate exponents to give the integer bits the right
6838 // magnitude.
6839 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00006840
Bill Wendling8b8a6362009-01-17 03:56:04 +00006841 // Subtract away the biases to deal with the IEEE-754 double precision
6842 // implicit 1.
6843 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00006844
Bill Wendling8b8a6362009-01-17 03:56:04 +00006845 // All conversions up to here are exact. The correctly rounded result is
6846 // calculated using the current rounding mode using the following
6847 // horizontal add.
6848 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
6849 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
6850 // store doesn't really need to be here (except
6851 // maybe to zero the other double)
6852 return sd;
6853 }
6854 */
Dale Johannesen040225f2008-10-21 23:07:49 +00006855
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006856 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00006857 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00006858
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006859 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00006860 std::vector<Constant*> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00006861 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
6862 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
6863 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
6864 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00006865 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006866 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006867
Bill Wendling8b8a6362009-01-17 03:56:04 +00006868 std::vector<Constant*> CV1;
Owen Andersona90b3dc2009-07-15 21:51:10 +00006869 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006870 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
Owen Andersona90b3dc2009-07-15 21:51:10 +00006871 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006872 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00006873 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006874 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006875
Owen Anderson825b72b2009-08-11 20:47:22 +00006876 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6877 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00006878 Op.getOperand(0),
6879 DAG.getIntPtrConstant(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006880 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6881 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00006882 Op.getOperand(0),
6883 DAG.getIntPtrConstant(0)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006884 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
6885 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00006886 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006887 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00006888 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006889 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck2);
Owen Anderson825b72b2009-08-11 20:47:22 +00006890 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00006891 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006892 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00006893 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006894
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006895 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00006896 int ShufMask[2] = { 1, -1 };
Owen Anderson825b72b2009-08-11 20:47:22 +00006897 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
6898 DAG.getUNDEF(MVT::v2f64), ShufMask);
6899 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
6900 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006901 DAG.getIntPtrConstant(0));
6902}
6903
Bill Wendling8b8a6362009-01-17 03:56:04 +00006904// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00006905SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
6906 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006907 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00006908 // FP constant to bias correct the final result.
6909 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00006910 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006911
6912 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00006913 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6914 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Bill Wendling8b8a6362009-01-17 03:56:04 +00006915 Op.getOperand(0),
6916 DAG.getIntPtrConstant(0)));
6917
Owen Anderson825b72b2009-08-11 20:47:22 +00006918 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006919 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00006920 DAG.getIntPtrConstant(0));
6921
6922 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00006923 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006924 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00006925 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006926 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006927 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00006928 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006929 MVT::v2f64, Bias)));
6930 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006931 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00006932 DAG.getIntPtrConstant(0));
6933
6934 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00006935 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006936
6937 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00006938 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00006939
Owen Anderson825b72b2009-08-11 20:47:22 +00006940 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006941 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00006942 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00006943 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006944 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00006945 }
6946
6947 // Handle final rounding.
6948 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00006949}
6950
Dan Gohmand858e902010-04-17 15:26:15 +00006951SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
6952 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00006953 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006954 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00006955
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006956 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00006957 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
6958 // the optimization here.
6959 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00006960 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00006961
Owen Andersone50ed302009-08-10 22:56:29 +00006962 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006963 EVT DstVT = Op.getValueType();
6964 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00006965 return LowerUINT_TO_FP_i64(Op, DAG);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006966 else if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00006967 return LowerUINT_TO_FP_i32(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00006968
6969 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00006970 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006971 if (SrcVT == MVT::i32) {
6972 SDValue WordOff = DAG.getConstant(4, getPointerTy());
6973 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
6974 getPointerTy(), StackSlot, WordOff);
6975 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006976 StackSlot, MachinePointerInfo(),
6977 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006978 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006979 OffsetSlot, MachinePointerInfo(),
6980 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006981 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
6982 return Fild;
6983 }
6984
6985 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
6986 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006987 StackSlot, MachinePointerInfo(),
6988 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006989 // For i64 source, we need to add the appropriate power of 2 if the input
6990 // was negative. This is the same as the optimization in
6991 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
6992 // we must be careful to do the computation in x87 extended precision, not
6993 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00006994 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
6995 MachineMemOperand *MMO =
6996 DAG.getMachineFunction()
6997 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6998 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006999
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007000 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
7001 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007002 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
7003 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007004
7005 APInt FF(32, 0x5F800000ULL);
7006
7007 // Check whether the sign bit is set.
7008 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
7009 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
7010 ISD::SETLT);
7011
7012 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
7013 SDValue FudgePtr = DAG.getConstantPool(
7014 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
7015 getPointerTy());
7016
7017 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
7018 SDValue Zero = DAG.getIntPtrConstant(0);
7019 SDValue Four = DAG.getIntPtrConstant(4);
7020 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
7021 Zero, Four);
7022 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
7023
7024 // Load the value out, extending it from f32 to f80.
7025 // FIXME: Avoid the extend by constructing the right constant pool?
Stuart Hastingsa9011292011-02-16 16:23:55 +00007026 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00007027 FudgePtr, MachinePointerInfo::getConstantPool(),
7028 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007029 // Extend everything to 80 bits to force it to be done on x87.
7030 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
7031 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00007032}
7033
Dan Gohman475871a2008-07-27 21:46:04 +00007034std::pair<SDValue,SDValue> X86TargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00007035FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) const {
Chris Lattner07290932010-09-22 01:05:16 +00007036 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00007037
Owen Andersone50ed302009-08-10 22:56:29 +00007038 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00007039
7040 if (!IsSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007041 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
7042 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00007043 }
7044
Owen Anderson825b72b2009-08-11 20:47:22 +00007045 assert(DstTy.getSimpleVT() <= MVT::i64 &&
7046 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00007047 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00007048
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007049 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007050 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00007051 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00007052 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00007053 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00007054 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00007055 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00007056 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007057
Evan Cheng87c89352007-10-15 20:11:21 +00007058 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
7059 // stack slot.
7060 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00007061 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00007062 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007063 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00007064
Michael J. Spencerec38de22010-10-10 22:04:20 +00007065
7066
Evan Cheng0db9fe62006-04-25 20:13:52 +00007067 unsigned Opc;
Owen Anderson825b72b2009-08-11 20:47:22 +00007068 switch (DstTy.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007069 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson825b72b2009-08-11 20:47:22 +00007070 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
7071 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
7072 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007073 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007074
Dan Gohman475871a2008-07-27 21:46:04 +00007075 SDValue Chain = DAG.getEntryNode();
7076 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00007077 EVT TheVT = Op.getOperand(0).getValueType();
7078 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007079 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00007080 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007081 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007082 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007083 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00007084 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00007085 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00007086 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00007087
Chris Lattner492a43e2010-09-22 01:28:21 +00007088 MachineMemOperand *MMO =
7089 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7090 MachineMemOperand::MOLoad, MemSize, MemSize);
7091 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
7092 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007093 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00007094 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007095 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
7096 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007097
Chris Lattner07290932010-09-22 01:05:16 +00007098 MachineMemOperand *MMO =
7099 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7100 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007101
Evan Cheng0db9fe62006-04-25 20:13:52 +00007102 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00007103 SDValue Ops[] = { Chain, Value, StackSlot };
Chris Lattner07290932010-09-22 01:05:16 +00007104 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
7105 Ops, 3, DstTy, MMO);
Evan Chengd9558e02006-01-06 00:43:03 +00007106
Chris Lattner27a6c732007-11-24 07:07:01 +00007107 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007108}
7109
Dan Gohmand858e902010-04-17 15:26:15 +00007110SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
7111 SelectionDAG &DAG) const {
Dale Johannesen0488fb62010-09-30 23:57:10 +00007112 if (Op.getValueType().isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007113 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007114
Eli Friedman948e95a2009-05-23 09:59:16 +00007115 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00007116 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00007117 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
7118 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00007119
Chris Lattner27a6c732007-11-24 07:07:01 +00007120 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007121 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00007122 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00007123}
7124
Dan Gohmand858e902010-04-17 15:26:15 +00007125SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
7126 SelectionDAG &DAG) const {
Eli Friedman948e95a2009-05-23 09:59:16 +00007127 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
7128 SDValue FIST = Vals.first, StackSlot = Vals.second;
7129 assert(FIST.getNode() && "Unexpected failure");
7130
7131 // Load the result.
7132 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00007133 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007134}
7135
Dan Gohmand858e902010-04-17 15:26:15 +00007136SDValue X86TargetLowering::LowerFABS(SDValue Op,
7137 SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007138 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007139 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007140 EVT VT = Op.getValueType();
7141 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007142 if (VT.isVector())
7143 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007144 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007145 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007146 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00007147 CV.push_back(C);
7148 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007149 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007150 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00007151 CV.push_back(C);
7152 CV.push_back(C);
7153 CV.push_back(C);
7154 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007155 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007156 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007157 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007158 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007159 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007160 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007161 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007162}
7163
Dan Gohmand858e902010-04-17 15:26:15 +00007164SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007165 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007166 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007167 EVT VT = Op.getValueType();
7168 EVT EltVT = VT;
Duncan Sandsda9ad382009-09-06 19:29:07 +00007169 if (VT.isVector())
Duncan Sands83ec4b62008-06-06 12:08:01 +00007170 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007171 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007172 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007173 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00007174 CV.push_back(C);
7175 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007176 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007177 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00007178 CV.push_back(C);
7179 CV.push_back(C);
7180 CV.push_back(C);
7181 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007182 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007183 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007184 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007185 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007186 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007187 false, false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00007188 if (VT.isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007189 return DAG.getNode(ISD::BITCAST, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007190 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007191 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007192 Op.getOperand(0)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007193 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00007194 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007195 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00007196 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00007197}
7198
Dan Gohmand858e902010-04-17 15:26:15 +00007199SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007200 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00007201 SDValue Op0 = Op.getOperand(0);
7202 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007203 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007204 EVT VT = Op.getValueType();
7205 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00007206
7207 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007208 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007209 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00007210 SrcVT = VT;
7211 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007212 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007213 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007214 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007215 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007216 }
7217
7218 // At this point the operands and the result should have the same
7219 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00007220
Evan Cheng68c47cb2007-01-05 07:55:56 +00007221 // First get the sign bit of second operand.
7222 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007223 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007224 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
7225 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007226 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007227 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
7228 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7229 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7230 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007231 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007232 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007233 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007234 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007235 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007236 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007237 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00007238
7239 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007240 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007241 // Op0 is MVT::f32, Op1 is MVT::f64.
7242 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
7243 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
7244 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007245 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00007246 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00007247 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007248 }
7249
Evan Cheng73d6cf12007-01-05 21:37:56 +00007250 // Clear first operand sign bit.
7251 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00007252 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007253 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
7254 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00007255 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007256 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
7257 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7258 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7259 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00007260 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007261 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007262 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007263 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007264 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007265 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007266 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00007267
7268 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00007269 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00007270}
7271
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00007272SDValue X86TargetLowering::LowerFGETSIGN(SDValue Op, SelectionDAG &DAG) const {
7273 SDValue N0 = Op.getOperand(0);
7274 DebugLoc dl = Op.getDebugLoc();
7275 EVT VT = Op.getValueType();
7276
7277 // Lower ISD::FGETSIGN to (AND (X86ISD::FGETSIGNx86 ...) 1).
7278 SDValue xFGETSIGN = DAG.getNode(X86ISD::FGETSIGNx86, dl, VT, N0,
7279 DAG.getConstant(1, VT));
7280 return DAG.getNode(ISD::AND, dl, VT, xFGETSIGN, DAG.getConstant(1, VT));
7281}
7282
Dan Gohman076aee32009-03-04 19:44:21 +00007283/// Emit nodes that will be selected as "test Op0,Op0", or something
7284/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00007285SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00007286 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00007287 DebugLoc dl = Op.getDebugLoc();
7288
Dan Gohman31125812009-03-07 01:58:32 +00007289 // CF and OF aren't always set the way we want. Determine which
7290 // of these we need.
7291 bool NeedCF = false;
7292 bool NeedOF = false;
7293 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007294 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00007295 case X86::COND_A: case X86::COND_AE:
7296 case X86::COND_B: case X86::COND_BE:
7297 NeedCF = true;
7298 break;
7299 case X86::COND_G: case X86::COND_GE:
7300 case X86::COND_L: case X86::COND_LE:
7301 case X86::COND_O: case X86::COND_NO:
7302 NeedOF = true;
7303 break;
Dan Gohman31125812009-03-07 01:58:32 +00007304 }
7305
Dan Gohman076aee32009-03-04 19:44:21 +00007306 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00007307 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
7308 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007309 if (Op.getResNo() != 0 || NeedOF || NeedCF)
7310 // Emit a CMP with 0, which is the TEST pattern.
7311 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
7312 DAG.getConstant(0, Op.getValueType()));
7313
7314 unsigned Opcode = 0;
7315 unsigned NumOperands = 0;
7316 switch (Op.getNode()->getOpcode()) {
7317 case ISD::ADD:
7318 // Due to an isel shortcoming, be conservative if this add is likely to be
7319 // selected as part of a load-modify-store instruction. When the root node
7320 // in a match is a store, isel doesn't know how to remap non-chain non-flag
7321 // uses of other nodes in the match, such as the ADD in this case. This
7322 // leads to the ADD being left around and reselected, with the result being
7323 // two adds in the output. Alas, even if none our users are stores, that
7324 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
7325 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
7326 // climbing the DAG back to the root, and it doesn't seem to be worth the
7327 // effort.
7328 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Dan Gohman076aee32009-03-04 19:44:21 +00007329 UE = Op.getNode()->use_end(); UI != UE; ++UI)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007330 if (UI->getOpcode() != ISD::CopyToReg && UI->getOpcode() != ISD::SETCC)
7331 goto default_case;
7332
7333 if (ConstantSDNode *C =
7334 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
7335 // An add of one will be selected as an INC.
7336 if (C->getAPIntValue() == 1) {
7337 Opcode = X86ISD::INC;
7338 NumOperands = 1;
7339 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00007340 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007341
7342 // An add of negative one (subtract of one) will be selected as a DEC.
7343 if (C->getAPIntValue().isAllOnesValue()) {
7344 Opcode = X86ISD::DEC;
7345 NumOperands = 1;
7346 break;
7347 }
Dan Gohman076aee32009-03-04 19:44:21 +00007348 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007349
7350 // Otherwise use a regular EFLAGS-setting add.
7351 Opcode = X86ISD::ADD;
7352 NumOperands = 2;
7353 break;
7354 case ISD::AND: {
7355 // If the primary and result isn't used, don't bother using X86ISD::AND,
7356 // because a TEST instruction will be better.
7357 bool NonFlagUse = false;
7358 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
7359 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
7360 SDNode *User = *UI;
7361 unsigned UOpNo = UI.getOperandNo();
7362 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
7363 // Look pass truncate.
7364 UOpNo = User->use_begin().getOperandNo();
7365 User = *User->use_begin();
7366 }
7367
7368 if (User->getOpcode() != ISD::BRCOND &&
7369 User->getOpcode() != ISD::SETCC &&
7370 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
7371 NonFlagUse = true;
7372 break;
7373 }
Dan Gohman076aee32009-03-04 19:44:21 +00007374 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007375
7376 if (!NonFlagUse)
7377 break;
7378 }
7379 // FALL THROUGH
7380 case ISD::SUB:
7381 case ISD::OR:
7382 case ISD::XOR:
7383 // Due to the ISEL shortcoming noted above, be conservative if this op is
7384 // likely to be selected as part of a load-modify-store instruction.
7385 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
7386 UE = Op.getNode()->use_end(); UI != UE; ++UI)
7387 if (UI->getOpcode() == ISD::STORE)
7388 goto default_case;
7389
7390 // Otherwise use a regular EFLAGS-setting instruction.
7391 switch (Op.getNode()->getOpcode()) {
7392 default: llvm_unreachable("unexpected operator!");
7393 case ISD::SUB: Opcode = X86ISD::SUB; break;
7394 case ISD::OR: Opcode = X86ISD::OR; break;
7395 case ISD::XOR: Opcode = X86ISD::XOR; break;
7396 case ISD::AND: Opcode = X86ISD::AND; break;
7397 }
7398
7399 NumOperands = 2;
7400 break;
7401 case X86ISD::ADD:
7402 case X86ISD::SUB:
7403 case X86ISD::INC:
7404 case X86ISD::DEC:
7405 case X86ISD::OR:
7406 case X86ISD::XOR:
7407 case X86ISD::AND:
7408 return SDValue(Op.getNode(), 1);
7409 default:
7410 default_case:
7411 break;
Dan Gohman076aee32009-03-04 19:44:21 +00007412 }
7413
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007414 if (Opcode == 0)
7415 // Emit a CMP with 0, which is the TEST pattern.
7416 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
7417 DAG.getConstant(0, Op.getValueType()));
7418
7419 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
7420 SmallVector<SDValue, 4> Ops;
7421 for (unsigned i = 0; i != NumOperands; ++i)
7422 Ops.push_back(Op.getOperand(i));
7423
7424 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
7425 DAG.ReplaceAllUsesWith(Op, New);
7426 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00007427}
7428
7429/// Emit nodes that will be selected as "cmp Op0,Op1", or something
7430/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00007431SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00007432 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00007433 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
7434 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00007435 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00007436
7437 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00007438 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00007439}
7440
Evan Chengd40d03e2010-01-06 19:38:29 +00007441/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
7442/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00007443SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
7444 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00007445 SDValue Op0 = And.getOperand(0);
7446 SDValue Op1 = And.getOperand(1);
7447 if (Op0.getOpcode() == ISD::TRUNCATE)
7448 Op0 = Op0.getOperand(0);
7449 if (Op1.getOpcode() == ISD::TRUNCATE)
7450 Op1 = Op1.getOperand(0);
7451
Evan Chengd40d03e2010-01-06 19:38:29 +00007452 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00007453 if (Op1.getOpcode() == ISD::SHL)
7454 std::swap(Op0, Op1);
7455 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00007456 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
7457 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00007458 // If we looked past a truncate, check that it's only truncating away
7459 // known zeros.
7460 unsigned BitWidth = Op0.getValueSizeInBits();
7461 unsigned AndBitWidth = And.getValueSizeInBits();
7462 if (BitWidth > AndBitWidth) {
7463 APInt Mask = APInt::getAllOnesValue(BitWidth), Zeros, Ones;
7464 DAG.ComputeMaskedBits(Op0, Mask, Zeros, Ones);
7465 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
7466 return SDValue();
7467 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007468 LHS = Op1;
7469 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00007470 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007471 } else if (Op1.getOpcode() == ISD::Constant) {
7472 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
7473 SDValue AndLHS = Op0;
Evan Chengd40d03e2010-01-06 19:38:29 +00007474 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
7475 LHS = AndLHS.getOperand(0);
7476 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00007477 }
Evan Chengd40d03e2010-01-06 19:38:29 +00007478 }
Evan Cheng0488db92007-09-25 01:57:46 +00007479
Evan Chengd40d03e2010-01-06 19:38:29 +00007480 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00007481 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00007482 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00007483 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00007484 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00007485 // Also promote i16 to i32 for performance / code size reason.
7486 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00007487 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00007488 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00007489
Evan Chengd40d03e2010-01-06 19:38:29 +00007490 // If the operand types disagree, extend the shift amount to match. Since
7491 // BT ignores high bits (like shifts) we can use anyextend.
7492 if (LHS.getValueType() != RHS.getValueType())
7493 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00007494
Evan Chengd40d03e2010-01-06 19:38:29 +00007495 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
7496 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
7497 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7498 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00007499 }
7500
Evan Cheng54de3ea2010-01-05 06:52:31 +00007501 return SDValue();
7502}
7503
Dan Gohmand858e902010-04-17 15:26:15 +00007504SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng54de3ea2010-01-05 06:52:31 +00007505 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
7506 SDValue Op0 = Op.getOperand(0);
7507 SDValue Op1 = Op.getOperand(1);
7508 DebugLoc dl = Op.getDebugLoc();
7509 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
7510
7511 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00007512 // Lower (X & (1 << N)) == 0 to BT(X, N).
7513 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
7514 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Andrew Trickf6c39412011-03-23 23:11:02 +00007515 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00007516 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00007517 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00007518 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
7519 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
7520 if (NewSetCC.getNode())
7521 return NewSetCC;
7522 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00007523
Chris Lattner481eebc2010-12-19 21:23:48 +00007524 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
7525 // these.
7526 if (Op1.getOpcode() == ISD::Constant &&
Andrew Trickf6c39412011-03-23 23:11:02 +00007527 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
Evan Cheng2c755ba2010-02-27 07:36:59 +00007528 cast<ConstantSDNode>(Op1)->isNullValue()) &&
7529 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007530
Chris Lattner481eebc2010-12-19 21:23:48 +00007531 // If the input is a setcc, then reuse the input setcc or use a new one with
7532 // the inverted condition.
7533 if (Op0.getOpcode() == X86ISD::SETCC) {
7534 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
7535 bool Invert = (CC == ISD::SETNE) ^
7536 cast<ConstantSDNode>(Op1)->isNullValue();
7537 if (!Invert) return Op0;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007538
Evan Cheng2c755ba2010-02-27 07:36:59 +00007539 CCode = X86::GetOppositeBranchCondition(CCode);
Chris Lattner481eebc2010-12-19 21:23:48 +00007540 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7541 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
7542 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007543 }
7544
Evan Chenge5b51ac2010-04-17 06:13:15 +00007545 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00007546 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00007547 if (X86CC == X86::COND_INVALID)
7548 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00007549
Chris Lattnerc19d1c32010-12-19 22:08:31 +00007550 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00007551 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnerc19d1c32010-12-19 22:08:31 +00007552 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
Evan Cheng0488db92007-09-25 01:57:46 +00007553}
7554
Dan Gohmand858e902010-04-17 15:26:15 +00007555SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00007556 SDValue Cond;
7557 SDValue Op0 = Op.getOperand(0);
7558 SDValue Op1 = Op.getOperand(1);
7559 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00007560 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00007561 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
7562 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007563 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00007564
7565 if (isFP) {
7566 unsigned SSECC = 8;
Owen Andersone50ed302009-08-10 22:56:29 +00007567 EVT VT0 = Op0.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00007568 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
7569 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00007570 bool Swap = false;
7571
7572 switch (SetCCOpcode) {
7573 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00007574 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00007575 case ISD::SETEQ: SSECC = 0; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00007576 case ISD::SETOGT:
Nate Begeman30a0de92008-07-17 16:51:19 +00007577 case ISD::SETGT: Swap = true; // Fallthrough
7578 case ISD::SETLT:
7579 case ISD::SETOLT: SSECC = 1; break;
7580 case ISD::SETOGE:
7581 case ISD::SETGE: Swap = true; // Fallthrough
7582 case ISD::SETLE:
7583 case ISD::SETOLE: SSECC = 2; break;
7584 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00007585 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00007586 case ISD::SETNE: SSECC = 4; break;
7587 case ISD::SETULE: Swap = true;
7588 case ISD::SETUGE: SSECC = 5; break;
7589 case ISD::SETULT: Swap = true;
7590 case ISD::SETUGT: SSECC = 6; break;
7591 case ISD::SETO: SSECC = 7; break;
7592 }
7593 if (Swap)
7594 std::swap(Op0, Op1);
7595
Nate Begemanfb8ead02008-07-25 19:05:58 +00007596 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00007597 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00007598 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00007599 SDValue UNORD, EQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00007600 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
7601 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00007602 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00007603 }
7604 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00007605 SDValue ORD, NEQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00007606 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
7607 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00007608 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00007609 }
Torok Edwinc23197a2009-07-14 16:55:14 +00007610 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00007611 }
7612 // Handle all other FP comparisons here.
Owen Anderson825b72b2009-08-11 20:47:22 +00007613 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00007614 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007615
Nate Begeman30a0de92008-07-17 16:51:19 +00007616 // We are handling one of the integer comparisons here. Since SSE only has
7617 // GT and EQ comparisons for integer, swapping operands and multiple
7618 // operations may be required for some comparisons.
7619 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
7620 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007621
Owen Anderson825b72b2009-08-11 20:47:22 +00007622 switch (VT.getSimpleVT().SimpleTy) {
Nate Begeman30a0de92008-07-17 16:51:19 +00007623 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007624 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007625 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007626 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
7627 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00007628 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007629
Nate Begeman30a0de92008-07-17 16:51:19 +00007630 switch (SetCCOpcode) {
7631 default: break;
7632 case ISD::SETNE: Invert = true;
7633 case ISD::SETEQ: Opc = EQOpc; break;
7634 case ISD::SETLT: Swap = true;
7635 case ISD::SETGT: Opc = GTOpc; break;
7636 case ISD::SETGE: Swap = true;
7637 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
7638 case ISD::SETULT: Swap = true;
7639 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
7640 case ISD::SETUGE: Swap = true;
7641 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
7642 }
7643 if (Swap)
7644 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007645
Nate Begeman30a0de92008-07-17 16:51:19 +00007646 // Since SSE has no unsigned integer comparisons, we need to flip the sign
7647 // bits of the inputs before performing those operations.
7648 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00007649 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00007650 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
7651 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00007652 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00007653 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
7654 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00007655 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
7656 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00007657 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007658
Dale Johannesenace16102009-02-03 19:33:06 +00007659 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00007660
7661 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00007662 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00007663 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00007664
Nate Begeman30a0de92008-07-17 16:51:19 +00007665 return Result;
7666}
Evan Cheng0488db92007-09-25 01:57:46 +00007667
Evan Cheng370e5342008-12-03 08:38:43 +00007668// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00007669static bool isX86LogicalCmp(SDValue Op) {
7670 unsigned Opc = Op.getNode()->getOpcode();
7671 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
7672 return true;
7673 if (Op.getResNo() == 1 &&
7674 (Opc == X86ISD::ADD ||
7675 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00007676 Opc == X86ISD::ADC ||
7677 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00007678 Opc == X86ISD::SMUL ||
7679 Opc == X86ISD::UMUL ||
7680 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00007681 Opc == X86ISD::DEC ||
7682 Opc == X86ISD::OR ||
7683 Opc == X86ISD::XOR ||
7684 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00007685 return true;
7686
Chris Lattner9637d5b2010-12-05 07:49:54 +00007687 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
7688 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007689
Dan Gohman076aee32009-03-04 19:44:21 +00007690 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00007691}
7692
Chris Lattnera2b56002010-12-05 01:23:24 +00007693static bool isZero(SDValue V) {
7694 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
7695 return C && C->isNullValue();
7696}
7697
Chris Lattner96908b12010-12-05 02:00:51 +00007698static bool isAllOnes(SDValue V) {
7699 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
7700 return C && C->isAllOnesValue();
7701}
7702
Dan Gohmand858e902010-04-17 15:26:15 +00007703SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00007704 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007705 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00007706 SDValue Op1 = Op.getOperand(1);
7707 SDValue Op2 = Op.getOperand(2);
7708 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00007709 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00007710
Dan Gohman1a492952009-10-20 16:22:37 +00007711 if (Cond.getOpcode() == ISD::SETCC) {
7712 SDValue NewCond = LowerSETCC(Cond, DAG);
7713 if (NewCond.getNode())
7714 Cond = NewCond;
7715 }
Evan Cheng734503b2006-09-11 02:19:56 +00007716
Chris Lattnera2b56002010-12-05 01:23:24 +00007717 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00007718 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00007719 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00007720 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007721 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00007722 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
7723 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007724 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007725
Chris Lattnera2b56002010-12-05 01:23:24 +00007726 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007727
7728 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00007729 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
7730 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00007731
7732 SDValue CmpOp0 = Cmp.getOperand(0);
7733 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
7734 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007735
Chris Lattner96908b12010-12-05 02:00:51 +00007736 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00007737 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
7738 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007739
Chris Lattner96908b12010-12-05 02:00:51 +00007740 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
7741 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007742
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007743 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00007744 if (N2C == 0 || !N2C->isNullValue())
7745 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
7746 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007747 }
7748 }
7749
Chris Lattnera2b56002010-12-05 01:23:24 +00007750 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00007751 if (Cond.getOpcode() == ISD::AND &&
7752 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
7753 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00007754 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00007755 Cond = Cond.getOperand(0);
7756 }
7757
Evan Cheng3f41d662007-10-08 22:16:29 +00007758 // If condition flag is set by a X86ISD::CMP, then use it as the condition
7759 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00007760 if (Cond.getOpcode() == X86ISD::SETCC ||
7761 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00007762 CC = Cond.getOperand(0);
7763
Dan Gohman475871a2008-07-27 21:46:04 +00007764 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00007765 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00007766 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00007767
Evan Cheng3f41d662007-10-08 22:16:29 +00007768 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007769 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00007770 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00007771 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00007772
Chris Lattnerd1980a52009-03-12 06:52:53 +00007773 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
7774 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00007775 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00007776 addTest = false;
7777 }
7778 }
7779
7780 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007781 // Look pass the truncate.
7782 if (Cond.getOpcode() == ISD::TRUNCATE)
7783 Cond = Cond.getOperand(0);
7784
7785 // We know the result of AND is compared against zero. Try to match
7786 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00007787 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00007788 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00007789 if (NewSetCC.getNode()) {
7790 CC = NewSetCC.getOperand(0);
7791 Cond = NewSetCC.getOperand(1);
7792 addTest = false;
7793 }
7794 }
7795 }
7796
7797 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007798 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00007799 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00007800 }
7801
Benjamin Kramere915ff32010-12-22 23:09:28 +00007802 // a < b ? -1 : 0 -> RES = ~setcc_carry
7803 // a < b ? 0 : -1 -> RES = setcc_carry
7804 // a >= b ? -1 : 0 -> RES = setcc_carry
7805 // a >= b ? 0 : -1 -> RES = ~setcc_carry
7806 if (Cond.getOpcode() == X86ISD::CMP) {
7807 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
7808
7809 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
7810 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
7811 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
7812 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
7813 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
7814 return DAG.getNOT(DL, Res, Res.getValueType());
7815 return Res;
7816 }
7817 }
7818
Evan Cheng0488db92007-09-25 01:57:46 +00007819 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
7820 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007821 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007822 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00007823 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00007824}
7825
Evan Cheng370e5342008-12-03 08:38:43 +00007826// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
7827// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
7828// from the AND / OR.
7829static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
7830 Opc = Op.getOpcode();
7831 if (Opc != ISD::OR && Opc != ISD::AND)
7832 return false;
7833 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
7834 Op.getOperand(0).hasOneUse() &&
7835 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
7836 Op.getOperand(1).hasOneUse());
7837}
7838
Evan Cheng961d6d42009-02-02 08:19:07 +00007839// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
7840// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00007841static bool isXor1OfSetCC(SDValue Op) {
7842 if (Op.getOpcode() != ISD::XOR)
7843 return false;
7844 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
7845 if (N1C && N1C->getAPIntValue() == 1) {
7846 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
7847 Op.getOperand(0).hasOneUse();
7848 }
7849 return false;
7850}
7851
Dan Gohmand858e902010-04-17 15:26:15 +00007852SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00007853 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007854 SDValue Chain = Op.getOperand(0);
7855 SDValue Cond = Op.getOperand(1);
7856 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007857 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00007858 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00007859
Dan Gohman1a492952009-10-20 16:22:37 +00007860 if (Cond.getOpcode() == ISD::SETCC) {
7861 SDValue NewCond = LowerSETCC(Cond, DAG);
7862 if (NewCond.getNode())
7863 Cond = NewCond;
7864 }
Chris Lattnere55484e2008-12-25 05:34:37 +00007865#if 0
7866 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00007867 else if (Cond.getOpcode() == X86ISD::ADD ||
7868 Cond.getOpcode() == X86ISD::SUB ||
7869 Cond.getOpcode() == X86ISD::SMUL ||
7870 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00007871 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00007872#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00007873
Evan Chengad9c0a32009-12-15 00:53:42 +00007874 // Look pass (and (setcc_carry (cmp ...)), 1).
7875 if (Cond.getOpcode() == ISD::AND &&
7876 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
7877 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00007878 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00007879 Cond = Cond.getOperand(0);
7880 }
7881
Evan Cheng3f41d662007-10-08 22:16:29 +00007882 // If condition flag is set by a X86ISD::CMP, then use it as the condition
7883 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00007884 if (Cond.getOpcode() == X86ISD::SETCC ||
7885 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00007886 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007887
Dan Gohman475871a2008-07-27 21:46:04 +00007888 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00007889 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00007890 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00007891 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00007892 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00007893 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00007894 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00007895 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00007896 default: break;
7897 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00007898 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00007899 // These can only come from an arithmetic instruction with overflow,
7900 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00007901 Cond = Cond.getNode()->getOperand(1);
7902 addTest = false;
7903 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00007904 }
Evan Cheng0488db92007-09-25 01:57:46 +00007905 }
Evan Cheng370e5342008-12-03 08:38:43 +00007906 } else {
7907 unsigned CondOpc;
7908 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
7909 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00007910 if (CondOpc == ISD::OR) {
7911 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
7912 // two branches instead of an explicit OR instruction with a
7913 // separate test.
7914 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00007915 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00007916 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007917 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00007918 Chain, Dest, CC, Cmp);
7919 CC = Cond.getOperand(1).getOperand(0);
7920 Cond = Cmp;
7921 addTest = false;
7922 }
7923 } else { // ISD::AND
7924 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
7925 // two branches instead of an explicit AND instruction with a
7926 // separate test. However, we only do this if this block doesn't
7927 // have a fall-through edge, because this requires an explicit
7928 // jmp when the condition is false.
7929 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00007930 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00007931 Op.getNode()->hasOneUse()) {
7932 X86::CondCode CCode =
7933 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
7934 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007935 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00007936 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00007937 // Look for an unconditional branch following this conditional branch.
7938 // We need this because we need to reverse the successors in order
7939 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00007940 if (User->getOpcode() == ISD::BR) {
7941 SDValue FalseBB = User->getOperand(1);
7942 SDNode *NewBR =
7943 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00007944 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00007945 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00007946 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00007947
Dale Johannesene4d209d2009-02-03 20:21:25 +00007948 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00007949 Chain, Dest, CC, Cmp);
7950 X86::CondCode CCode =
7951 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
7952 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007953 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00007954 Cond = Cmp;
7955 addTest = false;
7956 }
7957 }
Dan Gohman279c22e2008-10-21 03:29:32 +00007958 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00007959 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
7960 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
7961 // It should be transformed during dag combiner except when the condition
7962 // is set by a arithmetics with overflow node.
7963 X86::CondCode CCode =
7964 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
7965 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007966 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00007967 Cond = Cond.getOperand(0).getOperand(1);
7968 addTest = false;
Dan Gohman279c22e2008-10-21 03:29:32 +00007969 }
Evan Cheng0488db92007-09-25 01:57:46 +00007970 }
7971
7972 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007973 // Look pass the truncate.
7974 if (Cond.getOpcode() == ISD::TRUNCATE)
7975 Cond = Cond.getOperand(0);
7976
7977 // We know the result of AND is compared against zero. Try to match
7978 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00007979 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007980 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
7981 if (NewSetCC.getNode()) {
7982 CC = NewSetCC.getOperand(0);
7983 Cond = NewSetCC.getOperand(1);
7984 addTest = false;
7985 }
7986 }
7987 }
7988
7989 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007990 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00007991 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00007992 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00007993 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00007994 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00007995}
7996
Anton Korobeynikove060b532007-04-17 19:34:00 +00007997
7998// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
7999// Calls to _alloca is needed to probe the stack when allocating more than 4k
8000// bytes in one go. Touching the stack at 4K increments is necessary to ensure
8001// that the guard pages used by the OS virtual memory manager are allocated in
8002// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00008003SDValue
8004X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008005 SelectionDAG &DAG) const {
Duncan Sands1e1ca0b2010-10-21 16:02:12 +00008006 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows()) &&
Michael J. Spencere9c253e2010-10-21 01:41:01 +00008007 "This should be used only on Windows targets");
NAKAMURA Takumia2e07622011-03-24 07:07:00 +00008008 assert(!Subtarget->isTargetEnvMacho());
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008009 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008010
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008011 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00008012 SDValue Chain = Op.getOperand(0);
8013 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008014 // FIXME: Ensure alignment here
8015
Dan Gohman475871a2008-07-27 21:46:04 +00008016 SDValue Flag;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008017
Owen Anderson825b72b2009-08-11 20:47:22 +00008018 EVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
NAKAMURA Takumia2e07622011-03-24 07:07:00 +00008019 unsigned Reg = (Subtarget->is64Bit() ? X86::RAX : X86::EAX);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008020
NAKAMURA Takumia2e07622011-03-24 07:07:00 +00008021 Chain = DAG.getCopyToReg(Chain, dl, Reg, Size, Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00008022 Flag = Chain.getValue(1);
8023
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008024 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00008025
Michael J. Spencere9c253e2010-10-21 01:41:01 +00008026 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00008027 Flag = Chain.getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008028
Dale Johannesendd64c412009-02-04 00:33:20 +00008029 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008030
Dan Gohman475871a2008-07-27 21:46:04 +00008031 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008032 return DAG.getMergeValues(Ops1, 2, dl);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008033}
8034
Dan Gohmand858e902010-04-17 15:26:15 +00008035SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00008036 MachineFunction &MF = DAG.getMachineFunction();
8037 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
8038
Dan Gohman69de1932008-02-06 22:27:42 +00008039 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00008040 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00008041
Anton Korobeynikove7beda12010-10-03 22:52:07 +00008042 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00008043 // vastart just stores the address of the VarArgsFrameIndex slot into the
8044 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00008045 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
8046 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00008047 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
8048 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008049 }
8050
8051 // __va_list_tag:
8052 // gp_offset (0 - 6 * 8)
8053 // fp_offset (48 - 48 + 8 * 16)
8054 // overflow_arg_area (point to parameters coming in memory).
8055 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00008056 SmallVector<SDValue, 8> MemOps;
8057 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00008058 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00008059 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00008060 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
8061 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008062 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008063 MemOps.push_back(Store);
8064
8065 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00008066 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008067 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008068 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00008069 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
8070 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008071 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008072 MemOps.push_back(Store);
8073
8074 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +00008075 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008076 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00008077 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
8078 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00008079 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
8080 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +00008081 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008082 MemOps.push_back(Store);
8083
8084 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +00008085 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008086 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00008087 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
8088 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00008089 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
8090 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008091 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +00008092 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00008093 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00008094}
8095
Dan Gohmand858e902010-04-17 15:26:15 +00008096SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +00008097 assert(Subtarget->is64Bit() &&
8098 "LowerVAARG only handles 64-bit va_arg!");
8099 assert((Subtarget->isTargetLinux() ||
8100 Subtarget->isTargetDarwin()) &&
8101 "Unhandled target in LowerVAARG");
8102 assert(Op.getNode()->getNumOperands() == 4);
8103 SDValue Chain = Op.getOperand(0);
8104 SDValue SrcPtr = Op.getOperand(1);
8105 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
8106 unsigned Align = Op.getConstantOperandVal(3);
8107 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +00008108
Dan Gohman320afb82010-10-12 18:00:49 +00008109 EVT ArgVT = Op.getNode()->getValueType(0);
8110 const Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
8111 uint32_t ArgSize = getTargetData()->getTypeAllocSize(ArgTy);
8112 uint8_t ArgMode;
8113
8114 // Decide which area this value should be read from.
8115 // TODO: Implement the AMD64 ABI in its entirety. This simple
8116 // selection mechanism works only for the basic types.
8117 if (ArgVT == MVT::f80) {
8118 llvm_unreachable("va_arg for f80 not yet implemented");
8119 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
8120 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
8121 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
8122 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
8123 } else {
8124 llvm_unreachable("Unhandled argument type in LowerVAARG");
8125 }
8126
8127 if (ArgMode == 2) {
8128 // Sanity Check: Make sure using fp_offset makes sense.
Michael J. Spencer87b86652010-10-19 07:32:42 +00008129 assert(!UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +00008130 !(DAG.getMachineFunction()
8131 .getFunction()->hasFnAttr(Attribute::NoImplicitFloat)) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00008132 Subtarget->hasXMM());
Dan Gohman320afb82010-10-12 18:00:49 +00008133 }
8134
8135 // Insert VAARG_64 node into the DAG
8136 // VAARG_64 returns two values: Variable Argument Address, Chain
8137 SmallVector<SDValue, 11> InstOps;
8138 InstOps.push_back(Chain);
8139 InstOps.push_back(SrcPtr);
8140 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
8141 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
8142 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
8143 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
8144 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
8145 VTs, &InstOps[0], InstOps.size(),
8146 MVT::i64,
8147 MachinePointerInfo(SV),
8148 /*Align=*/0,
8149 /*Volatile=*/false,
8150 /*ReadMem=*/true,
8151 /*WriteMem=*/true);
8152 Chain = VAARG.getValue(1);
8153
8154 // Load the next argument and return it
8155 return DAG.getLoad(ArgVT, dl,
8156 Chain,
8157 VAARG,
8158 MachinePointerInfo(),
8159 false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +00008160}
8161
Dan Gohmand858e902010-04-17 15:26:15 +00008162SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) const {
Evan Chengae642192007-03-02 23:16:35 +00008163 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00008164 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00008165 SDValue Chain = Op.getOperand(0);
8166 SDValue DstPtr = Op.getOperand(1);
8167 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00008168 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
8169 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +00008170 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00008171
Chris Lattnere72f2022010-09-21 05:40:29 +00008172 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00008173 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +00008174 false,
Chris Lattnere72f2022010-09-21 05:40:29 +00008175 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +00008176}
8177
Dan Gohman475871a2008-07-27 21:46:04 +00008178SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00008179X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008180 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00008181 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00008182 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00008183 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00008184 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00008185 case Intrinsic::x86_sse_comieq_ss:
8186 case Intrinsic::x86_sse_comilt_ss:
8187 case Intrinsic::x86_sse_comile_ss:
8188 case Intrinsic::x86_sse_comigt_ss:
8189 case Intrinsic::x86_sse_comige_ss:
8190 case Intrinsic::x86_sse_comineq_ss:
8191 case Intrinsic::x86_sse_ucomieq_ss:
8192 case Intrinsic::x86_sse_ucomilt_ss:
8193 case Intrinsic::x86_sse_ucomile_ss:
8194 case Intrinsic::x86_sse_ucomigt_ss:
8195 case Intrinsic::x86_sse_ucomige_ss:
8196 case Intrinsic::x86_sse_ucomineq_ss:
8197 case Intrinsic::x86_sse2_comieq_sd:
8198 case Intrinsic::x86_sse2_comilt_sd:
8199 case Intrinsic::x86_sse2_comile_sd:
8200 case Intrinsic::x86_sse2_comigt_sd:
8201 case Intrinsic::x86_sse2_comige_sd:
8202 case Intrinsic::x86_sse2_comineq_sd:
8203 case Intrinsic::x86_sse2_ucomieq_sd:
8204 case Intrinsic::x86_sse2_ucomilt_sd:
8205 case Intrinsic::x86_sse2_ucomile_sd:
8206 case Intrinsic::x86_sse2_ucomigt_sd:
8207 case Intrinsic::x86_sse2_ucomige_sd:
8208 case Intrinsic::x86_sse2_ucomineq_sd: {
8209 unsigned Opc = 0;
8210 ISD::CondCode CC = ISD::SETCC_INVALID;
8211 switch (IntNo) {
8212 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008213 case Intrinsic::x86_sse_comieq_ss:
8214 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008215 Opc = X86ISD::COMI;
8216 CC = ISD::SETEQ;
8217 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00008218 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008219 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008220 Opc = X86ISD::COMI;
8221 CC = ISD::SETLT;
8222 break;
8223 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008224 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008225 Opc = X86ISD::COMI;
8226 CC = ISD::SETLE;
8227 break;
8228 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008229 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008230 Opc = X86ISD::COMI;
8231 CC = ISD::SETGT;
8232 break;
8233 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008234 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008235 Opc = X86ISD::COMI;
8236 CC = ISD::SETGE;
8237 break;
8238 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008239 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008240 Opc = X86ISD::COMI;
8241 CC = ISD::SETNE;
8242 break;
8243 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008244 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008245 Opc = X86ISD::UCOMI;
8246 CC = ISD::SETEQ;
8247 break;
8248 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008249 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008250 Opc = X86ISD::UCOMI;
8251 CC = ISD::SETLT;
8252 break;
8253 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008254 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008255 Opc = X86ISD::UCOMI;
8256 CC = ISD::SETLE;
8257 break;
8258 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008259 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008260 Opc = X86ISD::UCOMI;
8261 CC = ISD::SETGT;
8262 break;
8263 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008264 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008265 Opc = X86ISD::UCOMI;
8266 CC = ISD::SETGE;
8267 break;
8268 case Intrinsic::x86_sse_ucomineq_ss:
8269 case Intrinsic::x86_sse2_ucomineq_sd:
8270 Opc = X86ISD::UCOMI;
8271 CC = ISD::SETNE;
8272 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00008273 }
Evan Cheng734503b2006-09-11 02:19:56 +00008274
Dan Gohman475871a2008-07-27 21:46:04 +00008275 SDValue LHS = Op.getOperand(1);
8276 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00008277 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00008278 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00008279 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
8280 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8281 DAG.getConstant(X86CC, MVT::i8), Cond);
8282 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00008283 }
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008284 // ptest and testp intrinsics. The intrinsic these come from are designed to
8285 // return an integer value, not just an instruction so lower it to the ptest
8286 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00008287 case Intrinsic::x86_sse41_ptestz:
8288 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008289 case Intrinsic::x86_sse41_ptestnzc:
8290 case Intrinsic::x86_avx_ptestz_256:
8291 case Intrinsic::x86_avx_ptestc_256:
8292 case Intrinsic::x86_avx_ptestnzc_256:
8293 case Intrinsic::x86_avx_vtestz_ps:
8294 case Intrinsic::x86_avx_vtestc_ps:
8295 case Intrinsic::x86_avx_vtestnzc_ps:
8296 case Intrinsic::x86_avx_vtestz_pd:
8297 case Intrinsic::x86_avx_vtestc_pd:
8298 case Intrinsic::x86_avx_vtestnzc_pd:
8299 case Intrinsic::x86_avx_vtestz_ps_256:
8300 case Intrinsic::x86_avx_vtestc_ps_256:
8301 case Intrinsic::x86_avx_vtestnzc_ps_256:
8302 case Intrinsic::x86_avx_vtestz_pd_256:
8303 case Intrinsic::x86_avx_vtestc_pd_256:
8304 case Intrinsic::x86_avx_vtestnzc_pd_256: {
8305 bool IsTestPacked = false;
Eric Christopher71c67532009-07-29 00:28:05 +00008306 unsigned X86CC = 0;
8307 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00008308 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008309 case Intrinsic::x86_avx_vtestz_ps:
8310 case Intrinsic::x86_avx_vtestz_pd:
8311 case Intrinsic::x86_avx_vtestz_ps_256:
8312 case Intrinsic::x86_avx_vtestz_pd_256:
8313 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00008314 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008315 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +00008316 // ZF = 1
8317 X86CC = X86::COND_E;
8318 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008319 case Intrinsic::x86_avx_vtestc_ps:
8320 case Intrinsic::x86_avx_vtestc_pd:
8321 case Intrinsic::x86_avx_vtestc_ps_256:
8322 case Intrinsic::x86_avx_vtestc_pd_256:
8323 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00008324 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008325 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00008326 // CF = 1
8327 X86CC = X86::COND_B;
8328 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008329 case Intrinsic::x86_avx_vtestnzc_ps:
8330 case Intrinsic::x86_avx_vtestnzc_pd:
8331 case Intrinsic::x86_avx_vtestnzc_ps_256:
8332 case Intrinsic::x86_avx_vtestnzc_pd_256:
8333 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +00008334 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008335 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00008336 // ZF and CF = 0
8337 X86CC = X86::COND_A;
8338 break;
8339 }
Eric Christopherfd179292009-08-27 18:07:15 +00008340
Eric Christopher71c67532009-07-29 00:28:05 +00008341 SDValue LHS = Op.getOperand(1);
8342 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008343 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
8344 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00008345 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
8346 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
8347 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00008348 }
Evan Cheng5759f972008-05-04 09:15:50 +00008349
8350 // Fix vector shift instructions where the last operand is a non-immediate
8351 // i32 value.
8352 case Intrinsic::x86_sse2_pslli_w:
8353 case Intrinsic::x86_sse2_pslli_d:
8354 case Intrinsic::x86_sse2_pslli_q:
8355 case Intrinsic::x86_sse2_psrli_w:
8356 case Intrinsic::x86_sse2_psrli_d:
8357 case Intrinsic::x86_sse2_psrli_q:
8358 case Intrinsic::x86_sse2_psrai_w:
8359 case Intrinsic::x86_sse2_psrai_d:
8360 case Intrinsic::x86_mmx_pslli_w:
8361 case Intrinsic::x86_mmx_pslli_d:
8362 case Intrinsic::x86_mmx_pslli_q:
8363 case Intrinsic::x86_mmx_psrli_w:
8364 case Intrinsic::x86_mmx_psrli_d:
8365 case Intrinsic::x86_mmx_psrli_q:
8366 case Intrinsic::x86_mmx_psrai_w:
8367 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00008368 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00008369 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00008370 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00008371
8372 unsigned NewIntNo = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00008373 EVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00008374 switch (IntNo) {
8375 case Intrinsic::x86_sse2_pslli_w:
8376 NewIntNo = Intrinsic::x86_sse2_psll_w;
8377 break;
8378 case Intrinsic::x86_sse2_pslli_d:
8379 NewIntNo = Intrinsic::x86_sse2_psll_d;
8380 break;
8381 case Intrinsic::x86_sse2_pslli_q:
8382 NewIntNo = Intrinsic::x86_sse2_psll_q;
8383 break;
8384 case Intrinsic::x86_sse2_psrli_w:
8385 NewIntNo = Intrinsic::x86_sse2_psrl_w;
8386 break;
8387 case Intrinsic::x86_sse2_psrli_d:
8388 NewIntNo = Intrinsic::x86_sse2_psrl_d;
8389 break;
8390 case Intrinsic::x86_sse2_psrli_q:
8391 NewIntNo = Intrinsic::x86_sse2_psrl_q;
8392 break;
8393 case Intrinsic::x86_sse2_psrai_w:
8394 NewIntNo = Intrinsic::x86_sse2_psra_w;
8395 break;
8396 case Intrinsic::x86_sse2_psrai_d:
8397 NewIntNo = Intrinsic::x86_sse2_psra_d;
8398 break;
8399 default: {
Owen Anderson825b72b2009-08-11 20:47:22 +00008400 ShAmtVT = MVT::v2i32;
Evan Cheng5759f972008-05-04 09:15:50 +00008401 switch (IntNo) {
8402 case Intrinsic::x86_mmx_pslli_w:
8403 NewIntNo = Intrinsic::x86_mmx_psll_w;
8404 break;
8405 case Intrinsic::x86_mmx_pslli_d:
8406 NewIntNo = Intrinsic::x86_mmx_psll_d;
8407 break;
8408 case Intrinsic::x86_mmx_pslli_q:
8409 NewIntNo = Intrinsic::x86_mmx_psll_q;
8410 break;
8411 case Intrinsic::x86_mmx_psrli_w:
8412 NewIntNo = Intrinsic::x86_mmx_psrl_w;
8413 break;
8414 case Intrinsic::x86_mmx_psrli_d:
8415 NewIntNo = Intrinsic::x86_mmx_psrl_d;
8416 break;
8417 case Intrinsic::x86_mmx_psrli_q:
8418 NewIntNo = Intrinsic::x86_mmx_psrl_q;
8419 break;
8420 case Intrinsic::x86_mmx_psrai_w:
8421 NewIntNo = Intrinsic::x86_mmx_psra_w;
8422 break;
8423 case Intrinsic::x86_mmx_psrai_d:
8424 NewIntNo = Intrinsic::x86_mmx_psra_d;
8425 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00008426 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00008427 }
8428 break;
8429 }
8430 }
Mon P Wangefa42202009-09-03 19:56:25 +00008431
8432 // The vector shift intrinsics with scalars uses 32b shift amounts but
8433 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
8434 // to be zero.
8435 SDValue ShOps[4];
8436 ShOps[0] = ShAmt;
8437 ShOps[1] = DAG.getConstant(0, MVT::i32);
8438 if (ShAmtVT == MVT::v4i32) {
8439 ShOps[2] = DAG.getUNDEF(MVT::i32);
8440 ShOps[3] = DAG.getUNDEF(MVT::i32);
8441 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
8442 } else {
8443 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
Dale Johannesen0488fb62010-09-30 23:57:10 +00008444// FIXME this must be lowered to get rid of the invalid type.
Mon P Wangefa42202009-09-03 19:56:25 +00008445 }
8446
Owen Andersone50ed302009-08-10 22:56:29 +00008447 EVT VT = Op.getValueType();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008448 ShAmt = DAG.getNode(ISD::BITCAST, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008449 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008450 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00008451 Op.getOperand(1), ShAmt);
8452 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00008453 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008454}
Evan Cheng72261582005-12-20 06:22:03 +00008455
Dan Gohmand858e902010-04-17 15:26:15 +00008456SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
8457 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +00008458 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
8459 MFI->setReturnAddressIsTaken(true);
8460
Bill Wendling64e87322009-01-16 19:25:27 +00008461 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008462 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00008463
8464 if (Depth > 0) {
8465 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
8466 SDValue Offset =
8467 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00008468 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008469 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00008470 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008471 FrameAddr, Offset),
Chris Lattner51abfe42010-09-21 06:02:19 +00008472 MachinePointerInfo(), false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +00008473 }
8474
8475 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00008476 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00008477 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Chris Lattner51abfe42010-09-21 06:02:19 +00008478 RetAddrFI, MachinePointerInfo(), false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00008479}
8480
Dan Gohmand858e902010-04-17 15:26:15 +00008481SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +00008482 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
8483 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00008484
Owen Andersone50ed302009-08-10 22:56:29 +00008485 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008486 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00008487 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
8488 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00008489 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00008490 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +00008491 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
8492 MachinePointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +00008493 false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00008494 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00008495}
8496
Dan Gohman475871a2008-07-27 21:46:04 +00008497SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008498 SelectionDAG &DAG) const {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00008499 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008500}
8501
Dan Gohmand858e902010-04-17 15:26:15 +00008502SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008503 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00008504 SDValue Chain = Op.getOperand(0);
8505 SDValue Offset = Op.getOperand(1);
8506 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008507 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008508
Dan Gohmand8816272010-08-11 18:14:00 +00008509 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
8510 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
8511 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008512 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008513
Dan Gohmand8816272010-08-11 18:14:00 +00008514 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
8515 DAG.getIntPtrConstant(TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008516 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +00008517 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
8518 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00008519 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008520 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008521
Dale Johannesene4d209d2009-02-03 20:21:25 +00008522 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008523 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008524 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008525}
8526
Dan Gohman475871a2008-07-27 21:46:04 +00008527SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008528 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008529 SDValue Root = Op.getOperand(0);
8530 SDValue Trmp = Op.getOperand(1); // trampoline
8531 SDValue FPtr = Op.getOperand(2); // nested function
8532 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008533 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008534
Dan Gohman69de1932008-02-06 22:27:42 +00008535 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008536
8537 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00008538 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00008539
8540 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +00008541 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
8542 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +00008543
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00008544 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
8545 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00008546
8547 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
8548
8549 // Load the pointer to the nested function into R11.
8550 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00008551 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00008552 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008553 Addr, MachinePointerInfo(TrmpAddr),
8554 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008555
Owen Anderson825b72b2009-08-11 20:47:22 +00008556 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8557 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008558 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
8559 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +00008560 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00008561
8562 // Load the 'nest' parameter value into R10.
8563 // R10 is specified in X86CallingConv.td
8564 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +00008565 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8566 DAG.getConstant(10, MVT::i64));
8567 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008568 Addr, MachinePointerInfo(TrmpAddr, 10),
8569 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008570
Owen Anderson825b72b2009-08-11 20:47:22 +00008571 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8572 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008573 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
8574 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +00008575 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00008576
8577 // Jump to the nested function.
8578 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +00008579 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8580 DAG.getConstant(20, MVT::i64));
8581 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008582 Addr, MachinePointerInfo(TrmpAddr, 20),
8583 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008584
8585 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +00008586 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8587 DAG.getConstant(22, MVT::i64));
8588 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00008589 MachinePointerInfo(TrmpAddr, 22),
8590 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008591
Dan Gohman475871a2008-07-27 21:46:04 +00008592 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00008593 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008594 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008595 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00008596 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00008597 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00008598 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00008599 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008600
8601 switch (CC) {
8602 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00008603 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00008604 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00008605 case CallingConv::X86_StdCall: {
8606 // Pass 'nest' parameter in ECX.
8607 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00008608 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008609
8610 // Check that ECX wasn't needed by an 'inreg' parameter.
8611 const FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00008612 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008613
Chris Lattner58d74912008-03-12 17:45:29 +00008614 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00008615 unsigned InRegCount = 0;
8616 unsigned Idx = 1;
8617
8618 for (FunctionType::param_iterator I = FTy->param_begin(),
8619 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00008620 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00008621 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00008622 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008623
8624 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +00008625 report_fatal_error("Nest register in use - reduce number of inreg"
8626 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00008627 }
8628 }
8629 break;
8630 }
8631 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +00008632 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00008633 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00008634 // Pass 'nest' parameter in EAX.
8635 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00008636 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008637 break;
8638 }
8639
Dan Gohman475871a2008-07-27 21:46:04 +00008640 SDValue OutChains[4];
8641 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008642
Owen Anderson825b72b2009-08-11 20:47:22 +00008643 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8644 DAG.getConstant(10, MVT::i32));
8645 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008646
Chris Lattnera62fe662010-02-05 19:20:30 +00008647 // This is storing the opcode for MOV32ri.
8648 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00008649 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00008650 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008651 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008652 Trmp, MachinePointerInfo(TrmpAddr),
8653 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008654
Owen Anderson825b72b2009-08-11 20:47:22 +00008655 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8656 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008657 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
8658 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +00008659 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008660
Chris Lattnera62fe662010-02-05 19:20:30 +00008661 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +00008662 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8663 DAG.getConstant(5, MVT::i32));
8664 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00008665 MachinePointerInfo(TrmpAddr, 5),
8666 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008667
Owen Anderson825b72b2009-08-11 20:47:22 +00008668 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8669 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008670 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
8671 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +00008672 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008673
Dan Gohman475871a2008-07-27 21:46:04 +00008674 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00008675 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008676 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008677 }
8678}
8679
Dan Gohmand858e902010-04-17 15:26:15 +00008680SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
8681 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008682 /*
8683 The rounding mode is in bits 11:10 of FPSR, and has the following
8684 settings:
8685 00 Round to nearest
8686 01 Round to -inf
8687 10 Round to +inf
8688 11 Round to 0
8689
8690 FLT_ROUNDS, on the other hand, expects the following:
8691 -1 Undefined
8692 0 Round to 0
8693 1 Round to nearest
8694 2 Round to +inf
8695 3 Round to -inf
8696
8697 To perform the conversion, we do:
8698 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
8699 */
8700
8701 MachineFunction &MF = DAG.getMachineFunction();
8702 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00008703 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008704 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00008705 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +00008706 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008707
8708 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00008709 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +00008710 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008711
Michael J. Spencerec38de22010-10-10 22:04:20 +00008712
Chris Lattner2156b792010-09-22 01:11:26 +00008713 MachineMemOperand *MMO =
8714 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8715 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008716
Chris Lattner2156b792010-09-22 01:11:26 +00008717 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
8718 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
8719 DAG.getVTList(MVT::Other),
8720 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008721
8722 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +00008723 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Chris Lattner51abfe42010-09-21 06:02:19 +00008724 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008725
8726 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00008727 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +00008728 DAG.getNode(ISD::SRL, DL, MVT::i16,
8729 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00008730 CWD, DAG.getConstant(0x800, MVT::i16)),
8731 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00008732 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +00008733 DAG.getNode(ISD::SRL, DL, MVT::i16,
8734 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00008735 CWD, DAG.getConstant(0x400, MVT::i16)),
8736 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008737
Dan Gohman475871a2008-07-27 21:46:04 +00008738 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +00008739 DAG.getNode(ISD::AND, DL, MVT::i16,
8740 DAG.getNode(ISD::ADD, DL, MVT::i16,
8741 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +00008742 DAG.getConstant(1, MVT::i16)),
8743 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008744
8745
Duncan Sands83ec4b62008-06-06 12:08:01 +00008746 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +00008747 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008748}
8749
Dan Gohmand858e902010-04-17 15:26:15 +00008750SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008751 EVT VT = Op.getValueType();
8752 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008753 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008754 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00008755
8756 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008757 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00008758 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +00008759 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00008760 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008761 }
Evan Cheng18efe262007-12-14 02:13:44 +00008762
Evan Cheng152804e2007-12-14 08:30:15 +00008763 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008764 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008765 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00008766
8767 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00008768 SDValue Ops[] = {
8769 Op,
8770 DAG.getConstant(NumBits+NumBits-1, OpVT),
8771 DAG.getConstant(X86::COND_E, MVT::i8),
8772 Op.getValue(1)
8773 };
8774 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00008775
8776 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00008777 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00008778
Owen Anderson825b72b2009-08-11 20:47:22 +00008779 if (VT == MVT::i8)
8780 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008781 return Op;
8782}
8783
Dan Gohmand858e902010-04-17 15:26:15 +00008784SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008785 EVT VT = Op.getValueType();
8786 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008787 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008788 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00008789
8790 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008791 if (VT == MVT::i8) {
8792 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00008793 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008794 }
Evan Cheng152804e2007-12-14 08:30:15 +00008795
8796 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008797 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008798 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00008799
8800 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00008801 SDValue Ops[] = {
8802 Op,
8803 DAG.getConstant(NumBits, OpVT),
8804 DAG.getConstant(X86::COND_E, MVT::i8),
8805 Op.getValue(1)
8806 };
8807 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00008808
Owen Anderson825b72b2009-08-11 20:47:22 +00008809 if (VT == MVT::i8)
8810 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008811 return Op;
8812}
8813
Dan Gohmand858e902010-04-17 15:26:15 +00008814SDValue X86TargetLowering::LowerMUL_V2I64(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008815 EVT VT = Op.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00008816 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008817 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00008818
Mon P Wangaf9b9522008-12-18 21:42:19 +00008819 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
8820 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
8821 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
8822 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
8823 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
8824 //
8825 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
8826 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
8827 // return AloBlo + AloBhi + AhiBlo;
8828
8829 SDValue A = Op.getOperand(0);
8830 SDValue B = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008831
Dale Johannesene4d209d2009-02-03 20:21:25 +00008832 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008833 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8834 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008835 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008836 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8837 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008838 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008839 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008840 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008841 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008842 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008843 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008844 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008845 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008846 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008847 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008848 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8849 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008850 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008851 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8852 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008853 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
8854 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +00008855 return Res;
8856}
8857
Nadav Rotem43012222011-05-11 08:12:09 +00008858SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
8859
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008860 EVT VT = Op.getValueType();
8861 DebugLoc dl = Op.getDebugLoc();
8862 SDValue R = Op.getOperand(0);
Nadav Rotem43012222011-05-11 08:12:09 +00008863 SDValue Amt = Op.getOperand(1);
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008864
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008865 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008866
Nadav Rotem43012222011-05-11 08:12:09 +00008867 // Must have SSE2.
8868 if (!Subtarget->hasSSE2()) return SDValue();
Nate Begeman51409212010-07-28 00:21:48 +00008869
Nadav Rotem43012222011-05-11 08:12:09 +00008870 // Optimize shl/srl/sra with constant shift amount.
8871 if (isSplatVector(Amt.getNode())) {
8872 SDValue SclrAmt = Amt->getOperand(0);
8873 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SclrAmt)) {
8874 uint64_t ShiftAmt = C->getZExtValue();
8875
8876 if (VT == MVT::v2i64 && Op.getOpcode() == ISD::SHL)
8877 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8878 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8879 R, DAG.getConstant(ShiftAmt, MVT::i32));
8880
8881 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SHL)
8882 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8883 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
8884 R, DAG.getConstant(ShiftAmt, MVT::i32));
8885
8886 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SHL)
8887 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8888 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
8889 R, DAG.getConstant(ShiftAmt, MVT::i32));
8890
8891 if (VT == MVT::v2i64 && Op.getOpcode() == ISD::SRL)
8892 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8893 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8894 R, DAG.getConstant(ShiftAmt, MVT::i32));
8895
8896 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SRL)
8897 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8898 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
8899 R, DAG.getConstant(ShiftAmt, MVT::i32));
8900
8901 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SRL)
8902 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8903 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
8904 R, DAG.getConstant(ShiftAmt, MVT::i32));
8905
8906 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SRA)
8907 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8908 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
8909 R, DAG.getConstant(ShiftAmt, MVT::i32));
8910
8911 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SRA)
8912 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8913 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
8914 R, DAG.getConstant(ShiftAmt, MVT::i32));
8915 }
8916 }
8917
8918 // Lower SHL with variable shift amount.
8919 // Cannot lower SHL without SSE4.1 or later.
8920 if (!Subtarget->hasSSE41()) return SDValue();
8921
8922 if (VT == MVT::v4i32 && Op->getOpcode() == ISD::SHL) {
Nate Begeman51409212010-07-28 00:21:48 +00008923 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8924 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
8925 Op.getOperand(1), DAG.getConstant(23, MVT::i32));
8926
8927 ConstantInt *CI = ConstantInt::get(*Context, APInt(32, 0x3f800000U));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008928
Nate Begeman51409212010-07-28 00:21:48 +00008929 std::vector<Constant*> CV(4, CI);
8930 Constant *C = ConstantVector::get(CV);
8931 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8932 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008933 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00008934 false, false, 16);
8935
8936 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008937 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +00008938 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
8939 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
8940 }
Nadav Rotem43012222011-05-11 08:12:09 +00008941 if (VT == MVT::v16i8 && Op->getOpcode() == ISD::SHL) {
Nate Begeman51409212010-07-28 00:21:48 +00008942 // a = a << 5;
8943 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8944 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
8945 Op.getOperand(1), DAG.getConstant(5, MVT::i32));
8946
8947 ConstantInt *CM1 = ConstantInt::get(*Context, APInt(8, 15));
8948 ConstantInt *CM2 = ConstantInt::get(*Context, APInt(8, 63));
8949
8950 std::vector<Constant*> CVM1(16, CM1);
8951 std::vector<Constant*> CVM2(16, CM2);
8952 Constant *C = ConstantVector::get(CVM1);
8953 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8954 SDValue M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008955 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00008956 false, false, 16);
8957
8958 // r = pblendv(r, psllw(r & (char16)15, 4), a);
8959 M = DAG.getNode(ISD::AND, dl, VT, R, M);
8960 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8961 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
8962 DAG.getConstant(4, MVT::i32));
Nate Begeman672fb622010-12-20 22:04:24 +00008963 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT, R, M, Op);
Nate Begeman51409212010-07-28 00:21:48 +00008964 // a += a
8965 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008966
Nate Begeman51409212010-07-28 00:21:48 +00008967 C = ConstantVector::get(CVM2);
8968 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8969 M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008970 MachinePointerInfo::getConstantPool(),
Chris Lattner51abfe42010-09-21 06:02:19 +00008971 false, false, 16);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008972
Nate Begeman51409212010-07-28 00:21:48 +00008973 // r = pblendv(r, psllw(r & (char16)63, 2), a);
8974 M = DAG.getNode(ISD::AND, dl, VT, R, M);
8975 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8976 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
8977 DAG.getConstant(2, MVT::i32));
Nate Begeman672fb622010-12-20 22:04:24 +00008978 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT, R, M, Op);
Nate Begeman51409212010-07-28 00:21:48 +00008979 // a += a
8980 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008981
Nate Begeman51409212010-07-28 00:21:48 +00008982 // return pblendv(r, r+r, a);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008983 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT,
Nate Begeman51409212010-07-28 00:21:48 +00008984 R, DAG.getNode(ISD::ADD, dl, VT, R, R), Op);
8985 return R;
8986 }
8987 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008988}
Mon P Wangaf9b9522008-12-18 21:42:19 +00008989
Dan Gohmand858e902010-04-17 15:26:15 +00008990SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
Bill Wendling74c37652008-12-09 22:08:41 +00008991 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
8992 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +00008993 // looks for this combo and may remove the "setcc" instruction if the "setcc"
8994 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +00008995 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +00008996 SDValue LHS = N->getOperand(0);
8997 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +00008998 unsigned BaseOp = 0;
8999 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +00009000 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +00009001 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00009002 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +00009003 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +00009004 // A subtract of one will be selected as a INC. Note that INC doesn't
9005 // set CF, so we can't do this for UADDO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +00009006 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
9007 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +00009008 BaseOp = X86ISD::INC;
9009 Cond = X86::COND_O;
9010 break;
9011 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00009012 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +00009013 Cond = X86::COND_O;
9014 break;
9015 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00009016 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +00009017 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00009018 break;
9019 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +00009020 // A subtract of one will be selected as a DEC. Note that DEC doesn't
9021 // set CF, so we can't do this for USUBO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +00009022 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
9023 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +00009024 BaseOp = X86ISD::DEC;
9025 Cond = X86::COND_O;
9026 break;
9027 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00009028 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +00009029 Cond = X86::COND_O;
9030 break;
9031 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00009032 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +00009033 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00009034 break;
9035 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00009036 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +00009037 Cond = X86::COND_O;
9038 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +00009039 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
9040 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
9041 MVT::i32);
9042 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009043
Chris Lattnerb20e0b12010-12-05 07:30:36 +00009044 SDValue SetCC =
9045 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
9046 DAG.getConstant(X86::COND_O, MVT::i32),
9047 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009048
Chris Lattnerb20e0b12010-12-05 07:30:36 +00009049 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
9050 return Sum;
9051 }
Bill Wendling74c37652008-12-09 22:08:41 +00009052 }
Bill Wendling3fafd932008-11-26 22:37:40 +00009053
Bill Wendling61edeb52008-12-02 01:06:39 +00009054 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00009055 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +00009056 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +00009057
Bill Wendling61edeb52008-12-02 01:06:39 +00009058 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +00009059 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
9060 DAG.getConstant(Cond, MVT::i32),
9061 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +00009062
Bill Wendling61edeb52008-12-02 01:06:39 +00009063 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
9064 return Sum;
Bill Wendling41ea7e72008-11-24 19:21:46 +00009065}
9066
Eric Christopher9a9d2752010-07-22 02:48:34 +00009067SDValue X86TargetLowering::LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG) const{
9068 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00009069
Eric Christopherb6729dc2010-08-04 23:03:04 +00009070 if (!Subtarget->hasSSE2()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +00009071 SDValue Chain = Op.getOperand(0);
Michael J. Spencerec38de22010-10-10 22:04:20 +00009072 SDValue Zero = DAG.getConstant(0,
Eric Christopherb6729dc2010-08-04 23:03:04 +00009073 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +00009074 SDValue Ops[] = {
9075 DAG.getRegister(X86::ESP, MVT::i32), // Base
9076 DAG.getTargetConstant(1, MVT::i8), // Scale
9077 DAG.getRegister(0, MVT::i32), // Index
9078 DAG.getTargetConstant(0, MVT::i32), // Disp
9079 DAG.getRegister(0, MVT::i32), // Segment.
9080 Zero,
9081 Chain
9082 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00009083 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +00009084 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
9085 array_lengthof(Ops));
9086 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +00009087 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00009088
Eric Christopher9a9d2752010-07-22 02:48:34 +00009089 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +00009090 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +00009091 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009092
Chris Lattner132929a2010-08-14 17:26:09 +00009093 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
9094 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
9095 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
9096 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +00009097
Chris Lattner132929a2010-08-14 17:26:09 +00009098 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
9099 if (!Op1 && !Op2 && !Op3 && Op4)
9100 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009101
Chris Lattner132929a2010-08-14 17:26:09 +00009102 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
9103 if (Op1 && !Op2 && !Op3 && !Op4)
9104 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009105
9106 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +00009107 // (MFENCE)>;
9108 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +00009109}
9110
Dan Gohmand858e902010-04-17 15:26:15 +00009111SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009112 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +00009113 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +00009114 unsigned Reg = 0;
9115 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00009116 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00009117 default:
9118 assert(false && "Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +00009119 case MVT::i8: Reg = X86::AL; size = 1; break;
9120 case MVT::i16: Reg = X86::AX; size = 2; break;
9121 case MVT::i32: Reg = X86::EAX; size = 4; break;
9122 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +00009123 assert(Subtarget->is64Bit() && "Node not type legal!");
9124 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00009125 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00009126 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +00009127 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +00009128 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +00009129 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +00009130 Op.getOperand(1),
9131 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +00009132 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +00009133 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009134 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +00009135 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
9136 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
9137 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +00009138 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +00009139 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +00009140 return cpOut;
9141}
9142
Duncan Sands1607f052008-12-01 11:39:25 +00009143SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00009144 SelectionDAG &DAG) const {
Duncan Sands1607f052008-12-01 11:39:25 +00009145 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009146 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +00009147 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009148 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +00009149 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00009150 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
9151 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +00009152 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +00009153 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
9154 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +00009155 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +00009156 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +00009157 rdx.getValue(1)
9158 };
Dale Johannesene4d209d2009-02-03 20:21:25 +00009159 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009160}
9161
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009162SDValue X86TargetLowering::LowerBITCAST(SDValue Op,
Dale Johannesen7d07b482010-05-21 00:52:33 +00009163 SelectionDAG &DAG) const {
9164 EVT SrcVT = Op.getOperand(0).getValueType();
9165 EVT DstVT = Op.getValueType();
Chris Lattner2a786eb2010-12-19 20:19:20 +00009166 assert(Subtarget->is64Bit() && !Subtarget->hasSSE2() &&
9167 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +00009168 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +00009169 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009170 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +00009171 // i64 <=> MMX conversions are Legal.
9172 if (SrcVT==MVT::i64 && DstVT.isVector())
9173 return Op;
9174 if (DstVT==MVT::i64 && SrcVT.isVector())
9175 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +00009176 // MMX <=> MMX conversions are Legal.
9177 if (SrcVT.isVector() && DstVT.isVector())
9178 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +00009179 // All other conversions need to be expanded.
9180 return SDValue();
9181}
Chris Lattner5b856542010-12-20 00:59:46 +00009182
Dan Gohmand858e902010-04-17 15:26:15 +00009183SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen71d1bf52008-09-29 22:25:26 +00009184 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +00009185 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00009186 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009187 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +00009188 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009189 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009190 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +00009191 Node->getOperand(0),
9192 Node->getOperand(1), negOp,
9193 cast<AtomicSDNode>(Node)->getSrcValue(),
9194 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang63307c32008-05-05 19:05:59 +00009195}
9196
Chris Lattner5b856542010-12-20 00:59:46 +00009197static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
9198 EVT VT = Op.getNode()->getValueType(0);
9199
9200 // Let legalize expand this if it isn't a legal type yet.
9201 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
9202 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009203
Chris Lattner5b856542010-12-20 00:59:46 +00009204 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009205
Chris Lattner5b856542010-12-20 00:59:46 +00009206 unsigned Opc;
9207 bool ExtraOp = false;
9208 switch (Op.getOpcode()) {
9209 default: assert(0 && "Invalid code");
9210 case ISD::ADDC: Opc = X86ISD::ADD; break;
9211 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
9212 case ISD::SUBC: Opc = X86ISD::SUB; break;
9213 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
9214 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009215
Chris Lattner5b856542010-12-20 00:59:46 +00009216 if (!ExtraOp)
9217 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
9218 Op.getOperand(1));
9219 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
9220 Op.getOperand(1), Op.getOperand(2));
9221}
9222
Evan Cheng0db9fe62006-04-25 20:13:52 +00009223/// LowerOperation - Provide custom lowering hooks for some operations.
9224///
Dan Gohmand858e902010-04-17 15:26:15 +00009225SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00009226 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00009227 default: llvm_unreachable("Should not custom lower this!");
Eric Christopher9a9d2752010-07-22 02:48:34 +00009228 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op,DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009229 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
9230 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009231 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00009232 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009233 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
9234 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
9235 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
David Greene91585092011-01-26 15:38:49 +00009236 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
David Greenecfe33c42011-01-26 19:13:22 +00009237 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009238 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
9239 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
9240 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00009241 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +00009242 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +00009243 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009244 case ISD::SHL_PARTS:
9245 case ISD::SRA_PARTS:
Nadav Rotem43012222011-05-11 08:12:09 +00009246 case ISD::SRL_PARTS: return LowerShiftParts(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009247 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00009248 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009249 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00009250 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009251 case ISD::FABS: return LowerFABS(Op, DAG);
9252 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00009253 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00009254 case ISD::FGETSIGN: return LowerFGETSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00009255 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman30a0de92008-07-17 16:51:19 +00009256 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00009257 case ISD::SELECT: return LowerSELECT(Op, DAG);
9258 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009259 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009260 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +00009261 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +00009262 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009263 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00009264 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
9265 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009266 case ISD::FRAME_TO_ARGS_OFFSET:
9267 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009268 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009269 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009270 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00009271 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +00009272 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
9273 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Mon P Wangaf9b9522008-12-18 21:42:19 +00009274 case ISD::MUL: return LowerMUL_V2I64(Op, DAG);
Nadav Rotem43012222011-05-11 08:12:09 +00009275 case ISD::SRA:
9276 case ISD::SRL:
9277 case ISD::SHL: return LowerShift(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +00009278 case ISD::SADDO:
9279 case ISD::UADDO:
9280 case ISD::SSUBO:
9281 case ISD::USUBO:
9282 case ISD::SMULO:
9283 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00009284 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009285 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +00009286 case ISD::ADDC:
9287 case ISD::ADDE:
9288 case ISD::SUBC:
9289 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009290 }
Chris Lattner27a6c732007-11-24 07:07:01 +00009291}
9292
Duncan Sands1607f052008-12-01 11:39:25 +00009293void X86TargetLowering::
9294ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00009295 SelectionDAG &DAG, unsigned NewOp) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009296 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009297 DebugLoc dl = Node->getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00009298 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +00009299
9300 SDValue Chain = Node->getOperand(0);
9301 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00009302 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00009303 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00009304 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00009305 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +00009306 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +00009307 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +00009308 SDValue Result =
9309 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
9310 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +00009311 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +00009312 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00009313 Results.push_back(Result.getValue(2));
9314}
9315
Duncan Sands126d9072008-07-04 11:47:58 +00009316/// ReplaceNodeResults - Replace a node with an illegal result type
9317/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00009318void X86TargetLowering::ReplaceNodeResults(SDNode *N,
9319 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00009320 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +00009321 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +00009322 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +00009323 default:
Duncan Sands1607f052008-12-01 11:39:25 +00009324 assert(false && "Do not know how to custom type legalize this operation!");
9325 return;
Chris Lattner5b856542010-12-20 00:59:46 +00009326 case ISD::ADDC:
9327 case ISD::ADDE:
9328 case ISD::SUBC:
9329 case ISD::SUBE:
9330 // We don't want to expand or promote these.
9331 return;
Duncan Sands1607f052008-12-01 11:39:25 +00009332 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +00009333 std::pair<SDValue,SDValue> Vals =
9334 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +00009335 SDValue FIST = Vals.first, StackSlot = Vals.second;
9336 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +00009337 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +00009338 // Return a load from the stack slot.
Chris Lattner51abfe42010-09-21 06:02:19 +00009339 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
9340 MachinePointerInfo(), false, false, 0));
Duncan Sands1607f052008-12-01 11:39:25 +00009341 }
9342 return;
9343 }
9344 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009345 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +00009346 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009347 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00009348 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +00009349 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00009350 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00009351 eax.getValue(2));
9352 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
9353 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +00009354 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00009355 Results.push_back(edx.getValue(1));
9356 return;
9357 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009358 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +00009359 EVT T = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009360 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Duncan Sands1607f052008-12-01 11:39:25 +00009361 SDValue cpInL, cpInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00009362 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
9363 DAG.getConstant(0, MVT::i32));
9364 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
9365 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00009366 cpInL = DAG.getCopyToReg(N->getOperand(0), dl, X86::EAX, cpInL, SDValue());
9367 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl, X86::EDX, cpInH,
Duncan Sands1607f052008-12-01 11:39:25 +00009368 cpInL.getValue(1));
9369 SDValue swapInL, swapInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00009370 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
9371 DAG.getConstant(0, MVT::i32));
9372 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
9373 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00009374 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl, X86::EBX, swapInL,
Duncan Sands1607f052008-12-01 11:39:25 +00009375 cpInH.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00009376 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl, X86::ECX, swapInH,
Duncan Sands1607f052008-12-01 11:39:25 +00009377 swapInL.getValue(1));
9378 SDValue Ops[] = { swapInH.getValue(0),
9379 N->getOperand(1),
9380 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009381 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +00009382 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
9383 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG8_DAG, dl, Tys,
9384 Ops, 3, T, MMO);
Dale Johannesendd64c412009-02-04 00:33:20 +00009385 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl, X86::EAX,
Owen Anderson825b72b2009-08-11 20:47:22 +00009386 MVT::i32, Result.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00009387 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl, X86::EDX,
Owen Anderson825b72b2009-08-11 20:47:22 +00009388 MVT::i32, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +00009389 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Owen Anderson825b72b2009-08-11 20:47:22 +00009390 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00009391 Results.push_back(cpOutH.getValue(1));
9392 return;
9393 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009394 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +00009395 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
9396 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009397 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +00009398 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
9399 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009400 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +00009401 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
9402 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009403 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +00009404 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
9405 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009406 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +00009407 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
9408 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009409 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +00009410 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
9411 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009412 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +00009413 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
9414 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00009415 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00009416}
9417
Evan Cheng72261582005-12-20 06:22:03 +00009418const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
9419 switch (Opcode) {
9420 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +00009421 case X86ISD::BSF: return "X86ISD::BSF";
9422 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +00009423 case X86ISD::SHLD: return "X86ISD::SHLD";
9424 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00009425 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00009426 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00009427 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00009428 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00009429 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00009430 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00009431 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
9432 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
9433 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00009434 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00009435 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +00009436 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +00009437 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +00009438 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +00009439 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00009440 case X86ISD::COMI: return "X86ISD::COMI";
9441 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00009442 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +00009443 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Stuart Hastings865f0932011-06-03 23:53:54 +00009444 case X86ISD::FSETCCsd: return "X86ISD::FSETCCsd";
9445 case X86ISD::FSETCCss: return "X86ISD::FSETCCss";
Evan Cheng72261582005-12-20 06:22:03 +00009446 case X86ISD::CMOV: return "X86ISD::CMOV";
9447 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00009448 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00009449 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
9450 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +00009451 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00009452 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +00009453 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +00009454 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +00009455 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +00009456 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
9457 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +00009458 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +00009459 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Nate Begemanb65c1752010-12-17 22:55:37 +00009460 case X86ISD::PANDN: return "X86ISD::PANDN";
9461 case X86ISD::PSIGNB: return "X86ISD::PSIGNB";
9462 case X86ISD::PSIGNW: return "X86ISD::PSIGNW";
9463 case X86ISD::PSIGND: return "X86ISD::PSIGND";
Nate Begeman672fb622010-12-20 22:04:24 +00009464 case X86ISD::PBLENDVB: return "X86ISD::PBLENDVB";
Evan Cheng8ca29322006-11-10 21:43:37 +00009465 case X86ISD::FMAX: return "X86ISD::FMAX";
9466 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +00009467 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
9468 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00009469 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +00009470 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009471 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00009472 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009473 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +00009474 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
9475 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009476 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
9477 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
9478 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
9479 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
9480 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
9481 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +00009482 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
9483 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +00009484 case X86ISD::VSHL: return "X86ISD::VSHL";
9485 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +00009486 case X86ISD::CMPPD: return "X86ISD::CMPPD";
9487 case X86ISD::CMPPS: return "X86ISD::CMPPS";
9488 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
9489 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
9490 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
9491 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
9492 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
9493 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
9494 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
9495 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +00009496 case X86ISD::ADD: return "X86ISD::ADD";
9497 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +00009498 case X86ISD::ADC: return "X86ISD::ADC";
9499 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +00009500 case X86ISD::SMUL: return "X86ISD::SMUL";
9501 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +00009502 case X86ISD::INC: return "X86ISD::INC";
9503 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +00009504 case X86ISD::OR: return "X86ISD::OR";
9505 case X86ISD::XOR: return "X86ISD::XOR";
9506 case X86ISD::AND: return "X86ISD::AND";
Evan Cheng73f24c92009-03-30 21:36:47 +00009507 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +00009508 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009509 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009510 case X86ISD::PALIGN: return "X86ISD::PALIGN";
9511 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
9512 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
9513 case X86ISD::PSHUFHW_LD: return "X86ISD::PSHUFHW_LD";
9514 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
9515 case X86ISD::PSHUFLW_LD: return "X86ISD::PSHUFLW_LD";
9516 case X86ISD::SHUFPS: return "X86ISD::SHUFPS";
9517 case X86ISD::SHUFPD: return "X86ISD::SHUFPD";
9518 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009519 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00009520 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009521 case X86ISD::MOVHLPD: return "X86ISD::MOVHLPD";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00009522 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
9523 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009524 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
9525 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
9526 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
9527 case X86ISD::MOVSHDUP_LD: return "X86ISD::MOVSHDUP_LD";
9528 case X86ISD::MOVSLDUP_LD: return "X86ISD::MOVSLDUP_LD";
9529 case X86ISD::MOVSD: return "X86ISD::MOVSD";
9530 case X86ISD::MOVSS: return "X86ISD::MOVSS";
9531 case X86ISD::UNPCKLPS: return "X86ISD::UNPCKLPS";
9532 case X86ISD::UNPCKLPD: return "X86ISD::UNPCKLPD";
David Greenefbf05d32011-02-22 23:31:46 +00009533 case X86ISD::VUNPCKLPS: return "X86ISD::VUNPCKLPS";
9534 case X86ISD::VUNPCKLPD: return "X86ISD::VUNPCKLPD";
9535 case X86ISD::VUNPCKLPSY: return "X86ISD::VUNPCKLPSY";
9536 case X86ISD::VUNPCKLPDY: return "X86ISD::VUNPCKLPDY";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009537 case X86ISD::UNPCKHPS: return "X86ISD::UNPCKHPS";
9538 case X86ISD::UNPCKHPD: return "X86ISD::UNPCKHPD";
9539 case X86ISD::PUNPCKLBW: return "X86ISD::PUNPCKLBW";
9540 case X86ISD::PUNPCKLWD: return "X86ISD::PUNPCKLWD";
9541 case X86ISD::PUNPCKLDQ: return "X86ISD::PUNPCKLDQ";
9542 case X86ISD::PUNPCKLQDQ: return "X86ISD::PUNPCKLQDQ";
9543 case X86ISD::PUNPCKHBW: return "X86ISD::PUNPCKHBW";
9544 case X86ISD::PUNPCKHWD: return "X86ISD::PUNPCKHWD";
9545 case X86ISD::PUNPCKHDQ: return "X86ISD::PUNPCKHDQ";
9546 case X86ISD::PUNPCKHQDQ: return "X86ISD::PUNPCKHQDQ";
Dan Gohmand6708ea2009-08-15 01:38:56 +00009547 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +00009548 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +00009549 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Evan Cheng72261582005-12-20 06:22:03 +00009550 }
9551}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00009552
Chris Lattnerc9addb72007-03-30 23:15:24 +00009553// isLegalAddressingMode - Return true if the addressing mode represented
9554// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +00009555bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerc9addb72007-03-30 23:15:24 +00009556 const Type *Ty) const {
9557 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009558 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +00009559 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +00009560
Chris Lattnerc9addb72007-03-30 23:15:24 +00009561 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009562 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +00009563 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00009564
Chris Lattnerc9addb72007-03-30 23:15:24 +00009565 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +00009566 unsigned GVFlags =
9567 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009568
Chris Lattnerdfed4132009-07-10 07:38:24 +00009569 // If a reference to this global requires an extra load, we can't fold it.
9570 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +00009571 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009572
Chris Lattnerdfed4132009-07-10 07:38:24 +00009573 // If BaseGV requires a register for the PIC base, we cannot also have a
9574 // BaseReg specified.
9575 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +00009576 return false;
Evan Cheng52787842007-08-01 23:46:47 +00009577
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009578 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +00009579 if ((M != CodeModel::Small || R != Reloc::Static) &&
9580 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009581 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +00009582 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009583
Chris Lattnerc9addb72007-03-30 23:15:24 +00009584 switch (AM.Scale) {
9585 case 0:
9586 case 1:
9587 case 2:
9588 case 4:
9589 case 8:
9590 // These scales always work.
9591 break;
9592 case 3:
9593 case 5:
9594 case 9:
9595 // These scales are formed with basereg+scalereg. Only accept if there is
9596 // no basereg yet.
9597 if (AM.HasBaseReg)
9598 return false;
9599 break;
9600 default: // Other stuff never works.
9601 return false;
9602 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009603
Chris Lattnerc9addb72007-03-30 23:15:24 +00009604 return true;
9605}
9606
9607
Evan Cheng2bd122c2007-10-26 01:56:11 +00009608bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009609 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +00009610 return false;
Evan Chenge127a732007-10-29 07:57:50 +00009611 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
9612 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00009613 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +00009614 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00009615 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +00009616}
9617
Owen Andersone50ed302009-08-10 22:56:29 +00009618bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +00009619 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009620 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009621 unsigned NumBits1 = VT1.getSizeInBits();
9622 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00009623 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009624 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00009625 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009626}
Evan Cheng2bd122c2007-10-26 01:56:11 +00009627
Dan Gohman97121ba2009-04-08 00:15:30 +00009628bool X86TargetLowering::isZExtFree(const Type *Ty1, const Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00009629 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009630 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00009631}
9632
Owen Andersone50ed302009-08-10 22:56:29 +00009633bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00009634 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00009635 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00009636}
9637
Owen Andersone50ed302009-08-10 22:56:29 +00009638bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +00009639 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +00009640 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +00009641}
9642
Evan Cheng60c07e12006-07-05 22:17:51 +00009643/// isShuffleMaskLegal - Targets can use this to indicate that they only
9644/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
9645/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
9646/// are assumed to be legal.
9647bool
Eric Christopherfd179292009-08-27 18:07:15 +00009648X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +00009649 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +00009650 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +00009651 if (VT.getSizeInBits() == 64)
Eric Christophercff6f852010-04-15 01:40:20 +00009652 return isPALIGNRMask(M, VT, Subtarget->hasSSSE3());
Nate Begeman9008ca62009-04-27 18:41:29 +00009653
Nate Begemana09008b2009-10-19 02:17:23 +00009654 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +00009655 return (VT.getVectorNumElements() == 2 ||
9656 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
9657 isMOVLMask(M, VT) ||
9658 isSHUFPMask(M, VT) ||
9659 isPSHUFDMask(M, VT) ||
9660 isPSHUFHWMask(M, VT) ||
9661 isPSHUFLWMask(M, VT) ||
Nate Begemana09008b2009-10-19 02:17:23 +00009662 isPALIGNRMask(M, VT, Subtarget->hasSSSE3()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +00009663 isUNPCKLMask(M, VT) ||
9664 isUNPCKHMask(M, VT) ||
9665 isUNPCKL_v_undef_Mask(M, VT) ||
9666 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00009667}
9668
Dan Gohman7d8143f2008-04-09 20:09:42 +00009669bool
Nate Begeman5a5ca152009-04-29 05:20:52 +00009670X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +00009671 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00009672 unsigned NumElts = VT.getVectorNumElements();
9673 // FIXME: This collection of masks seems suspect.
9674 if (NumElts == 2)
9675 return true;
9676 if (NumElts == 4 && VT.getSizeInBits() == 128) {
9677 return (isMOVLMask(Mask, VT) ||
9678 isCommutedMOVLMask(Mask, VT, true) ||
9679 isSHUFPMask(Mask, VT) ||
9680 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00009681 }
9682 return false;
9683}
9684
9685//===----------------------------------------------------------------------===//
9686// X86 Scheduler Hooks
9687//===----------------------------------------------------------------------===//
9688
Mon P Wang63307c32008-05-05 19:05:59 +00009689// private utility function
9690MachineBasicBlock *
9691X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
9692 MachineBasicBlock *MBB,
9693 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009694 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009695 unsigned LoadOpc,
9696 unsigned CXchgOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009697 unsigned notOpc,
9698 unsigned EAXreg,
9699 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009700 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00009701 // For the atomic bitwise operator, we generate
9702 // thisMBB:
9703 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00009704 // ld t1 = [bitinstr.addr]
9705 // op t2 = t1, [bitinstr.val]
9706 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00009707 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
9708 // bz newMBB
9709 // fallthrough -->nextMBB
9710 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9711 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009712 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00009713 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009714
Mon P Wang63307c32008-05-05 19:05:59 +00009715 /// First build the CFG
9716 MachineFunction *F = MBB->getParent();
9717 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009718 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9719 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9720 F->insert(MBBIter, newMBB);
9721 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009722
Dan Gohman14152b42010-07-06 20:24:04 +00009723 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9724 nextMBB->splice(nextMBB->begin(), thisMBB,
9725 llvm::next(MachineBasicBlock::iterator(bInstr)),
9726 thisMBB->end());
9727 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009728
Mon P Wang63307c32008-05-05 19:05:59 +00009729 // Update thisMBB to fall through to newMBB
9730 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009731
Mon P Wang63307c32008-05-05 19:05:59 +00009732 // newMBB jumps to itself and fall through to nextMBB
9733 newMBB->addSuccessor(nextMBB);
9734 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009735
Mon P Wang63307c32008-05-05 19:05:59 +00009736 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009737 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009738 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +00009739 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00009740 MachineOperand& destOper = bInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009741 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00009742 int numArgs = bInstr->getNumOperands() - 1;
9743 for (int i=0; i < numArgs; ++i)
9744 argOpers[i] = &bInstr->getOperand(i+1);
9745
9746 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009747 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009748 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00009749
Dale Johannesen140be2d2008-08-19 18:47:28 +00009750 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009751 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00009752 for (int i=0; i <= lastAddrIndx; ++i)
9753 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009754
Dale Johannesen140be2d2008-08-19 18:47:28 +00009755 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009756 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00009757 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009758 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009759 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009760 tt = t1;
9761
Dale Johannesen140be2d2008-08-19 18:47:28 +00009762 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +00009763 assert((argOpers[valArgIndx]->isReg() ||
9764 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00009765 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +00009766 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009767 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00009768 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009769 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009770 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +00009771 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009772
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009773 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +00009774 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009775
Dale Johannesene4d209d2009-02-03 20:21:25 +00009776 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +00009777 for (int i=0; i <= lastAddrIndx; ++i)
9778 (*MIB).addOperand(*argOpers[i]);
9779 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +00009780 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009781 (*MIB).setMemRefs(bInstr->memoperands_begin(),
9782 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +00009783
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009784 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +00009785 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +00009786
Mon P Wang63307c32008-05-05 19:05:59 +00009787 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009788 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00009789
Dan Gohman14152b42010-07-06 20:24:04 +00009790 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00009791 return nextMBB;
9792}
9793
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00009794// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +00009795MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009796X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
9797 MachineBasicBlock *MBB,
9798 unsigned regOpcL,
9799 unsigned regOpcH,
9800 unsigned immOpcL,
9801 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009802 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009803 // For the atomic bitwise operator, we generate
9804 // thisMBB (instructions are in pairs, except cmpxchg8b)
9805 // ld t1,t2 = [bitinstr.addr]
9806 // newMBB:
9807 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
9808 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +00009809 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009810 // mov ECX, EBX <- t5, t6
9811 // mov EAX, EDX <- t1, t2
9812 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
9813 // mov t3, t4 <- EAX, EDX
9814 // bz newMBB
9815 // result in out1, out2
9816 // fallthrough -->nextMBB
9817
9818 const TargetRegisterClass *RC = X86::GR32RegisterClass;
9819 const unsigned LoadOpc = X86::MOV32rm;
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009820 const unsigned NotOpc = X86::NOT32r;
9821 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9822 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
9823 MachineFunction::iterator MBBIter = MBB;
9824 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009825
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009826 /// First build the CFG
9827 MachineFunction *F = MBB->getParent();
9828 MachineBasicBlock *thisMBB = MBB;
9829 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9830 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9831 F->insert(MBBIter, newMBB);
9832 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009833
Dan Gohman14152b42010-07-06 20:24:04 +00009834 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9835 nextMBB->splice(nextMBB->begin(), thisMBB,
9836 llvm::next(MachineBasicBlock::iterator(bInstr)),
9837 thisMBB->end());
9838 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009839
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009840 // Update thisMBB to fall through to newMBB
9841 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009842
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009843 // newMBB jumps to itself and fall through to nextMBB
9844 newMBB->addSuccessor(nextMBB);
9845 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009846
Dale Johannesene4d209d2009-02-03 20:21:25 +00009847 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009848 // Insert instructions into newMBB based on incoming instruction
9849 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009850 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009851 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009852 MachineOperand& dest1Oper = bInstr->getOperand(0);
9853 MachineOperand& dest2Oper = bInstr->getOperand(1);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009854 MachineOperand* argOpers[2 + X86::AddrNumOperands];
9855 for (int i=0; i < 2 + X86::AddrNumOperands; ++i) {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009856 argOpers[i] = &bInstr->getOperand(i+2);
9857
Dan Gohman71ea4e52010-05-14 21:01:44 +00009858 // We use some of the operands multiple times, so conservatively just
9859 // clear any kill flags that might be present.
9860 if (argOpers[i]->isReg() && argOpers[i]->isUse())
9861 argOpers[i]->setIsKill(false);
9862 }
9863
Evan Chengad5b52f2010-01-08 19:14:57 +00009864 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009865 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +00009866
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009867 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009868 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009869 for (int i=0; i <= lastAddrIndx; ++i)
9870 (*MIB).addOperand(*argOpers[i]);
9871 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009872 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +00009873 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +00009874 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009875 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +00009876 MachineOperand newOp3 = *(argOpers[3]);
9877 if (newOp3.isImm())
9878 newOp3.setImm(newOp3.getImm()+4);
9879 else
9880 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009881 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +00009882 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009883
9884 // t3/4 are defined later, at the bottom of the loop
9885 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
9886 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009887 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009888 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009889 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009890 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
9891
Evan Cheng306b4ca2010-01-08 23:41:50 +00009892 // The subsequent operations should be using the destination registers of
9893 //the PHI instructions.
Scott Michelfdc40a02009-02-17 22:15:04 +00009894 if (invSrc) {
Evan Cheng306b4ca2010-01-08 23:41:50 +00009895 t1 = F->getRegInfo().createVirtualRegister(RC);
9896 t2 = F->getRegInfo().createVirtualRegister(RC);
9897 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
9898 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009899 } else {
Evan Cheng306b4ca2010-01-08 23:41:50 +00009900 t1 = dest1Oper.getReg();
9901 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009902 }
9903
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009904 int valArgIndx = lastAddrIndx + 1;
9905 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +00009906 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009907 "invalid operand");
9908 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
9909 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009910 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009911 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009912 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009913 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +00009914 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00009915 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009916 (*MIB).addOperand(*argOpers[valArgIndx]);
9917 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00009918 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009919 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00009920 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009921 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009922 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009923 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009924 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +00009925 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00009926 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009927 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009928
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009929 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009930 MIB.addReg(t1);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009931 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009932 MIB.addReg(t2);
9933
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009934 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009935 MIB.addReg(t5);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009936 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009937 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +00009938
Dale Johannesene4d209d2009-02-03 20:21:25 +00009939 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009940 for (int i=0; i <= lastAddrIndx; ++i)
9941 (*MIB).addOperand(*argOpers[i]);
9942
9943 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009944 (*MIB).setMemRefs(bInstr->memoperands_begin(),
9945 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009946
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009947 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009948 MIB.addReg(X86::EAX);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009949 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009950 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +00009951
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009952 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009953 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009954
Dan Gohman14152b42010-07-06 20:24:04 +00009955 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009956 return nextMBB;
9957}
9958
9959// private utility function
9960MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +00009961X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
9962 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009963 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00009964 // For the atomic min/max operator, we generate
9965 // thisMBB:
9966 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00009967 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +00009968 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +00009969 // cmp t1, t2
9970 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +00009971 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00009972 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
9973 // bz newMBB
9974 // fallthrough -->nextMBB
9975 //
9976 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9977 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009978 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00009979 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009980
Mon P Wang63307c32008-05-05 19:05:59 +00009981 /// First build the CFG
9982 MachineFunction *F = MBB->getParent();
9983 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009984 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9985 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9986 F->insert(MBBIter, newMBB);
9987 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009988
Dan Gohman14152b42010-07-06 20:24:04 +00009989 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9990 nextMBB->splice(nextMBB->begin(), thisMBB,
9991 llvm::next(MachineBasicBlock::iterator(mInstr)),
9992 thisMBB->end());
9993 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009994
Mon P Wang63307c32008-05-05 19:05:59 +00009995 // Update thisMBB to fall through to newMBB
9996 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009997
Mon P Wang63307c32008-05-05 19:05:59 +00009998 // newMBB jumps to newMBB and fall through to nextMBB
9999 newMBB->addSuccessor(nextMBB);
10000 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010001
Dale Johannesene4d209d2009-02-03 20:21:25 +000010002 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +000010003 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010004 assert(mInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000010005 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +000010006 MachineOperand& destOper = mInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010007 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +000010008 int numArgs = mInstr->getNumOperands() - 1;
10009 for (int i=0; i < numArgs; ++i)
10010 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +000010011
Mon P Wang63307c32008-05-05 19:05:59 +000010012 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010013 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000010014 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +000010015
Mon P Wangab3e7472008-05-05 22:56:23 +000010016 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010017 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +000010018 for (int i=0; i <= lastAddrIndx; ++i)
10019 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +000010020
Mon P Wang63307c32008-05-05 19:05:59 +000010021 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +000010022 assert((argOpers[valArgIndx]->isReg() ||
10023 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +000010024 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +000010025
10026 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +000010027 if (argOpers[valArgIndx]->isReg())
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010028 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +000010029 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000010030 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +000010031 (*MIB).addOperand(*argOpers[valArgIndx]);
10032
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010033 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +000010034 MIB.addReg(t1);
10035
Dale Johannesene4d209d2009-02-03 20:21:25 +000010036 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +000010037 MIB.addReg(t1);
10038 MIB.addReg(t2);
10039
10040 // Generate movc
10041 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010042 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +000010043 MIB.addReg(t2);
10044 MIB.addReg(t1);
10045
10046 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +000010047 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +000010048 for (int i=0; i <= lastAddrIndx; ++i)
10049 (*MIB).addOperand(*argOpers[i]);
10050 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +000010051 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000010052 (*MIB).setMemRefs(mInstr->memoperands_begin(),
10053 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +000010054
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010055 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +000010056 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +000010057
Mon P Wang63307c32008-05-05 19:05:59 +000010058 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000010059 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000010060
Dan Gohman14152b42010-07-06 20:24:04 +000010061 mInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +000010062 return nextMBB;
10063}
10064
Eric Christopherf83a5de2009-08-27 18:08:16 +000010065// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010066// or XMM0_V32I8 in AVX all of this code can be replaced with that
10067// in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +000010068MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +000010069X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +000010070 unsigned numArgs, bool memArg) const {
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010071 assert((Subtarget->hasSSE42() || Subtarget->hasAVX()) &&
10072 "Target must have SSE4.2 or AVX features enabled");
10073
Eric Christopherb120ab42009-08-18 22:50:32 +000010074 DebugLoc dl = MI->getDebugLoc();
10075 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Eric Christopherb120ab42009-08-18 22:50:32 +000010076 unsigned Opc;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010077 if (!Subtarget->hasAVX()) {
10078 if (memArg)
10079 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
10080 else
10081 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
10082 } else {
10083 if (memArg)
10084 Opc = numArgs == 3 ? X86::VPCMPISTRM128rm : X86::VPCMPESTRM128rm;
10085 else
10086 Opc = numArgs == 3 ? X86::VPCMPISTRM128rr : X86::VPCMPESTRM128rr;
10087 }
Eric Christopherb120ab42009-08-18 22:50:32 +000010088
Eric Christopher41c902f2010-11-30 08:20:21 +000010089 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Eric Christopherb120ab42009-08-18 22:50:32 +000010090 for (unsigned i = 0; i < numArgs; ++i) {
10091 MachineOperand &Op = MI->getOperand(i+1);
Eric Christopherb120ab42009-08-18 22:50:32 +000010092 if (!(Op.isReg() && Op.isImplicit()))
10093 MIB.addOperand(Op);
10094 }
Eric Christopher41c902f2010-11-30 08:20:21 +000010095 BuildMI(*BB, MI, dl, TII->get(X86::MOVAPSrr), MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +000010096 .addReg(X86::XMM0);
10097
Dan Gohman14152b42010-07-06 20:24:04 +000010098 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +000010099 return BB;
10100}
10101
10102MachineBasicBlock *
Eric Christopher228232b2010-11-30 07:20:12 +000010103X86TargetLowering::EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000010104 DebugLoc dl = MI->getDebugLoc();
10105 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010106
Eric Christopher228232b2010-11-30 07:20:12 +000010107 // Address into RAX/EAX, other two args into ECX, EDX.
10108 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
10109 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
10110 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
10111 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +000010112 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010113
Eric Christopher228232b2010-11-30 07:20:12 +000010114 unsigned ValOps = X86::AddrNumOperands;
10115 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
10116 .addReg(MI->getOperand(ValOps).getReg());
10117 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
10118 .addReg(MI->getOperand(ValOps+1).getReg());
10119
10120 // The instruction doesn't actually take any operands though.
10121 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010122
Eric Christopher228232b2010-11-30 07:20:12 +000010123 MI->eraseFromParent(); // The pseudo is gone now.
10124 return BB;
10125}
10126
10127MachineBasicBlock *
10128X86TargetLowering::EmitMwait(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000010129 DebugLoc dl = MI->getDebugLoc();
10130 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010131
Eric Christopher228232b2010-11-30 07:20:12 +000010132 // First arg in ECX, the second in EAX.
10133 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
10134 .addReg(MI->getOperand(0).getReg());
10135 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EAX)
10136 .addReg(MI->getOperand(1).getReg());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010137
Eric Christopher228232b2010-11-30 07:20:12 +000010138 // The instruction doesn't actually take any operands though.
10139 BuildMI(*BB, MI, dl, TII->get(X86::MWAITrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010140
Eric Christopher228232b2010-11-30 07:20:12 +000010141 MI->eraseFromParent(); // The pseudo is gone now.
10142 return BB;
10143}
10144
10145MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +000010146X86TargetLowering::EmitVAARG64WithCustomInserter(
10147 MachineInstr *MI,
10148 MachineBasicBlock *MBB) const {
10149 // Emit va_arg instruction on X86-64.
10150
10151 // Operands to this pseudo-instruction:
10152 // 0 ) Output : destination address (reg)
10153 // 1-5) Input : va_list address (addr, i64mem)
10154 // 6 ) ArgSize : Size (in bytes) of vararg type
10155 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
10156 // 8 ) Align : Alignment of type
10157 // 9 ) EFLAGS (implicit-def)
10158
10159 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
10160 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
10161
10162 unsigned DestReg = MI->getOperand(0).getReg();
10163 MachineOperand &Base = MI->getOperand(1);
10164 MachineOperand &Scale = MI->getOperand(2);
10165 MachineOperand &Index = MI->getOperand(3);
10166 MachineOperand &Disp = MI->getOperand(4);
10167 MachineOperand &Segment = MI->getOperand(5);
10168 unsigned ArgSize = MI->getOperand(6).getImm();
10169 unsigned ArgMode = MI->getOperand(7).getImm();
10170 unsigned Align = MI->getOperand(8).getImm();
10171
10172 // Memory Reference
10173 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
10174 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
10175 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
10176
10177 // Machine Information
10178 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10179 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
10180 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
10181 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
10182 DebugLoc DL = MI->getDebugLoc();
10183
10184 // struct va_list {
10185 // i32 gp_offset
10186 // i32 fp_offset
10187 // i64 overflow_area (address)
10188 // i64 reg_save_area (address)
10189 // }
10190 // sizeof(va_list) = 24
10191 // alignment(va_list) = 8
10192
10193 unsigned TotalNumIntRegs = 6;
10194 unsigned TotalNumXMMRegs = 8;
10195 bool UseGPOffset = (ArgMode == 1);
10196 bool UseFPOffset = (ArgMode == 2);
10197 unsigned MaxOffset = TotalNumIntRegs * 8 +
10198 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
10199
10200 /* Align ArgSize to a multiple of 8 */
10201 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
10202 bool NeedsAlign = (Align > 8);
10203
10204 MachineBasicBlock *thisMBB = MBB;
10205 MachineBasicBlock *overflowMBB;
10206 MachineBasicBlock *offsetMBB;
10207 MachineBasicBlock *endMBB;
10208
10209 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
10210 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
10211 unsigned OffsetReg = 0;
10212
10213 if (!UseGPOffset && !UseFPOffset) {
10214 // If we only pull from the overflow region, we don't create a branch.
10215 // We don't need to alter control flow.
10216 OffsetDestReg = 0; // unused
10217 OverflowDestReg = DestReg;
10218
10219 offsetMBB = NULL;
10220 overflowMBB = thisMBB;
10221 endMBB = thisMBB;
10222 } else {
10223 // First emit code to check if gp_offset (or fp_offset) is below the bound.
10224 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
10225 // If not, pull from overflow_area. (branch to overflowMBB)
10226 //
10227 // thisMBB
10228 // | .
10229 // | .
10230 // offsetMBB overflowMBB
10231 // | .
10232 // | .
10233 // endMBB
10234
10235 // Registers for the PHI in endMBB
10236 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
10237 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
10238
10239 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
10240 MachineFunction *MF = MBB->getParent();
10241 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
10242 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
10243 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
10244
10245 MachineFunction::iterator MBBIter = MBB;
10246 ++MBBIter;
10247
10248 // Insert the new basic blocks
10249 MF->insert(MBBIter, offsetMBB);
10250 MF->insert(MBBIter, overflowMBB);
10251 MF->insert(MBBIter, endMBB);
10252
10253 // Transfer the remainder of MBB and its successor edges to endMBB.
10254 endMBB->splice(endMBB->begin(), thisMBB,
10255 llvm::next(MachineBasicBlock::iterator(MI)),
10256 thisMBB->end());
10257 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
10258
10259 // Make offsetMBB and overflowMBB successors of thisMBB
10260 thisMBB->addSuccessor(offsetMBB);
10261 thisMBB->addSuccessor(overflowMBB);
10262
10263 // endMBB is a successor of both offsetMBB and overflowMBB
10264 offsetMBB->addSuccessor(endMBB);
10265 overflowMBB->addSuccessor(endMBB);
10266
10267 // Load the offset value into a register
10268 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
10269 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
10270 .addOperand(Base)
10271 .addOperand(Scale)
10272 .addOperand(Index)
10273 .addDisp(Disp, UseFPOffset ? 4 : 0)
10274 .addOperand(Segment)
10275 .setMemRefs(MMOBegin, MMOEnd);
10276
10277 // Check if there is enough room left to pull this argument.
10278 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
10279 .addReg(OffsetReg)
10280 .addImm(MaxOffset + 8 - ArgSizeA8);
10281
10282 // Branch to "overflowMBB" if offset >= max
10283 // Fall through to "offsetMBB" otherwise
10284 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
10285 .addMBB(overflowMBB);
10286 }
10287
10288 // In offsetMBB, emit code to use the reg_save_area.
10289 if (offsetMBB) {
10290 assert(OffsetReg != 0);
10291
10292 // Read the reg_save_area address.
10293 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
10294 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
10295 .addOperand(Base)
10296 .addOperand(Scale)
10297 .addOperand(Index)
10298 .addDisp(Disp, 16)
10299 .addOperand(Segment)
10300 .setMemRefs(MMOBegin, MMOEnd);
10301
10302 // Zero-extend the offset
10303 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
10304 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
10305 .addImm(0)
10306 .addReg(OffsetReg)
10307 .addImm(X86::sub_32bit);
10308
10309 // Add the offset to the reg_save_area to get the final address.
10310 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
10311 .addReg(OffsetReg64)
10312 .addReg(RegSaveReg);
10313
10314 // Compute the offset for the next argument
10315 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
10316 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
10317 .addReg(OffsetReg)
10318 .addImm(UseFPOffset ? 16 : 8);
10319
10320 // Store it back into the va_list.
10321 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
10322 .addOperand(Base)
10323 .addOperand(Scale)
10324 .addOperand(Index)
10325 .addDisp(Disp, UseFPOffset ? 4 : 0)
10326 .addOperand(Segment)
10327 .addReg(NextOffsetReg)
10328 .setMemRefs(MMOBegin, MMOEnd);
10329
10330 // Jump to endMBB
10331 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
10332 .addMBB(endMBB);
10333 }
10334
10335 //
10336 // Emit code to use overflow area
10337 //
10338
10339 // Load the overflow_area address into a register.
10340 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
10341 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
10342 .addOperand(Base)
10343 .addOperand(Scale)
10344 .addOperand(Index)
10345 .addDisp(Disp, 8)
10346 .addOperand(Segment)
10347 .setMemRefs(MMOBegin, MMOEnd);
10348
10349 // If we need to align it, do so. Otherwise, just copy the address
10350 // to OverflowDestReg.
10351 if (NeedsAlign) {
10352 // Align the overflow address
10353 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
10354 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
10355
10356 // aligned_addr = (addr + (align-1)) & ~(align-1)
10357 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
10358 .addReg(OverflowAddrReg)
10359 .addImm(Align-1);
10360
10361 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
10362 .addReg(TmpReg)
10363 .addImm(~(uint64_t)(Align-1));
10364 } else {
10365 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
10366 .addReg(OverflowAddrReg);
10367 }
10368
10369 // Compute the next overflow address after this argument.
10370 // (the overflow address should be kept 8-byte aligned)
10371 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
10372 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
10373 .addReg(OverflowDestReg)
10374 .addImm(ArgSizeA8);
10375
10376 // Store the new overflow address.
10377 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
10378 .addOperand(Base)
10379 .addOperand(Scale)
10380 .addOperand(Index)
10381 .addDisp(Disp, 8)
10382 .addOperand(Segment)
10383 .addReg(NextAddrReg)
10384 .setMemRefs(MMOBegin, MMOEnd);
10385
10386 // If we branched, emit the PHI to the front of endMBB.
10387 if (offsetMBB) {
10388 BuildMI(*endMBB, endMBB->begin(), DL,
10389 TII->get(X86::PHI), DestReg)
10390 .addReg(OffsetDestReg).addMBB(offsetMBB)
10391 .addReg(OverflowDestReg).addMBB(overflowMBB);
10392 }
10393
10394 // Erase the pseudo instruction
10395 MI->eraseFromParent();
10396
10397 return endMBB;
10398}
10399
10400MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +000010401X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
10402 MachineInstr *MI,
10403 MachineBasicBlock *MBB) const {
10404 // Emit code to save XMM registers to the stack. The ABI says that the
10405 // number of registers to save is given in %al, so it's theoretically
10406 // possible to do an indirect jump trick to avoid saving all of them,
10407 // however this code takes a simpler approach and just executes all
10408 // of the stores if %al is non-zero. It's less code, and it's probably
10409 // easier on the hardware branch predictor, and stores aren't all that
10410 // expensive anyway.
10411
10412 // Create the new basic blocks. One block contains all the XMM stores,
10413 // and one block is the final destination regardless of whether any
10414 // stores were performed.
10415 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
10416 MachineFunction *F = MBB->getParent();
10417 MachineFunction::iterator MBBIter = MBB;
10418 ++MBBIter;
10419 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
10420 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
10421 F->insert(MBBIter, XMMSaveMBB);
10422 F->insert(MBBIter, EndMBB);
10423
Dan Gohman14152b42010-07-06 20:24:04 +000010424 // Transfer the remainder of MBB and its successor edges to EndMBB.
10425 EndMBB->splice(EndMBB->begin(), MBB,
10426 llvm::next(MachineBasicBlock::iterator(MI)),
10427 MBB->end());
10428 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
10429
Dan Gohmand6708ea2009-08-15 01:38:56 +000010430 // The original block will now fall through to the XMM save block.
10431 MBB->addSuccessor(XMMSaveMBB);
10432 // The XMMSaveMBB will fall through to the end block.
10433 XMMSaveMBB->addSuccessor(EndMBB);
10434
10435 // Now add the instructions.
10436 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10437 DebugLoc DL = MI->getDebugLoc();
10438
10439 unsigned CountReg = MI->getOperand(0).getReg();
10440 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
10441 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
10442
10443 if (!Subtarget->isTargetWin64()) {
10444 // If %al is 0, branch around the XMM save block.
10445 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +000010446 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010447 MBB->addSuccessor(EndMBB);
10448 }
10449
10450 // In the XMM save block, save all the XMM argument registers.
10451 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
10452 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +000010453 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +000010454 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +000010455 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +000010456 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +000010457 /*Size=*/16, /*Align=*/16);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010458 BuildMI(XMMSaveMBB, DL, TII->get(X86::MOVAPSmr))
10459 .addFrameIndex(RegSaveFrameIndex)
10460 .addImm(/*Scale=*/1)
10461 .addReg(/*IndexReg=*/0)
10462 .addImm(/*Disp=*/Offset)
10463 .addReg(/*Segment=*/0)
10464 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +000010465 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010466 }
10467
Dan Gohman14152b42010-07-06 20:24:04 +000010468 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +000010469
10470 return EndMBB;
10471}
Mon P Wang63307c32008-05-05 19:05:59 +000010472
Evan Cheng60c07e12006-07-05 22:17:51 +000010473MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +000010474X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010475 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +000010476 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10477 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +000010478
Chris Lattner52600972009-09-02 05:57:00 +000010479 // To "insert" a SELECT_CC instruction, we actually have to insert the
10480 // diamond control-flow pattern. The incoming instruction knows the
10481 // destination vreg to set, the condition code register to branch on, the
10482 // true/false values to select between, and a branch opcode to use.
10483 const BasicBlock *LLVM_BB = BB->getBasicBlock();
10484 MachineFunction::iterator It = BB;
10485 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +000010486
Chris Lattner52600972009-09-02 05:57:00 +000010487 // thisMBB:
10488 // ...
10489 // TrueVal = ...
10490 // cmpTY ccX, r1, r2
10491 // bCC copy1MBB
10492 // fallthrough --> copy0MBB
10493 MachineBasicBlock *thisMBB = BB;
10494 MachineFunction *F = BB->getParent();
10495 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
10496 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +000010497 F->insert(It, copy0MBB);
10498 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +000010499
Bill Wendling730c07e2010-06-25 20:48:10 +000010500 // If the EFLAGS register isn't dead in the terminator, then claim that it's
10501 // live into the sink and copy blocks.
10502 const MachineFunction *MF = BB->getParent();
10503 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
10504 BitVector ReservedRegs = TRI->getReservedRegs(*MF);
Bill Wendling730c07e2010-06-25 20:48:10 +000010505
Dan Gohman14152b42010-07-06 20:24:04 +000010506 for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) {
10507 const MachineOperand &MO = MI->getOperand(I);
10508 if (!MO.isReg() || !MO.isUse() || MO.isKill()) continue;
Bill Wendling730c07e2010-06-25 20:48:10 +000010509 unsigned Reg = MO.getReg();
10510 if (Reg != X86::EFLAGS) continue;
10511 copy0MBB->addLiveIn(Reg);
10512 sinkMBB->addLiveIn(Reg);
10513 }
10514
Dan Gohman14152b42010-07-06 20:24:04 +000010515 // Transfer the remainder of BB and its successor edges to sinkMBB.
10516 sinkMBB->splice(sinkMBB->begin(), BB,
10517 llvm::next(MachineBasicBlock::iterator(MI)),
10518 BB->end());
10519 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
10520
10521 // Add the true and fallthrough blocks as its successors.
10522 BB->addSuccessor(copy0MBB);
10523 BB->addSuccessor(sinkMBB);
10524
10525 // Create the conditional branch instruction.
10526 unsigned Opc =
10527 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
10528 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
10529
Chris Lattner52600972009-09-02 05:57:00 +000010530 // copy0MBB:
10531 // %FalseValue = ...
10532 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000010533 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000010534
Chris Lattner52600972009-09-02 05:57:00 +000010535 // sinkMBB:
10536 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
10537 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000010538 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
10539 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000010540 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
10541 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
10542
Dan Gohman14152b42010-07-06 20:24:04 +000010543 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000010544 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000010545}
10546
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010547MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010548X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010549 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010550 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10551 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010552
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000010553 assert(!Subtarget->isTargetEnvMacho());
10554
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010555 // The lowering is pretty easy: we're just emitting the call to _alloca. The
10556 // non-trivial part is impdef of ESP.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010557
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000010558 if (Subtarget->isTargetWin64()) {
10559 if (Subtarget->isTargetCygMing()) {
10560 // ___chkstk(Mingw64):
10561 // Clobbers R10, R11, RAX and EFLAGS.
10562 // Updates RSP.
10563 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
10564 .addExternalSymbol("___chkstk")
10565 .addReg(X86::RAX, RegState::Implicit)
10566 .addReg(X86::RSP, RegState::Implicit)
10567 .addReg(X86::RAX, RegState::Define | RegState::Implicit)
10568 .addReg(X86::RSP, RegState::Define | RegState::Implicit)
10569 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
10570 } else {
10571 // __chkstk(MSVCRT): does not update stack pointer.
10572 // Clobbers R10, R11 and EFLAGS.
10573 // FIXME: RAX(allocated size) might be reused and not killed.
10574 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
10575 .addExternalSymbol("__chkstk")
10576 .addReg(X86::RAX, RegState::Implicit)
10577 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
10578 // RAX has the offset to subtracted from RSP.
10579 BuildMI(*BB, MI, DL, TII->get(X86::SUB64rr), X86::RSP)
10580 .addReg(X86::RSP)
10581 .addReg(X86::RAX);
10582 }
10583 } else {
10584 const char *StackProbeSymbol =
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010585 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
10586
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000010587 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
10588 .addExternalSymbol(StackProbeSymbol)
10589 .addReg(X86::EAX, RegState::Implicit)
10590 .addReg(X86::ESP, RegState::Implicit)
10591 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
10592 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
10593 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
10594 }
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010595
Dan Gohman14152b42010-07-06 20:24:04 +000010596 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010597 return BB;
10598}
Chris Lattner52600972009-09-02 05:57:00 +000010599
10600MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000010601X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
10602 MachineBasicBlock *BB) const {
10603 // This is pretty easy. We're taking the value that we received from
10604 // our load from the relocation, sticking it in either RDI (x86-64)
10605 // or EAX and doing an indirect call. The return value will then
10606 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000010607 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000010608 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000010609 DebugLoc DL = MI->getDebugLoc();
10610 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000010611
10612 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000010613 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000010614
Eric Christopher30ef0e52010-06-03 04:07:48 +000010615 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000010616 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10617 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000010618 .addReg(X86::RIP)
10619 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010620 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000010621 MI->getOperand(3).getTargetFlags())
10622 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000010623 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000010624 addDirectMem(MIB, X86::RDI);
Eric Christopher61025492010-06-15 23:08:42 +000010625 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000010626 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10627 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000010628 .addReg(0)
10629 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010630 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000010631 MI->getOperand(3).getTargetFlags())
10632 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000010633 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000010634 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010635 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000010636 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10637 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000010638 .addReg(TII->getGlobalBaseReg(F))
10639 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010640 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000010641 MI->getOperand(3).getTargetFlags())
10642 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000010643 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000010644 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010645 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000010646
Dan Gohman14152b42010-07-06 20:24:04 +000010647 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000010648 return BB;
10649}
10650
10651MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000010652X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010653 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000010654 switch (MI->getOpcode()) {
10655 default: assert(false && "Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000010656 case X86::TAILJMPd64:
10657 case X86::TAILJMPr64:
10658 case X86::TAILJMPm64:
10659 assert(!"TAILJMP64 would not be touched here.");
10660 case X86::TCRETURNdi64:
10661 case X86::TCRETURNri64:
10662 case X86::TCRETURNmi64:
10663 // Defs of TCRETURNxx64 has Win64's callee-saved registers, as subset.
10664 // On AMD64, additional defs should be added before register allocation.
10665 if (!Subtarget->isTargetWin64()) {
10666 MI->addRegisterDefined(X86::RSI);
10667 MI->addRegisterDefined(X86::RDI);
10668 MI->addRegisterDefined(X86::XMM6);
10669 MI->addRegisterDefined(X86::XMM7);
10670 MI->addRegisterDefined(X86::XMM8);
10671 MI->addRegisterDefined(X86::XMM9);
10672 MI->addRegisterDefined(X86::XMM10);
10673 MI->addRegisterDefined(X86::XMM11);
10674 MI->addRegisterDefined(X86::XMM12);
10675 MI->addRegisterDefined(X86::XMM13);
10676 MI->addRegisterDefined(X86::XMM14);
10677 MI->addRegisterDefined(X86::XMM15);
10678 }
10679 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010680 case X86::WIN_ALLOCA:
10681 return EmitLoweredWinAlloca(MI, BB);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010682 case X86::TLSCall_32:
10683 case X86::TLSCall_64:
10684 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000010685 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000010686 case X86::CMOV_FR32:
10687 case X86::CMOV_FR64:
10688 case X86::CMOV_V4F32:
10689 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000010690 case X86::CMOV_V2I64:
Chris Lattner314a1132010-03-14 18:31:44 +000010691 case X86::CMOV_GR16:
10692 case X86::CMOV_GR32:
10693 case X86::CMOV_RFP32:
10694 case X86::CMOV_RFP64:
10695 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010696 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000010697
Dale Johannesen849f2142007-07-03 00:53:03 +000010698 case X86::FP32_TO_INT16_IN_MEM:
10699 case X86::FP32_TO_INT32_IN_MEM:
10700 case X86::FP32_TO_INT64_IN_MEM:
10701 case X86::FP64_TO_INT16_IN_MEM:
10702 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000010703 case X86::FP64_TO_INT64_IN_MEM:
10704 case X86::FP80_TO_INT16_IN_MEM:
10705 case X86::FP80_TO_INT32_IN_MEM:
10706 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000010707 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10708 DebugLoc DL = MI->getDebugLoc();
10709
Evan Cheng60c07e12006-07-05 22:17:51 +000010710 // Change the floating point control register to use "round towards zero"
10711 // mode when truncating to an integer value.
10712 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000010713 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000010714 addFrameReference(BuildMI(*BB, MI, DL,
10715 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010716
10717 // Load the old value of the high byte of the control word...
10718 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +000010719 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohman14152b42010-07-06 20:24:04 +000010720 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000010721 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010722
10723 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000010724 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000010725 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000010726
10727 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000010728 addFrameReference(BuildMI(*BB, MI, DL,
10729 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010730
10731 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000010732 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000010733 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000010734
10735 // Get the X86 opcode to use.
10736 unsigned Opc;
10737 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010738 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000010739 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
10740 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
10741 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
10742 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
10743 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
10744 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000010745 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
10746 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
10747 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000010748 }
10749
10750 X86AddressMode AM;
10751 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000010752 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000010753 AM.BaseType = X86AddressMode::RegBase;
10754 AM.Base.Reg = Op.getReg();
10755 } else {
10756 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000010757 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000010758 }
10759 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000010760 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000010761 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010762 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000010763 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000010764 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010765 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000010766 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000010767 AM.GV = Op.getGlobal();
10768 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000010769 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010770 }
Dan Gohman14152b42010-07-06 20:24:04 +000010771 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010772 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000010773
10774 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000010775 addFrameReference(BuildMI(*BB, MI, DL,
10776 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010777
Dan Gohman14152b42010-07-06 20:24:04 +000010778 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000010779 return BB;
10780 }
Eric Christopherb120ab42009-08-18 22:50:32 +000010781 // String/text processing lowering.
10782 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010783 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000010784 return EmitPCMP(MI, BB, 3, false /* in-mem */);
10785 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010786 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000010787 return EmitPCMP(MI, BB, 3, true /* in-mem */);
10788 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010789 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000010790 return EmitPCMP(MI, BB, 5, false /* in mem */);
10791 case X86::PCMPESTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010792 case X86::VPCMPESTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000010793 return EmitPCMP(MI, BB, 5, true /* in mem */);
10794
Eric Christopher228232b2010-11-30 07:20:12 +000010795 // Thread synchronization.
10796 case X86::MONITOR:
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010797 return EmitMonitor(MI, BB);
Eric Christopher228232b2010-11-30 07:20:12 +000010798 case X86::MWAIT:
10799 return EmitMwait(MI, BB);
10800
Eric Christopherb120ab42009-08-18 22:50:32 +000010801 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +000010802 case X86::ATOMAND32:
10803 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010804 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010805 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010806 X86::NOT32r, X86::EAX,
10807 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000010808 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +000010809 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
10810 X86::OR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010811 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010812 X86::NOT32r, X86::EAX,
10813 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000010814 case X86::ATOMXOR32:
10815 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010816 X86::XOR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010817 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010818 X86::NOT32r, X86::EAX,
10819 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010820 case X86::ATOMNAND32:
10821 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010822 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010823 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010824 X86::NOT32r, X86::EAX,
10825 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +000010826 case X86::ATOMMIN32:
10827 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
10828 case X86::ATOMMAX32:
10829 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
10830 case X86::ATOMUMIN32:
10831 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
10832 case X86::ATOMUMAX32:
10833 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +000010834
10835 case X86::ATOMAND16:
10836 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
10837 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010838 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010839 X86::NOT16r, X86::AX,
10840 X86::GR16RegisterClass);
10841 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +000010842 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010843 X86::OR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010844 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010845 X86::NOT16r, X86::AX,
10846 X86::GR16RegisterClass);
10847 case X86::ATOMXOR16:
10848 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
10849 X86::XOR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010850 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010851 X86::NOT16r, X86::AX,
10852 X86::GR16RegisterClass);
10853 case X86::ATOMNAND16:
10854 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
10855 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010856 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010857 X86::NOT16r, X86::AX,
10858 X86::GR16RegisterClass, true);
10859 case X86::ATOMMIN16:
10860 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
10861 case X86::ATOMMAX16:
10862 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
10863 case X86::ATOMUMIN16:
10864 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
10865 case X86::ATOMUMAX16:
10866 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
10867
10868 case X86::ATOMAND8:
10869 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
10870 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010871 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010872 X86::NOT8r, X86::AL,
10873 X86::GR8RegisterClass);
10874 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +000010875 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010876 X86::OR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010877 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010878 X86::NOT8r, X86::AL,
10879 X86::GR8RegisterClass);
10880 case X86::ATOMXOR8:
10881 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
10882 X86::XOR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010883 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010884 X86::NOT8r, X86::AL,
10885 X86::GR8RegisterClass);
10886 case X86::ATOMNAND8:
10887 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
10888 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010889 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010890 X86::NOT8r, X86::AL,
10891 X86::GR8RegisterClass, true);
10892 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010893 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +000010894 case X86::ATOMAND64:
10895 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010896 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010897 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010898 X86::NOT64r, X86::RAX,
10899 X86::GR64RegisterClass);
10900 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +000010901 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
10902 X86::OR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010903 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010904 X86::NOT64r, X86::RAX,
10905 X86::GR64RegisterClass);
10906 case X86::ATOMXOR64:
10907 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010908 X86::XOR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010909 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010910 X86::NOT64r, X86::RAX,
10911 X86::GR64RegisterClass);
10912 case X86::ATOMNAND64:
10913 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
10914 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010915 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010916 X86::NOT64r, X86::RAX,
10917 X86::GR64RegisterClass, true);
10918 case X86::ATOMMIN64:
10919 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
10920 case X86::ATOMMAX64:
10921 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
10922 case X86::ATOMUMIN64:
10923 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
10924 case X86::ATOMUMAX64:
10925 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010926
10927 // This group does 64-bit operations on a 32-bit host.
10928 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010929 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010930 X86::AND32rr, X86::AND32rr,
10931 X86::AND32ri, X86::AND32ri,
10932 false);
10933 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010934 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010935 X86::OR32rr, X86::OR32rr,
10936 X86::OR32ri, X86::OR32ri,
10937 false);
10938 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010939 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010940 X86::XOR32rr, X86::XOR32rr,
10941 X86::XOR32ri, X86::XOR32ri,
10942 false);
10943 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010944 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010945 X86::AND32rr, X86::AND32rr,
10946 X86::AND32ri, X86::AND32ri,
10947 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010948 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010949 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010950 X86::ADD32rr, X86::ADC32rr,
10951 X86::ADD32ri, X86::ADC32ri,
10952 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010953 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010954 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010955 X86::SUB32rr, X86::SBB32rr,
10956 X86::SUB32ri, X86::SBB32ri,
10957 false);
Dale Johannesen880ae362008-10-03 22:25:52 +000010958 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010959 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +000010960 X86::MOV32rr, X86::MOV32rr,
10961 X86::MOV32ri, X86::MOV32ri,
10962 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010963 case X86::VASTART_SAVE_XMM_REGS:
10964 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000010965
10966 case X86::VAARG_64:
10967 return EmitVAARG64WithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000010968 }
10969}
10970
10971//===----------------------------------------------------------------------===//
10972// X86 Optimization Hooks
10973//===----------------------------------------------------------------------===//
10974
Dan Gohman475871a2008-07-27 21:46:04 +000010975void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +000010976 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000010977 APInt &KnownZero,
10978 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000010979 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000010980 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010981 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000010982 assert((Opc >= ISD::BUILTIN_OP_END ||
10983 Opc == ISD::INTRINSIC_WO_CHAIN ||
10984 Opc == ISD::INTRINSIC_W_CHAIN ||
10985 Opc == ISD::INTRINSIC_VOID) &&
10986 "Should use MaskedValueIsZero if you don't know whether Op"
10987 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010988
Dan Gohmanf4f92f52008-02-13 23:07:24 +000010989 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010990 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000010991 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000010992 case X86ISD::ADD:
10993 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000010994 case X86ISD::ADC:
10995 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000010996 case X86ISD::SMUL:
10997 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000010998 case X86ISD::INC:
10999 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000011000 case X86ISD::OR:
11001 case X86ISD::XOR:
11002 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000011003 // These nodes' second result is a boolean.
11004 if (Op.getResNo() == 0)
11005 break;
11006 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011007 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000011008 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
11009 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000011010 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +000011011 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000011012}
Chris Lattner259e97c2006-01-31 19:43:35 +000011013
Owen Andersonbc146b02010-09-21 20:42:50 +000011014unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
11015 unsigned Depth) const {
11016 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
11017 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
11018 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000011019
Owen Andersonbc146b02010-09-21 20:42:50 +000011020 // Fallback case.
11021 return 1;
11022}
11023
Evan Cheng206ee9d2006-07-07 08:33:52 +000011024/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000011025/// node is a GlobalAddress + offset.
11026bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000011027 const GlobalValue* &GA,
11028 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000011029 if (N->getOpcode() == X86ISD::Wrapper) {
11030 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000011031 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000011032 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000011033 return true;
11034 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000011035 }
Evan Chengad4196b2008-05-12 19:56:52 +000011036 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000011037}
11038
Evan Cheng206ee9d2006-07-07 08:33:52 +000011039/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
11040/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
11041/// if the load addresses are consecutive, non-overlapping, and in the right
Nate Begemanfdea31a2010-03-24 20:49:50 +000011042/// order.
Dan Gohman475871a2008-07-27 21:46:04 +000011043static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Mon P Wanga0fd0d52010-12-19 23:55:53 +000011044 TargetLowering::DAGCombinerInfo &DCI) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000011045 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000011046 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000011047
Eli Friedman7a5e5552009-06-07 06:52:44 +000011048 if (VT.getSizeInBits() != 128)
11049 return SDValue();
11050
Mon P Wanga0fd0d52010-12-19 23:55:53 +000011051 // Don't create instructions with illegal types after legalize types has run.
11052 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
11053 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
11054 return SDValue();
11055
Nate Begemanfdea31a2010-03-24 20:49:50 +000011056 SmallVector<SDValue, 16> Elts;
11057 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000011058 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000011059
Nate Begemanfdea31a2010-03-24 20:49:50 +000011060 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000011061}
Evan Chengd880b972008-05-09 21:53:03 +000011062
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000011063/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
11064/// generation and convert it from being a bunch of shuffles and extracts
11065/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000011066static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
11067 const TargetLowering &TLI) {
11068 SDValue InputVector = N->getOperand(0);
11069
11070 // Only operate on vectors of 4 elements, where the alternative shuffling
11071 // gets to be more expensive.
11072 if (InputVector.getValueType() != MVT::v4i32)
11073 return SDValue();
11074
11075 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
11076 // single use which is a sign-extend or zero-extend, and all elements are
11077 // used.
11078 SmallVector<SDNode *, 4> Uses;
11079 unsigned ExtractedElements = 0;
11080 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
11081 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
11082 if (UI.getUse().getResNo() != InputVector.getResNo())
11083 return SDValue();
11084
11085 SDNode *Extract = *UI;
11086 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
11087 return SDValue();
11088
11089 if (Extract->getValueType(0) != MVT::i32)
11090 return SDValue();
11091 if (!Extract->hasOneUse())
11092 return SDValue();
11093 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
11094 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
11095 return SDValue();
11096 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
11097 return SDValue();
11098
11099 // Record which element was extracted.
11100 ExtractedElements |=
11101 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
11102
11103 Uses.push_back(Extract);
11104 }
11105
11106 // If not all the elements were used, this may not be worthwhile.
11107 if (ExtractedElements != 15)
11108 return SDValue();
11109
11110 // Ok, we've now decided to do the transformation.
11111 DebugLoc dl = InputVector.getDebugLoc();
11112
11113 // Store the value to a temporary stack slot.
11114 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000011115 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
11116 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000011117
11118 // Replace each use (extract) with a load of the appropriate element.
11119 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
11120 UE = Uses.end(); UI != UE; ++UI) {
11121 SDNode *Extract = *UI;
11122
Nadav Rotem86694292011-05-17 08:31:57 +000011123 // cOMpute the element's address.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000011124 SDValue Idx = Extract->getOperand(1);
11125 unsigned EltSize =
11126 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
11127 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
11128 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
11129
Nadav Rotem86694292011-05-17 08:31:57 +000011130 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
Chris Lattner51abfe42010-09-21 06:02:19 +000011131 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000011132
11133 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000011134 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000011135 ScalarAddr, MachinePointerInfo(),
11136 false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000011137
11138 // Replace the exact with the load.
11139 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
11140 }
11141
11142 // The replacement was made in place; don't return anything.
11143 return SDValue();
11144}
11145
Chris Lattner83e6c992006-10-04 06:57:07 +000011146/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000011147static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +000011148 const X86Subtarget *Subtarget) {
11149 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000011150 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000011151 // Get the LHS/RHS of the select.
11152 SDValue LHS = N->getOperand(1);
11153 SDValue RHS = N->getOperand(2);
Eric Christopherfd179292009-08-27 18:07:15 +000011154
Dan Gohman670e5392009-09-21 18:03:22 +000011155 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000011156 // instructions match the semantics of the common C idiom x<y?x:y but not
11157 // x<=y?x:y, because of how they handle negative zero (which can be
11158 // ignored in unsafe-math mode).
Chris Lattner83e6c992006-10-04 06:57:07 +000011159 if (Subtarget->hasSSE2() &&
Owen Anderson825b72b2009-08-11 20:47:22 +000011160 (LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64) &&
Chris Lattner47b4ce82009-03-11 05:48:52 +000011161 Cond.getOpcode() == ISD::SETCC) {
11162 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011163
Chris Lattner47b4ce82009-03-11 05:48:52 +000011164 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000011165 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000011166 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
11167 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000011168 switch (CC) {
11169 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000011170 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000011171 // Converting this to a min would handle NaNs incorrectly, and swapping
11172 // the operands would cause it to handle comparisons between positive
11173 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000011174 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000011175 if (!UnsafeFPMath &&
11176 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
11177 break;
11178 std::swap(LHS, RHS);
11179 }
Dan Gohman670e5392009-09-21 18:03:22 +000011180 Opcode = X86ISD::FMIN;
11181 break;
11182 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000011183 // Converting this to a min would handle comparisons between positive
11184 // and negative zero incorrectly.
11185 if (!UnsafeFPMath &&
11186 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
11187 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011188 Opcode = X86ISD::FMIN;
11189 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000011190 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000011191 // Converting this to a min would handle both negative zeros and NaNs
11192 // incorrectly, but we can swap the operands to fix both.
11193 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000011194 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011195 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000011196 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011197 Opcode = X86ISD::FMIN;
11198 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011199
Dan Gohman670e5392009-09-21 18:03:22 +000011200 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000011201 // Converting this to a max would handle comparisons between positive
11202 // and negative zero incorrectly.
11203 if (!UnsafeFPMath &&
11204 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(LHS))
11205 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011206 Opcode = X86ISD::FMAX;
11207 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000011208 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000011209 // Converting this to a max would handle NaNs incorrectly, and swapping
11210 // the operands would cause it to handle comparisons between positive
11211 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000011212 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000011213 if (!UnsafeFPMath &&
11214 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
11215 break;
11216 std::swap(LHS, RHS);
11217 }
Dan Gohman670e5392009-09-21 18:03:22 +000011218 Opcode = X86ISD::FMAX;
11219 break;
11220 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000011221 // Converting this to a max would handle both negative zeros and NaNs
11222 // incorrectly, but we can swap the operands to fix both.
11223 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000011224 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011225 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011226 case ISD::SETGE:
11227 Opcode = X86ISD::FMAX;
11228 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000011229 }
Dan Gohman670e5392009-09-21 18:03:22 +000011230 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000011231 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
11232 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000011233 switch (CC) {
11234 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000011235 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000011236 // Converting this to a min would handle comparisons between positive
11237 // and negative zero incorrectly, and swapping the operands would
11238 // cause it to handle NaNs incorrectly.
11239 if (!UnsafeFPMath &&
11240 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000011241 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000011242 break;
11243 std::swap(LHS, RHS);
11244 }
Dan Gohman670e5392009-09-21 18:03:22 +000011245 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000011246 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011247 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000011248 // Converting this to a min would handle NaNs incorrectly.
11249 if (!UnsafeFPMath &&
11250 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
11251 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011252 Opcode = X86ISD::FMIN;
11253 break;
11254 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000011255 // Converting this to a min would handle both negative zeros and NaNs
11256 // incorrectly, but we can swap the operands to fix both.
11257 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000011258 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011259 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011260 case ISD::SETGE:
11261 Opcode = X86ISD::FMIN;
11262 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011263
Dan Gohman670e5392009-09-21 18:03:22 +000011264 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000011265 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000011266 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000011267 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011268 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000011269 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011270 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000011271 // Converting this to a max would handle comparisons between positive
11272 // and negative zero incorrectly, and swapping the operands would
11273 // cause it to handle NaNs incorrectly.
11274 if (!UnsafeFPMath &&
11275 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000011276 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000011277 break;
11278 std::swap(LHS, RHS);
11279 }
Dan Gohman670e5392009-09-21 18:03:22 +000011280 Opcode = X86ISD::FMAX;
11281 break;
11282 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000011283 // Converting this to a max would handle both negative zeros and NaNs
11284 // incorrectly, but we can swap the operands to fix both.
11285 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000011286 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011287 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000011288 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011289 Opcode = X86ISD::FMAX;
11290 break;
11291 }
Chris Lattner83e6c992006-10-04 06:57:07 +000011292 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011293
Chris Lattner47b4ce82009-03-11 05:48:52 +000011294 if (Opcode)
11295 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000011296 }
Eric Christopherfd179292009-08-27 18:07:15 +000011297
Chris Lattnerd1980a52009-03-12 06:52:53 +000011298 // If this is a select between two integer constants, try to do some
11299 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000011300 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
11301 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000011302 // Don't do this for crazy integer types.
11303 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
11304 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000011305 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000011306 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000011307
Chris Lattnercee56e72009-03-13 05:53:31 +000011308 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000011309 // Efficiently invertible.
11310 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
11311 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
11312 isa<ConstantSDNode>(Cond.getOperand(1))))) {
11313 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000011314 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000011315 }
Eric Christopherfd179292009-08-27 18:07:15 +000011316
Chris Lattnerd1980a52009-03-12 06:52:53 +000011317 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000011318 if (FalseC->getAPIntValue() == 0 &&
11319 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000011320 if (NeedsCondInvert) // Invert the condition if needed.
11321 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
11322 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011323
Chris Lattnerd1980a52009-03-12 06:52:53 +000011324 // Zero extend the condition if needed.
11325 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011326
Chris Lattnercee56e72009-03-13 05:53:31 +000011327 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000011328 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000011329 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000011330 }
Eric Christopherfd179292009-08-27 18:07:15 +000011331
Chris Lattner97a29a52009-03-13 05:22:11 +000011332 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000011333 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000011334 if (NeedsCondInvert) // Invert the condition if needed.
11335 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
11336 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011337
Chris Lattner97a29a52009-03-13 05:22:11 +000011338 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000011339 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
11340 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000011341 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000011342 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000011343 }
Eric Christopherfd179292009-08-27 18:07:15 +000011344
Chris Lattnercee56e72009-03-13 05:53:31 +000011345 // Optimize cases that will turn into an LEA instruction. This requires
11346 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000011347 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000011348 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000011349 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000011350
Chris Lattnercee56e72009-03-13 05:53:31 +000011351 bool isFastMultiplier = false;
11352 if (Diff < 10) {
11353 switch ((unsigned char)Diff) {
11354 default: break;
11355 case 1: // result = add base, cond
11356 case 2: // result = lea base( , cond*2)
11357 case 3: // result = lea base(cond, cond*2)
11358 case 4: // result = lea base( , cond*4)
11359 case 5: // result = lea base(cond, cond*4)
11360 case 8: // result = lea base( , cond*8)
11361 case 9: // result = lea base(cond, cond*8)
11362 isFastMultiplier = true;
11363 break;
11364 }
11365 }
Eric Christopherfd179292009-08-27 18:07:15 +000011366
Chris Lattnercee56e72009-03-13 05:53:31 +000011367 if (isFastMultiplier) {
11368 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
11369 if (NeedsCondInvert) // Invert the condition if needed.
11370 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
11371 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011372
Chris Lattnercee56e72009-03-13 05:53:31 +000011373 // Zero extend the condition if needed.
11374 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
11375 Cond);
11376 // Scale the condition by the difference.
11377 if (Diff != 1)
11378 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
11379 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011380
Chris Lattnercee56e72009-03-13 05:53:31 +000011381 // Add the base if non-zero.
11382 if (FalseC->getAPIntValue() != 0)
11383 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
11384 SDValue(FalseC, 0));
11385 return Cond;
11386 }
Eric Christopherfd179292009-08-27 18:07:15 +000011387 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000011388 }
11389 }
Eric Christopherfd179292009-08-27 18:07:15 +000011390
Dan Gohman475871a2008-07-27 21:46:04 +000011391 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000011392}
11393
Chris Lattnerd1980a52009-03-12 06:52:53 +000011394/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
11395static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
11396 TargetLowering::DAGCombinerInfo &DCI) {
11397 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000011398
Chris Lattnerd1980a52009-03-12 06:52:53 +000011399 // If the flag operand isn't dead, don't touch this CMOV.
11400 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
11401 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000011402
Evan Chengb5a55d92011-05-24 01:48:22 +000011403 SDValue FalseOp = N->getOperand(0);
11404 SDValue TrueOp = N->getOperand(1);
11405 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
11406 SDValue Cond = N->getOperand(3);
11407 if (CC == X86::COND_E || CC == X86::COND_NE) {
11408 switch (Cond.getOpcode()) {
11409 default: break;
11410 case X86ISD::BSR:
11411 case X86ISD::BSF:
11412 // If operand of BSR / BSF are proven never zero, then ZF cannot be set.
11413 if (DAG.isKnownNeverZero(Cond.getOperand(0)))
11414 return (CC == X86::COND_E) ? FalseOp : TrueOp;
11415 }
11416 }
11417
Chris Lattnerd1980a52009-03-12 06:52:53 +000011418 // If this is a select between two integer constants, try to do some
11419 // optimizations. Note that the operands are ordered the opposite of SELECT
11420 // operands.
Evan Chengb5a55d92011-05-24 01:48:22 +000011421 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(TrueOp)) {
11422 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(FalseOp)) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000011423 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
11424 // larger than FalseC (the false value).
Chris Lattnerd1980a52009-03-12 06:52:53 +000011425 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
11426 CC = X86::GetOppositeBranchCondition(CC);
11427 std::swap(TrueC, FalseC);
11428 }
Eric Christopherfd179292009-08-27 18:07:15 +000011429
Chris Lattnerd1980a52009-03-12 06:52:53 +000011430 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000011431 // This is efficient for any integer data type (including i8/i16) and
11432 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000011433 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000011434 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11435 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011436
Chris Lattnerd1980a52009-03-12 06:52:53 +000011437 // Zero extend the condition if needed.
11438 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011439
Chris Lattnerd1980a52009-03-12 06:52:53 +000011440 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
11441 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000011442 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000011443 if (N->getNumValues() == 2) // Dead flag value?
11444 return DCI.CombineTo(N, Cond, SDValue());
11445 return Cond;
11446 }
Eric Christopherfd179292009-08-27 18:07:15 +000011447
Chris Lattnercee56e72009-03-13 05:53:31 +000011448 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
11449 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000011450 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000011451 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11452 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011453
Chris Lattner97a29a52009-03-13 05:22:11 +000011454 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000011455 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
11456 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000011457 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
11458 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000011459
Chris Lattner97a29a52009-03-13 05:22:11 +000011460 if (N->getNumValues() == 2) // Dead flag value?
11461 return DCI.CombineTo(N, Cond, SDValue());
11462 return Cond;
11463 }
Eric Christopherfd179292009-08-27 18:07:15 +000011464
Chris Lattnercee56e72009-03-13 05:53:31 +000011465 // Optimize cases that will turn into an LEA instruction. This requires
11466 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000011467 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000011468 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000011469 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000011470
Chris Lattnercee56e72009-03-13 05:53:31 +000011471 bool isFastMultiplier = false;
11472 if (Diff < 10) {
11473 switch ((unsigned char)Diff) {
11474 default: break;
11475 case 1: // result = add base, cond
11476 case 2: // result = lea base( , cond*2)
11477 case 3: // result = lea base(cond, cond*2)
11478 case 4: // result = lea base( , cond*4)
11479 case 5: // result = lea base(cond, cond*4)
11480 case 8: // result = lea base( , cond*8)
11481 case 9: // result = lea base(cond, cond*8)
11482 isFastMultiplier = true;
11483 break;
11484 }
11485 }
Eric Christopherfd179292009-08-27 18:07:15 +000011486
Chris Lattnercee56e72009-03-13 05:53:31 +000011487 if (isFastMultiplier) {
11488 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000011489 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11490 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000011491 // Zero extend the condition if needed.
11492 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
11493 Cond);
11494 // Scale the condition by the difference.
11495 if (Diff != 1)
11496 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
11497 DAG.getConstant(Diff, Cond.getValueType()));
11498
11499 // Add the base if non-zero.
11500 if (FalseC->getAPIntValue() != 0)
11501 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
11502 SDValue(FalseC, 0));
11503 if (N->getNumValues() == 2) // Dead flag value?
11504 return DCI.CombineTo(N, Cond, SDValue());
11505 return Cond;
11506 }
Eric Christopherfd179292009-08-27 18:07:15 +000011507 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000011508 }
11509 }
11510 return SDValue();
11511}
11512
11513
Evan Cheng0b0cd912009-03-28 05:57:29 +000011514/// PerformMulCombine - Optimize a single multiply with constant into two
11515/// in order to implement it with two cheaper instructions, e.g.
11516/// LEA + SHL, LEA + LEA.
11517static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
11518 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000011519 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
11520 return SDValue();
11521
Owen Andersone50ed302009-08-10 22:56:29 +000011522 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000011523 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000011524 return SDValue();
11525
11526 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
11527 if (!C)
11528 return SDValue();
11529 uint64_t MulAmt = C->getZExtValue();
11530 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
11531 return SDValue();
11532
11533 uint64_t MulAmt1 = 0;
11534 uint64_t MulAmt2 = 0;
11535 if ((MulAmt % 9) == 0) {
11536 MulAmt1 = 9;
11537 MulAmt2 = MulAmt / 9;
11538 } else if ((MulAmt % 5) == 0) {
11539 MulAmt1 = 5;
11540 MulAmt2 = MulAmt / 5;
11541 } else if ((MulAmt % 3) == 0) {
11542 MulAmt1 = 3;
11543 MulAmt2 = MulAmt / 3;
11544 }
11545 if (MulAmt2 &&
11546 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
11547 DebugLoc DL = N->getDebugLoc();
11548
11549 if (isPowerOf2_64(MulAmt2) &&
11550 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
11551 // If second multiplifer is pow2, issue it first. We want the multiply by
11552 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
11553 // is an add.
11554 std::swap(MulAmt1, MulAmt2);
11555
11556 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000011557 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000011558 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000011559 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000011560 else
Evan Cheng73f24c92009-03-30 21:36:47 +000011561 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000011562 DAG.getConstant(MulAmt1, VT));
11563
Eric Christopherfd179292009-08-27 18:07:15 +000011564 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000011565 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000011566 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000011567 else
Evan Cheng73f24c92009-03-30 21:36:47 +000011568 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000011569 DAG.getConstant(MulAmt2, VT));
11570
11571 // Do not add new nodes to DAG combiner worklist.
11572 DCI.CombineTo(N, NewMul, false);
11573 }
11574 return SDValue();
11575}
11576
Evan Chengad9c0a32009-12-15 00:53:42 +000011577static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
11578 SDValue N0 = N->getOperand(0);
11579 SDValue N1 = N->getOperand(1);
11580 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
11581 EVT VT = N0.getValueType();
11582
11583 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
11584 // since the result of setcc_c is all zero's or all ones.
11585 if (N1C && N0.getOpcode() == ISD::AND &&
11586 N0.getOperand(1).getOpcode() == ISD::Constant) {
11587 SDValue N00 = N0.getOperand(0);
11588 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
11589 ((N00.getOpcode() == ISD::ANY_EXTEND ||
11590 N00.getOpcode() == ISD::ZERO_EXTEND) &&
11591 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
11592 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
11593 APInt ShAmt = N1C->getAPIntValue();
11594 Mask = Mask.shl(ShAmt);
11595 if (Mask != 0)
11596 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
11597 N00, DAG.getConstant(Mask, VT));
11598 }
11599 }
11600
11601 return SDValue();
11602}
Evan Cheng0b0cd912009-03-28 05:57:29 +000011603
Nate Begeman740ab032009-01-26 00:52:55 +000011604/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
11605/// when possible.
11606static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
11607 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000011608 EVT VT = N->getValueType(0);
11609 if (!VT.isVector() && VT.isInteger() &&
11610 N->getOpcode() == ISD::SHL)
11611 return PerformSHLCombine(N, DAG);
11612
Nate Begeman740ab032009-01-26 00:52:55 +000011613 // On X86 with SSE2 support, we can transform this to a vector shift if
11614 // all elements are shifted by the same amount. We can't do this in legalize
11615 // because the a constant vector is typically transformed to a constant pool
11616 // so we have no knowledge of the shift amount.
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011617 if (!Subtarget->hasSSE2())
11618 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000011619
Owen Anderson825b72b2009-08-11 20:47:22 +000011620 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011621 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000011622
Mon P Wang3becd092009-01-28 08:12:05 +000011623 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000011624 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000011625 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000011626 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000011627 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
11628 unsigned NumElts = VT.getVectorNumElements();
11629 unsigned i = 0;
11630 for (; i != NumElts; ++i) {
11631 SDValue Arg = ShAmtOp.getOperand(i);
11632 if (Arg.getOpcode() == ISD::UNDEF) continue;
11633 BaseShAmt = Arg;
11634 break;
11635 }
11636 for (; i != NumElts; ++i) {
11637 SDValue Arg = ShAmtOp.getOperand(i);
11638 if (Arg.getOpcode() == ISD::UNDEF) continue;
11639 if (Arg != BaseShAmt) {
11640 return SDValue();
11641 }
11642 }
11643 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000011644 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000011645 SDValue InVec = ShAmtOp.getOperand(0);
11646 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
11647 unsigned NumElts = InVec.getValueType().getVectorNumElements();
11648 unsigned i = 0;
11649 for (; i != NumElts; ++i) {
11650 SDValue Arg = InVec.getOperand(i);
11651 if (Arg.getOpcode() == ISD::UNDEF) continue;
11652 BaseShAmt = Arg;
11653 break;
11654 }
11655 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
11656 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000011657 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000011658 if (C->getZExtValue() == SplatIdx)
11659 BaseShAmt = InVec.getOperand(1);
11660 }
11661 }
11662 if (BaseShAmt.getNode() == 0)
11663 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
11664 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +000011665 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011666 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000011667
Mon P Wangefa42202009-09-03 19:56:25 +000011668 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000011669 if (EltVT.bitsGT(MVT::i32))
11670 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
11671 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000011672 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000011673
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011674 // The shift amount is identical so we can do a vector shift.
11675 SDValue ValOp = N->getOperand(0);
11676 switch (N->getOpcode()) {
11677 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000011678 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011679 break;
11680 case ISD::SHL:
Owen Anderson825b72b2009-08-11 20:47:22 +000011681 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011682 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011683 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011684 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011685 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011686 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011687 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011688 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011689 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011690 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011691 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011692 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011693 break;
11694 case ISD::SRA:
Owen Anderson825b72b2009-08-11 20:47:22 +000011695 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011696 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011697 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011698 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011699 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011700 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011701 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011702 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011703 break;
11704 case ISD::SRL:
Owen Anderson825b72b2009-08-11 20:47:22 +000011705 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011706 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011707 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011708 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011709 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011710 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011711 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011712 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011713 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011714 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011715 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011716 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011717 break;
Nate Begeman740ab032009-01-26 00:52:55 +000011718 }
11719 return SDValue();
11720}
11721
Nate Begemanb65c1752010-12-17 22:55:37 +000011722
Stuart Hastings865f0932011-06-03 23:53:54 +000011723// CMPEQCombine - Recognize the distinctive (AND (setcc ...) (setcc ..))
11724// where both setccs reference the same FP CMP, and rewrite for CMPEQSS
11725// and friends. Likewise for OR -> CMPNEQSS.
11726static SDValue CMPEQCombine(SDNode *N, SelectionDAG &DAG,
11727 TargetLowering::DAGCombinerInfo &DCI,
11728 const X86Subtarget *Subtarget) {
11729 unsigned opcode;
11730
11731 // SSE1 supports CMP{eq|ne}SS, and SSE2 added CMP{eq|ne}SD, but
11732 // we're requiring SSE2 for both.
11733 if (Subtarget->hasSSE2() && isAndOrOfSetCCs(SDValue(N, 0U), opcode)) {
11734 SDValue N0 = N->getOperand(0);
11735 SDValue N1 = N->getOperand(1);
11736 SDValue CMP0 = N0->getOperand(1);
11737 SDValue CMP1 = N1->getOperand(1);
11738 DebugLoc DL = N->getDebugLoc();
11739
11740 // The SETCCs should both refer to the same CMP.
11741 if (CMP0.getOpcode() != X86ISD::CMP || CMP0 != CMP1)
11742 return SDValue();
11743
11744 SDValue CMP00 = CMP0->getOperand(0);
11745 SDValue CMP01 = CMP0->getOperand(1);
11746 EVT VT = CMP00.getValueType();
11747
11748 if (VT == MVT::f32 || VT == MVT::f64) {
11749 bool ExpectingFlags = false;
11750 // Check for any users that want flags:
11751 for (SDNode::use_iterator UI = N->use_begin(),
11752 UE = N->use_end();
11753 !ExpectingFlags && UI != UE; ++UI)
11754 switch (UI->getOpcode()) {
11755 default:
11756 case ISD::BR_CC:
11757 case ISD::BRCOND:
11758 case ISD::SELECT:
11759 ExpectingFlags = true;
11760 break;
11761 case ISD::CopyToReg:
11762 case ISD::SIGN_EXTEND:
11763 case ISD::ZERO_EXTEND:
11764 case ISD::ANY_EXTEND:
11765 break;
11766 }
11767
11768 if (!ExpectingFlags) {
11769 enum X86::CondCode cc0 = (enum X86::CondCode)N0.getConstantOperandVal(0);
11770 enum X86::CondCode cc1 = (enum X86::CondCode)N1.getConstantOperandVal(0);
11771
11772 if (cc1 == X86::COND_E || cc1 == X86::COND_NE) {
11773 X86::CondCode tmp = cc0;
11774 cc0 = cc1;
11775 cc1 = tmp;
11776 }
11777
11778 if ((cc0 == X86::COND_E && cc1 == X86::COND_NP) ||
11779 (cc0 == X86::COND_NE && cc1 == X86::COND_P)) {
11780 bool is64BitFP = (CMP00.getValueType() == MVT::f64);
11781 X86ISD::NodeType NTOperator = is64BitFP ?
11782 X86ISD::FSETCCsd : X86ISD::FSETCCss;
11783 // FIXME: need symbolic constants for these magic numbers.
11784 // See X86ATTInstPrinter.cpp:printSSECC().
11785 unsigned x86cc = (cc0 == X86::COND_E) ? 0 : 4;
11786 SDValue OnesOrZeroesF = DAG.getNode(NTOperator, DL, MVT::f32, CMP00, CMP01,
11787 DAG.getConstant(x86cc, MVT::i8));
11788 SDValue OnesOrZeroesI = DAG.getNode(ISD::BITCAST, DL, MVT::i32,
11789 OnesOrZeroesF);
11790 SDValue ANDed = DAG.getNode(ISD::AND, DL, MVT::i32, OnesOrZeroesI,
11791 DAG.getConstant(1, MVT::i32));
11792 SDValue OneBitOfTruth = DAG.getNode(ISD::TRUNCATE, DL, MVT::i8, ANDed);
11793 return OneBitOfTruth;
11794 }
11795 }
11796 }
11797 }
11798 return SDValue();
11799}
11800
Nate Begemanb65c1752010-12-17 22:55:37 +000011801static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
11802 TargetLowering::DAGCombinerInfo &DCI,
11803 const X86Subtarget *Subtarget) {
11804 if (DCI.isBeforeLegalizeOps())
11805 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011806
Stuart Hastings865f0932011-06-03 23:53:54 +000011807 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
11808 if (R.getNode())
11809 return R;
11810
Nate Begemanb65c1752010-12-17 22:55:37 +000011811 // Want to form PANDN nodes, in the hopes of then easily combining them with
11812 // OR and AND nodes to form PBLEND/PSIGN.
11813 EVT VT = N->getValueType(0);
11814 if (VT != MVT::v2i64)
11815 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011816
Nate Begemanb65c1752010-12-17 22:55:37 +000011817 SDValue N0 = N->getOperand(0);
11818 SDValue N1 = N->getOperand(1);
11819 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011820
Nate Begemanb65c1752010-12-17 22:55:37 +000011821 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011822 if (N0.getOpcode() == ISD::XOR &&
Nate Begemanb65c1752010-12-17 22:55:37 +000011823 ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
11824 return DAG.getNode(X86ISD::PANDN, DL, VT, N0.getOperand(0), N1);
11825
11826 // Check RHS for vnot
11827 if (N1.getOpcode() == ISD::XOR &&
11828 ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
11829 return DAG.getNode(X86ISD::PANDN, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011830
Nate Begemanb65c1752010-12-17 22:55:37 +000011831 return SDValue();
11832}
11833
Evan Cheng760d1942010-01-04 21:22:48 +000011834static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000011835 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000011836 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +000011837 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000011838 return SDValue();
11839
Stuart Hastings865f0932011-06-03 23:53:54 +000011840 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
11841 if (R.getNode())
11842 return R;
11843
Evan Cheng760d1942010-01-04 21:22:48 +000011844 EVT VT = N->getValueType(0);
Nate Begemanb65c1752010-12-17 22:55:37 +000011845 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64 && VT != MVT::v2i64)
Evan Cheng760d1942010-01-04 21:22:48 +000011846 return SDValue();
11847
Evan Cheng760d1942010-01-04 21:22:48 +000011848 SDValue N0 = N->getOperand(0);
11849 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011850
Nate Begemanb65c1752010-12-17 22:55:37 +000011851 // look for psign/blend
11852 if (Subtarget->hasSSSE3()) {
11853 if (VT == MVT::v2i64) {
11854 // Canonicalize pandn to RHS
11855 if (N0.getOpcode() == X86ISD::PANDN)
11856 std::swap(N0, N1);
11857 // or (and (m, x), (pandn m, y))
11858 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::PANDN) {
11859 SDValue Mask = N1.getOperand(0);
11860 SDValue X = N1.getOperand(1);
11861 SDValue Y;
11862 if (N0.getOperand(0) == Mask)
11863 Y = N0.getOperand(1);
11864 if (N0.getOperand(1) == Mask)
11865 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011866
Nate Begemanb65c1752010-12-17 22:55:37 +000011867 // Check to see if the mask appeared in both the AND and PANDN and
11868 if (!Y.getNode())
11869 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011870
Nate Begemanb65c1752010-12-17 22:55:37 +000011871 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
11872 if (Mask.getOpcode() != ISD::BITCAST ||
11873 X.getOpcode() != ISD::BITCAST ||
11874 Y.getOpcode() != ISD::BITCAST)
11875 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011876
Nate Begemanb65c1752010-12-17 22:55:37 +000011877 // Look through mask bitcast.
11878 Mask = Mask.getOperand(0);
11879 EVT MaskVT = Mask.getValueType();
11880
11881 // Validate that the Mask operand is a vector sra node. The sra node
11882 // will be an intrinsic.
11883 if (Mask.getOpcode() != ISD::INTRINSIC_WO_CHAIN)
11884 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011885
Nate Begemanb65c1752010-12-17 22:55:37 +000011886 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
11887 // there is no psrai.b
11888 switch (cast<ConstantSDNode>(Mask.getOperand(0))->getZExtValue()) {
11889 case Intrinsic::x86_sse2_psrai_w:
11890 case Intrinsic::x86_sse2_psrai_d:
11891 break;
11892 default: return SDValue();
11893 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011894
Nate Begemanb65c1752010-12-17 22:55:37 +000011895 // Check that the SRA is all signbits.
11896 SDValue SraC = Mask.getOperand(2);
11897 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
11898 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
11899 if ((SraAmt + 1) != EltBits)
11900 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011901
Nate Begemanb65c1752010-12-17 22:55:37 +000011902 DebugLoc DL = N->getDebugLoc();
11903
11904 // Now we know we at least have a plendvb with the mask val. See if
11905 // we can form a psignb/w/d.
11906 // psign = x.type == y.type == mask.type && y = sub(0, x);
11907 X = X.getOperand(0);
11908 Y = Y.getOperand(0);
11909 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
11910 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
11911 X.getValueType() == MaskVT && X.getValueType() == Y.getValueType()){
11912 unsigned Opc = 0;
11913 switch (EltBits) {
11914 case 8: Opc = X86ISD::PSIGNB; break;
11915 case 16: Opc = X86ISD::PSIGNW; break;
11916 case 32: Opc = X86ISD::PSIGND; break;
11917 default: break;
11918 }
11919 if (Opc) {
11920 SDValue Sign = DAG.getNode(Opc, DL, MaskVT, X, Mask.getOperand(1));
11921 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Sign);
11922 }
11923 }
11924 // PBLENDVB only available on SSE 4.1
11925 if (!Subtarget->hasSSE41())
11926 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011927
Nate Begemanb65c1752010-12-17 22:55:37 +000011928 X = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, X);
11929 Y = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Y);
11930 Mask = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Mask);
Nate Begeman672fb622010-12-20 22:04:24 +000011931 Mask = DAG.getNode(X86ISD::PBLENDVB, DL, MVT::v16i8, X, Y, Mask);
Nate Begemanb65c1752010-12-17 22:55:37 +000011932 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Mask);
11933 }
11934 }
11935 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011936
Nate Begemanb65c1752010-12-17 22:55:37 +000011937 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000011938 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
11939 std::swap(N0, N1);
11940 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
11941 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000011942 if (!N0.hasOneUse() || !N1.hasOneUse())
11943 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000011944
11945 SDValue ShAmt0 = N0.getOperand(1);
11946 if (ShAmt0.getValueType() != MVT::i8)
11947 return SDValue();
11948 SDValue ShAmt1 = N1.getOperand(1);
11949 if (ShAmt1.getValueType() != MVT::i8)
11950 return SDValue();
11951 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
11952 ShAmt0 = ShAmt0.getOperand(0);
11953 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
11954 ShAmt1 = ShAmt1.getOperand(0);
11955
11956 DebugLoc DL = N->getDebugLoc();
11957 unsigned Opc = X86ISD::SHLD;
11958 SDValue Op0 = N0.getOperand(0);
11959 SDValue Op1 = N1.getOperand(0);
11960 if (ShAmt0.getOpcode() == ISD::SUB) {
11961 Opc = X86ISD::SHRD;
11962 std::swap(Op0, Op1);
11963 std::swap(ShAmt0, ShAmt1);
11964 }
11965
Evan Cheng8b1190a2010-04-28 01:18:01 +000011966 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000011967 if (ShAmt1.getOpcode() == ISD::SUB) {
11968 SDValue Sum = ShAmt1.getOperand(0);
11969 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000011970 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
11971 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
11972 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
11973 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000011974 return DAG.getNode(Opc, DL, VT,
11975 Op0, Op1,
11976 DAG.getNode(ISD::TRUNCATE, DL,
11977 MVT::i8, ShAmt0));
11978 }
11979 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
11980 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
11981 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000011982 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000011983 return DAG.getNode(Opc, DL, VT,
11984 N0.getOperand(0), N1.getOperand(0),
11985 DAG.getNode(ISD::TRUNCATE, DL,
11986 MVT::i8, ShAmt0));
11987 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011988
Evan Cheng760d1942010-01-04 21:22:48 +000011989 return SDValue();
11990}
11991
Chris Lattner149a4e52008-02-22 02:09:43 +000011992/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000011993static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000011994 const X86Subtarget *Subtarget) {
Chris Lattner149a4e52008-02-22 02:09:43 +000011995 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
11996 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000011997 // A preferable solution to the general problem is to figure out the right
11998 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000011999
12000 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng7e2ff772008-05-08 00:57:18 +000012001 StoreSDNode *St = cast<StoreSDNode>(N);
Owen Andersone50ed302009-08-10 22:56:29 +000012002 EVT VT = St->getValue().getValueType();
Evan Cheng536e6672009-03-12 05:59:15 +000012003 if (VT.getSizeInBits() != 64)
12004 return SDValue();
12005
Devang Patel578efa92009-06-05 21:57:13 +000012006 const Function *F = DAG.getMachineFunction().getFunction();
12007 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Eric Christopherfd179292009-08-27 18:07:15 +000012008 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
Devang Patel578efa92009-06-05 21:57:13 +000012009 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +000012010 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000012011 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000012012 isa<LoadSDNode>(St->getValue()) &&
12013 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
12014 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000012015 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000012016 LoadSDNode *Ld = 0;
12017 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000012018 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000012019 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000012020 // Must be a store of a load. We currently handle two cases: the load
12021 // is a direct child, and it's under an intervening TokenFactor. It is
12022 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000012023 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000012024 Ld = cast<LoadSDNode>(St->getChain());
12025 else if (St->getValue().hasOneUse() &&
12026 ChainVal->getOpcode() == ISD::TokenFactor) {
12027 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000012028 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000012029 TokenFactorIndex = i;
12030 Ld = cast<LoadSDNode>(St->getValue());
12031 } else
12032 Ops.push_back(ChainVal->getOperand(i));
12033 }
12034 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000012035
Evan Cheng536e6672009-03-12 05:59:15 +000012036 if (!Ld || !ISD::isNormalLoad(Ld))
12037 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000012038
Evan Cheng536e6672009-03-12 05:59:15 +000012039 // If this is not the MMX case, i.e. we are just turning i64 load/store
12040 // into f64 load/store, avoid the transformation if there are multiple
12041 // uses of the loaded value.
12042 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
12043 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000012044
Evan Cheng536e6672009-03-12 05:59:15 +000012045 DebugLoc LdDL = Ld->getDebugLoc();
12046 DebugLoc StDL = N->getDebugLoc();
12047 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
12048 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
12049 // pair instead.
12050 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000012051 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000012052 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
12053 Ld->getPointerInfo(), Ld->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000012054 Ld->isNonTemporal(), Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000012055 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000012056 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000012057 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000012058 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000012059 Ops.size());
12060 }
Evan Cheng536e6672009-03-12 05:59:15 +000012061 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000012062 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000012063 St->isVolatile(), St->isNonTemporal(),
12064 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000012065 }
Evan Cheng536e6672009-03-12 05:59:15 +000012066
12067 // Otherwise, lower to two pairs of 32-bit loads / stores.
12068 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000012069 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
12070 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000012071
Owen Anderson825b72b2009-08-11 20:47:22 +000012072 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000012073 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000012074 Ld->isVolatile(), Ld->isNonTemporal(),
12075 Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000012076 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000012077 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000012078 Ld->isVolatile(), Ld->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000012079 MinAlign(Ld->getAlignment(), 4));
12080
12081 SDValue NewChain = LoLd.getValue(1);
12082 if (TokenFactorIndex != -1) {
12083 Ops.push_back(LoLd);
12084 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000012085 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000012086 Ops.size());
12087 }
12088
12089 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000012090 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
12091 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000012092
12093 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000012094 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000012095 St->isVolatile(), St->isNonTemporal(),
12096 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000012097 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000012098 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000012099 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000012100 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000012101 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000012102 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000012103 }
Dan Gohman475871a2008-07-27 21:46:04 +000012104 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000012105}
12106
Chris Lattner6cf73262008-01-25 06:14:17 +000012107/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
12108/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000012109static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000012110 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
12111 // F[X]OR(0.0, x) -> x
12112 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000012113 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
12114 if (C->getValueAPF().isPosZero())
12115 return N->getOperand(1);
12116 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
12117 if (C->getValueAPF().isPosZero())
12118 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000012119 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000012120}
12121
12122/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000012123static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000012124 // FAND(0.0, x) -> 0.0
12125 // FAND(x, 0.0) -> 0.0
12126 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
12127 if (C->getValueAPF().isPosZero())
12128 return N->getOperand(0);
12129 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
12130 if (C->getValueAPF().isPosZero())
12131 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000012132 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000012133}
12134
Dan Gohmane5af2d32009-01-29 01:59:02 +000012135static SDValue PerformBTCombine(SDNode *N,
12136 SelectionDAG &DAG,
12137 TargetLowering::DAGCombinerInfo &DCI) {
12138 // BT ignores high bits in the bit index operand.
12139 SDValue Op1 = N->getOperand(1);
12140 if (Op1.hasOneUse()) {
12141 unsigned BitWidth = Op1.getValueSizeInBits();
12142 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
12143 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000012144 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
12145 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000012146 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000012147 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
12148 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
12149 DCI.CommitTargetLoweringOpt(TLO);
12150 }
12151 return SDValue();
12152}
Chris Lattner83e6c992006-10-04 06:57:07 +000012153
Eli Friedman7a5e5552009-06-07 06:52:44 +000012154static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
12155 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000012156 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000012157 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000012158 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000012159 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000012160 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000012161 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000012162 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000012163 }
12164 return SDValue();
12165}
12166
Evan Cheng2e489c42009-12-16 00:53:11 +000012167static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
12168 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
12169 // (and (i32 x86isd::setcc_carry), 1)
12170 // This eliminates the zext. This transformation is necessary because
12171 // ISD::SETCC is always legalized to i8.
12172 DebugLoc dl = N->getDebugLoc();
12173 SDValue N0 = N->getOperand(0);
12174 EVT VT = N->getValueType(0);
12175 if (N0.getOpcode() == ISD::AND &&
12176 N0.hasOneUse() &&
12177 N0.getOperand(0).hasOneUse()) {
12178 SDValue N00 = N0.getOperand(0);
12179 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
12180 return SDValue();
12181 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
12182 if (!C || C->getZExtValue() != 1)
12183 return SDValue();
12184 return DAG.getNode(ISD::AND, dl, VT,
12185 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
12186 N00.getOperand(0), N00.getOperand(1)),
12187 DAG.getConstant(1, VT));
12188 }
12189
12190 return SDValue();
12191}
12192
Chris Lattnerc19d1c32010-12-19 22:08:31 +000012193// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
12194static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG) {
12195 unsigned X86CC = N->getConstantOperandVal(0);
12196 SDValue EFLAG = N->getOperand(1);
12197 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012198
Chris Lattnerc19d1c32010-12-19 22:08:31 +000012199 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
12200 // a zext and produces an all-ones bit which is more useful than 0/1 in some
12201 // cases.
12202 if (X86CC == X86::COND_B)
12203 return DAG.getNode(ISD::AND, DL, MVT::i8,
12204 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
12205 DAG.getConstant(X86CC, MVT::i8), EFLAG),
12206 DAG.getConstant(1, MVT::i8));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012207
Chris Lattnerc19d1c32010-12-19 22:08:31 +000012208 return SDValue();
12209}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012210
Benjamin Kramer1396c402011-06-18 11:09:41 +000012211static SDValue PerformSINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG,
12212 const X86TargetLowering *XTLI) {
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000012213 SDValue Op0 = N->getOperand(0);
12214 // Transform (SINT_TO_FP (i64 ...)) into an x87 operation if we have
12215 // a 32-bit target where SSE doesn't support i64->FP operations.
12216 if (Op0.getOpcode() == ISD::LOAD) {
12217 LoadSDNode *Ld = cast<LoadSDNode>(Op0.getNode());
12218 EVT VT = Ld->getValueType(0);
12219 if (!Ld->isVolatile() && !N->getValueType(0).isVector() &&
12220 ISD::isNON_EXTLoad(Op0.getNode()) && Op0.hasOneUse() &&
12221 !XTLI->getSubtarget()->is64Bit() &&
12222 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Benjamin Kramer1396c402011-06-18 11:09:41 +000012223 SDValue FILDChain = XTLI->BuildFILD(SDValue(N, 0), Ld->getValueType(0),
12224 Ld->getChain(), Op0, DAG);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000012225 DAG.ReplaceAllUsesOfValueWith(Op0.getValue(1), FILDChain.getValue(1));
12226 return FILDChain;
12227 }
12228 }
12229 return SDValue();
12230}
12231
Chris Lattner23a01992010-12-20 01:37:09 +000012232// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
12233static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
12234 X86TargetLowering::DAGCombinerInfo &DCI) {
12235 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
12236 // the result is either zero or one (depending on the input carry bit).
12237 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
12238 if (X86::isZeroNode(N->getOperand(0)) &&
12239 X86::isZeroNode(N->getOperand(1)) &&
12240 // We don't have a good way to replace an EFLAGS use, so only do this when
12241 // dead right now.
12242 SDValue(N, 1).use_empty()) {
12243 DebugLoc DL = N->getDebugLoc();
12244 EVT VT = N->getValueType(0);
12245 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
12246 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
12247 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
12248 DAG.getConstant(X86::COND_B,MVT::i8),
12249 N->getOperand(2)),
12250 DAG.getConstant(1, VT));
12251 return DCI.CombineTo(N, Res1, CarryOut);
12252 }
12253
12254 return SDValue();
12255}
12256
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000012257// fold (add Y, (sete X, 0)) -> adc 0, Y
12258// (add Y, (setne X, 0)) -> sbb -1, Y
12259// (sub (sete X, 0), Y) -> sbb 0, Y
12260// (sub (setne X, 0), Y) -> adc -1, Y
12261static SDValue OptimizeConditonalInDecrement(SDNode *N, SelectionDAG &DAG) {
12262 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012263
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000012264 // Look through ZExts.
12265 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
12266 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
12267 return SDValue();
12268
12269 SDValue SetCC = Ext.getOperand(0);
12270 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
12271 return SDValue();
12272
12273 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
12274 if (CC != X86::COND_E && CC != X86::COND_NE)
12275 return SDValue();
12276
12277 SDValue Cmp = SetCC.getOperand(1);
12278 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000012279 !X86::isZeroNode(Cmp.getOperand(1)) ||
12280 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000012281 return SDValue();
12282
12283 SDValue CmpOp0 = Cmp.getOperand(0);
12284 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
12285 DAG.getConstant(1, CmpOp0.getValueType()));
12286
12287 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
12288 if (CC == X86::COND_NE)
12289 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
12290 DL, OtherVal.getValueType(), OtherVal,
12291 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
12292 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
12293 DL, OtherVal.getValueType(), OtherVal,
12294 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
12295}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000012296
Dan Gohman475871a2008-07-27 21:46:04 +000012297SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000012298 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000012299 SelectionDAG &DAG = DCI.DAG;
12300 switch (N->getOpcode()) {
12301 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012302 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerc19d1c32010-12-19 22:08:31 +000012303 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, *this);
Chris Lattneraf723b92008-01-25 05:46:26 +000012304 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +000012305 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000012306 case ISD::ADD:
12307 case ISD::SUB: return OptimizeConditonalInDecrement(N, DAG);
Chris Lattner23a01992010-12-20 01:37:09 +000012308 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000012309 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000012310 case ISD::SHL:
12311 case ISD::SRA:
12312 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000012313 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000012314 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000012315 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000012316 case ISD::SINT_TO_FP: return PerformSINT_TO_FPCombine(N, DAG, this);
Chris Lattner6cf73262008-01-25 06:14:17 +000012317 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000012318 case X86ISD::FOR: return PerformFORCombine(N, DAG);
12319 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000012320 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000012321 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Evan Cheng2e489c42009-12-16 00:53:11 +000012322 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG);
Chris Lattnerc19d1c32010-12-19 22:08:31 +000012323 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000012324 case X86ISD::SHUFPS: // Handle all target specific shuffles
12325 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000012326 case X86ISD::PALIGN:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000012327 case X86ISD::PUNPCKHBW:
12328 case X86ISD::PUNPCKHWD:
12329 case X86ISD::PUNPCKHDQ:
12330 case X86ISD::PUNPCKHQDQ:
12331 case X86ISD::UNPCKHPS:
12332 case X86ISD::UNPCKHPD:
12333 case X86ISD::PUNPCKLBW:
12334 case X86ISD::PUNPCKLWD:
12335 case X86ISD::PUNPCKLDQ:
12336 case X86ISD::PUNPCKLQDQ:
12337 case X86ISD::UNPCKLPS:
12338 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +000012339 case X86ISD::VUNPCKLPS:
12340 case X86ISD::VUNPCKLPD:
12341 case X86ISD::VUNPCKLPSY:
12342 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000012343 case X86ISD::MOVHLPS:
12344 case X86ISD::MOVLHPS:
12345 case X86ISD::PSHUFD:
12346 case X86ISD::PSHUFHW:
12347 case X86ISD::PSHUFLW:
12348 case X86ISD::MOVSS:
12349 case X86ISD::MOVSD:
Mon P Wanga0fd0d52010-12-19 23:55:53 +000012350 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI);
Evan Cheng206ee9d2006-07-07 08:33:52 +000012351 }
12352
Dan Gohman475871a2008-07-27 21:46:04 +000012353 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000012354}
12355
Evan Chenge5b51ac2010-04-17 06:13:15 +000012356/// isTypeDesirableForOp - Return true if the target has native support for
12357/// the specified value type and it is 'desirable' to use the type for the
12358/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
12359/// instruction encodings are longer and some i16 instructions are slow.
12360bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
12361 if (!isTypeLegal(VT))
12362 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000012363 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000012364 return true;
12365
12366 switch (Opc) {
12367 default:
12368 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000012369 case ISD::LOAD:
12370 case ISD::SIGN_EXTEND:
12371 case ISD::ZERO_EXTEND:
12372 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000012373 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000012374 case ISD::SRL:
12375 case ISD::SUB:
12376 case ISD::ADD:
12377 case ISD::MUL:
12378 case ISD::AND:
12379 case ISD::OR:
12380 case ISD::XOR:
12381 return false;
12382 }
12383}
12384
12385/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000012386/// beneficial for dag combiner to promote the specified node. If true, it
12387/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000012388bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000012389 EVT VT = Op.getValueType();
12390 if (VT != MVT::i16)
12391 return false;
12392
Evan Cheng4c26e932010-04-19 19:29:22 +000012393 bool Promote = false;
12394 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000012395 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000012396 default: break;
12397 case ISD::LOAD: {
12398 LoadSDNode *LD = cast<LoadSDNode>(Op);
12399 // If the non-extending load has a single use and it's not live out, then it
12400 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000012401 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
12402 Op.hasOneUse()*/) {
12403 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
12404 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
12405 // The only case where we'd want to promote LOAD (rather then it being
12406 // promoted as an operand is when it's only use is liveout.
12407 if (UI->getOpcode() != ISD::CopyToReg)
12408 return false;
12409 }
12410 }
Evan Cheng4c26e932010-04-19 19:29:22 +000012411 Promote = true;
12412 break;
12413 }
12414 case ISD::SIGN_EXTEND:
12415 case ISD::ZERO_EXTEND:
12416 case ISD::ANY_EXTEND:
12417 Promote = true;
12418 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000012419 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000012420 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000012421 SDValue N0 = Op.getOperand(0);
12422 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000012423 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000012424 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000012425 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000012426 break;
12427 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000012428 case ISD::ADD:
12429 case ISD::MUL:
12430 case ISD::AND:
12431 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000012432 case ISD::XOR:
12433 Commute = true;
12434 // fallthrough
12435 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000012436 SDValue N0 = Op.getOperand(0);
12437 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000012438 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000012439 return false;
12440 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000012441 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000012442 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000012443 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000012444 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000012445 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000012446 }
12447 }
12448
12449 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000012450 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000012451}
12452
Evan Cheng60c07e12006-07-05 22:17:51 +000012453//===----------------------------------------------------------------------===//
12454// X86 Inline Assembly Support
12455//===----------------------------------------------------------------------===//
12456
Chris Lattnerb8105652009-07-20 17:51:36 +000012457bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
12458 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000012459
12460 std::string AsmStr = IA->getAsmString();
12461
12462 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000012463 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000012464 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000012465
12466 switch (AsmPieces.size()) {
12467 default: return false;
12468 case 1:
12469 AsmStr = AsmPieces[0];
12470 AsmPieces.clear();
12471 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
12472
Chris Lattner7a2bdde2011-04-15 05:18:47 +000012473 // FIXME: this should verify that we are targeting a 486 or better. If not,
Evan Cheng55d42002011-01-08 01:24:27 +000012474 // we will turn this bswap into something that will be lowered to logical ops
12475 // instead of emitting the bswap asm. For now, we don't support 486 or lower
12476 // so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000012477 // bswap $0
12478 if (AsmPieces.size() == 2 &&
12479 (AsmPieces[0] == "bswap" ||
12480 AsmPieces[0] == "bswapq" ||
12481 AsmPieces[0] == "bswapl") &&
12482 (AsmPieces[1] == "$0" ||
12483 AsmPieces[1] == "${0:q}")) {
12484 // No need to check constraints, nothing other than the equivalent of
12485 // "=r,0" would be valid here.
Evan Cheng55d42002011-01-08 01:24:27 +000012486 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
12487 if (!Ty || Ty->getBitWidth() % 16 != 0)
12488 return false;
12489 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000012490 }
12491 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000012492 if (CI->getType()->isIntegerTy(16) &&
Chris Lattnerb8105652009-07-20 17:51:36 +000012493 AsmPieces.size() == 3 &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000012494 (AsmPieces[0] == "rorw" || AsmPieces[0] == "rolw") &&
Chris Lattnerb8105652009-07-20 17:51:36 +000012495 AsmPieces[1] == "$$8," &&
12496 AsmPieces[2] == "${0:w}" &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000012497 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
12498 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000012499 const std::string &ConstraintsStr = IA->getConstraintString();
12500 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000012501 std::sort(AsmPieces.begin(), AsmPieces.end());
12502 if (AsmPieces.size() == 4 &&
12503 AsmPieces[0] == "~{cc}" &&
12504 AsmPieces[1] == "~{dirflag}" &&
12505 AsmPieces[2] == "~{flags}" &&
12506 AsmPieces[3] == "~{fpsr}") {
Evan Cheng55d42002011-01-08 01:24:27 +000012507 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
12508 if (!Ty || Ty->getBitWidth() % 16 != 0)
12509 return false;
12510 return IntrinsicLowering::LowerToByteSwap(CI);
Dan Gohman0ef701e2010-03-04 19:58:08 +000012511 }
Chris Lattnerb8105652009-07-20 17:51:36 +000012512 }
12513 break;
12514 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000012515 if (CI->getType()->isIntegerTy(32) &&
12516 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
12517 SmallVector<StringRef, 4> Words;
12518 SplitString(AsmPieces[0], Words, " \t,");
12519 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
12520 Words[2] == "${0:w}") {
12521 Words.clear();
12522 SplitString(AsmPieces[1], Words, " \t,");
12523 if (Words.size() == 3 && Words[0] == "rorl" && Words[1] == "$$16" &&
12524 Words[2] == "$0") {
12525 Words.clear();
12526 SplitString(AsmPieces[2], Words, " \t,");
12527 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
12528 Words[2] == "${0:w}") {
12529 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000012530 const std::string &ConstraintsStr = IA->getConstraintString();
12531 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Peter Collingbourne948cf022010-11-13 19:54:30 +000012532 std::sort(AsmPieces.begin(), AsmPieces.end());
12533 if (AsmPieces.size() == 4 &&
12534 AsmPieces[0] == "~{cc}" &&
12535 AsmPieces[1] == "~{dirflag}" &&
12536 AsmPieces[2] == "~{flags}" &&
12537 AsmPieces[3] == "~{fpsr}") {
Evan Cheng55d42002011-01-08 01:24:27 +000012538 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
12539 if (!Ty || Ty->getBitWidth() % 16 != 0)
12540 return false;
12541 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000012542 }
12543 }
12544 }
12545 }
12546 }
Evan Cheng55d42002011-01-08 01:24:27 +000012547
12548 if (CI->getType()->isIntegerTy(64)) {
12549 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
12550 if (Constraints.size() >= 2 &&
12551 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
12552 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
12553 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
12554 SmallVector<StringRef, 4> Words;
12555 SplitString(AsmPieces[0], Words, " \t");
12556 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
Chris Lattnerb8105652009-07-20 17:51:36 +000012557 Words.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000012558 SplitString(AsmPieces[1], Words, " \t");
12559 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
12560 Words.clear();
12561 SplitString(AsmPieces[2], Words, " \t,");
12562 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
12563 Words[2] == "%edx") {
12564 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
12565 if (!Ty || Ty->getBitWidth() % 16 != 0)
12566 return false;
12567 return IntrinsicLowering::LowerToByteSwap(CI);
12568 }
Chris Lattnerb8105652009-07-20 17:51:36 +000012569 }
12570 }
12571 }
12572 }
12573 break;
12574 }
12575 return false;
12576}
12577
12578
12579
Chris Lattnerf4dff842006-07-11 02:54:03 +000012580/// getConstraintType - Given a constraint letter, return the type of
12581/// constraint it is for this target.
12582X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000012583X86TargetLowering::getConstraintType(const std::string &Constraint) const {
12584 if (Constraint.size() == 1) {
12585 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000012586 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000012587 case 'q':
12588 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000012589 case 'f':
12590 case 't':
12591 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000012592 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000012593 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000012594 case 'Y':
Eric Christopher31b5f002011-07-07 22:29:07 +000012595 case 'l':
Chris Lattner4234f572007-03-25 02:14:49 +000012596 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000012597 case 'a':
12598 case 'b':
12599 case 'c':
12600 case 'd':
12601 case 'S':
12602 case 'D':
12603 case 'A':
12604 return C_Register;
12605 case 'I':
12606 case 'J':
12607 case 'K':
12608 case 'L':
12609 case 'M':
12610 case 'N':
12611 case 'G':
12612 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000012613 case 'e':
12614 case 'Z':
12615 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000012616 default:
12617 break;
12618 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000012619 }
Chris Lattner4234f572007-03-25 02:14:49 +000012620 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000012621}
12622
John Thompson44ab89e2010-10-29 17:29:13 +000012623/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000012624/// This object must already have been set up with the operand type
12625/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000012626TargetLowering::ConstraintWeight
12627 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000012628 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000012629 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012630 Value *CallOperandVal = info.CallOperandVal;
12631 // If we don't have a value, we can't do a match,
12632 // but allow it at the lowest weight.
12633 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000012634 return CW_Default;
12635 const Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000012636 // Look at the constraint type.
12637 switch (*constraint) {
12638 default:
John Thompson44ab89e2010-10-29 17:29:13 +000012639 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
12640 case 'R':
12641 case 'q':
12642 case 'Q':
12643 case 'a':
12644 case 'b':
12645 case 'c':
12646 case 'd':
12647 case 'S':
12648 case 'D':
12649 case 'A':
12650 if (CallOperandVal->getType()->isIntegerTy())
12651 weight = CW_SpecificReg;
12652 break;
12653 case 'f':
12654 case 't':
12655 case 'u':
12656 if (type->isFloatingPointTy())
12657 weight = CW_SpecificReg;
12658 break;
12659 case 'y':
Chris Lattner2a786eb2010-12-19 20:19:20 +000012660 if (type->isX86_MMXTy() && Subtarget->hasMMX())
John Thompson44ab89e2010-10-29 17:29:13 +000012661 weight = CW_SpecificReg;
12662 break;
12663 case 'x':
12664 case 'Y':
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012665 if ((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasXMM())
John Thompson44ab89e2010-10-29 17:29:13 +000012666 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012667 break;
12668 case 'I':
12669 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
12670 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000012671 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012672 }
12673 break;
John Thompson44ab89e2010-10-29 17:29:13 +000012674 case 'J':
12675 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12676 if (C->getZExtValue() <= 63)
12677 weight = CW_Constant;
12678 }
12679 break;
12680 case 'K':
12681 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12682 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
12683 weight = CW_Constant;
12684 }
12685 break;
12686 case 'L':
12687 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12688 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
12689 weight = CW_Constant;
12690 }
12691 break;
12692 case 'M':
12693 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12694 if (C->getZExtValue() <= 3)
12695 weight = CW_Constant;
12696 }
12697 break;
12698 case 'N':
12699 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12700 if (C->getZExtValue() <= 0xff)
12701 weight = CW_Constant;
12702 }
12703 break;
12704 case 'G':
12705 case 'C':
12706 if (dyn_cast<ConstantFP>(CallOperandVal)) {
12707 weight = CW_Constant;
12708 }
12709 break;
12710 case 'e':
12711 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12712 if ((C->getSExtValue() >= -0x80000000LL) &&
12713 (C->getSExtValue() <= 0x7fffffffLL))
12714 weight = CW_Constant;
12715 }
12716 break;
12717 case 'Z':
12718 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12719 if (C->getZExtValue() <= 0xffffffff)
12720 weight = CW_Constant;
12721 }
12722 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012723 }
12724 return weight;
12725}
12726
Dale Johannesenba2a0b92008-01-29 02:21:21 +000012727/// LowerXConstraint - try to replace an X constraint, which matches anything,
12728/// with another that has more specific requirements based on the type of the
12729/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000012730const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000012731LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000012732 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
12733 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000012734 if (ConstraintVT.isFloatingPoint()) {
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012735 if (Subtarget->hasXMMInt())
Chris Lattner5e764232008-04-26 23:02:14 +000012736 return "Y";
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012737 if (Subtarget->hasXMM())
Chris Lattner5e764232008-04-26 23:02:14 +000012738 return "x";
12739 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012740
Chris Lattner5e764232008-04-26 23:02:14 +000012741 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000012742}
12743
Chris Lattner48884cd2007-08-25 00:47:38 +000012744/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
12745/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000012746void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +000012747 std::string &Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000012748 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000012749 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000012750 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000012751
Eric Christopher100c8332011-06-02 23:16:42 +000012752 // Only support length 1 constraints for now.
12753 if (Constraint.length() > 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +000012754
Eric Christopher100c8332011-06-02 23:16:42 +000012755 char ConstraintLetter = Constraint[0];
12756 switch (ConstraintLetter) {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012757 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000012758 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000012759 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000012760 if (C->getZExtValue() <= 31) {
12761 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000012762 break;
12763 }
Devang Patel84f7fd22007-03-17 00:13:28 +000012764 }
Chris Lattner48884cd2007-08-25 00:47:38 +000012765 return;
Evan Cheng364091e2008-09-22 23:57:37 +000012766 case 'J':
12767 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000012768 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000012769 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
12770 break;
12771 }
12772 }
12773 return;
12774 case 'K':
12775 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000012776 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000012777 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
12778 break;
12779 }
12780 }
12781 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000012782 case 'N':
12783 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000012784 if (C->getZExtValue() <= 255) {
12785 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000012786 break;
12787 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000012788 }
Chris Lattner48884cd2007-08-25 00:47:38 +000012789 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000012790 case 'e': {
12791 // 32-bit signed value
12792 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000012793 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
12794 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000012795 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000012796 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000012797 break;
12798 }
12799 // FIXME gcc accepts some relocatable values here too, but only in certain
12800 // memory models; it's complicated.
12801 }
12802 return;
12803 }
12804 case 'Z': {
12805 // 32-bit unsigned value
12806 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000012807 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
12808 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000012809 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
12810 break;
12811 }
12812 }
12813 // FIXME gcc accepts some relocatable values here too, but only in certain
12814 // memory models; it's complicated.
12815 return;
12816 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012817 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012818 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000012819 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000012820 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000012821 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000012822 break;
12823 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012824
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000012825 // In any sort of PIC mode addresses need to be computed at runtime by
12826 // adding in a register or some sort of table lookup. These can't
12827 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000012828 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000012829 return;
12830
Chris Lattnerdc43a882007-05-03 16:52:29 +000012831 // If we are in non-pic codegen mode, we allow the address of a global (with
12832 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000012833 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000012834 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000012835
Chris Lattner49921962009-05-08 18:23:14 +000012836 // Match either (GA), (GA+C), (GA+C1+C2), etc.
12837 while (1) {
12838 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
12839 Offset += GA->getOffset();
12840 break;
12841 } else if (Op.getOpcode() == ISD::ADD) {
12842 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
12843 Offset += C->getZExtValue();
12844 Op = Op.getOperand(0);
12845 continue;
12846 }
12847 } else if (Op.getOpcode() == ISD::SUB) {
12848 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
12849 Offset += -C->getZExtValue();
12850 Op = Op.getOperand(0);
12851 continue;
12852 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012853 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012854
Chris Lattner49921962009-05-08 18:23:14 +000012855 // Otherwise, this isn't something we can handle, reject it.
12856 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000012857 }
Eric Christopherfd179292009-08-27 18:07:15 +000012858
Dan Gohman46510a72010-04-15 01:51:59 +000012859 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012860 // If we require an extra load to get this address, as in PIC mode, we
12861 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000012862 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
12863 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012864 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000012865
Devang Patel0d881da2010-07-06 22:08:15 +000012866 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
12867 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000012868 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012869 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012870 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012871
Gabor Greifba36cb52008-08-28 21:40:38 +000012872 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000012873 Ops.push_back(Result);
12874 return;
12875 }
Dale Johannesen1784d162010-06-25 21:55:36 +000012876 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012877}
12878
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012879std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000012880X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000012881 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000012882 // First, see if this is a constraint that directly corresponds to an LLVM
12883 // register class.
12884 if (Constraint.size() == 1) {
12885 // GCC Constraint Letters
12886 switch (Constraint[0]) {
12887 default: break;
Eric Christopherd176af82011-06-29 17:23:50 +000012888 // TODO: Slight differences here in allocation order and leaving
12889 // RIP in the class. Do they matter any more here than they do
12890 // in the normal allocation?
12891 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
12892 if (Subtarget->is64Bit()) {
12893 if (VT == MVT::i32)
12894 return std::make_pair(0U, X86::GR32RegisterClass);
12895 else if (VT == MVT::i16)
12896 return std::make_pair(0U, X86::GR16RegisterClass);
12897 else if (VT == MVT::i8)
12898 return std::make_pair(0U, X86::GR8RegisterClass);
12899 else if (VT == MVT::i64)
12900 return std::make_pair(0U, X86::GR64RegisterClass);
12901 break;
12902 }
12903 // 32-bit fallthrough
12904 case 'Q': // Q_REGS
12905 if (VT == MVT::i32)
12906 return std::make_pair(0U, X86::GR32_ABCDRegisterClass);
12907 else if (VT == MVT::i16)
12908 return std::make_pair(0U, X86::GR16_ABCDRegisterClass);
12909 else if (VT == MVT::i8)
12910 return std::make_pair(0U, X86::GR8_ABCD_LRegisterClass);
12911 else if (VT == MVT::i64)
12912 return std::make_pair(0U, X86::GR64_ABCDRegisterClass);
12913 break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000012914 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000012915 case 'l': // INDEX_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000012916 if (VT == MVT::i8)
Chris Lattner0f65cad2007-04-09 05:49:22 +000012917 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012918 if (VT == MVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +000012919 return std::make_pair(0U, X86::GR16RegisterClass);
Eric Christopher2bbecd82011-05-19 21:33:47 +000012920 if (VT == MVT::i32 || VT == MVT::f32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +000012921 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +000012922 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000012923 case 'R': // LEGACY_REGS
12924 if (VT == MVT::i8)
12925 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
12926 if (VT == MVT::i16)
12927 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
12928 if (VT == MVT::i32 || !Subtarget->is64Bit())
12929 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
12930 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000012931 case 'f': // FP Stack registers.
12932 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
12933 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000012934 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000012935 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012936 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000012937 return std::make_pair(0U, X86::RFP64RegisterClass);
12938 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000012939 case 'y': // MMX_REGS if MMX allowed.
12940 if (!Subtarget->hasMMX()) break;
12941 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000012942 case 'Y': // SSE_REGS if SSE2 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012943 if (!Subtarget->hasXMMInt()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000012944 // FALL THROUGH.
12945 case 'x': // SSE_REGS if SSE1 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012946 if (!Subtarget->hasXMM()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000012947
Owen Anderson825b72b2009-08-11 20:47:22 +000012948 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000012949 default: break;
12950 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000012951 case MVT::f32:
12952 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +000012953 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012954 case MVT::f64:
12955 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +000012956 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000012957 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000012958 case MVT::v16i8:
12959 case MVT::v8i16:
12960 case MVT::v4i32:
12961 case MVT::v2i64:
12962 case MVT::v4f32:
12963 case MVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +000012964 return std::make_pair(0U, X86::VR128RegisterClass);
12965 }
Chris Lattnerad043e82007-04-09 05:11:28 +000012966 break;
12967 }
12968 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012969
Chris Lattnerf76d1802006-07-31 23:26:50 +000012970 // Use the default implementation in TargetLowering to convert the register
12971 // constraint into a member of a register class.
12972 std::pair<unsigned, const TargetRegisterClass*> Res;
12973 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000012974
12975 // Not found as a standard register?
12976 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000012977 // Map st(0) -> st(7) -> ST0
12978 if (Constraint.size() == 7 && Constraint[0] == '{' &&
12979 tolower(Constraint[1]) == 's' &&
12980 tolower(Constraint[2]) == 't' &&
12981 Constraint[3] == '(' &&
12982 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
12983 Constraint[5] == ')' &&
12984 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000012985
Chris Lattner56d77c72009-09-13 22:41:48 +000012986 Res.first = X86::ST0+Constraint[4]-'0';
12987 Res.second = X86::RFP80RegisterClass;
12988 return Res;
12989 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000012990
Chris Lattner56d77c72009-09-13 22:41:48 +000012991 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000012992 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000012993 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +000012994 Res.second = X86::RFP80RegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000012995 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000012996 }
Chris Lattner56d77c72009-09-13 22:41:48 +000012997
12998 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000012999 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000013000 Res.first = X86::EFLAGS;
13001 Res.second = X86::CCRRegisterClass;
13002 return Res;
13003 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000013004
Dale Johannesen330169f2008-11-13 21:52:36 +000013005 // 'A' means EAX + EDX.
13006 if (Constraint == "A") {
13007 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +000013008 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000013009 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000013010 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000013011 return Res;
13012 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013013
Chris Lattnerf76d1802006-07-31 23:26:50 +000013014 // Otherwise, check to see if this is a register class of the wrong value
13015 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
13016 // turn into {ax},{dx}.
13017 if (Res.second->hasType(VT))
13018 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013019
Chris Lattnerf76d1802006-07-31 23:26:50 +000013020 // All of the single-register GCC register classes map their values onto
13021 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
13022 // really want an 8-bit or 32-bit register, map to the appropriate register
13023 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +000013024 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000013025 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000013026 unsigned DestReg = 0;
13027 switch (Res.first) {
13028 default: break;
13029 case X86::AX: DestReg = X86::AL; break;
13030 case X86::DX: DestReg = X86::DL; break;
13031 case X86::CX: DestReg = X86::CL; break;
13032 case X86::BX: DestReg = X86::BL; break;
13033 }
13034 if (DestReg) {
13035 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000013036 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000013037 }
Owen Anderson825b72b2009-08-11 20:47:22 +000013038 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000013039 unsigned DestReg = 0;
13040 switch (Res.first) {
13041 default: break;
13042 case X86::AX: DestReg = X86::EAX; break;
13043 case X86::DX: DestReg = X86::EDX; break;
13044 case X86::CX: DestReg = X86::ECX; break;
13045 case X86::BX: DestReg = X86::EBX; break;
13046 case X86::SI: DestReg = X86::ESI; break;
13047 case X86::DI: DestReg = X86::EDI; break;
13048 case X86::BP: DestReg = X86::EBP; break;
13049 case X86::SP: DestReg = X86::ESP; break;
13050 }
13051 if (DestReg) {
13052 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000013053 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000013054 }
Owen Anderson825b72b2009-08-11 20:47:22 +000013055 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000013056 unsigned DestReg = 0;
13057 switch (Res.first) {
13058 default: break;
13059 case X86::AX: DestReg = X86::RAX; break;
13060 case X86::DX: DestReg = X86::RDX; break;
13061 case X86::CX: DestReg = X86::RCX; break;
13062 case X86::BX: DestReg = X86::RBX; break;
13063 case X86::SI: DestReg = X86::RSI; break;
13064 case X86::DI: DestReg = X86::RDI; break;
13065 case X86::BP: DestReg = X86::RBP; break;
13066 case X86::SP: DestReg = X86::RSP; break;
13067 }
13068 if (DestReg) {
13069 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000013070 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000013071 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000013072 }
Chris Lattner6ba50a92008-08-26 06:19:02 +000013073 } else if (Res.second == X86::FR32RegisterClass ||
13074 Res.second == X86::FR64RegisterClass ||
13075 Res.second == X86::VR128RegisterClass) {
13076 // Handle references to XMM physical registers that got mapped into the
13077 // wrong class. This can happen with constraints like {xmm0} where the
13078 // target independent register mapper will just pick the first match it can
13079 // find, ignoring the required type.
Owen Anderson825b72b2009-08-11 20:47:22 +000013080 if (VT == MVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +000013081 Res.second = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +000013082 else if (VT == MVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +000013083 Res.second = X86::FR64RegisterClass;
13084 else if (X86::VR128RegisterClass->hasType(VT))
13085 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000013086 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013087
Chris Lattnerf76d1802006-07-31 23:26:50 +000013088 return Res;
13089}