blob: 5096d9ae2edfdabe8ec8d1b336f355abbe825179 [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");
Julien Lerougef2960822011-07-08 21:40:25 +0000238 setLibcallName(RTLIB::SREM_I64, "_allrem");
239 setLibcallName(RTLIB::UREM_I64, "_aullrem");
240 setLibcallName(RTLIB::MUL_I64, "_allmul");
Michael J. Spencer335b8062010-10-11 05:29:15 +0000241 setLibcallName(RTLIB::FPTOUINT_F64_I64, "_ftol2");
Michael J. Spencer94f7eeb2010-10-19 07:32:52 +0000242 setLibcallName(RTLIB::FPTOUINT_F32_I64, "_ftol2");
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000243 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::X86_StdCall);
Michael J. Spencer335b8062010-10-11 05:29:15 +0000244 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::X86_StdCall);
Julien Lerougef2960822011-07-08 21:40:25 +0000245 setLibcallCallingConv(RTLIB::SREM_I64, CallingConv::X86_StdCall);
246 setLibcallCallingConv(RTLIB::UREM_I64, CallingConv::X86_StdCall);
247 setLibcallCallingConv(RTLIB::MUL_I64, CallingConv::X86_StdCall);
Michael J. Spencer6dad10e2010-10-27 18:52:38 +0000248 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I64, CallingConv::C);
249 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I64, CallingConv::C);
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000250 }
251
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000252 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +0000253 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000254 setUseUnderscoreSetJmp(false);
255 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000256 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000257 // MS runtime is weird: it exports _setjmp, but longjmp!
258 setUseUnderscoreSetJmp(true);
259 setUseUnderscoreLongJmp(false);
260 } else {
261 setUseUnderscoreSetJmp(true);
262 setUseUnderscoreLongJmp(true);
263 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000264
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000265 // Set up the register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000266 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
Dan Gohman71edb242010-04-30 18:30:26 +0000267 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000268 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
Evan Cheng25ab6902006-09-08 06:48:29 +0000269 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000270 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000271
Owen Anderson825b72b2009-08-11 20:47:22 +0000272 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Evan Chengc5484282006-10-04 00:56:09 +0000273
Scott Michelfdc40a02009-02-17 22:15:04 +0000274 // We don't accept any truncstore of integer registers.
Owen Anderson825b72b2009-08-11 20:47:22 +0000275 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000276 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000277 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000278 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000279 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
280 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
Evan Cheng7f042682008-10-15 02:05:31 +0000281
282 // SETOEQ and SETUNE require checking two conditions.
Owen Anderson825b72b2009-08-11 20:47:22 +0000283 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
284 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
285 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
286 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
287 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
288 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
Chris Lattnerddf89562008-01-17 19:59:44 +0000289
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000290 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
291 // operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000292 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
293 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
294 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +0000295
Evan Cheng25ab6902006-09-08 06:48:29 +0000296 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000297 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
298 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
Eli Friedman948e95a2009-05-23 09:59:16 +0000299 } else if (!UseSoftFloat) {
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000300 // We have an algorithm for SSE2->double, and we turn this into a
301 // 64-bit FILD followed by conditional FADD for other targets.
302 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Eli Friedman948e95a2009-05-23 09:59:16 +0000303 // We have an algorithm for SSE2, and we turn this into a 64-bit
304 // FILD for other targets.
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000305 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000306 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000307
308 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
309 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000310 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
311 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000312
Devang Patel6a784892009-06-05 18:48:29 +0000313 if (!UseSoftFloat) {
Bill Wendling105be5a2009-03-13 08:41:47 +0000314 // SSE has no i16 to fp conversion, only i32
315 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000316 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000317 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000318 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000319 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000320 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
321 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000322 }
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000323 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000324 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
325 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000326 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000327
Dale Johannesen73328d12007-09-19 23:55:34 +0000328 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
329 // are Legal, f80 is custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000330 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
331 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000332
Evan Cheng02568ff2006-01-30 22:13:22 +0000333 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
334 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000335 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
336 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
Evan Cheng02568ff2006-01-30 22:13:22 +0000337
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000338 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000339 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000340 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000341 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000342 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000343 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
344 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000345 }
346
347 // Handle FP_TO_UINT by promoting the destination to a larger signed
348 // conversion.
Owen Anderson825b72b2009-08-11 20:47:22 +0000349 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
350 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
351 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000352
Evan Cheng25ab6902006-09-08 06:48:29 +0000353 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000354 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
355 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Eli Friedman948e95a2009-05-23 09:59:16 +0000356 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000357 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000358 // Expand FP_TO_UINT into a select.
359 // FIXME: We would like to use a Custom expander here eventually to do
360 // the optimal thing for SSE vs. the default expansion in the legalizer.
Owen Anderson825b72b2009-08-11 20:47:22 +0000361 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000362 else
Eli Friedman948e95a2009-05-23 09:59:16 +0000363 // With SSE3 we can use fisttpll to convert to a signed i64; without
364 // SSE, we're stuck with a fistpll.
Owen Anderson825b72b2009-08-11 20:47:22 +0000365 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000366 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000367
Chris Lattner399610a2006-12-05 18:22:22 +0000368 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Michael J. Spencerec38de22010-10-10 22:04:20 +0000369 if (!X86ScalarSSEf64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000370 setOperationAction(ISD::BITCAST , MVT::f32 , Expand);
371 setOperationAction(ISD::BITCAST , MVT::i32 , Expand);
Dale Johannesene39859a2010-05-21 18:40:15 +0000372 if (Subtarget->is64Bit()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000373 setOperationAction(ISD::BITCAST , MVT::f64 , Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000374 // Without SSE, i64->f64 goes through memory.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000375 setOperationAction(ISD::BITCAST , MVT::i64 , Expand);
Dale Johannesen7d07b482010-05-21 00:52:33 +0000376 }
Chris Lattnerf3597a12006-12-05 18:45:06 +0000377 }
Chris Lattner21f66852005-12-23 05:15:23 +0000378
Dan Gohmanb00ee212008-02-18 19:34:53 +0000379 // Scalar integer divide and remainder are lowered to use operations that
380 // produce two results, to match the available instructions. This exposes
381 // the two-result form to trivial CSE, which is able to combine x/y and x%y
382 // into a single instruction.
383 //
384 // Scalar integer multiply-high is also lowered to use two-result
385 // operations, to match the available instructions. However, plain multiply
386 // (low) operations are left as Legal, as there are single-result
387 // instructions for this in x86. Using the two-result multiply instructions
388 // when both high and low results are needed must be arranged by dagcombine.
Chris Lattnere019ec12010-12-19 20:07:10 +0000389 for (unsigned i = 0, e = 4; i != e; ++i) {
390 MVT VT = IntVTs[i];
391 setOperationAction(ISD::MULHS, VT, Expand);
392 setOperationAction(ISD::MULHU, VT, Expand);
393 setOperationAction(ISD::SDIV, VT, Expand);
394 setOperationAction(ISD::UDIV, VT, Expand);
395 setOperationAction(ISD::SREM, VT, Expand);
396 setOperationAction(ISD::UREM, VT, Expand);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000397
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000398 // Add/Sub overflow ops with MVT::Glues are lowered to EFLAGS dependences.
Chris Lattnerd8ff7ec2010-12-20 01:03:27 +0000399 setOperationAction(ISD::ADDC, VT, Custom);
400 setOperationAction(ISD::ADDE, VT, Custom);
401 setOperationAction(ISD::SUBC, VT, Custom);
402 setOperationAction(ISD::SUBE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000403 }
Dan Gohmana37c9f72007-09-25 18:23:27 +0000404
Owen Anderson825b72b2009-08-11 20:47:22 +0000405 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
406 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
407 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
408 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000409 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000410 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
411 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
412 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
413 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
414 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
415 setOperationAction(ISD::FREM , MVT::f32 , Expand);
416 setOperationAction(ISD::FREM , MVT::f64 , Expand);
417 setOperationAction(ISD::FREM , MVT::f80 , Expand);
418 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000419
Owen Anderson825b72b2009-08-11 20:47:22 +0000420 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
421 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000422 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
423 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000424 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
425 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000426 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000427 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
428 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000429 }
430
Benjamin Kramer1292c222010-12-04 20:32:23 +0000431 if (Subtarget->hasPOPCNT()) {
432 setOperationAction(ISD::CTPOP , MVT::i8 , Promote);
433 } else {
434 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
435 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
436 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
437 if (Subtarget->is64Bit())
438 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
439 }
440
Owen Anderson825b72b2009-08-11 20:47:22 +0000441 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
442 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000443
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000444 // These should be promoted to a larger select which is supported.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000445 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000446 // X86 wants to expand cmov itself.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000447 setOperationAction(ISD::SELECT , MVT::i8 , Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000448 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000449 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
450 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
451 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
452 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
453 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000454 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000455 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
456 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
457 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
458 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000459 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000460 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
Andrew Trickf6c39412011-03-23 23:11:02 +0000461 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000462 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000463 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000464
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000465 // Darwin ABI issue.
Owen Anderson825b72b2009-08-11 20:47:22 +0000466 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
467 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
468 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
469 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000470 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000471 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
472 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000473 setOperationAction(ISD::BlockAddress , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000474 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000475 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
476 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
477 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
478 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000479 setOperationAction(ISD::BlockAddress , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000480 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000481 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Owen Anderson825b72b2009-08-11 20:47:22 +0000482 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
483 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
484 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000485 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000486 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
487 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
488 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000489 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000490
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000491 if (Subtarget->hasXMM())
Owen Anderson825b72b2009-08-11 20:47:22 +0000492 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000493
Eric Christopher9a9d2752010-07-22 02:48:34 +0000494 // We may not have a libcall for MEMBARRIER so we should lower this.
495 setOperationAction(ISD::MEMBARRIER , MVT::Other, Custom);
Michael J. Spencerec38de22010-10-10 22:04:20 +0000496
Jim Grosbachf1ab49e2010-06-23 16:25:07 +0000497 // On X86 and X86-64, atomic operations are lowered to locked instructions.
498 // Locked instructions, in turn, have implicit fence semantics (all memory
499 // operations are flushed before issuing the locked instruction, and they
500 // are not buffered), so we can fold away the common pattern of
501 // fence-atomic-fence.
502 setShouldFoldAtomicFences(true);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000503
Mon P Wang63307c32008-05-05 19:05:59 +0000504 // Expand certain atomics
Chris Lattnere019ec12010-12-19 20:07:10 +0000505 for (unsigned i = 0, e = 4; i != e; ++i) {
506 MVT VT = IntVTs[i];
507 setOperationAction(ISD::ATOMIC_CMP_SWAP, VT, Custom);
508 setOperationAction(ISD::ATOMIC_LOAD_SUB, VT, Custom);
509 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000510
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000511 if (!Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000512 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
513 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
514 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
515 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
516 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
517 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
518 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000519 }
520
Evan Cheng3c992d22006-03-07 02:02:57 +0000521 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000522 if (!Subtarget->isTargetDarwin() &&
523 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000524 !Subtarget->isTargetCygMing()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000525 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Dan Gohman44066042008-07-01 00:05:16 +0000526 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000527
Owen Anderson825b72b2009-08-11 20:47:22 +0000528 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
529 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
530 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
531 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000532 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000533 setExceptionPointerRegister(X86::RAX);
534 setExceptionSelectorRegister(X86::RDX);
535 } else {
536 setExceptionPointerRegister(X86::EAX);
537 setExceptionSelectorRegister(X86::EDX);
538 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000539 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
540 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000541
Owen Anderson825b72b2009-08-11 20:47:22 +0000542 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000543
Owen Anderson825b72b2009-08-11 20:47:22 +0000544 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000545
Nate Begemanacc398c2006-01-25 18:21:52 +0000546 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000547 setOperationAction(ISD::VASTART , MVT::Other, Custom);
548 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000549 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000550 setOperationAction(ISD::VAARG , MVT::Other, Custom);
551 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000552 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000553 setOperationAction(ISD::VAARG , MVT::Other, Expand);
554 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000555 }
Evan Chengae642192007-03-02 23:16:35 +0000556
Owen Anderson825b72b2009-08-11 20:47:22 +0000557 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
558 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
NAKAMURA Takumia2e07622011-03-24 07:07:00 +0000559 setOperationAction(ISD::DYNAMIC_STACKALLOC,
560 (Subtarget->is64Bit() ? MVT::i64 : MVT::i32),
561 (Subtarget->isTargetCOFF()
562 && !Subtarget->isTargetEnvMacho()
563 ? Custom : Expand));
Chris Lattnerb99329e2006-01-13 02:42:53 +0000564
Evan Chengc7ce29b2009-02-13 22:36:38 +0000565 if (!UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000566 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000567 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000568 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
569 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000570
Evan Cheng223547a2006-01-31 22:28:30 +0000571 // Use ANDPD to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000572 setOperationAction(ISD::FABS , MVT::f64, Custom);
573 setOperationAction(ISD::FABS , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000574
575 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000576 setOperationAction(ISD::FNEG , MVT::f64, Custom);
577 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000578
Evan Cheng68c47cb2007-01-05 07:55:56 +0000579 // Use ANDPD and ORPD to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000580 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
581 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000582
Stuart Hastings4fd0dee2011-06-01 04:39:42 +0000583 // Lower this to FGETSIGNx86 plus an AND.
584 setOperationAction(ISD::FGETSIGN, MVT::i64, Custom);
585 setOperationAction(ISD::FGETSIGN, MVT::i32, Custom);
586
Evan Chengd25e9e82006-02-02 00:28:23 +0000587 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000588 setOperationAction(ISD::FSIN , MVT::f64, Expand);
589 setOperationAction(ISD::FCOS , MVT::f64, Expand);
590 setOperationAction(ISD::FSIN , MVT::f32, Expand);
591 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000592
Chris Lattnera54aa942006-01-29 06:26:08 +0000593 // Expand FP immediates into loads from the stack, except for the special
594 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000595 addLegalFPImmediate(APFloat(+0.0)); // xorpd
596 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Evan Chengc7ce29b2009-02-13 22:36:38 +0000597 } else if (!UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000598 // Use SSE for f32, x87 for f64.
599 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000600 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
601 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000602
603 // Use ANDPS to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000604 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000605
606 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000607 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000608
Owen Anderson825b72b2009-08-11 20:47:22 +0000609 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000610
611 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000612 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
613 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000614
615 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000616 setOperationAction(ISD::FSIN , MVT::f32, Expand);
617 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000618
Nate Begemane1795842008-02-14 08:57:00 +0000619 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000620 addLegalFPImmediate(APFloat(+0.0f)); // xorps
621 addLegalFPImmediate(APFloat(+0.0)); // FLD0
622 addLegalFPImmediate(APFloat(+1.0)); // FLD1
623 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
624 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
625
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000626 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000627 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
628 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000629 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000630 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000631 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000632 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000633 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
634 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000635
Owen Anderson825b72b2009-08-11 20:47:22 +0000636 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
637 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
638 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
639 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000640
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000641 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000642 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
643 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000644 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000645 addLegalFPImmediate(APFloat(+0.0)); // FLD0
646 addLegalFPImmediate(APFloat(+1.0)); // FLD1
647 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
648 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000649 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
650 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
651 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
652 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000653 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000654
Cameron Zwarich33390842011-07-08 21:39:21 +0000655 // We don't support FMA.
656 setOperationAction(ISD::FMA, MVT::f64, Expand);
657 setOperationAction(ISD::FMA, MVT::f32, Expand);
658
Dale Johannesen59a58732007-08-05 18:49:15 +0000659 // Long double always uses X87.
Evan Cheng92722532009-03-26 23:06:32 +0000660 if (!UseSoftFloat) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000661 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
662 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
663 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000664 {
Benjamin Kramer98383962010-12-04 14:22:24 +0000665 APFloat TmpFlt = APFloat::getZero(APFloat::x87DoubleExtended);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000666 addLegalFPImmediate(TmpFlt); // FLD0
667 TmpFlt.changeSign();
668 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
Benjamin Kramer98383962010-12-04 14:22:24 +0000669
670 bool ignored;
Evan Chengc7ce29b2009-02-13 22:36:38 +0000671 APFloat TmpFlt2(+1.0);
672 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
673 &ignored);
674 addLegalFPImmediate(TmpFlt2); // FLD1
675 TmpFlt2.changeSign();
676 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
677 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000678
Evan Chengc7ce29b2009-02-13 22:36:38 +0000679 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000680 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
681 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000682 }
Cameron Zwarich33390842011-07-08 21:39:21 +0000683
684 setOperationAction(ISD::FMA, MVT::f80, Expand);
Dale Johannesen2f429012007-09-26 21:10:55 +0000685 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000686
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000687 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000688 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
689 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
690 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000691
Owen Anderson825b72b2009-08-11 20:47:22 +0000692 setOperationAction(ISD::FLOG, MVT::f80, Expand);
693 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
694 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
695 setOperationAction(ISD::FEXP, MVT::f80, Expand);
696 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000697
Mon P Wangf007a8b2008-11-06 05:31:54 +0000698 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000699 // (for widening) or expand (for scalarization). Then we will selectively
700 // turn on ones that can be effectively codegen'd.
Owen Anderson825b72b2009-08-11 20:47:22 +0000701 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
702 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
703 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
704 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
705 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
706 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
707 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
708 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
709 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
710 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
711 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
712 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
713 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
714 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
715 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
716 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
717 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000718 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
David Greenecfe33c42011-01-26 19:13:22 +0000719 setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
720 setOperationAction(ISD::INSERT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000721 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
722 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
723 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
724 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
725 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
726 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
727 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
728 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
729 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
730 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
731 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
732 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
733 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
734 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
735 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
736 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
737 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
738 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
739 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
740 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
741 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
742 setOperationAction(ISD::VSETCC, (MVT::SimpleValueType)VT, Expand);
743 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
744 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
745 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
746 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
747 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
748 setOperationAction(ISD::FP_TO_UINT, (MVT::SimpleValueType)VT, Expand);
749 setOperationAction(ISD::FP_TO_SINT, (MVT::SimpleValueType)VT, Expand);
750 setOperationAction(ISD::UINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
751 setOperationAction(ISD::SINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
Dan Gohman87862e72009-12-11 21:31:27 +0000752 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000753 setOperationAction(ISD::TRUNCATE, (MVT::SimpleValueType)VT, Expand);
754 setOperationAction(ISD::SIGN_EXTEND, (MVT::SimpleValueType)VT, Expand);
755 setOperationAction(ISD::ZERO_EXTEND, (MVT::SimpleValueType)VT, Expand);
756 setOperationAction(ISD::ANY_EXTEND, (MVT::SimpleValueType)VT, Expand);
757 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
758 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
759 setTruncStoreAction((MVT::SimpleValueType)VT,
760 (MVT::SimpleValueType)InnerVT, Expand);
761 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
762 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
763 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000764 }
765
Evan Chengc7ce29b2009-02-13 22:36:38 +0000766 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
767 // with -msoft-float, disable use of MMX as well.
Chris Lattner2a786eb2010-12-19 20:19:20 +0000768 if (!UseSoftFloat && Subtarget->hasMMX()) {
Dale Johannesene93d99c2010-10-20 21:32:10 +0000769 addRegisterClass(MVT::x86mmx, X86::VR64RegisterClass);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000770 // No operations on x86mmx supported, everything uses intrinsics.
Evan Cheng470a6ad2006-02-22 02:26:30 +0000771 }
772
Dale Johannesen0488fb62010-09-30 23:57:10 +0000773 // MMX-sized vectors (other than x86mmx) are expected to be expanded
774 // into smaller operations.
775 setOperationAction(ISD::MULHS, MVT::v8i8, Expand);
776 setOperationAction(ISD::MULHS, MVT::v4i16, Expand);
777 setOperationAction(ISD::MULHS, MVT::v2i32, Expand);
778 setOperationAction(ISD::MULHS, MVT::v1i64, Expand);
779 setOperationAction(ISD::AND, MVT::v8i8, Expand);
780 setOperationAction(ISD::AND, MVT::v4i16, Expand);
781 setOperationAction(ISD::AND, MVT::v2i32, Expand);
782 setOperationAction(ISD::AND, MVT::v1i64, Expand);
783 setOperationAction(ISD::OR, MVT::v8i8, Expand);
784 setOperationAction(ISD::OR, MVT::v4i16, Expand);
785 setOperationAction(ISD::OR, MVT::v2i32, Expand);
786 setOperationAction(ISD::OR, MVT::v1i64, Expand);
787 setOperationAction(ISD::XOR, MVT::v8i8, Expand);
788 setOperationAction(ISD::XOR, MVT::v4i16, Expand);
789 setOperationAction(ISD::XOR, MVT::v2i32, Expand);
790 setOperationAction(ISD::XOR, MVT::v1i64, Expand);
791 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Expand);
792 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Expand);
793 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Expand);
794 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Expand);
795 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v1i64, Expand);
796 setOperationAction(ISD::SELECT, MVT::v8i8, Expand);
797 setOperationAction(ISD::SELECT, MVT::v4i16, Expand);
798 setOperationAction(ISD::SELECT, MVT::v2i32, Expand);
799 setOperationAction(ISD::SELECT, MVT::v1i64, Expand);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000800 setOperationAction(ISD::BITCAST, MVT::v8i8, Expand);
801 setOperationAction(ISD::BITCAST, MVT::v4i16, Expand);
802 setOperationAction(ISD::BITCAST, MVT::v2i32, Expand);
803 setOperationAction(ISD::BITCAST, MVT::v1i64, Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000804
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000805 if (!UseSoftFloat && Subtarget->hasXMM()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000806 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000807
Owen Anderson825b72b2009-08-11 20:47:22 +0000808 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
809 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
810 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
811 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
812 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
813 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
814 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
815 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
816 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
817 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
818 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
819 setOperationAction(ISD::VSETCC, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000820 }
821
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000822 if (!UseSoftFloat && Subtarget->hasXMMInt()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000823 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000824
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000825 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
826 // registers cannot be used even for integer operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000827 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
828 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
829 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
830 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000831
Owen Anderson825b72b2009-08-11 20:47:22 +0000832 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
833 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
834 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
835 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
836 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
837 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
838 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
839 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
840 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
841 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
842 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
843 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
844 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
845 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
846 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
847 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000848
Owen Anderson825b72b2009-08-11 20:47:22 +0000849 setOperationAction(ISD::VSETCC, MVT::v2f64, Custom);
850 setOperationAction(ISD::VSETCC, MVT::v16i8, Custom);
851 setOperationAction(ISD::VSETCC, MVT::v8i16, Custom);
852 setOperationAction(ISD::VSETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000853
Owen Anderson825b72b2009-08-11 20:47:22 +0000854 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
855 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
856 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
857 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
858 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000859
Mon P Wangeb38ebf2010-01-24 00:05:03 +0000860 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2f64, Custom);
861 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2i64, Custom);
862 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i8, Custom);
863 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i16, Custom);
864 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Custom);
865
Evan Cheng2c3ae372006-04-12 21:21:57 +0000866 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Owen Anderson825b72b2009-08-11 20:47:22 +0000867 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
868 EVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000869 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000870 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000871 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000872 // Do not attempt to custom lower non-128-bit vectors
873 if (!VT.is128BitVector())
874 continue;
Owen Anderson825b72b2009-08-11 20:47:22 +0000875 setOperationAction(ISD::BUILD_VECTOR,
876 VT.getSimpleVT().SimpleTy, Custom);
877 setOperationAction(ISD::VECTOR_SHUFFLE,
878 VT.getSimpleVT().SimpleTy, Custom);
879 setOperationAction(ISD::EXTRACT_VECTOR_ELT,
880 VT.getSimpleVT().SimpleTy, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000881 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000882
Owen Anderson825b72b2009-08-11 20:47:22 +0000883 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
884 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
885 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
886 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
887 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
888 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000889
Nate Begemancdd1eec2008-02-12 22:51:28 +0000890 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000891 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
892 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000893 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000894
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000895 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000896 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; i++) {
897 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
Owen Andersone50ed302009-08-10 22:56:29 +0000898 EVT VT = SVT;
David Greene9b9838d2009-06-29 16:47:10 +0000899
900 // Do not attempt to promote non-128-bit vectors
Chris Lattner32b4b5a2010-07-05 05:53:14 +0000901 if (!VT.is128BitVector())
David Greene9b9838d2009-06-29 16:47:10 +0000902 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +0000903
Owen Andersond6662ad2009-08-10 20:46:15 +0000904 setOperationAction(ISD::AND, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000905 AddPromotedToType (ISD::AND, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000906 setOperationAction(ISD::OR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000907 AddPromotedToType (ISD::OR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000908 setOperationAction(ISD::XOR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000909 AddPromotedToType (ISD::XOR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000910 setOperationAction(ISD::LOAD, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000911 AddPromotedToType (ISD::LOAD, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000912 setOperationAction(ISD::SELECT, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000913 AddPromotedToType (ISD::SELECT, SVT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000914 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000915
Owen Anderson825b72b2009-08-11 20:47:22 +0000916 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000917
Evan Cheng2c3ae372006-04-12 21:21:57 +0000918 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000919 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
920 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
921 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
922 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000923
Owen Anderson825b72b2009-08-11 20:47:22 +0000924 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
925 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000926 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000927
Nate Begeman14d12ca2008-02-11 04:19:36 +0000928 if (Subtarget->hasSSE41()) {
Dale Johannesen54feef22010-05-27 20:12:41 +0000929 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
930 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
931 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
932 setOperationAction(ISD::FRINT, MVT::f32, Legal);
933 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
934 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
935 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
936 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
937 setOperationAction(ISD::FRINT, MVT::f64, Legal);
938 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
939
Nate Begeman14d12ca2008-02-11 04:19:36 +0000940 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +0000941 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000942
Nate Begemanbdcb5af2010-07-27 22:37:06 +0000943 // Can turn SHL into an integer multiply.
944 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
Nate Begeman51409212010-07-28 00:21:48 +0000945 setOperationAction(ISD::SHL, MVT::v16i8, Custom);
Nate Begemanbdcb5af2010-07-27 22:37:06 +0000946
Nate Begeman14d12ca2008-02-11 04:19:36 +0000947 // i8 and i16 vectors are custom , because the source register and source
948 // source memory operand types are not the same width. f32 vectors are
949 // custom since the immediate controlling the insert encodes additional
950 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +0000951 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
952 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
953 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
954 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000955
Owen Anderson825b72b2009-08-11 20:47:22 +0000956 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
957 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
958 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
959 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000960
961 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000962 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
963 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000964 }
965 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000966
Nadav Rotem43012222011-05-11 08:12:09 +0000967 if (Subtarget->hasSSE2()) {
968 setOperationAction(ISD::SRL, MVT::v2i64, Custom);
969 setOperationAction(ISD::SRL, MVT::v4i32, Custom);
970 setOperationAction(ISD::SRL, MVT::v16i8, Custom);
971
972 setOperationAction(ISD::SHL, MVT::v2i64, Custom);
973 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
974 setOperationAction(ISD::SHL, MVT::v8i16, Custom);
975
976 setOperationAction(ISD::SRA, MVT::v4i32, Custom);
977 setOperationAction(ISD::SRA, MVT::v8i16, Custom);
978 }
979
Chris Lattnera34b3cf2010-12-19 20:03:11 +0000980 if (Subtarget->hasSSE42())
Owen Anderson825b72b2009-08-11 20:47:22 +0000981 setOperationAction(ISD::VSETCC, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000982
David Greene9b9838d2009-06-29 16:47:10 +0000983 if (!UseSoftFloat && Subtarget->hasAVX()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000984 addRegisterClass(MVT::v8f32, X86::VR256RegisterClass);
985 addRegisterClass(MVT::v4f64, X86::VR256RegisterClass);
986 addRegisterClass(MVT::v8i32, X86::VR256RegisterClass);
987 addRegisterClass(MVT::v4i64, X86::VR256RegisterClass);
Bruno Cardoso Lopes405f11b2010-08-10 01:43:16 +0000988 addRegisterClass(MVT::v32i8, X86::VR256RegisterClass);
David Greened94c1012009-06-29 22:50:51 +0000989
Owen Anderson825b72b2009-08-11 20:47:22 +0000990 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +0000991 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
992 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
David Greene54d8eba2011-01-27 22:38:56 +0000993
Owen Anderson825b72b2009-08-11 20:47:22 +0000994 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
995 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
996 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
997 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
998 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
999 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001000
Owen Anderson825b72b2009-08-11 20:47:22 +00001001 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
1002 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
1003 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
1004 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
1005 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
1006 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001007
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001008 // Custom lower several nodes for 256-bit types.
David Greene54d8eba2011-01-27 22:38:56 +00001009 for (unsigned i = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001010 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++i) {
1011 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
1012 EVT VT = SVT;
1013
1014 // Extract subvector is special because the value type
1015 // (result) is 128-bit but the source is 256-bit wide.
1016 if (VT.is128BitVector())
1017 setOperationAction(ISD::EXTRACT_SUBVECTOR, SVT, Custom);
1018
1019 // Do not attempt to custom lower other non-256-bit vectors
1020 if (!VT.is256BitVector())
David Greene9b9838d2009-06-29 16:47:10 +00001021 continue;
David Greene54d8eba2011-01-27 22:38:56 +00001022
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001023 setOperationAction(ISD::BUILD_VECTOR, SVT, Custom);
1024 setOperationAction(ISD::VECTOR_SHUFFLE, SVT, Custom);
1025 setOperationAction(ISD::INSERT_VECTOR_ELT, SVT, Custom);
1026 setOperationAction(ISD::EXTRACT_VECTOR_ELT, SVT, Custom);
1027 setOperationAction(ISD::SCALAR_TO_VECTOR, SVT, Custom);
1028 setOperationAction(ISD::INSERT_SUBVECTOR, SVT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001029 }
1030
David Greene54d8eba2011-01-27 22:38:56 +00001031 // Promote v32i8, v16i16, v8i32 select, and, or, xor to v4i64.
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001032 for (unsigned i = (unsigned)MVT::v32i8; i != (unsigned)MVT::v4i64; ++i) {
1033 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
1034 EVT VT = SVT;
David Greene54d8eba2011-01-27 22:38:56 +00001035
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001036 // Do not attempt to promote non-256-bit vectors
1037 if (!VT.is256BitVector())
David Greene54d8eba2011-01-27 22:38:56 +00001038 continue;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001039
1040 setOperationAction(ISD::AND, SVT, Promote);
1041 AddPromotedToType (ISD::AND, SVT, MVT::v4i64);
1042 setOperationAction(ISD::OR, SVT, Promote);
1043 AddPromotedToType (ISD::OR, SVT, MVT::v4i64);
1044 setOperationAction(ISD::XOR, SVT, Promote);
1045 AddPromotedToType (ISD::XOR, SVT, MVT::v4i64);
1046 setOperationAction(ISD::LOAD, SVT, Promote);
1047 AddPromotedToType (ISD::LOAD, SVT, MVT::v4i64);
1048 setOperationAction(ISD::SELECT, SVT, Promote);
1049 AddPromotedToType (ISD::SELECT, SVT, MVT::v4i64);
David Greene54d8eba2011-01-27 22:38:56 +00001050 }
David Greene9b9838d2009-06-29 16:47:10 +00001051 }
1052
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001053 // SIGN_EXTEND_INREGs are evaluated by the extend type. Handle the expansion
1054 // of this type with custom code.
1055 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
1056 VT != (unsigned)MVT::LAST_VECTOR_VALUETYPE; VT++) {
1057 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT, Custom);
1058 }
1059
Evan Cheng6be2c582006-04-05 23:38:46 +00001060 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +00001061 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +00001062
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001063
Eli Friedman962f5492010-06-02 19:35:46 +00001064 // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
1065 // handle type legalization for these operations here.
Dan Gohman71c62a22010-06-02 19:13:40 +00001066 //
Eli Friedman962f5492010-06-02 19:35:46 +00001067 // FIXME: We really should do custom legalization for addition and
1068 // subtraction on x86-32 once PR3203 is fixed. We really can't do much better
1069 // than generic legalization for 64-bit multiplication-with-overflow, though.
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001070 for (unsigned i = 0, e = 3+Subtarget->is64Bit(); i != e; ++i) {
1071 // Add/Sub/Mul with overflow operations are custom lowered.
1072 MVT VT = IntVTs[i];
1073 setOperationAction(ISD::SADDO, VT, Custom);
1074 setOperationAction(ISD::UADDO, VT, Custom);
1075 setOperationAction(ISD::SSUBO, VT, Custom);
1076 setOperationAction(ISD::USUBO, VT, Custom);
1077 setOperationAction(ISD::SMULO, VT, Custom);
1078 setOperationAction(ISD::UMULO, VT, Custom);
Eli Friedmana993f0a2010-06-02 00:27:18 +00001079 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001080
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001081 // There are no 8-bit 3-address imul/mul instructions
1082 setOperationAction(ISD::SMULO, MVT::i8, Expand);
1083 setOperationAction(ISD::UMULO, MVT::i8, Expand);
Bill Wendling41ea7e72008-11-24 19:21:46 +00001084
Evan Chengd54f2d52009-03-31 19:38:51 +00001085 if (!Subtarget->is64Bit()) {
1086 // These libcalls are not available in 32-bit.
1087 setLibcallName(RTLIB::SHL_I128, 0);
1088 setLibcallName(RTLIB::SRL_I128, 0);
1089 setLibcallName(RTLIB::SRA_I128, 0);
1090 }
1091
Evan Cheng206ee9d2006-07-07 08:33:52 +00001092 // We have target-specific dag combine patterns for the following nodes:
1093 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00001094 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Evan Chengd880b972008-05-09 21:53:03 +00001095 setTargetDAGCombine(ISD::BUILD_VECTOR);
Chris Lattner83e6c992006-10-04 06:57:07 +00001096 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +00001097 setTargetDAGCombine(ISD::SHL);
1098 setTargetDAGCombine(ISD::SRA);
1099 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +00001100 setTargetDAGCombine(ISD::OR);
Nate Begemanb65c1752010-12-17 22:55:37 +00001101 setTargetDAGCombine(ISD::AND);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001102 setTargetDAGCombine(ISD::ADD);
1103 setTargetDAGCombine(ISD::SUB);
Chris Lattner149a4e52008-02-22 02:09:43 +00001104 setTargetDAGCombine(ISD::STORE);
Evan Cheng2e489c42009-12-16 00:53:11 +00001105 setTargetDAGCombine(ISD::ZERO_EXTEND);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +00001106 setTargetDAGCombine(ISD::SINT_TO_FP);
Evan Cheng0b0cd912009-03-28 05:57:29 +00001107 if (Subtarget->is64Bit())
1108 setTargetDAGCombine(ISD::MUL);
Evan Cheng206ee9d2006-07-07 08:33:52 +00001109
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001110 computeRegisterProperties();
1111
Evan Cheng05219282011-01-06 06:52:41 +00001112 // On Darwin, -Os means optimize for size without hurting performance,
1113 // do not reduce the limit.
Dan Gohman87060f52008-06-30 21:00:56 +00001114 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001115 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 16 : 8;
Evan Cheng255f20f2010-04-01 06:04:33 +00001116 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001117 maxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
1118 maxStoresPerMemmove = 8; // For @llvm.memmove -> sequence of stores
1119 maxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Evan Chengfb8075d2008-02-28 00:43:03 +00001120 setPrefLoopAlignment(16);
Evan Cheng6ebf7bc2009-05-13 21:42:09 +00001121 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +00001122
1123 setPrefFunctionAlignment(4);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001124}
1125
Scott Michel5b8f82e2008-03-10 15:42:14 +00001126
Owen Anderson825b72b2009-08-11 20:47:22 +00001127MVT::SimpleValueType X86TargetLowering::getSetCCResultType(EVT VT) const {
1128 return MVT::i8;
Scott Michel5b8f82e2008-03-10 15:42:14 +00001129}
1130
1131
Evan Cheng29286502008-01-23 23:17:41 +00001132/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1133/// the desired ByVal argument alignment.
1134static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
1135 if (MaxAlign == 16)
1136 return;
1137 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
1138 if (VTy->getBitWidth() == 128)
1139 MaxAlign = 16;
Evan Cheng29286502008-01-23 23:17:41 +00001140 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
1141 unsigned EltAlign = 0;
1142 getMaxByValAlign(ATy->getElementType(), EltAlign);
1143 if (EltAlign > MaxAlign)
1144 MaxAlign = EltAlign;
1145 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
1146 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1147 unsigned EltAlign = 0;
1148 getMaxByValAlign(STy->getElementType(i), EltAlign);
1149 if (EltAlign > MaxAlign)
1150 MaxAlign = EltAlign;
1151 if (MaxAlign == 16)
1152 break;
1153 }
1154 }
1155 return;
1156}
1157
1158/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1159/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001160/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1161/// are at 4-byte boundaries.
Evan Cheng29286502008-01-23 23:17:41 +00001162unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001163 if (Subtarget->is64Bit()) {
1164 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001165 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001166 if (TyAlign > 8)
1167 return TyAlign;
1168 return 8;
1169 }
1170
Evan Cheng29286502008-01-23 23:17:41 +00001171 unsigned Align = 4;
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001172 if (Subtarget->hasXMM())
Dale Johannesen0c191872008-02-08 19:48:20 +00001173 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001174 return Align;
1175}
Chris Lattner2b02a442007-02-25 08:29:00 +00001176
Evan Chengf0df0312008-05-15 08:39:06 +00001177/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001178/// and store operations as a result of memset, memcpy, and memmove
1179/// lowering. If DstAlign is zero that means it's safe to destination
1180/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1181/// means there isn't a need to check it against alignment requirement,
1182/// probably because the source does not need to be loaded. If
1183/// 'NonScalarIntSafe' is true, that means it's safe to return a
1184/// non-scalar-integer type, e.g. empty string source, constant, or loaded
1185/// from memory. 'MemcpyStrSrc' indicates whether the memcpy source is
1186/// constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001187/// It returns EVT::Other if the type should be determined using generic
1188/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001189EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001190X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1191 unsigned DstAlign, unsigned SrcAlign,
Evan Chengf28f8bc2010-04-02 19:36:14 +00001192 bool NonScalarIntSafe,
Evan Chengc3b0c342010-04-08 07:37:57 +00001193 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001194 MachineFunction &MF) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001195 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1196 // linux. This is because the stack realignment code can't handle certain
1197 // cases like PR2962. This should be removed when PR2962 is fixed.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001198 const Function *F = MF.getFunction();
Evan Chenga5e13622011-01-07 19:35:30 +00001199 if (NonScalarIntSafe &&
1200 !F->hasFnAttr(Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001201 if (Size >= 16 &&
Evan Chenga5e13622011-01-07 19:35:30 +00001202 (Subtarget->isUnalignedMemAccessFast() ||
1203 ((DstAlign == 0 || DstAlign >= 16) &&
1204 (SrcAlign == 0 || SrcAlign >= 16))) &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001205 Subtarget->getStackAlignment() >= 16) {
1206 if (Subtarget->hasSSE2())
1207 return MVT::v4i32;
Evan Chengf28f8bc2010-04-02 19:36:14 +00001208 if (Subtarget->hasSSE1())
Evan Cheng255f20f2010-04-01 06:04:33 +00001209 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001210 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001211 !Subtarget->is64Bit() &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001212 Subtarget->getStackAlignment() >= 8 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001213 Subtarget->hasXMMInt()) {
Evan Chengc3b0c342010-04-08 07:37:57 +00001214 // Do not use f64 to lower memcpy if source is string constant. It's
1215 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001216 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001217 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001218 }
Evan Chengf0df0312008-05-15 08:39:06 +00001219 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001220 return MVT::i64;
1221 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001222}
1223
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001224/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1225/// current function. The returned value is a member of the
1226/// MachineJumpTableInfo::JTEntryKind enum.
1227unsigned X86TargetLowering::getJumpTableEncoding() const {
1228 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1229 // symbol.
1230 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1231 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001232 return MachineJumpTableInfo::EK_Custom32;
Michael J. Spencerec38de22010-10-10 22:04:20 +00001233
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001234 // Otherwise, use the normal jump table encoding heuristics.
1235 return TargetLowering::getJumpTableEncoding();
1236}
1237
Chris Lattnerc64daab2010-01-26 05:02:42 +00001238const MCExpr *
1239X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1240 const MachineBasicBlock *MBB,
1241 unsigned uid,MCContext &Ctx) const{
1242 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1243 Subtarget->isPICStyleGOT());
1244 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1245 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001246 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1247 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001248}
1249
Evan Chengcc415862007-11-09 01:32:10 +00001250/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1251/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001252SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001253 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001254 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001255 // This doesn't have DebugLoc associated with it, but is not really the
1256 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001257 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001258 return Table;
1259}
1260
Chris Lattner589c6f62010-01-26 06:28:43 +00001261/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1262/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1263/// MCExpr.
1264const MCExpr *X86TargetLowering::
1265getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1266 MCContext &Ctx) const {
1267 // X86-64 uses RIP relative addressing based on the jump table label.
1268 if (Subtarget->isPICStyleRIPRel())
1269 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1270
1271 // Otherwise, the reference is relative to the PIC base.
Chris Lattner142b5312010-11-14 22:48:15 +00001272 return MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
Chris Lattner589c6f62010-01-26 06:28:43 +00001273}
1274
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001275// FIXME: Why this routine is here? Move to RegInfo!
Evan Chengdee81012010-07-26 21:50:05 +00001276std::pair<const TargetRegisterClass*, uint8_t>
1277X86TargetLowering::findRepresentativeClass(EVT VT) const{
1278 const TargetRegisterClass *RRC = 0;
1279 uint8_t Cost = 1;
1280 switch (VT.getSimpleVT().SimpleTy) {
1281 default:
1282 return TargetLowering::findRepresentativeClass(VT);
1283 case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
1284 RRC = (Subtarget->is64Bit()
1285 ? X86::GR64RegisterClass : X86::GR32RegisterClass);
1286 break;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001287 case MVT::x86mmx:
Evan Chengdee81012010-07-26 21:50:05 +00001288 RRC = X86::VR64RegisterClass;
1289 break;
1290 case MVT::f32: case MVT::f64:
1291 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1292 case MVT::v4f32: case MVT::v2f64:
1293 case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1294 case MVT::v4f64:
1295 RRC = X86::VR128RegisterClass;
1296 break;
1297 }
1298 return std::make_pair(RRC, Cost);
1299}
1300
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001301bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1302 unsigned &Offset) const {
1303 if (!Subtarget->isTargetLinux())
1304 return false;
1305
1306 if (Subtarget->is64Bit()) {
1307 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1308 Offset = 0x28;
1309 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1310 AddressSpace = 256;
1311 else
1312 AddressSpace = 257;
1313 } else {
1314 // %gs:0x14 on i386
1315 Offset = 0x14;
1316 AddressSpace = 256;
1317 }
1318 return true;
1319}
1320
1321
Chris Lattner2b02a442007-02-25 08:29:00 +00001322//===----------------------------------------------------------------------===//
1323// Return Value Calling Convention Implementation
1324//===----------------------------------------------------------------------===//
1325
Chris Lattner59ed56b2007-02-28 04:55:35 +00001326#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001327
Michael J. Spencerec38de22010-10-10 22:04:20 +00001328bool
Eric Christopher471e4222011-06-08 23:55:35 +00001329X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv,
1330 MachineFunction &MF, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001331 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001332 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001333 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001334 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001335 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001336 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001337}
1338
Dan Gohman98ca4f22009-08-05 01:29:28 +00001339SDValue
1340X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001341 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001342 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001343 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001344 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001345 MachineFunction &MF = DAG.getMachineFunction();
1346 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001347
Chris Lattner9774c912007-02-27 05:28:59 +00001348 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001349 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001350 RVLocs, *DAG.getContext());
1351 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001352
Evan Chengdcea1632010-02-04 02:40:39 +00001353 // Add the regs to the liveout set for the function.
1354 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1355 for (unsigned i = 0; i != RVLocs.size(); ++i)
1356 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1357 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001358
Dan Gohman475871a2008-07-27 21:46:04 +00001359 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001360
Dan Gohman475871a2008-07-27 21:46:04 +00001361 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001362 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1363 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001364 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1365 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001366
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001367 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001368 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1369 CCValAssign &VA = RVLocs[i];
1370 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001371 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001372 EVT ValVT = ValToCopy.getValueType();
1373
Dale Johannesenc4510512010-09-24 19:05:48 +00001374 // If this is x86-64, and we disabled SSE, we can't return FP values,
1375 // or SSE or MMX vectors.
1376 if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
1377 VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001378 (Subtarget->is64Bit() && !Subtarget->hasXMM())) {
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001379 report_fatal_error("SSE register return with SSE disabled");
1380 }
1381 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1382 // llvm-gcc has never done it right and no one has noticed, so this
1383 // should be OK for now.
1384 if (ValVT == MVT::f64 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001385 (Subtarget->is64Bit() && !Subtarget->hasXMMInt()))
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001386 report_fatal_error("SSE2 register return with SSE2 disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001387
Chris Lattner447ff682008-03-11 03:23:40 +00001388 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1389 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001390 if (VA.getLocReg() == X86::ST0 ||
1391 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001392 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1393 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001394 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001395 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001396 RetOps.push_back(ValToCopy);
1397 // Don't emit a copytoreg.
1398 continue;
1399 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001400
Evan Cheng242b38b2009-02-23 09:03:22 +00001401 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1402 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001403 if (Subtarget->is64Bit()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001404 if (ValVT == MVT::x86mmx) {
Chris Lattner97a2a562010-08-26 05:24:29 +00001405 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001406 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ValToCopy);
Eric Christopher90eb4022010-07-22 00:26:08 +00001407 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1408 ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001409 // If we don't have SSE2 available, convert to v4f32 so the generated
1410 // register is legal.
1411 if (!Subtarget->hasSSE2())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001412 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32,ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001413 }
Evan Cheng242b38b2009-02-23 09:03:22 +00001414 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001415 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00001416
Dale Johannesendd64c412009-02-04 00:33:20 +00001417 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001418 Flag = Chain.getValue(1);
1419 }
Dan Gohman61a92132008-04-21 23:59:07 +00001420
1421 // The x86-64 ABI for returning structs by value requires that we copy
1422 // the sret argument into %rax for the return. We saved the argument into
1423 // a virtual register in the entry block, so now we copy the value out
1424 // and into %rax.
1425 if (Subtarget->is64Bit() &&
1426 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1427 MachineFunction &MF = DAG.getMachineFunction();
1428 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1429 unsigned Reg = FuncInfo->getSRetReturnReg();
Michael J. Spencerec38de22010-10-10 22:04:20 +00001430 assert(Reg &&
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001431 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001432 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001433
Dale Johannesendd64c412009-02-04 00:33:20 +00001434 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001435 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001436
1437 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001438 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001439 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001440
Chris Lattner447ff682008-03-11 03:23:40 +00001441 RetOps[0] = Chain; // Update chain.
1442
1443 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001444 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001445 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001446
1447 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001448 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001449}
1450
Evan Cheng3d2125c2010-11-30 23:55:39 +00001451bool X86TargetLowering::isUsedByReturnOnly(SDNode *N) const {
1452 if (N->getNumValues() != 1)
1453 return false;
1454 if (!N->hasNUsesOfValue(1, 0))
1455 return false;
1456
1457 SDNode *Copy = *N->use_begin();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001458 if (Copy->getOpcode() != ISD::CopyToReg &&
1459 Copy->getOpcode() != ISD::FP_EXTEND)
Evan Cheng3d2125c2010-11-30 23:55:39 +00001460 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001461
1462 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001463 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001464 UI != UE; ++UI) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001465 if (UI->getOpcode() != X86ISD::RET_FLAG)
1466 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001467 HasRet = true;
1468 }
Evan Cheng3d2125c2010-11-30 23:55:39 +00001469
Evan Cheng1bf891a2010-12-01 22:59:46 +00001470 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001471}
1472
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001473EVT
1474X86TargetLowering::getTypeForExtArgOrReturn(LLVMContext &Context, EVT VT,
Cameron Zwarich44579682011-03-17 14:21:56 +00001475 ISD::NodeType ExtendKind) const {
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001476 MVT ReturnMVT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001477 // TODO: Is this also valid on 32-bit?
1478 if (Subtarget->is64Bit() && VT == MVT::i1 && ExtendKind == ISD::ZERO_EXTEND)
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001479 ReturnMVT = MVT::i8;
1480 else
1481 ReturnMVT = MVT::i32;
1482
1483 EVT MinVT = getRegisterType(Context, ReturnMVT);
1484 return VT.bitsLT(MinVT) ? MinVT : VT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001485}
1486
Dan Gohman98ca4f22009-08-05 01:29:28 +00001487/// LowerCallResult - Lower the result values of a call into the
1488/// appropriate copies out of appropriate physical registers.
1489///
1490SDValue
1491X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001492 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001493 const SmallVectorImpl<ISD::InputArg> &Ins,
1494 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001495 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001496
Chris Lattnere32bbf62007-02-28 07:09:55 +00001497 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001498 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001499 bool Is64Bit = Subtarget->is64Bit();
Eric Christopher471e4222011-06-08 23:55:35 +00001500 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1501 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001502 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001503
Chris Lattner3085e152007-02-25 08:59:22 +00001504 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001505 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001506 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001507 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001508
Torok Edwin3f142c32009-02-01 18:15:56 +00001509 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001510 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001511 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasXMM())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001512 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001513 }
1514
Evan Cheng79fb3b42009-02-20 20:43:02 +00001515 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001516
1517 // If this is a call to a function that returns an fp value on the floating
1518 // point stack, we must guarantee the the value is popped from the stack, so
1519 // a CopyFromReg is not good enough - the copy instruction may be eliminated
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001520 // if the return value is not used. We use the FpPOP_RETVAL instruction
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001521 // instead.
1522 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1523 // If we prefer to use the value in xmm registers, copy it out as f80 and
1524 // use a truncate to move it from fp stack reg to xmm reg.
1525 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001526 SDValue Ops[] = { Chain, InFlag };
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001527 Chain = SDValue(DAG.getMachineNode(X86::FpPOP_RETVAL, dl, CopyVT,
1528 MVT::Other, MVT::Glue, Ops, 2), 1);
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001529 Val = Chain.getValue(0);
1530
1531 // Round the f80 to the right size, which also moves it to the appropriate
1532 // xmm register.
1533 if (CopyVT != VA.getValVT())
1534 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1535 // This truncation won't change the value.
1536 DAG.getIntPtrConstant(1));
Evan Cheng79fb3b42009-02-20 20:43:02 +00001537 } else {
1538 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1539 CopyVT, InFlag).getValue(1);
1540 Val = Chain.getValue(0);
1541 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001542 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001543 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001544 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001545
Dan Gohman98ca4f22009-08-05 01:29:28 +00001546 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001547}
1548
1549
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001550//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001551// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001552//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001553// StdCall calling convention seems to be standard for many Windows' API
1554// routines and around. It differs from C calling convention just a little:
1555// callee should clean up the stack, not caller. Symbols should be also
1556// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001557// For info on fast calling convention see Fast Calling Convention (tail call)
1558// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001559
Dan Gohman98ca4f22009-08-05 01:29:28 +00001560/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001561/// semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001562static bool CallIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
1563 if (Outs.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001564 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001565
Dan Gohman98ca4f22009-08-05 01:29:28 +00001566 return Outs[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001567}
1568
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001569/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001570/// return semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001571static bool
1572ArgsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
1573 if (Ins.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001574 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001575
Dan Gohman98ca4f22009-08-05 01:29:28 +00001576 return Ins[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001577}
1578
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001579/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1580/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001581/// the specific parameter attribute. The copy will be passed as a byval
1582/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001583static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001584CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001585 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1586 DebugLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00001587 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Michael J. Spencerec38de22010-10-10 22:04:20 +00001588
Dale Johannesendd64c412009-02-04 00:33:20 +00001589 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Stuart Hastings03d58262011-03-10 00:25:53 +00001590 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001591 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001592}
1593
Chris Lattner29689432010-03-11 00:22:57 +00001594/// IsTailCallConvention - Return true if the calling convention is one that
1595/// supports tail call optimization.
1596static bool IsTailCallConvention(CallingConv::ID CC) {
1597 return (CC == CallingConv::Fast || CC == CallingConv::GHC);
1598}
1599
Evan Cheng485fafc2011-03-21 01:19:09 +00001600bool X86TargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
1601 if (!CI->isTailCall())
1602 return false;
1603
1604 CallSite CS(CI);
1605 CallingConv::ID CalleeCC = CS.getCallingConv();
1606 if (!IsTailCallConvention(CalleeCC) && CalleeCC != CallingConv::C)
1607 return false;
1608
1609 return true;
1610}
1611
Evan Cheng0c439eb2010-01-27 00:07:07 +00001612/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1613/// a tailcall target by changing its ABI.
1614static bool FuncIsMadeTailCallSafe(CallingConv::ID CC) {
Chris Lattner29689432010-03-11 00:22:57 +00001615 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001616}
1617
Dan Gohman98ca4f22009-08-05 01:29:28 +00001618SDValue
1619X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001620 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001621 const SmallVectorImpl<ISD::InputArg> &Ins,
1622 DebugLoc dl, SelectionDAG &DAG,
1623 const CCValAssign &VA,
1624 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001625 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001626 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001627 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Evan Cheng0c439eb2010-01-27 00:07:07 +00001628 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001629 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001630 EVT ValVT;
1631
1632 // If value is passed by pointer we have address passed instead of the value
1633 // itself.
1634 if (VA.getLocInfo() == CCValAssign::Indirect)
1635 ValVT = VA.getLocVT();
1636 else
1637 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001638
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001639 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001640 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001641 // In case of tail call optimization mark all arguments mutable. Since they
1642 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001643 if (Flags.isByVal()) {
Evan Chengee2e0e32011-03-30 23:44:13 +00001644 unsigned Bytes = Flags.getByValSize();
1645 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
1646 int FI = MFI->CreateFixedObject(Bytes, VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001647 return DAG.getFrameIndex(FI, getPointerTy());
1648 } else {
1649 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001650 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001651 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1652 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001653 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00001654 false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001655 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001656}
1657
Dan Gohman475871a2008-07-27 21:46:04 +00001658SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001659X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001660 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001661 bool isVarArg,
1662 const SmallVectorImpl<ISD::InputArg> &Ins,
1663 DebugLoc dl,
1664 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001665 SmallVectorImpl<SDValue> &InVals)
1666 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001667 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001668 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001669
Gordon Henriksen86737662008-01-05 16:56:59 +00001670 const Function* Fn = MF.getFunction();
1671 if (Fn->hasExternalLinkage() &&
1672 Subtarget->isTargetCygMing() &&
1673 Fn->getName() == "main")
1674 FuncInfo->setForceFramePointer(true);
1675
Evan Cheng1bc78042006-04-26 01:20:17 +00001676 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001677 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001678 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001679
Chris Lattner29689432010-03-11 00:22:57 +00001680 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1681 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001682
Chris Lattner638402b2007-02-28 07:00:42 +00001683 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001684 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001685 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001686 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001687
1688 // Allocate shadow area for Win64
1689 if (IsWin64) {
1690 CCInfo.AllocateStack(32, 8);
1691 }
1692
Duncan Sands45907662010-10-31 13:21:44 +00001693 CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001694
Chris Lattnerf39f7712007-02-28 05:46:49 +00001695 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001696 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001697 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1698 CCValAssign &VA = ArgLocs[i];
1699 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1700 // places.
1701 assert(VA.getValNo() != LastVal &&
1702 "Don't support value assigned to multiple locs yet");
1703 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001704
Chris Lattnerf39f7712007-02-28 05:46:49 +00001705 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001706 EVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001707 TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001708 if (RegVT == MVT::i32)
Chris Lattnerf39f7712007-02-28 05:46:49 +00001709 RC = X86::GR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001710 else if (Is64Bit && RegVT == MVT::i64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001711 RC = X86::GR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001712 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001713 RC = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001714 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001715 RC = X86::FR64RegisterClass;
Bruno Cardoso Lopesac098352010-08-05 23:35:51 +00001716 else if (RegVT.isVector() && RegVT.getSizeInBits() == 256)
1717 RC = X86::VR256RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001718 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001719 RC = X86::VR128RegisterClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001720 else if (RegVT == MVT::x86mmx)
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001721 RC = X86::VR64RegisterClass;
1722 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001723 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001724
Devang Patel68e6bee2011-02-21 23:21:26 +00001725 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001726 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001727
Chris Lattnerf39f7712007-02-28 05:46:49 +00001728 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1729 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1730 // right size.
1731 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001732 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001733 DAG.getValueType(VA.getValVT()));
1734 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001735 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001736 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001737 else if (VA.getLocInfo() == CCValAssign::BCvt)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001738 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001739
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001740 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001741 // Handle MMX values passed in XMM regs.
1742 if (RegVT.isVector()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001743 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(),
1744 ArgValue);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001745 } else
1746 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001747 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001748 } else {
1749 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001750 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001751 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001752
1753 // If value is passed via pointer - do a load.
1754 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00001755 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
1756 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001757
Dan Gohman98ca4f22009-08-05 01:29:28 +00001758 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001759 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001760
Dan Gohman61a92132008-04-21 23:59:07 +00001761 // The x86-64 ABI for returning structs by value requires that we copy
1762 // the sret argument into %rax for the return. Save the argument into
1763 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001764 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001765 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1766 unsigned Reg = FuncInfo->getSRetReturnReg();
1767 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001768 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001769 FuncInfo->setSRetReturnReg(Reg);
1770 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001771 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001772 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001773 }
1774
Chris Lattnerf39f7712007-02-28 05:46:49 +00001775 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001776 // Align stack specially for tail calls.
1777 if (FuncIsMadeTailCallSafe(CallConv))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001778 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001779
Evan Cheng1bc78042006-04-26 01:20:17 +00001780 // If the function takes variable number of arguments, make a frame index for
1781 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001782 if (isVarArg) {
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001783 if (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
1784 CallConv != CallingConv::X86_ThisCall)) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00001785 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00001786 }
1787 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001788 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1789
1790 // FIXME: We should really autogenerate these arrays
1791 static const unsigned GPR64ArgRegsWin64[] = {
1792 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001793 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001794 static const unsigned GPR64ArgRegs64Bit[] = {
1795 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1796 };
1797 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001798 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1799 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1800 };
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001801 const unsigned *GPR64ArgRegs;
1802 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001803
1804 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001805 // The XMM registers which might contain var arg parameters are shadowed
1806 // in their paired GPR. So we only need to save the GPR to their home
1807 // slots.
1808 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001809 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001810 } else {
1811 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1812 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001813
1814 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit, TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001815 }
1816 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1817 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001818
Devang Patel578efa92009-06-05 21:57:13 +00001819 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001820 assert(!(NumXMMRegs && !Subtarget->hasXMM()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001821 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001822 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001823 "SSE register cannot be used when SSE is disabled!");
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001824 if (UseSoftFloat || NoImplicitFloatOps || !Subtarget->hasXMM())
Torok Edwin3f142c32009-02-01 18:15:56 +00001825 // Kernel mode asks for SSE to be disabled, so don't push them
1826 // on the stack.
1827 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001828
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001829 if (IsWin64) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001830 const TargetFrameLowering &TFI = *getTargetMachine().getFrameLowering();
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001831 // Get to the caller-allocated home save location. Add 8 to account
1832 // for the return address.
1833 int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001834 FuncInfo->setRegSaveFrameIndex(
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001835 MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001836 // Fixup to set vararg frame on shadow area (4 x i64).
1837 if (NumIntRegs < 4)
1838 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001839 } else {
1840 // For X86-64, if there are vararg parameters that are passed via
1841 // registers, then we must store them to their spots on the stack so they
1842 // may be loaded by deferencing the result of va_next.
1843 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
1844 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
1845 FuncInfo->setRegSaveFrameIndex(
1846 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00001847 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001848 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001849
Gordon Henriksen86737662008-01-05 16:56:59 +00001850 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001851 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00001852 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
1853 getPointerTy());
1854 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001855 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00001856 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
1857 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00001858 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00001859 X86::GR64RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00001860 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001861 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001862 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001863 MachinePointerInfo::getFixedStack(
1864 FuncInfo->getRegSaveFrameIndex(), Offset),
1865 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001866 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001867 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001868 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001869
Dan Gohmanface41a2009-08-16 21:24:25 +00001870 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
1871 // Now store the XMM (fp + vector) parameter registers.
1872 SmallVector<SDValue, 11> SaveXMMOps;
1873 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001874
Devang Patel68e6bee2011-02-21 23:21:26 +00001875 unsigned AL = MF.addLiveIn(X86::AL, X86::GR8RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00001876 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
1877 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001878
Dan Gohman1e93df62010-04-17 14:41:14 +00001879 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1880 FuncInfo->getRegSaveFrameIndex()));
1881 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1882 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00001883
Dan Gohmanface41a2009-08-16 21:24:25 +00001884 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001885 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00001886 X86::VR128RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00001887 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
1888 SaveXMMOps.push_back(Val);
1889 }
1890 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
1891 MVT::Other,
1892 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00001893 }
Dan Gohmanface41a2009-08-16 21:24:25 +00001894
1895 if (!MemOps.empty())
1896 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1897 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001898 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001899 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001900
Gordon Henriksen86737662008-01-05 16:56:59 +00001901 // Some CCs need callee pop.
Evan Chengef41ff62011-06-23 17:54:54 +00001902 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg, GuaranteedTailCallOpt)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001903 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001904 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00001905 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001906 // If this is an sret function, the return should pop the hidden pointer.
Chris Lattner29689432010-03-11 00:22:57 +00001907 if (!Is64Bit && !IsTailCallConvention(CallConv) && ArgsAreStructReturn(Ins))
Dan Gohman1e93df62010-04-17 14:41:14 +00001908 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001909 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001910
Gordon Henriksen86737662008-01-05 16:56:59 +00001911 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001912 // RegSaveFrameIndex is X86-64 only.
1913 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001914 if (CallConv == CallingConv::X86_FastCall ||
1915 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00001916 // fastcc functions can't have varargs.
1917 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00001918 }
Evan Cheng25caf632006-05-23 21:06:34 +00001919
Dan Gohman98ca4f22009-08-05 01:29:28 +00001920 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001921}
1922
Dan Gohman475871a2008-07-27 21:46:04 +00001923SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001924X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
1925 SDValue StackPtr, SDValue Arg,
1926 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00001927 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001928 ISD::ArgFlagsTy Flags) const {
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001929 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00001930 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00001931 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001932 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00001933 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001934
1935 return DAG.getStore(Chain, dl, Arg, PtrOff,
1936 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00001937 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00001938}
1939
Bill Wendling64e87322009-01-16 19:25:27 +00001940/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001941/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00001942SDValue
1943X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00001944 SDValue &OutRetAddr, SDValue Chain,
1945 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00001946 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001947 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00001948 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001949 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00001950
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001951 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00001952 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
1953 false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001954 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001955}
1956
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001957/// EmitTailCallStoreRetAddr - Emit a store of the return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001958/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00001959static SDValue
1960EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00001961 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00001962 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001963 // Store the return address to the appropriate stack slot.
1964 if (!FPDiff) return Chain;
1965 // Calculate the new stack slot for the return address.
1966 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00001967 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00001968 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00001969 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00001970 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001971 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00001972 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00001973 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001974 return Chain;
1975}
1976
Dan Gohman98ca4f22009-08-05 01:29:28 +00001977SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001978X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001979 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001980 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001981 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001982 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001983 const SmallVectorImpl<ISD::InputArg> &Ins,
1984 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001985 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001986 MachineFunction &MF = DAG.getMachineFunction();
1987 bool Is64Bit = Subtarget->is64Bit();
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00001988 bool IsWin64 = Subtarget->isTargetWin64();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001989 bool IsStructRet = CallIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00001990 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001991
Evan Cheng5f941932010-02-05 02:21:12 +00001992 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00001993 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00001994 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1995 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001996 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00001997
1998 // Sibcalls are automatically detected tailcalls which do not require
1999 // ABI changes.
Dan Gohman1797ed52010-02-08 20:27:50 +00002000 if (!GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00002001 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00002002
2003 if (isTailCall)
2004 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00002005 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00002006
Chris Lattner29689432010-03-11 00:22:57 +00002007 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
2008 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00002009
Chris Lattner638402b2007-02-28 07:00:42 +00002010 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00002011 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002012 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00002013 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002014
2015 // Allocate shadow area for Win64
2016 if (IsWin64) {
2017 CCInfo.AllocateStack(32, 8);
2018 }
2019
Duncan Sands45907662010-10-31 13:21:44 +00002020 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00002021
Chris Lattner423c5f42007-02-28 05:31:48 +00002022 // Get a count of how many bytes are to be pushed on the stack.
2023 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00002024 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00002025 // This is a sibcall. The memory operands are available in caller's
2026 // own caller's stack.
2027 NumBytes = 0;
Chris Lattner29689432010-03-11 00:22:57 +00002028 else if (GuaranteedTailCallOpt && IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00002029 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002030
Gordon Henriksen86737662008-01-05 16:56:59 +00002031 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00002032 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002033 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00002034 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00002035 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
2036 FPDiff = NumBytesCallerPushed - NumBytes;
2037
2038 // Set the delta of movement of the returnaddr stackslot.
2039 // But only set if delta is greater than previous delta.
2040 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
2041 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
2042 }
2043
Evan Chengf22f9b32010-02-06 03:28:46 +00002044 if (!IsSibcall)
2045 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002046
Dan Gohman475871a2008-07-27 21:46:04 +00002047 SDValue RetAddrFrIdx;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002048 // Load return address for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00002049 if (isTailCall && FPDiff)
2050 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
2051 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002052
Dan Gohman475871a2008-07-27 21:46:04 +00002053 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
2054 SmallVector<SDValue, 8> MemOpChains;
2055 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00002056
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002057 // Walk the register/memloc assignments, inserting copies/loads. In the case
2058 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00002059 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2060 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00002061 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00002062 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002063 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00002064 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00002065
Chris Lattner423c5f42007-02-28 05:31:48 +00002066 // Promote the value if needed.
2067 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002068 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00002069 case CCValAssign::Full: break;
2070 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002071 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002072 break;
2073 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002074 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002075 break;
2076 case CCValAssign::AExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002077 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
2078 // Special case: passing MMX values in XMM registers.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002079 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00002080 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
2081 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002082 } else
2083 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
2084 break;
2085 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002086 Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002087 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002088 case CCValAssign::Indirect: {
2089 // Store the argument.
2090 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00002091 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002092 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00002093 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002094 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002095 Arg = SpillSlot;
2096 break;
2097 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00002098 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002099
Chris Lattner423c5f42007-02-28 05:31:48 +00002100 if (VA.isRegLoc()) {
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002101 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
2102 if (isVarArg && IsWin64) {
2103 // Win64 ABI requires argument XMM reg to be copied to the corresponding
2104 // shadow reg if callee is a varargs function.
2105 unsigned ShadowReg = 0;
2106 switch (VA.getLocReg()) {
2107 case X86::XMM0: ShadowReg = X86::RCX; break;
2108 case X86::XMM1: ShadowReg = X86::RDX; break;
2109 case X86::XMM2: ShadowReg = X86::R8; break;
2110 case X86::XMM3: ShadowReg = X86::R9; break;
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002111 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002112 if (ShadowReg)
2113 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002114 }
Evan Chengf22f9b32010-02-06 03:28:46 +00002115 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00002116 assert(VA.isMemLoc());
2117 if (StackPtr.getNode() == 0)
2118 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
2119 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
2120 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002121 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002122 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002123
Evan Cheng32fe1032006-05-25 00:59:30 +00002124 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002125 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002126 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002127
Evan Cheng347d5f72006-04-28 21:29:37 +00002128 // Build a sequence of copy-to-reg nodes chained together with token chain
2129 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002130 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002131 // Tail call byval lowering might overwrite argument registers so in case of
2132 // tail call optimization the copies to registers are lowered later.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002133 if (!isTailCall)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002134 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002135 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002136 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002137 InFlag = Chain.getValue(1);
2138 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002139
Chris Lattner88e1fd52009-07-09 04:24:46 +00002140 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002141 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2142 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002143 if (!isTailCall) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002144 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
2145 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00002146 DebugLoc(), getPointerTy()),
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002147 InFlag);
2148 InFlag = Chain.getValue(1);
2149 } else {
2150 // If we are tail calling and generating PIC/GOT style code load the
2151 // address of the callee into ECX. The value in ecx is used as target of
2152 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2153 // for tail calls on PIC/GOT architectures. Normally we would just put the
2154 // address of GOT into ebx and then call target@PLT. But for tail calls
2155 // ebx would be restored (since ebx is callee saved) before jumping to the
2156 // target@PLT.
2157
2158 // Note: The actual moving to ECX is done further down.
2159 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2160 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2161 !G->getGlobal()->hasProtectedVisibility())
2162 Callee = LowerGlobalAddress(Callee, DAG);
2163 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002164 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002165 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002166 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002167
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002168 if (Is64Bit && isVarArg && !IsWin64) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002169 // From AMD64 ABI document:
2170 // For calls that may call functions that use varargs or stdargs
2171 // (prototype-less calls or calls to functions containing ellipsis (...) in
2172 // the declaration) %al is used as hidden argument to specify the number
2173 // of SSE registers used. The contents of %al do not need to match exactly
2174 // the number of registers, but must be an ubound on the number of SSE
2175 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002176
Gordon Henriksen86737662008-01-05 16:56:59 +00002177 // Count the number of XMM registers allocated.
2178 static const unsigned XMMArgRegs[] = {
2179 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2180 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2181 };
2182 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00002183 assert((Subtarget->hasXMM() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002184 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002185
Dale Johannesendd64c412009-02-04 00:33:20 +00002186 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Owen Anderson825b72b2009-08-11 20:47:22 +00002187 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002188 InFlag = Chain.getValue(1);
2189 }
2190
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002191
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002192 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002193 if (isTailCall) {
2194 // Force all the incoming stack arguments to be loaded from the stack
2195 // before any new outgoing arguments are stored to the stack, because the
2196 // outgoing stack slots may alias the incoming argument stack slots, and
2197 // the alias isn't otherwise explicit. This is slightly more conservative
2198 // than necessary, because it means that each store effectively depends
2199 // on every argument instead of just those arguments it would clobber.
2200 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2201
Dan Gohman475871a2008-07-27 21:46:04 +00002202 SmallVector<SDValue, 8> MemOpChains2;
2203 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002204 int FI = 0;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002205 // Do not flag preceding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00002206 InFlag = SDValue();
Dan Gohman1797ed52010-02-08 20:27:50 +00002207 if (GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002208 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2209 CCValAssign &VA = ArgLocs[i];
2210 if (VA.isRegLoc())
2211 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002212 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002213 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002214 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002215 // Create frame index.
2216 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002217 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002218 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002219 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002220
Duncan Sands276dcbd2008-03-21 09:14:45 +00002221 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002222 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002223 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002224 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002225 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00002226 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002227 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002228
Dan Gohman98ca4f22009-08-05 01:29:28 +00002229 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2230 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002231 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002232 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002233 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002234 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002235 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002236 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002237 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002238 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002239 }
2240 }
2241
2242 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002243 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002244 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002245
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002246 // Copy arguments to their registers.
2247 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002248 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002249 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002250 InFlag = Chain.getValue(1);
2251 }
Dan Gohman475871a2008-07-27 21:46:04 +00002252 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002253
Gordon Henriksen86737662008-01-05 16:56:59 +00002254 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002255 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002256 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002257 }
2258
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002259 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2260 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2261 // In the 64-bit large code model, we have to make all calls
2262 // through a register, since the call instruction's 32-bit
2263 // pc-relative offset may not be large enough to hold the whole
2264 // address.
2265 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002266 // If the callee is a GlobalAddress node (quite common, every direct call
2267 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2268 // it.
2269
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002270 // We should use extra load for direct calls to dllimported functions in
2271 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002272 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002273 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002274 unsigned char OpFlags = 0;
John McCall3a3465b2011-06-15 20:36:13 +00002275 bool ExtraLoad = false;
2276 unsigned WrapperKind = ISD::DELETED_NODE;
Eric Christopherfd179292009-08-27 18:07:15 +00002277
Chris Lattner48a7d022009-07-09 05:02:21 +00002278 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2279 // external symbols most go through the PLT in PIC mode. If the symbol
2280 // has hidden or protected visibility, or if it is static or local, then
2281 // we don't need to use the PLT - we can directly call it.
2282 if (Subtarget->isTargetELF() &&
2283 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002284 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002285 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002286 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002287 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002288 (!Subtarget->getTargetTriple().isMacOSX() ||
2289 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002290 // PC-relative references to external symbols should go through $stub,
2291 // unless we're building with the leopard linker or later, which
2292 // automatically synthesizes these stubs.
2293 OpFlags = X86II::MO_DARWIN_STUB;
John McCall3a3465b2011-06-15 20:36:13 +00002294 } else if (Subtarget->isPICStyleRIPRel() &&
2295 isa<Function>(GV) &&
2296 cast<Function>(GV)->hasFnAttr(Attribute::NonLazyBind)) {
2297 // If the function is marked as non-lazy, generate an indirect call
2298 // which loads from the GOT directly. This avoids runtime overhead
2299 // at the cost of eager binding (and one extra byte of encoding).
2300 OpFlags = X86II::MO_GOTPCREL;
2301 WrapperKind = X86ISD::WrapperRIP;
2302 ExtraLoad = true;
Chris Lattner74e726e2009-07-09 05:27:35 +00002303 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002304
Devang Patel0d881da2010-07-06 22:08:15 +00002305 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002306 G->getOffset(), OpFlags);
John McCall3a3465b2011-06-15 20:36:13 +00002307
2308 // Add a wrapper if needed.
2309 if (WrapperKind != ISD::DELETED_NODE)
2310 Callee = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Callee);
2311 // Add extra indirection if needed.
2312 if (ExtraLoad)
2313 Callee = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Callee,
2314 MachinePointerInfo::getGOT(),
2315 false, false, 0);
Chris Lattner48a7d022009-07-09 05:02:21 +00002316 }
Bill Wendling056292f2008-09-16 21:48:12 +00002317 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002318 unsigned char OpFlags = 0;
2319
Evan Cheng1bf891a2010-12-01 22:59:46 +00002320 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2321 // external symbols should go through the PLT.
2322 if (Subtarget->isTargetELF() &&
2323 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2324 OpFlags = X86II::MO_PLT;
2325 } else if (Subtarget->isPICStyleStubAny() &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002326 (!Subtarget->getTargetTriple().isMacOSX() ||
2327 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Evan Cheng1bf891a2010-12-01 22:59:46 +00002328 // PC-relative references to external symbols should go through $stub,
2329 // unless we're building with the leopard linker or later, which
2330 // automatically synthesizes these stubs.
2331 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002332 }
Eric Christopherfd179292009-08-27 18:07:15 +00002333
Chris Lattner48a7d022009-07-09 05:02:21 +00002334 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2335 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002336 }
2337
Chris Lattnerd96d0722007-02-25 06:40:16 +00002338 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002339 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002340 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002341
Evan Chengf22f9b32010-02-06 03:28:46 +00002342 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002343 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2344 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002345 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002346 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002347
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002348 Ops.push_back(Chain);
2349 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002350
Dan Gohman98ca4f22009-08-05 01:29:28 +00002351 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002352 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002353
Gordon Henriksen86737662008-01-05 16:56:59 +00002354 // Add argument registers to the end of the list so that they are known live
2355 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002356 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2357 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2358 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002359
Evan Cheng586ccac2008-03-18 23:36:35 +00002360 // Add an implicit use GOT pointer in EBX.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002361 if (!isTailCall && Subtarget->isPICStyleGOT())
Evan Cheng586ccac2008-03-18 23:36:35 +00002362 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
2363
Anton Korobeynikov3a1e54a2010-08-17 21:06:07 +00002364 // Add an implicit use of AL for non-Windows x86 64-bit vararg functions.
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002365 if (Is64Bit && isVarArg && !IsWin64)
Owen Anderson825b72b2009-08-11 20:47:22 +00002366 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
Evan Cheng586ccac2008-03-18 23:36:35 +00002367
Gabor Greifba36cb52008-08-28 21:40:38 +00002368 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002369 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002370
Dan Gohman98ca4f22009-08-05 01:29:28 +00002371 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002372 // We used to do:
2373 //// If this is the first return lowered for this function, add the regs
2374 //// to the liveout set for the function.
2375 // This isn't right, although it's probably harmless on x86; liveouts
2376 // should be computed from returns not tail calls. Consider a void
2377 // function making a tail call to a function returning int.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002378 return DAG.getNode(X86ISD::TC_RETURN, dl,
2379 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002380 }
2381
Dale Johannesenace16102009-02-03 19:33:06 +00002382 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002383 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002384
Chris Lattner2d297092006-05-23 18:50:38 +00002385 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002386 unsigned NumBytesForCalleeToPush;
Evan Chengef41ff62011-06-23 17:54:54 +00002387 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg, GuaranteedTailCallOpt))
Gordon Henriksen86737662008-01-05 16:56:59 +00002388 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Chris Lattner29689432010-03-11 00:22:57 +00002389 else if (!Is64Bit && !IsTailCallConvention(CallConv) && IsStructRet)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002390 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002391 // pops the hidden struct pointer, so we have to push it back.
2392 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002393 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002394 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002395 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002396
Gordon Henriksenae636f82008-01-03 16:47:34 +00002397 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002398 if (!IsSibcall) {
2399 Chain = DAG.getCALLSEQ_END(Chain,
2400 DAG.getIntPtrConstant(NumBytes, true),
2401 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2402 true),
2403 InFlag);
2404 InFlag = Chain.getValue(1);
2405 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002406
Chris Lattner3085e152007-02-25 08:59:22 +00002407 // Handle result values, copying them out of physregs into vregs that we
2408 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002409 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2410 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002411}
2412
Evan Cheng25ab6902006-09-08 06:48:29 +00002413
2414//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002415// Fast Calling Convention (tail call) implementation
2416//===----------------------------------------------------------------------===//
2417
2418// Like std call, callee cleans arguments, convention except that ECX is
2419// reserved for storing the tail called function address. Only 2 registers are
2420// free for argument passing (inreg). Tail call optimization is performed
2421// provided:
2422// * tailcallopt is enabled
2423// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002424// On X86_64 architecture with GOT-style position independent code only local
2425// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002426// To keep the stack aligned according to platform abi the function
2427// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2428// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002429// If a tail called function callee has more arguments than the caller the
2430// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002431// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002432// original REtADDR, but before the saved framepointer or the spilled registers
2433// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2434// stack layout:
2435// arg1
2436// arg2
2437// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002438// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002439// move area ]
2440// (possible EBP)
2441// ESI
2442// EDI
2443// local1 ..
2444
2445/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2446/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002447unsigned
2448X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2449 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002450 MachineFunction &MF = DAG.getMachineFunction();
2451 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002452 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002453 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002454 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002455 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002456 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002457 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2458 // Number smaller than 12 so just add the difference.
2459 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2460 } else {
2461 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002462 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002463 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002464 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002465 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002466}
2467
Evan Cheng5f941932010-02-05 02:21:12 +00002468/// MatchingStackOffset - Return true if the given stack call argument is
2469/// already available in the same position (relatively) of the caller's
2470/// incoming argument stack.
2471static
2472bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2473 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2474 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002475 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2476 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002477 if (Arg.getOpcode() == ISD::CopyFromReg) {
2478 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00002479 if (!TargetRegisterInfo::isVirtualRegister(VR))
Evan Cheng5f941932010-02-05 02:21:12 +00002480 return false;
2481 MachineInstr *Def = MRI->getVRegDef(VR);
2482 if (!Def)
2483 return false;
2484 if (!Flags.isByVal()) {
2485 if (!TII->isLoadFromStackSlot(Def, FI))
2486 return false;
2487 } else {
2488 unsigned Opcode = Def->getOpcode();
2489 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2490 Def->getOperand(1).isFI()) {
2491 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002492 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002493 } else
2494 return false;
2495 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002496 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2497 if (Flags.isByVal())
2498 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002499 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002500 // define @foo(%struct.X* %A) {
2501 // tail call @bar(%struct.X* byval %A)
2502 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002503 return false;
2504 SDValue Ptr = Ld->getBasePtr();
2505 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2506 if (!FINode)
2507 return false;
2508 FI = FINode->getIndex();
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002509 } else if (Arg.getOpcode() == ISD::FrameIndex && Flags.isByVal()) {
Chad Rosier14d71aa2011-06-25 18:51:28 +00002510 FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Arg);
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002511 FI = FINode->getIndex();
2512 Bytes = Flags.getByValSize();
Evan Cheng4cae1332010-03-05 08:38:04 +00002513 } else
2514 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002515
Evan Cheng4cae1332010-03-05 08:38:04 +00002516 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002517 if (!MFI->isFixedObjectIndex(FI))
2518 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002519 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002520}
2521
Dan Gohman98ca4f22009-08-05 01:29:28 +00002522/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2523/// for tail call optimization. Targets which want to do tail call
2524/// optimization should implement this function.
2525bool
2526X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002527 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002528 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002529 bool isCalleeStructRet,
2530 bool isCallerStructRet,
Evan Chengb1712452010-01-27 06:25:16 +00002531 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002532 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002533 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002534 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002535 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002536 CalleeCC != CallingConv::C)
2537 return false;
2538
Evan Cheng7096ae42010-01-29 06:45:59 +00002539 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002540 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002541 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Cheng13617962010-04-30 01:12:32 +00002542 CallingConv::ID CallerCC = CallerF->getCallingConv();
2543 bool CCMatch = CallerCC == CalleeCC;
2544
Dan Gohman1797ed52010-02-08 20:27:50 +00002545 if (GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002546 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002547 return true;
2548 return false;
2549 }
2550
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002551 // Look for obvious safe cases to perform tail call optimization that do not
2552 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002553
Evan Cheng2c12cb42010-03-26 16:26:03 +00002554 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2555 // emit a special epilogue.
2556 if (RegInfo->needsStackRealignment(MF))
2557 return false;
2558
Evan Chenga375d472010-03-15 18:54:48 +00002559 // Also avoid sibcall optimization if either caller or callee uses struct
2560 // return semantics.
2561 if (isCalleeStructRet || isCallerStructRet)
2562 return false;
2563
Chad Rosier2416da32011-06-24 21:15:36 +00002564 // An stdcall caller is expected to clean up its arguments; the callee
2565 // isn't going to do that.
2566 if (!CCMatch && CallerCC==CallingConv::X86_StdCall)
2567 return false;
2568
Chad Rosier871f6642011-05-18 19:59:50 +00002569 // Do not sibcall optimize vararg calls unless all arguments are passed via
Chad Rosiera1660892011-05-20 00:59:28 +00002570 // registers.
Chad Rosier871f6642011-05-18 19:59:50 +00002571 if (isVarArg && !Outs.empty()) {
Chad Rosiera1660892011-05-20 00:59:28 +00002572
2573 // Optimizing for varargs on Win64 is unlikely to be safe without
2574 // additional testing.
2575 if (Subtarget->isTargetWin64())
2576 return false;
2577
Chad Rosier871f6642011-05-18 19:59:50 +00002578 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002579 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
2580 getTargetMachine(), ArgLocs, *DAG.getContext());
Chad Rosier871f6642011-05-18 19:59:50 +00002581
Chad Rosier871f6642011-05-18 19:59:50 +00002582 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
2583 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i)
2584 if (!ArgLocs[i].isRegLoc())
2585 return false;
2586 }
2587
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002588 // If the call result is in ST0 / ST1, it needs to be popped off the x87 stack.
2589 // Therefore if it's not used by the call it is not safe to optimize this into
2590 // a sibcall.
2591 bool Unused = false;
2592 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2593 if (!Ins[i].Used) {
2594 Unused = true;
2595 break;
2596 }
2597 }
2598 if (Unused) {
2599 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002600 CCState CCInfo(CalleeCC, false, DAG.getMachineFunction(),
2601 getTargetMachine(), RVLocs, *DAG.getContext());
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002602 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002603 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002604 CCValAssign &VA = RVLocs[i];
2605 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2606 return false;
2607 }
2608 }
2609
Evan Cheng13617962010-04-30 01:12:32 +00002610 // If the calling conventions do not match, then we'd better make sure the
2611 // results are returned in the same way as what the caller expects.
2612 if (!CCMatch) {
2613 SmallVector<CCValAssign, 16> RVLocs1;
Eric Christopher471e4222011-06-08 23:55:35 +00002614 CCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
2615 getTargetMachine(), RVLocs1, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002616 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2617
2618 SmallVector<CCValAssign, 16> RVLocs2;
Eric Christopher471e4222011-06-08 23:55:35 +00002619 CCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
2620 getTargetMachine(), RVLocs2, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002621 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2622
2623 if (RVLocs1.size() != RVLocs2.size())
2624 return false;
2625 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2626 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2627 return false;
2628 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2629 return false;
2630 if (RVLocs1[i].isRegLoc()) {
2631 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2632 return false;
2633 } else {
2634 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2635 return false;
2636 }
2637 }
2638 }
2639
Evan Chenga6bff982010-01-30 01:22:00 +00002640 // If the callee takes no arguments then go on to check the results of the
2641 // call.
2642 if (!Outs.empty()) {
2643 // Check if stack adjustment is needed. For now, do not do this if any
2644 // argument is passed on the stack.
2645 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002646 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
2647 getTargetMachine(), ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002648
2649 // Allocate shadow area for Win64
2650 if (Subtarget->isTargetWin64()) {
2651 CCInfo.AllocateStack(32, 8);
2652 }
2653
Duncan Sands45907662010-10-31 13:21:44 +00002654 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Stuart Hastings6db2c2f2011-05-17 16:59:46 +00002655 if (CCInfo.getNextStackOffset()) {
Evan Chengb2c92902010-02-02 02:22:50 +00002656 MachineFunction &MF = DAG.getMachineFunction();
2657 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2658 return false;
Evan Chengb2c92902010-02-02 02:22:50 +00002659
2660 // Check if the arguments are already laid out in the right way as
2661 // the caller's fixed stack objects.
2662 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002663 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2664 const X86InstrInfo *TII =
2665 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002666 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2667 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002668 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002669 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002670 if (VA.getLocInfo() == CCValAssign::Indirect)
2671 return false;
2672 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002673 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2674 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002675 return false;
2676 }
2677 }
2678 }
Evan Cheng9c044672010-05-29 01:35:22 +00002679
2680 // If the tailcall address may be in a register, then make sure it's
2681 // possible to register allocate for it. In 32-bit, the call address can
2682 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002683 // callee-saved registers are restored. These happen to be the same
2684 // registers used to pass 'inreg' arguments so watch out for those.
2685 if (!Subtarget->is64Bit() &&
2686 !isa<GlobalAddressSDNode>(Callee) &&
Evan Cheng9c044672010-05-29 01:35:22 +00002687 !isa<ExternalSymbolSDNode>(Callee)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002688 unsigned NumInRegs = 0;
2689 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2690 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002691 if (!VA.isRegLoc())
2692 continue;
2693 unsigned Reg = VA.getLocReg();
2694 switch (Reg) {
2695 default: break;
2696 case X86::EAX: case X86::EDX: case X86::ECX:
2697 if (++NumInRegs == 3)
Evan Cheng9c044672010-05-29 01:35:22 +00002698 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002699 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002700 }
2701 }
2702 }
Evan Chenga6bff982010-01-30 01:22:00 +00002703 }
Evan Chengb1712452010-01-27 06:25:16 +00002704
Evan Cheng86809cc2010-02-03 03:28:02 +00002705 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002706}
2707
Dan Gohman3df24e62008-09-03 23:12:08 +00002708FastISel *
Dan Gohmana4160c32010-07-07 16:29:44 +00002709X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
2710 return X86::createFastISel(funcInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00002711}
2712
2713
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002714//===----------------------------------------------------------------------===//
2715// Other Lowering Hooks
2716//===----------------------------------------------------------------------===//
2717
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00002718static bool MayFoldLoad(SDValue Op) {
2719 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
2720}
2721
2722static bool MayFoldIntoStore(SDValue Op) {
2723 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
2724}
2725
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002726static bool isTargetShuffle(unsigned Opcode) {
2727 switch(Opcode) {
2728 default: return false;
2729 case X86ISD::PSHUFD:
2730 case X86ISD::PSHUFHW:
2731 case X86ISD::PSHUFLW:
2732 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002733 case X86ISD::PALIGN:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002734 case X86ISD::SHUFPS:
2735 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002736 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002737 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002738 case X86ISD::MOVLPS:
2739 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002740 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002741 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002742 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002743 case X86ISD::MOVSS:
2744 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002745 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002746 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +00002747 case X86ISD::VUNPCKLPS:
2748 case X86ISD::VUNPCKLPD:
2749 case X86ISD::VUNPCKLPSY:
2750 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002751 case X86ISD::PUNPCKLWD:
2752 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002753 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002754 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002755 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002756 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002757 case X86ISD::PUNPCKHWD:
2758 case X86ISD::PUNPCKHBW:
2759 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002760 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002761 return true;
2762 }
2763 return false;
2764}
2765
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002766static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002767 SDValue V1, SelectionDAG &DAG) {
2768 switch(Opc) {
2769 default: llvm_unreachable("Unknown x86 shuffle node");
2770 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002771 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002772 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002773 return DAG.getNode(Opc, dl, VT, V1);
2774 }
2775
2776 return SDValue();
2777}
2778
2779static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00002780 SDValue V1, unsigned TargetMask, SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002781 switch(Opc) {
2782 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002783 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002784 case X86ISD::PSHUFHW:
2785 case X86ISD::PSHUFLW:
2786 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
2787 }
2788
2789 return SDValue();
2790}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002791
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002792static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2793 SDValue V1, SDValue V2, unsigned TargetMask, SelectionDAG &DAG) {
2794 switch(Opc) {
2795 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002796 case X86ISD::PALIGN:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002797 case X86ISD::SHUFPD:
2798 case X86ISD::SHUFPS:
2799 return DAG.getNode(Opc, dl, VT, V1, V2,
2800 DAG.getConstant(TargetMask, MVT::i8));
2801 }
2802 return SDValue();
2803}
2804
2805static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2806 SDValue V1, SDValue V2, SelectionDAG &DAG) {
2807 switch(Opc) {
2808 default: llvm_unreachable("Unknown x86 shuffle node");
2809 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00002810 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002811 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002812 case X86ISD::MOVLPS:
2813 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002814 case X86ISD::MOVSS:
2815 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002816 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002817 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +00002818 case X86ISD::VUNPCKLPS:
2819 case X86ISD::VUNPCKLPD:
2820 case X86ISD::VUNPCKLPSY:
2821 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002822 case X86ISD::PUNPCKLWD:
2823 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002824 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002825 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002826 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002827 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002828 case X86ISD::PUNPCKHWD:
2829 case X86ISD::PUNPCKHBW:
2830 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002831 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002832 return DAG.getNode(Opc, dl, VT, V1, V2);
2833 }
2834 return SDValue();
2835}
2836
Dan Gohmand858e902010-04-17 15:26:15 +00002837SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002838 MachineFunction &MF = DAG.getMachineFunction();
2839 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2840 int ReturnAddrIndex = FuncInfo->getRAIndex();
2841
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002842 if (ReturnAddrIndex == 0) {
2843 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002844 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002845 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00002846 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002847 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002848 }
2849
Evan Cheng25ab6902006-09-08 06:48:29 +00002850 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002851}
2852
2853
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002854bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2855 bool hasSymbolicDisplacement) {
2856 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00002857 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002858 return false;
2859
2860 // If we don't have a symbolic displacement - we don't have any extra
2861 // restrictions.
2862 if (!hasSymbolicDisplacement)
2863 return true;
2864
2865 // FIXME: Some tweaks might be needed for medium code model.
2866 if (M != CodeModel::Small && M != CodeModel::Kernel)
2867 return false;
2868
2869 // For small code model we assume that latest object is 16MB before end of 31
2870 // bits boundary. We may also accept pretty large negative constants knowing
2871 // that all objects are in the positive half of address space.
2872 if (M == CodeModel::Small && Offset < 16*1024*1024)
2873 return true;
2874
2875 // For kernel code model we know that all object resist in the negative half
2876 // of 32bits address space. We may not accept negative offsets, since they may
2877 // be just off and we may accept pretty large positive ones.
2878 if (M == CodeModel::Kernel && Offset > 0)
2879 return true;
2880
2881 return false;
2882}
2883
Evan Chengef41ff62011-06-23 17:54:54 +00002884/// isCalleePop - Determines whether the callee is required to pop its
2885/// own arguments. Callee pop is necessary to support tail calls.
2886bool X86::isCalleePop(CallingConv::ID CallingConv,
2887 bool is64Bit, bool IsVarArg, bool TailCallOpt) {
2888 if (IsVarArg)
2889 return false;
2890
2891 switch (CallingConv) {
2892 default:
2893 return false;
2894 case CallingConv::X86_StdCall:
2895 return !is64Bit;
2896 case CallingConv::X86_FastCall:
2897 return !is64Bit;
2898 case CallingConv::X86_ThisCall:
2899 return !is64Bit;
2900 case CallingConv::Fast:
2901 return TailCallOpt;
2902 case CallingConv::GHC:
2903 return TailCallOpt;
2904 }
2905}
2906
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002907/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2908/// specific condition code, returning the condition code and the LHS/RHS of the
2909/// comparison to make.
2910static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2911 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00002912 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002913 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2914 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2915 // X > -1 -> X == 0, jump !sign.
2916 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002917 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002918 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2919 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002920 return X86::COND_S;
Andrew Trickf6c39412011-03-23 23:11:02 +00002921 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00002922 // X < 1 -> X <= 0
2923 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002924 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002925 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002926 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002927
Evan Chengd9558e02006-01-06 00:43:03 +00002928 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002929 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002930 case ISD::SETEQ: return X86::COND_E;
2931 case ISD::SETGT: return X86::COND_G;
2932 case ISD::SETGE: return X86::COND_GE;
2933 case ISD::SETLT: return X86::COND_L;
2934 case ISD::SETLE: return X86::COND_LE;
2935 case ISD::SETNE: return X86::COND_NE;
2936 case ISD::SETULT: return X86::COND_B;
2937 case ISD::SETUGT: return X86::COND_A;
2938 case ISD::SETULE: return X86::COND_BE;
2939 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00002940 }
Chris Lattner4c78e022008-12-23 23:42:27 +00002941 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002942
Chris Lattner4c78e022008-12-23 23:42:27 +00002943 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00002944
Chris Lattner4c78e022008-12-23 23:42:27 +00002945 // If LHS is a foldable load, but RHS is not, flip the condition.
Rafael Espindolaf297c932011-02-03 03:58:05 +00002946 if (ISD::isNON_EXTLoad(LHS.getNode()) &&
2947 !ISD::isNON_EXTLoad(RHS.getNode())) {
Chris Lattner4c78e022008-12-23 23:42:27 +00002948 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
2949 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00002950 }
2951
Chris Lattner4c78e022008-12-23 23:42:27 +00002952 switch (SetCCOpcode) {
2953 default: break;
2954 case ISD::SETOLT:
2955 case ISD::SETOLE:
2956 case ISD::SETUGT:
2957 case ISD::SETUGE:
2958 std::swap(LHS, RHS);
2959 break;
2960 }
2961
2962 // On a floating point condition, the flags are set as follows:
2963 // ZF PF CF op
2964 // 0 | 0 | 0 | X > Y
2965 // 0 | 0 | 1 | X < Y
2966 // 1 | 0 | 0 | X == Y
2967 // 1 | 1 | 1 | unordered
2968 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002969 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00002970 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002971 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00002972 case ISD::SETOLT: // flipped
2973 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002974 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00002975 case ISD::SETOLE: // flipped
2976 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002977 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002978 case ISD::SETUGT: // flipped
2979 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002980 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00002981 case ISD::SETUGE: // flipped
2982 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002983 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002984 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002985 case ISD::SETNE: return X86::COND_NE;
2986 case ISD::SETUO: return X86::COND_P;
2987 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00002988 case ISD::SETOEQ:
2989 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00002990 }
Evan Chengd9558e02006-01-06 00:43:03 +00002991}
2992
Evan Cheng4a460802006-01-11 00:33:36 +00002993/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2994/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00002995/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00002996static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00002997 switch (X86CC) {
2998 default:
2999 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00003000 case X86::COND_B:
3001 case X86::COND_BE:
3002 case X86::COND_E:
3003 case X86::COND_P:
3004 case X86::COND_A:
3005 case X86::COND_AE:
3006 case X86::COND_NE:
3007 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00003008 return true;
3009 }
3010}
3011
Evan Chengeb2f9692009-10-27 19:56:55 +00003012/// isFPImmLegal - Returns true if the target can instruction select the
3013/// specified FP immediate natively. If false, the legalizer will
3014/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00003015bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00003016 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
3017 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
3018 return true;
3019 }
3020 return false;
3021}
3022
Nate Begeman9008ca62009-04-27 18:41:29 +00003023/// isUndefOrInRange - Return true if Val is undef or if its value falls within
3024/// the specified range (L, H].
3025static bool isUndefOrInRange(int Val, int Low, int Hi) {
3026 return (Val < 0) || (Val >= Low && Val < Hi);
3027}
3028
3029/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
3030/// specified value.
3031static bool isUndefOrEqual(int Val, int CmpVal) {
3032 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003033 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00003034 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00003035}
3036
Nate Begeman9008ca62009-04-27 18:41:29 +00003037/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
3038/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
3039/// the second operand.
Owen Andersone50ed302009-08-10 22:56:29 +00003040static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00003041 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00003042 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00003043 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00003044 return (Mask[0] < 2 && Mask[1] < 2);
3045 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003046}
3047
Nate Begeman9008ca62009-04-27 18:41:29 +00003048bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003049 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003050 N->getMask(M);
3051 return ::isPSHUFDMask(M, N->getValueType(0));
3052}
Evan Cheng0188ecb2006-03-22 18:59:22 +00003053
Nate Begeman9008ca62009-04-27 18:41:29 +00003054/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
3055/// is suitable for input to PSHUFHW.
Owen Andersone50ed302009-08-10 22:56:29 +00003056static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003057 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00003058 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003059
Nate Begeman9008ca62009-04-27 18:41:29 +00003060 // Lower quadword copied in order or undef.
3061 for (int i = 0; i != 4; ++i)
3062 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00003063 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003064
Evan Cheng506d3df2006-03-29 23:07:14 +00003065 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00003066 for (int i = 4; i != 8; ++i)
3067 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00003068 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003069
Evan Cheng506d3df2006-03-29 23:07:14 +00003070 return true;
3071}
3072
Nate Begeman9008ca62009-04-27 18:41:29 +00003073bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003074 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003075 N->getMask(M);
3076 return ::isPSHUFHWMask(M, N->getValueType(0));
3077}
Evan Cheng506d3df2006-03-29 23:07:14 +00003078
Nate Begeman9008ca62009-04-27 18:41:29 +00003079/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
3080/// is suitable for input to PSHUFLW.
Owen Andersone50ed302009-08-10 22:56:29 +00003081static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003082 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00003083 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003084
Rafael Espindola15684b22009-04-24 12:40:33 +00003085 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00003086 for (int i = 4; i != 8; ++i)
3087 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00003088 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003089
Rafael Espindola15684b22009-04-24 12:40:33 +00003090 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00003091 for (int i = 0; i != 4; ++i)
3092 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00003093 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003094
Rafael Espindola15684b22009-04-24 12:40:33 +00003095 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003096}
3097
Nate Begeman9008ca62009-04-27 18:41:29 +00003098bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003099 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003100 N->getMask(M);
3101 return ::isPSHUFLWMask(M, N->getValueType(0));
3102}
3103
Nate Begemana09008b2009-10-19 02:17:23 +00003104/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
3105/// is suitable for input to PALIGNR.
3106static bool isPALIGNRMask(const SmallVectorImpl<int> &Mask, EVT VT,
3107 bool hasSSSE3) {
3108 int i, e = VT.getVectorNumElements();
Michael J. Spencerec38de22010-10-10 22:04:20 +00003109
Nate Begemana09008b2009-10-19 02:17:23 +00003110 // Do not handle v2i64 / v2f64 shuffles with palignr.
3111 if (e < 4 || !hasSSSE3)
3112 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003113
Nate Begemana09008b2009-10-19 02:17:23 +00003114 for (i = 0; i != e; ++i)
3115 if (Mask[i] >= 0)
3116 break;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003117
Nate Begemana09008b2009-10-19 02:17:23 +00003118 // All undef, not a palignr.
3119 if (i == e)
3120 return false;
3121
3122 // Determine if it's ok to perform a palignr with only the LHS, since we
3123 // don't have access to the actual shuffle elements to see if RHS is undef.
3124 bool Unary = Mask[i] < (int)e;
3125 bool NeedsUnary = false;
3126
3127 int s = Mask[i] - i;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003128
Nate Begemana09008b2009-10-19 02:17:23 +00003129 // Check the rest of the elements to see if they are consecutive.
3130 for (++i; i != e; ++i) {
3131 int m = Mask[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00003132 if (m < 0)
Nate Begemana09008b2009-10-19 02:17:23 +00003133 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003134
Nate Begemana09008b2009-10-19 02:17:23 +00003135 Unary = Unary && (m < (int)e);
3136 NeedsUnary = NeedsUnary || (m < s);
3137
3138 if (NeedsUnary && !Unary)
3139 return false;
3140 if (Unary && m != ((s+i) & (e-1)))
3141 return false;
3142 if (!Unary && m != (s+i))
3143 return false;
3144 }
3145 return true;
3146}
3147
3148bool X86::isPALIGNRMask(ShuffleVectorSDNode *N) {
3149 SmallVector<int, 8> M;
3150 N->getMask(M);
3151 return ::isPALIGNRMask(M, N->getValueType(0), true);
3152}
3153
Evan Cheng14aed5e2006-03-24 01:18:28 +00003154/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
3155/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Owen Andersone50ed302009-08-10 22:56:29 +00003156static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003157 int NumElems = VT.getVectorNumElements();
3158 if (NumElems != 2 && NumElems != 4)
3159 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003160
Nate Begeman9008ca62009-04-27 18:41:29 +00003161 int Half = NumElems / 2;
3162 for (int i = 0; i < Half; ++i)
3163 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003164 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003165 for (int i = Half; i < NumElems; ++i)
3166 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00003167 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003168
Evan Cheng14aed5e2006-03-24 01:18:28 +00003169 return true;
3170}
3171
Nate Begeman9008ca62009-04-27 18:41:29 +00003172bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
3173 SmallVector<int, 8> M;
3174 N->getMask(M);
3175 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003176}
3177
Evan Cheng213d2cf2007-05-17 18:45:50 +00003178/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00003179/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
3180/// half elements to come from vector 1 (which would equal the dest.) and
3181/// the upper half to come from vector 2.
Owen Andersone50ed302009-08-10 22:56:29 +00003182static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003183 int NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003184
3185 if (NumElems != 2 && NumElems != 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00003186 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003187
Nate Begeman9008ca62009-04-27 18:41:29 +00003188 int Half = NumElems / 2;
3189 for (int i = 0; i < Half; ++i)
3190 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00003191 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003192 for (int i = Half; i < NumElems; ++i)
3193 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003194 return false;
3195 return true;
3196}
3197
Nate Begeman9008ca62009-04-27 18:41:29 +00003198static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
3199 SmallVector<int, 8> M;
3200 N->getMask(M);
3201 return isCommutedSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003202}
3203
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003204/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
3205/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00003206bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
3207 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003208 return false;
3209
Evan Cheng2064a2b2006-03-28 06:50:32 +00003210 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00003211 return isUndefOrEqual(N->getMaskElt(0), 6) &&
3212 isUndefOrEqual(N->getMaskElt(1), 7) &&
3213 isUndefOrEqual(N->getMaskElt(2), 2) &&
3214 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003215}
3216
Nate Begeman0b10b912009-11-07 23:17:15 +00003217/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3218/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3219/// <2, 3, 2, 3>
3220bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
3221 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Michael J. Spencerec38de22010-10-10 22:04:20 +00003222
Nate Begeman0b10b912009-11-07 23:17:15 +00003223 if (NumElems != 4)
3224 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003225
Nate Begeman0b10b912009-11-07 23:17:15 +00003226 return isUndefOrEqual(N->getMaskElt(0), 2) &&
3227 isUndefOrEqual(N->getMaskElt(1), 3) &&
3228 isUndefOrEqual(N->getMaskElt(2), 2) &&
3229 isUndefOrEqual(N->getMaskElt(3), 3);
3230}
3231
Evan Cheng5ced1d82006-04-06 23:23:56 +00003232/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3233/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00003234bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
3235 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003236
Evan Cheng5ced1d82006-04-06 23:23:56 +00003237 if (NumElems != 2 && NumElems != 4)
3238 return false;
3239
Evan Chengc5cdff22006-04-07 21:53:05 +00003240 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003241 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003242 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003243
Evan Chengc5cdff22006-04-07 21:53:05 +00003244 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003245 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003246 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003247
3248 return true;
3249}
3250
Nate Begeman0b10b912009-11-07 23:17:15 +00003251/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3252/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
3253bool X86::isMOVLHPSMask(ShuffleVectorSDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003254 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003255
David Greenea20244d2011-03-02 17:23:43 +00003256 if ((NumElems != 2 && NumElems != 4)
3257 || N->getValueType(0).getSizeInBits() > 128)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003258 return false;
3259
Evan Chengc5cdff22006-04-07 21:53:05 +00003260 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003261 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003262 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003263
Nate Begeman9008ca62009-04-27 18:41:29 +00003264 for (unsigned i = 0; i < NumElems/2; ++i)
3265 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003266 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003267
3268 return true;
3269}
3270
Evan Cheng0038e592006-03-28 00:39:58 +00003271/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3272/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Owen Andersone50ed302009-08-10 22:56:29 +00003273static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003274 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003275 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003276 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng0038e592006-03-28 00:39:58 +00003277 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003278
David Greenea20244d2011-03-02 17:23:43 +00003279 // Handle vector lengths > 128 bits. Define a "section" as a set of
3280 // 128 bits. AVX defines UNPCK* to operate independently on 128-bit
3281 // sections.
3282 unsigned NumSections = VT.getSizeInBits() / 128;
3283 if (NumSections == 0 ) NumSections = 1; // Handle MMX
3284 unsigned NumSectionElts = NumElts / NumSections;
3285
3286 unsigned Start = 0;
3287 unsigned End = NumSectionElts;
3288 for (unsigned s = 0; s < NumSections; ++s) {
3289 for (unsigned i = Start, j = s * NumSectionElts;
3290 i != End;
3291 i += 2, ++j) {
3292 int BitI = Mask[i];
3293 int BitI1 = Mask[i+1];
3294 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003295 return false;
David Greenea20244d2011-03-02 17:23:43 +00003296 if (V2IsSplat) {
3297 if (!isUndefOrEqual(BitI1, NumElts))
3298 return false;
3299 } else {
3300 if (!isUndefOrEqual(BitI1, j + NumElts))
3301 return false;
3302 }
Evan Cheng39623da2006-04-20 08:58:49 +00003303 }
David Greenea20244d2011-03-02 17:23:43 +00003304 // Process the next 128 bits.
3305 Start += NumSectionElts;
3306 End += NumSectionElts;
Evan Cheng0038e592006-03-28 00:39:58 +00003307 }
David Greenea20244d2011-03-02 17:23:43 +00003308
Evan Cheng0038e592006-03-28 00:39:58 +00003309 return true;
3310}
3311
Nate Begeman9008ca62009-04-27 18:41:29 +00003312bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3313 SmallVector<int, 8> M;
3314 N->getMask(M);
3315 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003316}
3317
Evan Cheng4fcb9222006-03-28 02:43:26 +00003318/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3319/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Eric Christopherfd179292009-08-27 18:07:15 +00003320static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003321 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003322 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003323 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng4fcb9222006-03-28 02:43:26 +00003324 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003325
Nate Begeman9008ca62009-04-27 18:41:29 +00003326 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
3327 int BitI = Mask[i];
3328 int BitI1 = Mask[i+1];
Chris Lattner5a88b832007-02-25 07:10:00 +00003329 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengc5cdff22006-04-07 21:53:05 +00003330 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00003331 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00003332 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003333 return false;
3334 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00003335 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003336 return false;
3337 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003338 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003339 return true;
3340}
3341
Nate Begeman9008ca62009-04-27 18:41:29 +00003342bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3343 SmallVector<int, 8> M;
3344 N->getMask(M);
3345 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003346}
3347
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003348/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3349/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3350/// <0, 0, 1, 1>
Owen Andersone50ed302009-08-10 22:56:29 +00003351static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003352 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003353 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003354 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003355
David Greenea20244d2011-03-02 17:23:43 +00003356 // Handle vector lengths > 128 bits. Define a "section" as a set of
3357 // 128 bits. AVX defines UNPCK* to operate independently on 128-bit
3358 // sections.
3359 unsigned NumSections = VT.getSizeInBits() / 128;
3360 if (NumSections == 0 ) NumSections = 1; // Handle MMX
3361 unsigned NumSectionElts = NumElems / NumSections;
3362
3363 for (unsigned s = 0; s < NumSections; ++s) {
3364 for (unsigned i = s * NumSectionElts, j = s * NumSectionElts;
3365 i != NumSectionElts * (s + 1);
3366 i += 2, ++j) {
3367 int BitI = Mask[i];
3368 int BitI1 = Mask[i+1];
3369
3370 if (!isUndefOrEqual(BitI, j))
3371 return false;
3372 if (!isUndefOrEqual(BitI1, j))
3373 return false;
3374 }
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003375 }
David Greenea20244d2011-03-02 17:23:43 +00003376
Rafael Espindola15684b22009-04-24 12:40:33 +00003377 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003378}
3379
Nate Begeman9008ca62009-04-27 18:41:29 +00003380bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
3381 SmallVector<int, 8> M;
3382 N->getMask(M);
3383 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
3384}
3385
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003386/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3387/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3388/// <2, 2, 3, 3>
Owen Andersone50ed302009-08-10 22:56:29 +00003389static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003390 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003391 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
3392 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003393
Nate Begeman9008ca62009-04-27 18:41:29 +00003394 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
3395 int BitI = Mask[i];
3396 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003397 if (!isUndefOrEqual(BitI, j))
3398 return false;
3399 if (!isUndefOrEqual(BitI1, j))
3400 return false;
3401 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003402 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003403}
3404
Nate Begeman9008ca62009-04-27 18:41:29 +00003405bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
3406 SmallVector<int, 8> M;
3407 N->getMask(M);
3408 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
3409}
3410
Evan Cheng017dcc62006-04-21 01:05:10 +00003411/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3412/// specifies a shuffle of elements that is suitable for input to MOVSS,
3413/// MOVSD, and MOVD, i.e. setting the lowest element.
Owen Andersone50ed302009-08-10 22:56:29 +00003414static bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003415 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003416 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003417
3418 int NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003419
Nate Begeman9008ca62009-04-27 18:41:29 +00003420 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003421 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003422
Nate Begeman9008ca62009-04-27 18:41:29 +00003423 for (int i = 1; i < NumElts; ++i)
3424 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003425 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003426
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003427 return true;
3428}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003429
Nate Begeman9008ca62009-04-27 18:41:29 +00003430bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
3431 SmallVector<int, 8> M;
3432 N->getMask(M);
3433 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003434}
3435
Evan Cheng017dcc62006-04-21 01:05:10 +00003436/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
3437/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003438/// element of vector 2 and the other elements to come from vector 1 in order.
Owen Andersone50ed302009-08-10 22:56:29 +00003439static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003440 bool V2IsSplat = false, bool V2IsUndef = false) {
3441 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003442 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003443 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003444
Nate Begeman9008ca62009-04-27 18:41:29 +00003445 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003446 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003447
Nate Begeman9008ca62009-04-27 18:41:29 +00003448 for (int i = 1; i < NumOps; ++i)
3449 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3450 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3451 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003452 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003453
Evan Cheng39623da2006-04-20 08:58:49 +00003454 return true;
3455}
3456
Nate Begeman9008ca62009-04-27 18:41:29 +00003457static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00003458 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003459 SmallVector<int, 8> M;
3460 N->getMask(M);
3461 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00003462}
3463
Evan Chengd9539472006-04-14 21:59:03 +00003464/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3465/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003466bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N) {
3467 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003468 return false;
3469
3470 // Expect 1, 1, 3, 3
Rafael Espindola15684b22009-04-24 12:40:33 +00003471 for (unsigned i = 0; i < 2; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003472 int Elt = N->getMaskElt(i);
3473 if (Elt >= 0 && Elt != 1)
3474 return false;
Rafael Espindola15684b22009-04-24 12:40:33 +00003475 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003476
3477 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003478 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003479 int Elt = N->getMaskElt(i);
3480 if (Elt >= 0 && Elt != 3)
3481 return false;
3482 if (Elt == 3)
3483 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003484 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003485 // Don't use movshdup if it can be done with a shufps.
Nate Begeman9008ca62009-04-27 18:41:29 +00003486 // FIXME: verify that matching u, u, 3, 3 is what we want.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003487 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003488}
3489
3490/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3491/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003492bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N) {
3493 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003494 return false;
3495
3496 // Expect 0, 0, 2, 2
Nate Begeman9008ca62009-04-27 18:41:29 +00003497 for (unsigned i = 0; i < 2; ++i)
3498 if (N->getMaskElt(i) > 0)
3499 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003500
3501 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003502 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003503 int Elt = N->getMaskElt(i);
3504 if (Elt >= 0 && Elt != 2)
3505 return false;
3506 if (Elt == 2)
3507 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003508 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003509 // Don't use movsldup if it can be done with a shufps.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003510 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003511}
3512
Evan Cheng0b457f02008-09-25 20:50:48 +00003513/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3514/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003515bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
3516 int e = N->getValueType(0).getVectorNumElements() / 2;
Eric Christopherfd179292009-08-27 18:07:15 +00003517
Nate Begeman9008ca62009-04-27 18:41:29 +00003518 for (int i = 0; i < e; ++i)
3519 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003520 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003521 for (int i = 0; i < e; ++i)
3522 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003523 return false;
3524 return true;
3525}
3526
David Greenec38a03e2011-02-03 15:50:00 +00003527/// isVEXTRACTF128Index - Return true if the specified
3528/// EXTRACT_SUBVECTOR operand specifies a vector extract that is
3529/// suitable for input to VEXTRACTF128.
3530bool X86::isVEXTRACTF128Index(SDNode *N) {
3531 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3532 return false;
3533
3534 // The index should be aligned on a 128-bit boundary.
3535 uint64_t Index =
3536 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
3537
3538 unsigned VL = N->getValueType(0).getVectorNumElements();
3539 unsigned VBits = N->getValueType(0).getSizeInBits();
3540 unsigned ElSize = VBits / VL;
3541 bool Result = (Index * ElSize) % 128 == 0;
3542
3543 return Result;
3544}
3545
David Greeneccacdc12011-02-04 16:08:29 +00003546/// isVINSERTF128Index - Return true if the specified INSERT_SUBVECTOR
3547/// operand specifies a subvector insert that is suitable for input to
3548/// VINSERTF128.
3549bool X86::isVINSERTF128Index(SDNode *N) {
3550 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
3551 return false;
3552
3553 // The index should be aligned on a 128-bit boundary.
3554 uint64_t Index =
3555 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
3556
3557 unsigned VL = N->getValueType(0).getVectorNumElements();
3558 unsigned VBits = N->getValueType(0).getSizeInBits();
3559 unsigned ElSize = VBits / VL;
3560 bool Result = (Index * ElSize) % 128 == 0;
3561
3562 return Result;
3563}
3564
Evan Cheng63d33002006-03-22 08:01:21 +00003565/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003566/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Evan Cheng63d33002006-03-22 08:01:21 +00003567unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003568 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3569 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
3570
Evan Chengb9df0ca2006-03-22 02:53:00 +00003571 unsigned Shift = (NumOperands == 4) ? 2 : 1;
3572 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00003573 for (int i = 0; i < NumOperands; ++i) {
3574 int Val = SVOp->getMaskElt(NumOperands-i-1);
3575 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00003576 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00003577 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00003578 if (i != NumOperands - 1)
3579 Mask <<= Shift;
3580 }
Evan Cheng63d33002006-03-22 08:01:21 +00003581 return Mask;
3582}
3583
Evan Cheng506d3df2006-03-29 23:07:14 +00003584/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003585/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003586unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003587 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003588 unsigned Mask = 0;
3589 // 8 nodes, but we only care about the last 4.
3590 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003591 int Val = SVOp->getMaskElt(i);
3592 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00003593 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00003594 if (i != 4)
3595 Mask <<= 2;
3596 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003597 return Mask;
3598}
3599
3600/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003601/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003602unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003603 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003604 unsigned Mask = 0;
3605 // 8 nodes, but we only care about the first 4.
3606 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003607 int Val = SVOp->getMaskElt(i);
3608 if (Val >= 0)
3609 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00003610 if (i != 0)
3611 Mask <<= 2;
3612 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003613 return Mask;
3614}
3615
Nate Begemana09008b2009-10-19 02:17:23 +00003616/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
3617/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
3618unsigned X86::getShufflePALIGNRImmediate(SDNode *N) {
3619 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3620 EVT VVT = N->getValueType(0);
3621 unsigned EltSize = VVT.getVectorElementType().getSizeInBits() >> 3;
3622 int Val = 0;
3623
3624 unsigned i, e;
3625 for (i = 0, e = VVT.getVectorNumElements(); i != e; ++i) {
3626 Val = SVOp->getMaskElt(i);
3627 if (Val >= 0)
3628 break;
3629 }
3630 return (Val - i) * EltSize;
3631}
3632
David Greenec38a03e2011-02-03 15:50:00 +00003633/// getExtractVEXTRACTF128Immediate - Return the appropriate immediate
3634/// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF128
3635/// instructions.
3636unsigned X86::getExtractVEXTRACTF128Immediate(SDNode *N) {
3637 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3638 llvm_unreachable("Illegal extract subvector for VEXTRACTF128");
3639
3640 uint64_t Index =
3641 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
3642
3643 EVT VecVT = N->getOperand(0).getValueType();
3644 EVT ElVT = VecVT.getVectorElementType();
3645
3646 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
3647
3648 return Index / NumElemsPerChunk;
3649}
3650
David Greeneccacdc12011-02-04 16:08:29 +00003651/// getInsertVINSERTF128Immediate - Return the appropriate immediate
3652/// to insert at the specified INSERT_SUBVECTOR index with VINSERTF128
3653/// instructions.
3654unsigned X86::getInsertVINSERTF128Immediate(SDNode *N) {
3655 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
3656 llvm_unreachable("Illegal insert subvector for VINSERTF128");
3657
3658 uint64_t Index =
NAKAMURA Takumi27635382011-02-05 15:10:54 +00003659 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
David Greeneccacdc12011-02-04 16:08:29 +00003660
3661 EVT VecVT = N->getValueType(0);
3662 EVT ElVT = VecVT.getVectorElementType();
3663
3664 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
3665
3666 return Index / NumElemsPerChunk;
3667}
3668
Evan Cheng37b73872009-07-30 08:33:02 +00003669/// isZeroNode - Returns true if Elt is a constant zero or a floating point
3670/// constant +0.0.
3671bool X86::isZeroNode(SDValue Elt) {
3672 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00003673 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00003674 (isa<ConstantFPSDNode>(Elt) &&
3675 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
3676}
3677
Nate Begeman9008ca62009-04-27 18:41:29 +00003678/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
3679/// their permute mask.
3680static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
3681 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003682 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003683 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00003684 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00003685
Nate Begeman5a5ca152009-04-29 05:20:52 +00003686 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003687 int idx = SVOp->getMaskElt(i);
3688 if (idx < 0)
3689 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003690 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003691 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003692 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003693 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003694 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003695 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
3696 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003697}
3698
Evan Cheng779ccea2007-12-07 21:30:01 +00003699/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3700/// the two vector operands have swapped position.
Owen Andersone50ed302009-08-10 22:56:29 +00003701static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00003702 unsigned NumElems = VT.getVectorNumElements();
3703 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003704 int idx = Mask[i];
3705 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003706 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003707 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003708 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003709 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003710 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003711 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003712}
3713
Evan Cheng533a0aa2006-04-19 20:35:22 +00003714/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
3715/// match movhlps. The lower half elements should come from upper half of
3716/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003717/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00003718static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
3719 if (Op->getValueType(0).getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00003720 return false;
3721 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003722 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003723 return false;
3724 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003725 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003726 return false;
3727 return true;
3728}
3729
Evan Cheng5ced1d82006-04-06 23:23:56 +00003730/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00003731/// is promoted to a vector. It also returns the LoadSDNode by reference if
3732/// required.
3733static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00003734 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
3735 return false;
3736 N = N->getOperand(0).getNode();
3737 if (!ISD::isNON_EXTLoad(N))
3738 return false;
3739 if (LD)
3740 *LD = cast<LoadSDNode>(N);
3741 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003742}
3743
Evan Cheng533a0aa2006-04-19 20:35:22 +00003744/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
3745/// match movlp{s|d}. The lower half elements should come from lower half of
3746/// V1 (and in order), and the upper half elements should come from the upper
3747/// half of V2 (and in order). And since V1 will become the source of the
3748/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003749static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
3750 ShuffleVectorSDNode *Op) {
Evan Cheng466685d2006-10-09 20:57:25 +00003751 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003752 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00003753 // Is V2 is a vector load, don't do this transformation. We will try to use
3754 // load folding shufps op.
3755 if (ISD::isNON_EXTLoad(V2))
3756 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003757
Nate Begeman5a5ca152009-04-29 05:20:52 +00003758 unsigned NumElems = Op->getValueType(0).getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003759
Evan Cheng533a0aa2006-04-19 20:35:22 +00003760 if (NumElems != 2 && NumElems != 4)
3761 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003762 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003763 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003764 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003765 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003766 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003767 return false;
3768 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003769}
3770
Evan Cheng39623da2006-04-20 08:58:49 +00003771/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
3772/// all the same.
3773static bool isSplatVector(SDNode *N) {
3774 if (N->getOpcode() != ISD::BUILD_VECTOR)
3775 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003776
Dan Gohman475871a2008-07-27 21:46:04 +00003777 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00003778 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
3779 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003780 return false;
3781 return true;
3782}
3783
Evan Cheng213d2cf2007-05-17 18:45:50 +00003784/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00003785/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00003786/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00003787static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00003788 SDValue V1 = N->getOperand(0);
3789 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003790 unsigned NumElems = N->getValueType(0).getVectorNumElements();
3791 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003792 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003793 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003794 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00003795 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
3796 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003797 if (Opc != ISD::BUILD_VECTOR ||
3798 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00003799 return false;
3800 } else if (Idx >= 0) {
3801 unsigned Opc = V1.getOpcode();
3802 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
3803 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003804 if (Opc != ISD::BUILD_VECTOR ||
3805 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00003806 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00003807 }
3808 }
3809 return true;
3810}
3811
3812/// getZeroVector - Returns a vector of specified type with all zero elements.
3813///
Owen Andersone50ed302009-08-10 22:56:29 +00003814static SDValue getZeroVector(EVT VT, bool HasSSE2, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00003815 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003816 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003817
Dale Johannesen0488fb62010-09-30 23:57:10 +00003818 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003819 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00003820 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00003821 if (VT.getSizeInBits() == 128) { // SSE
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003822 if (HasSSE2) { // SSE2
3823 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3824 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
3825 } else { // SSE1
3826 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
3827 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
3828 }
3829 } else if (VT.getSizeInBits() == 256) { // AVX
3830 // 256-bit logic and arithmetic instructions in AVX are
3831 // all floating-point, no support for integer ops. Default
3832 // to emitting fp zeroed vectors then.
Owen Anderson825b72b2009-08-11 20:47:22 +00003833 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003834 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
3835 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
Evan Chengf0df0312008-05-15 08:39:06 +00003836 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003837 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00003838}
3839
Chris Lattner8a594482007-11-25 00:24:49 +00003840/// getOnesVector - Returns a vector of specified type with all bits set.
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00003841/// Always build ones vectors as <4 x i32> or <8 x i32> bitcasted to
3842/// their original type, ensuring they get CSE'd.
Owen Andersone50ed302009-08-10 22:56:29 +00003843static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003844 assert(VT.isVector() && "Expected a vector type");
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00003845 assert((VT.is128BitVector() || VT.is256BitVector())
3846 && "Expected a 128-bit or 256-bit vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003847
Owen Anderson825b72b2009-08-11 20:47:22 +00003848 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00003849
Dan Gohman475871a2008-07-27 21:46:04 +00003850 SDValue Vec;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00003851 if (VT.is256BitVector()) {
3852 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
3853 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32, Ops, 8);
3854 } else
3855 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003856 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00003857}
3858
Evan Cheng39623da2006-04-20 08:58:49 +00003859/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
3860/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00003861static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003862 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003863 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003864
Evan Cheng39623da2006-04-20 08:58:49 +00003865 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003866 SmallVector<int, 8> MaskVec;
3867 SVOp->getMask(MaskVec);
Eric Christopherfd179292009-08-27 18:07:15 +00003868
Nate Begeman5a5ca152009-04-29 05:20:52 +00003869 for (unsigned i = 0; i != NumElems; ++i) {
3870 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003871 MaskVec[i] = NumElems;
3872 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00003873 }
Evan Cheng39623da2006-04-20 08:58:49 +00003874 }
Evan Cheng39623da2006-04-20 08:58:49 +00003875 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00003876 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
3877 SVOp->getOperand(1), &MaskVec[0]);
3878 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00003879}
3880
Evan Cheng017dcc62006-04-21 01:05:10 +00003881/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
3882/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00003883static SDValue getMOVL(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;
3887 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00003888 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003889 Mask.push_back(i);
3890 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00003891}
3892
Nate Begeman9008ca62009-04-27 18:41:29 +00003893/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003894static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003895 SDValue V2) {
3896 unsigned NumElems = VT.getVectorNumElements();
3897 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00003898 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003899 Mask.push_back(i);
3900 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00003901 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003902 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00003903}
3904
Nate Begeman9008ca62009-04-27 18:41:29 +00003905/// getUnpackhMask - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003906static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003907 SDValue V2) {
3908 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00003909 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00003910 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00003911 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003912 Mask.push_back(i + Half);
3913 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00003914 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003915 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003916}
3917
Bruno Cardoso Lopesbb0a9482010-08-13 17:50:47 +00003918/// PromoteSplat - Promote a splat of v4i32, v8i16 or v16i8 to v4f32.
3919static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003920 EVT PVT = MVT::v4f32;
Owen Andersone50ed302009-08-10 22:56:29 +00003921 EVT VT = SV->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00003922 DebugLoc dl = SV->getDebugLoc();
3923 SDValue V1 = SV->getOperand(0);
3924 int NumElems = VT.getVectorNumElements();
3925 int EltNo = SV->getSplatIndex();
Rafael Espindola15684b22009-04-24 12:40:33 +00003926
Nate Begeman9008ca62009-04-27 18:41:29 +00003927 // unpack elements to the correct location
3928 while (NumElems > 4) {
3929 if (EltNo < NumElems/2) {
3930 V1 = getUnpackl(DAG, dl, VT, V1, V1);
3931 } else {
3932 V1 = getUnpackh(DAG, dl, VT, V1, V1);
3933 EltNo -= NumElems/2;
3934 }
3935 NumElems >>= 1;
3936 }
Eric Christopherfd179292009-08-27 18:07:15 +00003937
Nate Begeman9008ca62009-04-27 18:41:29 +00003938 // Perform the splat.
3939 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003940 V1 = DAG.getNode(ISD::BITCAST, dl, PVT, V1);
Nate Begeman9008ca62009-04-27 18:41:29 +00003941 V1 = DAG.getVectorShuffle(PVT, dl, V1, DAG.getUNDEF(PVT), &SplatMask[0]);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003942 return DAG.getNode(ISD::BITCAST, dl, VT, V1);
Evan Chengc575ca22006-04-17 20:43:08 +00003943}
3944
Evan Chengba05f722006-04-21 23:03:30 +00003945/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00003946/// vector of zero or undef vector. This produces a shuffle where the low
3947/// element of V2 is swizzled into the zero/undef vector, landing at element
3948/// 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 +00003949static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Chengf0df0312008-05-15 08:39:06 +00003950 bool isZero, bool HasSSE2,
3951 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003952 EVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00003953 SDValue V1 = isZero
Nate Begeman9008ca62009-04-27 18:41:29 +00003954 ? getZeroVector(VT, HasSSE2, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
3955 unsigned NumElems = VT.getVectorNumElements();
3956 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00003957 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003958 // If this is the insertion idx, put the low elt of V2 here.
3959 MaskVec.push_back(i == Idx ? NumElems : i);
3960 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00003961}
3962
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003963/// getShuffleScalarElt - Returns the scalar element that will make up the ith
3964/// element of the result of the vector shuffle.
Benjamin Kramer050db522011-03-26 12:38:19 +00003965static SDValue getShuffleScalarElt(SDNode *N, int Index, SelectionDAG &DAG,
3966 unsigned Depth) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003967 if (Depth == 6)
3968 return SDValue(); // Limit search depth.
3969
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003970 SDValue V = SDValue(N, 0);
3971 EVT VT = V.getValueType();
3972 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003973
3974 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
3975 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
3976 Index = SV->getMaskElt(Index);
3977
3978 if (Index < 0)
3979 return DAG.getUNDEF(VT.getVectorElementType());
3980
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003981 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003982 SDValue NewV = (Index < NumElems) ? SV->getOperand(0) : SV->getOperand(1);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003983 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00003984 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003985
3986 // Recurse into target specific vector shuffles to find scalars.
3987 if (isTargetShuffle(Opcode)) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003988 int NumElems = VT.getVectorNumElements();
3989 SmallVector<unsigned, 16> ShuffleMask;
3990 SDValue ImmN;
3991
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003992 switch(Opcode) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003993 case X86ISD::SHUFPS:
3994 case X86ISD::SHUFPD:
3995 ImmN = N->getOperand(N->getNumOperands()-1);
3996 DecodeSHUFPSMask(NumElems,
3997 cast<ConstantSDNode>(ImmN)->getZExtValue(),
3998 ShuffleMask);
3999 break;
4000 case X86ISD::PUNPCKHBW:
4001 case X86ISD::PUNPCKHWD:
4002 case X86ISD::PUNPCKHDQ:
4003 case X86ISD::PUNPCKHQDQ:
4004 DecodePUNPCKHMask(NumElems, ShuffleMask);
4005 break;
4006 case X86ISD::UNPCKHPS:
4007 case X86ISD::UNPCKHPD:
4008 DecodeUNPCKHPMask(NumElems, ShuffleMask);
4009 break;
4010 case X86ISD::PUNPCKLBW:
4011 case X86ISD::PUNPCKLWD:
4012 case X86ISD::PUNPCKLDQ:
4013 case X86ISD::PUNPCKLQDQ:
David Greenec4db4e52011-02-28 19:06:56 +00004014 DecodePUNPCKLMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004015 break;
4016 case X86ISD::UNPCKLPS:
4017 case X86ISD::UNPCKLPD:
David Greenec4db4e52011-02-28 19:06:56 +00004018 case X86ISD::VUNPCKLPS:
4019 case X86ISD::VUNPCKLPD:
4020 case X86ISD::VUNPCKLPSY:
4021 case X86ISD::VUNPCKLPDY:
4022 DecodeUNPCKLPMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004023 break;
4024 case X86ISD::MOVHLPS:
4025 DecodeMOVHLPSMask(NumElems, ShuffleMask);
4026 break;
4027 case X86ISD::MOVLHPS:
4028 DecodeMOVLHPSMask(NumElems, ShuffleMask);
4029 break;
4030 case X86ISD::PSHUFD:
4031 ImmN = N->getOperand(N->getNumOperands()-1);
4032 DecodePSHUFMask(NumElems,
4033 cast<ConstantSDNode>(ImmN)->getZExtValue(),
4034 ShuffleMask);
4035 break;
4036 case X86ISD::PSHUFHW:
4037 ImmN = N->getOperand(N->getNumOperands()-1);
4038 DecodePSHUFHWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
4039 ShuffleMask);
4040 break;
4041 case X86ISD::PSHUFLW:
4042 ImmN = N->getOperand(N->getNumOperands()-1);
4043 DecodePSHUFLWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
4044 ShuffleMask);
4045 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004046 case X86ISD::MOVSS:
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00004047 case X86ISD::MOVSD: {
4048 // The index 0 always comes from the first element of the second source,
4049 // this is why MOVSS and MOVSD are used in the first place. The other
4050 // elements come from the other positions of the first source vector.
4051 unsigned OpNum = (Index == 0) ? 1 : 0;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004052 return getShuffleScalarElt(V.getOperand(OpNum).getNode(), Index, DAG,
4053 Depth+1);
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00004054 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004055 default:
4056 assert("not implemented for target shuffle node");
4057 return SDValue();
4058 }
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004059
4060 Index = ShuffleMask[Index];
4061 if (Index < 0)
4062 return DAG.getUNDEF(VT.getVectorElementType());
4063
4064 SDValue NewV = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
4065 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG,
4066 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004067 }
4068
4069 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004070 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004071 V = V.getOperand(0);
4072 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004073 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004074
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004075 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004076 return SDValue();
4077 }
4078
4079 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
4080 return (Index == 0) ? V.getOperand(0)
4081 : DAG.getUNDEF(VT.getVectorElementType());
4082
4083 if (V.getOpcode() == ISD::BUILD_VECTOR)
4084 return V.getOperand(Index);
4085
4086 return SDValue();
4087}
4088
4089/// getNumOfConsecutiveZeros - Return the number of elements of a vector
4090/// shuffle operation which come from a consecutively from a zero. The
Chris Lattner7a2bdde2011-04-15 05:18:47 +00004091/// search can start in two different directions, from left or right.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004092static
4093unsigned getNumOfConsecutiveZeros(SDNode *N, int NumElems,
4094 bool ZerosFromLeft, SelectionDAG &DAG) {
4095 int i = 0;
4096
4097 while (i < NumElems) {
4098 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004099 SDValue Elt = getShuffleScalarElt(N, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004100 if (!(Elt.getNode() &&
4101 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
4102 break;
4103 ++i;
4104 }
4105
4106 return i;
4107}
4108
4109/// isShuffleMaskConsecutive - Check if the shuffle mask indicies from MaskI to
4110/// MaskE correspond consecutively to elements from one of the vector operands,
4111/// starting from its index OpIdx. Also tell OpNum which source vector operand.
4112static
4113bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp, int MaskI, int MaskE,
4114 int OpIdx, int NumElems, unsigned &OpNum) {
4115 bool SeenV1 = false;
4116 bool SeenV2 = false;
4117
4118 for (int i = MaskI; i <= MaskE; ++i, ++OpIdx) {
4119 int Idx = SVOp->getMaskElt(i);
4120 // Ignore undef indicies
4121 if (Idx < 0)
4122 continue;
4123
4124 if (Idx < NumElems)
4125 SeenV1 = true;
4126 else
4127 SeenV2 = true;
4128
4129 // Only accept consecutive elements from the same vector
4130 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
4131 return false;
4132 }
4133
4134 OpNum = SeenV1 ? 0 : 1;
4135 return true;
4136}
4137
4138/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
4139/// logical left shift of a vector.
4140static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4141 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4142 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4143 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4144 false /* check zeros from right */, DAG);
4145 unsigned OpSrc;
4146
4147 if (!NumZeros)
4148 return false;
4149
4150 // Considering the elements in the mask that are not consecutive zeros,
4151 // check if they consecutively come from only one of the source vectors.
4152 //
4153 // V1 = {X, A, B, C} 0
4154 // \ \ \ /
4155 // vector_shuffle V1, V2 <1, 2, 3, X>
4156 //
4157 if (!isShuffleMaskConsecutive(SVOp,
4158 0, // Mask Start Index
4159 NumElems-NumZeros-1, // Mask End Index
4160 NumZeros, // Where to start looking in the src vector
4161 NumElems, // Number of elements in vector
4162 OpSrc)) // Which source operand ?
4163 return false;
4164
4165 isLeft = false;
4166 ShAmt = NumZeros;
4167 ShVal = SVOp->getOperand(OpSrc);
4168 return true;
4169}
4170
4171/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
4172/// logical left shift of a vector.
4173static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4174 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4175 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4176 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4177 true /* check zeros from left */, DAG);
4178 unsigned OpSrc;
4179
4180 if (!NumZeros)
4181 return false;
4182
4183 // Considering the elements in the mask that are not consecutive zeros,
4184 // check if they consecutively come from only one of the source vectors.
4185 //
4186 // 0 { A, B, X, X } = V2
4187 // / \ / /
4188 // vector_shuffle V1, V2 <X, X, 4, 5>
4189 //
4190 if (!isShuffleMaskConsecutive(SVOp,
4191 NumZeros, // Mask Start Index
4192 NumElems-1, // Mask End Index
4193 0, // Where to start looking in the src vector
4194 NumElems, // Number of elements in vector
4195 OpSrc)) // Which source operand ?
4196 return false;
4197
4198 isLeft = true;
4199 ShAmt = NumZeros;
4200 ShVal = SVOp->getOperand(OpSrc);
4201 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004202}
4203
4204/// isVectorShift - Returns true if the shuffle can be implemented as a
4205/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004206static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00004207 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004208 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
4209 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
4210 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004211
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004212 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00004213}
4214
Evan Chengc78d3b42006-04-24 18:01:45 +00004215/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
4216///
Dan Gohman475871a2008-07-27 21:46:04 +00004217static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00004218 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00004219 SelectionDAG &DAG,
4220 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004221 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00004222 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004223
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004224 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004225 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004226 bool First = true;
4227 for (unsigned i = 0; i < 16; ++i) {
4228 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
4229 if (ThisIsNonZero && First) {
4230 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004231 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004232 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004233 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004234 First = false;
4235 }
4236
4237 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00004238 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004239 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
4240 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004241 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004242 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00004243 }
4244 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004245 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
4246 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
4247 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00004248 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004249 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00004250 } else
4251 ThisElt = LastElt;
4252
Gabor Greifba36cb52008-08-28 21:40:38 +00004253 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00004254 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00004255 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00004256 }
4257 }
4258
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004259 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00004260}
4261
Bill Wendlinga348c562007-03-22 18:42:45 +00004262/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00004263///
Dan Gohman475871a2008-07-27 21:46:04 +00004264static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00004265 unsigned NumNonZero, unsigned NumZero,
4266 SelectionDAG &DAG,
4267 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004268 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00004269 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004270
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004271 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004272 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004273 bool First = true;
4274 for (unsigned i = 0; i < 8; ++i) {
4275 bool isNonZero = (NonZeros & (1 << i)) != 0;
4276 if (isNonZero) {
4277 if (First) {
4278 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004279 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004280 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004281 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004282 First = false;
4283 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004284 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004285 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00004286 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00004287 }
4288 }
4289
4290 return V;
4291}
4292
Evan Chengf26ffe92008-05-29 08:22:04 +00004293/// getVShift - Return a vector logical shift node.
4294///
Owen Andersone50ed302009-08-10 22:56:29 +00004295static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00004296 unsigned NumBits, SelectionDAG &DAG,
4297 const TargetLowering &TLI, DebugLoc dl) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004298 EVT ShVT = MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00004299 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004300 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
4301 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004302 DAG.getNode(Opc, dl, ShVT, SrcOp,
Owen Anderson95771af2011-02-25 21:41:48 +00004303 DAG.getConstant(NumBits,
4304 TLI.getShiftAmountTy(SrcOp.getValueType()))));
Evan Chengf26ffe92008-05-29 08:22:04 +00004305}
4306
Dan Gohman475871a2008-07-27 21:46:04 +00004307SDValue
Evan Chengc3630942009-12-09 21:00:30 +00004308X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00004309 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00004310
Evan Chengc3630942009-12-09 21:00:30 +00004311 // Check if the scalar load can be widened into a vector load. And if
4312 // the address is "base + cst" see if the cst can be "absorbed" into
4313 // the shuffle mask.
4314 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
4315 SDValue Ptr = LD->getBasePtr();
4316 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
4317 return SDValue();
4318 EVT PVT = LD->getValueType(0);
4319 if (PVT != MVT::i32 && PVT != MVT::f32)
4320 return SDValue();
4321
4322 int FI = -1;
4323 int64_t Offset = 0;
4324 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
4325 FI = FINode->getIndex();
4326 Offset = 0;
Chris Lattner0a9481f2011-02-13 22:25:43 +00004327 } else if (DAG.isBaseWithConstantOffset(Ptr) &&
Evan Chengc3630942009-12-09 21:00:30 +00004328 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4329 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4330 Offset = Ptr.getConstantOperandVal(1);
4331 Ptr = Ptr.getOperand(0);
4332 } else {
4333 return SDValue();
4334 }
4335
4336 SDValue Chain = LD->getChain();
4337 // Make sure the stack object alignment is at least 16.
4338 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
4339 if (DAG.InferPtrAlignment(Ptr) < 16) {
4340 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00004341 // Can't change the alignment. FIXME: It's possible to compute
4342 // the exact stack offset and reference FI + adjust offset instead.
4343 // If someone *really* cares about this. That's the way to implement it.
4344 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004345 } else {
4346 MFI->setObjectAlignment(FI, 16);
4347 }
4348 }
4349
4350 // (Offset % 16) must be multiple of 4. Then address is then
4351 // Ptr + (Offset & ~15).
4352 if (Offset < 0)
4353 return SDValue();
4354 if ((Offset % 16) & 3)
4355 return SDValue();
4356 int64_t StartOffset = Offset & ~15;
4357 if (StartOffset)
4358 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
4359 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
4360
4361 int EltNo = (Offset - StartOffset) >> 2;
4362 int Mask[4] = { EltNo, EltNo, EltNo, EltNo };
4363 EVT VT = (PVT == MVT::i32) ? MVT::v4i32 : MVT::v4f32;
Chris Lattner51abfe42010-09-21 06:02:19 +00004364 SDValue V1 = DAG.getLoad(VT, dl, Chain, Ptr,
4365 LD->getPointerInfo().getWithOffset(StartOffset),
David Greene67c9d422010-02-15 16:53:33 +00004366 false, false, 0);
Evan Chengc3630942009-12-09 21:00:30 +00004367 // Canonicalize it to a v4i32 shuffle.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004368 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, V1);
4369 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Chengc3630942009-12-09 21:00:30 +00004370 DAG.getVectorShuffle(MVT::v4i32, dl, V1,
Chris Lattner51abfe42010-09-21 06:02:19 +00004371 DAG.getUNDEF(MVT::v4i32),&Mask[0]));
Evan Chengc3630942009-12-09 21:00:30 +00004372 }
4373
4374 return SDValue();
4375}
4376
Michael J. Spencerec38de22010-10-10 22:04:20 +00004377/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
4378/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00004379/// load which has the same value as a build_vector whose operands are 'elts'.
4380///
4381/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00004382///
Nate Begeman1449f292010-03-24 22:19:06 +00004383/// FIXME: we'd also like to handle the case where the last elements are zero
4384/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
4385/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004386static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00004387 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004388 EVT EltVT = VT.getVectorElementType();
4389 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00004390
Nate Begemanfdea31a2010-03-24 20:49:50 +00004391 LoadSDNode *LDBase = NULL;
4392 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004393
Nate Begeman1449f292010-03-24 22:19:06 +00004394 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00004395 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00004396 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004397 for (unsigned i = 0; i < NumElems; ++i) {
4398 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00004399
Nate Begemanfdea31a2010-03-24 20:49:50 +00004400 if (!Elt.getNode() ||
4401 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
4402 return SDValue();
4403 if (!LDBase) {
4404 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
4405 return SDValue();
4406 LDBase = cast<LoadSDNode>(Elt.getNode());
4407 LastLoadedElt = i;
4408 continue;
4409 }
4410 if (Elt.getOpcode() == ISD::UNDEF)
4411 continue;
4412
4413 LoadSDNode *LD = cast<LoadSDNode>(Elt);
4414 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
4415 return SDValue();
4416 LastLoadedElt = i;
4417 }
Nate Begeman1449f292010-03-24 22:19:06 +00004418
4419 // If we have found an entire vector of loads and undefs, then return a large
4420 // load of the entire vector width starting at the base pointer. If we found
4421 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004422 if (LastLoadedElt == NumElems - 1) {
4423 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00004424 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004425 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004426 LDBase->isVolatile(), LDBase->isNonTemporal(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00004427 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004428 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004429 LDBase->isVolatile(), LDBase->isNonTemporal(),
4430 LDBase->getAlignment());
4431 } else if (NumElems == 4 && LastLoadedElt == 1) {
4432 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
4433 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Chris Lattner88641552010-09-22 00:34:38 +00004434 SDValue ResNode = DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys,
4435 Ops, 2, MVT::i32,
4436 LDBase->getMemOperand());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004437 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00004438 }
4439 return SDValue();
4440}
4441
Evan Chengc3630942009-12-09 21:00:30 +00004442SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004443X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004444 DebugLoc dl = Op.getDebugLoc();
David Greenea5f26012011-02-07 19:36:54 +00004445
David Greenef125a292011-02-08 19:04:41 +00004446 EVT VT = Op.getValueType();
4447 EVT ExtVT = VT.getVectorElementType();
4448
4449 unsigned NumElems = Op.getNumOperands();
4450
4451 // For AVX-length vectors, build the individual 128-bit pieces and
4452 // use shuffles to put them in place.
Owen Anderson95771af2011-02-25 21:41:48 +00004453 if (VT.getSizeInBits() > 256 &&
4454 Subtarget->hasAVX() &&
David Greenef125a292011-02-08 19:04:41 +00004455 !ISD::isBuildVectorAllZeros(Op.getNode())) {
4456 SmallVector<SDValue, 8> V;
4457 V.resize(NumElems);
4458 for (unsigned i = 0; i < NumElems; ++i) {
4459 V[i] = Op.getOperand(i);
4460 }
Owen Anderson95771af2011-02-25 21:41:48 +00004461
David Greenef125a292011-02-08 19:04:41 +00004462 EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElems/2);
4463
4464 // Build the lower subvector.
4465 SDValue Lower = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[0], NumElems/2);
4466 // Build the upper subvector.
4467 SDValue Upper = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[NumElems / 2],
4468 NumElems/2);
4469
4470 return ConcatVectors(Lower, Upper, DAG);
4471 }
4472
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00004473 // All zero's:
4474 // - pxor (SSE2), xorps (SSE1), vpxor (128 AVX), xorp[s|d] (256 AVX)
4475 // All one's:
4476 // - pcmpeqd (SSE2 and 128 AVX), fallback to constant pools (256 AVX)
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004477 if (ISD::isBuildVectorAllZeros(Op.getNode()) ||
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00004478 ISD::isBuildVectorAllOnes(Op.getNode())) {
4479 // Canonicalize this to <4 x i32> or <8 x 32> (SSE) to
Chris Lattner8a594482007-11-25 00:24:49 +00004480 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
4481 // eliminated on x86-32 hosts.
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00004482 if (Op.getValueType() == MVT::v4i32 ||
4483 Op.getValueType() == MVT::v8i32)
Chris Lattner8a594482007-11-25 00:24:49 +00004484 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004485
Gabor Greifba36cb52008-08-28 21:40:38 +00004486 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00004487 return getOnesVector(Op.getValueType(), DAG, dl);
4488 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00004489 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004490
Owen Andersone50ed302009-08-10 22:56:29 +00004491 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004492
Evan Cheng0db9fe62006-04-25 20:13:52 +00004493 unsigned NumZero = 0;
4494 unsigned NumNonZero = 0;
4495 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004496 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00004497 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004498 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00004499 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00004500 if (Elt.getOpcode() == ISD::UNDEF)
4501 continue;
4502 Values.insert(Elt);
4503 if (Elt.getOpcode() != ISD::Constant &&
4504 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004505 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00004506 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00004507 NumZero++;
4508 else {
4509 NonZeros |= (1 << i);
4510 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004511 }
4512 }
4513
Chris Lattner97a2a562010-08-26 05:24:29 +00004514 // All undef vector. Return an UNDEF. All zero vectors were handled above.
4515 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00004516 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004517
Chris Lattner67f453a2008-03-09 05:42:06 +00004518 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00004519 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004520 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00004521 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00004522
Chris Lattner62098042008-03-09 01:05:04 +00004523 // If this is an insertion of an i64 value on x86-32, and if the top bits of
4524 // the value are obviously zero, truncate the value to i32 and do the
4525 // insertion that way. Only do this if the value is non-constant or if the
4526 // value is a constant being inserted into element 0. It is cheaper to do
4527 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00004528 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00004529 (!IsAllConstants || Idx == 0)) {
4530 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004531 // Handle SSE only.
4532 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
4533 EVT VecVT = MVT::v4i32;
4534 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00004535
Chris Lattner62098042008-03-09 01:05:04 +00004536 // Truncate the value (which may itself be a constant) to i32, and
4537 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00004538 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00004539 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00004540 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
4541 Subtarget->hasSSE2(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00004542
Chris Lattner62098042008-03-09 01:05:04 +00004543 // Now we have our 32-bit value zero extended in the low element of
4544 // a vector. If Idx != 0, swizzle it into place.
4545 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004546 SmallVector<int, 4> Mask;
4547 Mask.push_back(Idx);
4548 for (unsigned i = 1; i != VecElts; ++i)
4549 Mask.push_back(i);
4550 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopherfd179292009-08-27 18:07:15 +00004551 DAG.getUNDEF(Item.getValueType()),
Nate Begeman9008ca62009-04-27 18:41:29 +00004552 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004553 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004554 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00004555 }
4556 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004557
Chris Lattner19f79692008-03-08 22:59:52 +00004558 // If we have a constant or non-constant insertion into the low element of
4559 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
4560 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00004561 // depending on what the source datatype is.
4562 if (Idx == 0) {
4563 if (NumZero == 0) {
4564 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Owen Anderson825b72b2009-08-11 20:47:22 +00004565 } else if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
4566 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Eli Friedman10415532009-06-06 06:05:10 +00004567 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
4568 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
4569 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget->hasSSE2(),
4570 DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00004571 } else if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
4572 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Dale Johannesen0488fb62010-09-30 23:57:10 +00004573 assert(VT.getSizeInBits() == 128 && "Expected an SSE value type!");
4574 EVT MiddleVT = MVT::v4i32;
Eli Friedman10415532009-06-06 06:05:10 +00004575 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
4576 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
4577 Subtarget->hasSSE2(), DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004578 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00004579 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004580 }
Evan Chengf26ffe92008-05-29 08:22:04 +00004581
4582 // Is it a vector logical left shift?
4583 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00004584 X86::isZeroNode(Op.getOperand(0)) &&
4585 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004586 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00004587 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00004588 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004589 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00004590 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004591 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004592
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004593 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00004594 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004595
Chris Lattner19f79692008-03-08 22:59:52 +00004596 // Otherwise, if this is a vector with i32 or f32 elements, and the element
4597 // is a non-constant being inserted into an element other than the low one,
4598 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
4599 // movd/movss) to move this into the low element, then shuffle it into
4600 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004601 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00004602 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00004603
Evan Cheng0db9fe62006-04-25 20:13:52 +00004604 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00004605 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
4606 Subtarget->hasSSE2(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00004607 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004608 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00004609 MaskVec.push_back(i == Idx ? 0 : 1);
4610 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004611 }
4612 }
4613
Chris Lattner67f453a2008-03-09 05:42:06 +00004614 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00004615 if (Values.size() == 1) {
4616 if (EVTBits == 32) {
4617 // Instead of a shuffle like this:
4618 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
4619 // Check if it's possible to issue this instead.
4620 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
4621 unsigned Idx = CountTrailingZeros_32(NonZeros);
4622 SDValue Item = Op.getOperand(Idx);
4623 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
4624 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
4625 }
Dan Gohman475871a2008-07-27 21:46:04 +00004626 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004627 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004628
Dan Gohmana3941172007-07-24 22:55:08 +00004629 // A vector full of immediates; various special cases are already
4630 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004631 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00004632 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00004633
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00004634 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00004635 if (EVTBits == 64) {
4636 if (NumNonZero == 1) {
4637 // One half is zero or undef.
4638 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00004639 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00004640 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00004641 return getShuffleVectorZeroOrUndef(V2, Idx, true,
4642 Subtarget->hasSSE2(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00004643 }
Dan Gohman475871a2008-07-27 21:46:04 +00004644 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00004645 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004646
4647 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00004648 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00004649 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00004650 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004651 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004652 }
4653
Bill Wendling826f36f2007-03-28 00:57:11 +00004654 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00004655 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Chris Lattner97a2a562010-08-26 05:24:29 +00004656 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004657 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004658 }
4659
4660 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00004661 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00004662 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004663 if (NumElems == 4 && NumZero > 0) {
4664 for (unsigned i = 0; i < 4; ++i) {
4665 bool isZero = !(NonZeros & (1 << i));
4666 if (isZero)
Dale Johannesenace16102009-02-03 19:33:06 +00004667 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004668 else
Dale Johannesenace16102009-02-03 19:33:06 +00004669 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004670 }
4671
4672 for (unsigned i = 0; i < 2; ++i) {
4673 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
4674 default: break;
4675 case 0:
4676 V[i] = V[i*2]; // Must be a zero vector.
4677 break;
4678 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00004679 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004680 break;
4681 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00004682 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004683 break;
4684 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00004685 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004686 break;
4687 }
4688 }
4689
Nate Begeman9008ca62009-04-27 18:41:29 +00004690 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004691 bool Reverse = (NonZeros & 0x3) == 2;
4692 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004693 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004694 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
4695 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004696 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
4697 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004698 }
4699
Nate Begemanfdea31a2010-03-24 20:49:50 +00004700 if (Values.size() > 1 && VT.getSizeInBits() == 128) {
4701 // Check for a build vector of consecutive loads.
4702 for (unsigned i = 0; i < NumElems; ++i)
4703 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00004704
Nate Begemanfdea31a2010-03-24 20:49:50 +00004705 // Check for elements which are consecutive loads.
4706 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
4707 if (LD.getNode())
4708 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004709
4710 // For SSE 4.1, use insertps to put the high elements into the low element.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004711 if (getSubtarget()->hasSSE41()) {
Chris Lattner24faf612010-08-28 17:59:08 +00004712 SDValue Result;
4713 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
4714 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
4715 else
4716 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00004717
Chris Lattner24faf612010-08-28 17:59:08 +00004718 for (unsigned i = 1; i < NumElems; ++i) {
4719 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
4720 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00004721 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00004722 }
4723 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00004724 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00004725
Chris Lattner6e80e442010-08-28 17:15:43 +00004726 // Otherwise, expand into a number of unpckl*, start by extending each of
4727 // our (non-undef) elements to the full vector width with the element in the
4728 // bottom slot of the vector (which generates no code for SSE).
4729 for (unsigned i = 0; i < NumElems; ++i) {
4730 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
4731 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
4732 else
4733 V[i] = DAG.getUNDEF(VT);
4734 }
4735
4736 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004737 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
4738 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
4739 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00004740 unsigned EltStride = NumElems >> 1;
4741 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00004742 for (unsigned i = 0; i < EltStride; ++i) {
4743 // If V[i+EltStride] is undef and this is the first round of mixing,
4744 // then it is safe to just drop this shuffle: V[i] is already in the
4745 // right place, the one element (since it's the first round) being
4746 // inserted as undef can be dropped. This isn't safe for successive
4747 // rounds because they will permute elements within both vectors.
4748 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
4749 EltStride == NumElems/2)
4750 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004751
Chris Lattner6e80e442010-08-28 17:15:43 +00004752 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00004753 }
Chris Lattner6e80e442010-08-28 17:15:43 +00004754 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004755 }
4756 return V[0];
4757 }
Dan Gohman475871a2008-07-27 21:46:04 +00004758 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004759}
4760
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004761SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004762X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const {
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004763 // We support concatenate two MMX registers and place them in a MMX
4764 // register. This is better than doing a stack convert.
4765 DebugLoc dl = Op.getDebugLoc();
4766 EVT ResVT = Op.getValueType();
4767 assert(Op.getNumOperands() == 2);
4768 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
4769 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
4770 int Mask[2];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004771 SDValue InVec = DAG.getNode(ISD::BITCAST,dl, MVT::v1i64, Op.getOperand(0));
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004772 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4773 InVec = Op.getOperand(1);
4774 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4775 unsigned NumElts = ResVT.getVectorNumElements();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004776 VecOp = DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004777 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
4778 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
4779 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004780 InVec = DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, InVec);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004781 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4782 Mask[0] = 0; Mask[1] = 2;
4783 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
4784 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004785 return DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004786}
4787
Nate Begemanb9a47b82009-02-23 08:49:38 +00004788// v8i16 shuffles - Prefer shuffles in the following order:
4789// 1. [all] pshuflw, pshufhw, optional move
4790// 2. [ssse3] 1 x pshufb
4791// 3. [ssse3] 2 x pshufb + 1 x por
4792// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004793SDValue
4794X86TargetLowering::LowerVECTOR_SHUFFLEv8i16(SDValue Op,
4795 SelectionDAG &DAG) const {
4796 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00004797 SDValue V1 = SVOp->getOperand(0);
4798 SDValue V2 = SVOp->getOperand(1);
4799 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004800 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00004801
Nate Begemanb9a47b82009-02-23 08:49:38 +00004802 // Determine if more than 1 of the words in each of the low and high quadwords
4803 // of the result come from the same quadword of one of the two inputs. Undef
4804 // mask values count as coming from any quadword, for better codegen.
4805 SmallVector<unsigned, 4> LoQuad(4);
4806 SmallVector<unsigned, 4> HiQuad(4);
4807 BitVector InputQuads(4);
4808 for (unsigned i = 0; i < 8; ++i) {
4809 SmallVectorImpl<unsigned> &Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00004810 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004811 MaskVals.push_back(EltIdx);
4812 if (EltIdx < 0) {
4813 ++Quad[0];
4814 ++Quad[1];
4815 ++Quad[2];
4816 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00004817 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004818 }
4819 ++Quad[EltIdx / 4];
4820 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00004821 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004822
Nate Begemanb9a47b82009-02-23 08:49:38 +00004823 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004824 unsigned MaxQuad = 1;
4825 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004826 if (LoQuad[i] > MaxQuad) {
4827 BestLoQuad = i;
4828 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004829 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004830 }
4831
Nate Begemanb9a47b82009-02-23 08:49:38 +00004832 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004833 MaxQuad = 1;
4834 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004835 if (HiQuad[i] > MaxQuad) {
4836 BestHiQuad = i;
4837 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004838 }
4839 }
4840
Nate Begemanb9a47b82009-02-23 08:49:38 +00004841 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00004842 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00004843 // single pshufb instruction is necessary. If There are more than 2 input
4844 // quads, disable the next transformation since it does not help SSSE3.
4845 bool V1Used = InputQuads[0] || InputQuads[1];
4846 bool V2Used = InputQuads[2] || InputQuads[3];
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004847 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004848 if (InputQuads.count() == 2 && V1Used && V2Used) {
4849 BestLoQuad = InputQuads.find_first();
4850 BestHiQuad = InputQuads.find_next(BestLoQuad);
4851 }
4852 if (InputQuads.count() > 2) {
4853 BestLoQuad = -1;
4854 BestHiQuad = -1;
4855 }
4856 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004857
Nate Begemanb9a47b82009-02-23 08:49:38 +00004858 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
4859 // the shuffle mask. If a quad is scored as -1, that means that it contains
4860 // words from all 4 input quadwords.
4861 SDValue NewV;
4862 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004863 SmallVector<int, 8> MaskV;
4864 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
4865 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Eric Christopherfd179292009-08-27 18:07:15 +00004866 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004867 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
4868 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
4869 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00004870
Nate Begemanb9a47b82009-02-23 08:49:38 +00004871 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
4872 // source words for the shuffle, to aid later transformations.
4873 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00004874 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00004875 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004876 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00004877 if (idx != (int)i)
4878 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004879 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00004880 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004881 AllWordsInNewV = false;
4882 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00004883 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004884
Nate Begemanb9a47b82009-02-23 08:49:38 +00004885 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
4886 if (AllWordsInNewV) {
4887 for (int i = 0; i != 8; ++i) {
4888 int idx = MaskVals[i];
4889 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00004890 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004891 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004892 if ((idx != i) && idx < 4)
4893 pshufhw = false;
4894 if ((idx != i) && idx > 3)
4895 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00004896 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00004897 V1 = NewV;
4898 V2Used = false;
4899 BestLoQuad = 0;
4900 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004901 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004902
Nate Begemanb9a47b82009-02-23 08:49:38 +00004903 // If we've eliminated the use of V2, and the new mask is a pshuflw or
4904 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00004905 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00004906 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
4907 unsigned TargetMask = 0;
4908 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00004909 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00004910 TargetMask = pshufhw ? X86::getShufflePSHUFHWImmediate(NewV.getNode()):
4911 X86::getShufflePSHUFLWImmediate(NewV.getNode());
4912 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004913 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00004914 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004915 }
Eric Christopherfd179292009-08-27 18:07:15 +00004916
Nate Begemanb9a47b82009-02-23 08:49:38 +00004917 // If we have SSSE3, and all words of the result are from 1 input vector,
4918 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
4919 // is present, fall back to case 4.
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004920 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004921 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004922
Nate Begemanb9a47b82009-02-23 08:49:38 +00004923 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00004924 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00004925 // mask, and elements that come from V1 in the V2 mask, so that the two
4926 // results can be OR'd together.
4927 bool TwoInputs = V1Used && V2Used;
4928 for (unsigned i = 0; i != 8; ++i) {
4929 int EltIdx = MaskVals[i] * 2;
4930 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004931 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4932 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004933 continue;
4934 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004935 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
4936 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004937 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004938 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004939 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004940 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004941 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004942 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004943 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004944
Nate Begemanb9a47b82009-02-23 08:49:38 +00004945 // Calculate the shuffle mask for the second input, shuffle it, and
4946 // OR it with the first shuffled input.
4947 pshufbMask.clear();
4948 for (unsigned i = 0; i != 8; ++i) {
4949 int EltIdx = MaskVals[i] * 2;
4950 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004951 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4952 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004953 continue;
4954 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004955 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
4956 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004957 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004958 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00004959 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00004960 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004961 MVT::v16i8, &pshufbMask[0], 16));
4962 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004963 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004964 }
4965
4966 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
4967 // and update MaskVals with new element order.
4968 BitVector InOrder(8);
4969 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004970 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004971 for (int i = 0; i != 4; ++i) {
4972 int idx = MaskVals[i];
4973 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004974 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004975 InOrder.set(i);
4976 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004977 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004978 InOrder.set(i);
4979 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004980 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004981 }
4982 }
4983 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004984 MaskV.push_back(i);
Owen Anderson825b72b2009-08-11 20:47:22 +00004985 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004986 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004987
4988 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
4989 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
4990 NewV.getOperand(0),
4991 X86::getShufflePSHUFLWImmediate(NewV.getNode()),
4992 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004993 }
Eric Christopherfd179292009-08-27 18:07:15 +00004994
Nate Begemanb9a47b82009-02-23 08:49:38 +00004995 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
4996 // and update MaskVals with the new element order.
4997 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004998 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004999 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005000 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005001 for (unsigned i = 4; i != 8; ++i) {
5002 int idx = MaskVals[i];
5003 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005004 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005005 InOrder.set(i);
5006 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005007 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005008 InOrder.set(i);
5009 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005010 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005011 }
5012 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005013 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005014 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005015
5016 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
5017 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
5018 NewV.getOperand(0),
5019 X86::getShufflePSHUFHWImmediate(NewV.getNode()),
5020 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005021 }
Eric Christopherfd179292009-08-27 18:07:15 +00005022
Nate Begemanb9a47b82009-02-23 08:49:38 +00005023 // In case BestHi & BestLo were both -1, which means each quadword has a word
5024 // from each of the four input quadwords, calculate the InOrder bitvector now
5025 // before falling through to the insert/extract cleanup.
5026 if (BestLoQuad == -1 && BestHiQuad == -1) {
5027 NewV = V1;
5028 for (int i = 0; i != 8; ++i)
5029 if (MaskVals[i] < 0 || MaskVals[i] == i)
5030 InOrder.set(i);
5031 }
Eric Christopherfd179292009-08-27 18:07:15 +00005032
Nate Begemanb9a47b82009-02-23 08:49:38 +00005033 // The other elements are put in the right place using pextrw and pinsrw.
5034 for (unsigned i = 0; i != 8; ++i) {
5035 if (InOrder[i])
5036 continue;
5037 int EltIdx = MaskVals[i];
5038 if (EltIdx < 0)
5039 continue;
5040 SDValue ExtOp = (EltIdx < 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00005041 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005042 DAG.getIntPtrConstant(EltIdx))
Owen Anderson825b72b2009-08-11 20:47:22 +00005043 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005044 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00005045 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005046 DAG.getIntPtrConstant(i));
5047 }
5048 return NewV;
5049}
5050
5051// v16i8 shuffles - Prefer shuffles in the following order:
5052// 1. [ssse3] 1 x pshufb
5053// 2. [ssse3] 2 x pshufb + 1 x por
5054// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
5055static
Nate Begeman9008ca62009-04-27 18:41:29 +00005056SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00005057 SelectionDAG &DAG,
5058 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005059 SDValue V1 = SVOp->getOperand(0);
5060 SDValue V2 = SVOp->getOperand(1);
5061 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005062 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00005063 SVOp->getMask(MaskVals);
Eric Christopherfd179292009-08-27 18:07:15 +00005064
Nate Begemanb9a47b82009-02-23 08:49:38 +00005065 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00005066 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00005067 // present, fall back to case 3.
5068 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
5069 bool V1Only = true;
5070 bool V2Only = true;
5071 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005072 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00005073 if (EltIdx < 0)
5074 continue;
5075 if (EltIdx < 16)
5076 V2Only = false;
5077 else
5078 V1Only = false;
5079 }
Eric Christopherfd179292009-08-27 18:07:15 +00005080
Nate Begemanb9a47b82009-02-23 08:49:38 +00005081 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
5082 if (TLI.getSubtarget()->hasSSSE3()) {
5083 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005084
Nate Begemanb9a47b82009-02-23 08:49:38 +00005085 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00005086 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005087 //
5088 // Otherwise, we have elements from both input vectors, and must zero out
5089 // elements that come from V2 in the first mask, and V1 in the second mask
5090 // so that we can OR them together.
5091 bool TwoInputs = !(V1Only || V2Only);
5092 for (unsigned i = 0; i != 16; ++i) {
5093 int EltIdx = MaskVals[i];
5094 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005095 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005096 continue;
5097 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005098 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005099 }
5100 // If all the elements are from V2, assign it to V1 and return after
5101 // building the first pshufb.
5102 if (V2Only)
5103 V1 = V2;
Owen Anderson825b72b2009-08-11 20:47:22 +00005104 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005105 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005106 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005107 if (!TwoInputs)
5108 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00005109
Nate Begemanb9a47b82009-02-23 08:49:38 +00005110 // Calculate the shuffle mask for the second input, shuffle it, and
5111 // OR it with the first shuffled input.
5112 pshufbMask.clear();
5113 for (unsigned i = 0; i != 16; ++i) {
5114 int EltIdx = MaskVals[i];
5115 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005116 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005117 continue;
5118 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005119 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005120 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005121 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005122 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005123 MVT::v16i8, &pshufbMask[0], 16));
5124 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005125 }
Eric Christopherfd179292009-08-27 18:07:15 +00005126
Nate Begemanb9a47b82009-02-23 08:49:38 +00005127 // No SSSE3 - Calculate in place words and then fix all out of place words
5128 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
5129 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005130 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
5131 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005132 SDValue NewV = V2Only ? V2 : V1;
5133 for (int i = 0; i != 8; ++i) {
5134 int Elt0 = MaskVals[i*2];
5135 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00005136
Nate Begemanb9a47b82009-02-23 08:49:38 +00005137 // This word of the result is all undef, skip it.
5138 if (Elt0 < 0 && Elt1 < 0)
5139 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005140
Nate Begemanb9a47b82009-02-23 08:49:38 +00005141 // This word of the result is already in the correct place, skip it.
5142 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
5143 continue;
5144 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
5145 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005146
Nate Begemanb9a47b82009-02-23 08:49:38 +00005147 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
5148 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
5149 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00005150
5151 // If Elt0 and Elt1 are defined, are consecutive, and can be load
5152 // using a single extract together, load it and store it.
5153 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005154 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005155 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00005156 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005157 DAG.getIntPtrConstant(i));
5158 continue;
5159 }
5160
Nate Begemanb9a47b82009-02-23 08:49:38 +00005161 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00005162 // source byte is not also odd, shift the extracted word left 8 bits
5163 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005164 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005165 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005166 DAG.getIntPtrConstant(Elt1 / 2));
5167 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005168 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Owen Anderson95771af2011-02-25 21:41:48 +00005169 DAG.getConstant(8,
5170 TLI.getShiftAmountTy(InsElt.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005171 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005172 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
5173 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005174 }
5175 // If Elt0 is defined, extract it from the appropriate source. If the
5176 // source byte is not also even, shift the extracted word right 8 bits. If
5177 // Elt1 was also defined, OR the extracted values together before
5178 // inserting them in the result.
5179 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005180 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005181 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
5182 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005183 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Owen Anderson95771af2011-02-25 21:41:48 +00005184 DAG.getConstant(8,
5185 TLI.getShiftAmountTy(InsElt0.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005186 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005187 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
5188 DAG.getConstant(0x00FF, MVT::i16));
5189 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00005190 : InsElt0;
5191 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005192 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005193 DAG.getIntPtrConstant(i));
5194 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005195 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005196}
5197
Evan Cheng7a831ce2007-12-15 03:00:47 +00005198/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005199/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00005200/// done when every pair / quad of shuffle mask elements point to elements in
5201/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005202/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00005203static
Nate Begeman9008ca62009-04-27 18:41:29 +00005204SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005205 SelectionDAG &DAG, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00005206 EVT VT = SVOp->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00005207 SDValue V1 = SVOp->getOperand(0);
5208 SDValue V2 = SVOp->getOperand(1);
5209 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00005210 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005211 EVT NewVT;
Owen Anderson825b72b2009-08-11 20:47:22 +00005212 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005213 default: assert(false && "Unexpected!");
Owen Anderson825b72b2009-08-11 20:47:22 +00005214 case MVT::v4f32: NewVT = MVT::v2f64; break;
5215 case MVT::v4i32: NewVT = MVT::v2i64; break;
5216 case MVT::v8i16: NewVT = MVT::v4i32; break;
5217 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00005218 }
5219
Nate Begeman9008ca62009-04-27 18:41:29 +00005220 int Scale = NumElems / NewWidth;
5221 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00005222 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005223 int StartIdx = -1;
5224 for (int j = 0; j < Scale; ++j) {
5225 int EltIdx = SVOp->getMaskElt(i+j);
5226 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005227 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00005228 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00005229 StartIdx = EltIdx - (EltIdx % Scale);
5230 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00005231 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00005232 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005233 if (StartIdx == -1)
5234 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00005235 else
Nate Begeman9008ca62009-04-27 18:41:29 +00005236 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005237 }
5238
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005239 V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, V1);
5240 V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00005241 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005242}
5243
Evan Chengd880b972008-05-09 21:53:03 +00005244/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005245///
Owen Andersone50ed302009-08-10 22:56:29 +00005246static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00005247 SDValue SrcOp, SelectionDAG &DAG,
5248 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005249 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005250 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00005251 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00005252 LD = dyn_cast<LoadSDNode>(SrcOp);
5253 if (!LD) {
5254 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
5255 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00005256 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00005257 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00005258 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005259 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00005260 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005261 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00005262 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005263 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005264 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
5265 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
5266 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00005267 SrcOp.getOperand(0)
5268 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005269 }
5270 }
5271 }
5272
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005273 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005274 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005275 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00005276 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005277}
5278
Evan Chengace3c172008-07-22 21:13:36 +00005279/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
5280/// shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00005281static SDValue
Nate Begeman9008ca62009-04-27 18:41:29 +00005282LowerVECTOR_SHUFFLE_4wide(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
5283 SDValue V1 = SVOp->getOperand(0);
5284 SDValue V2 = SVOp->getOperand(1);
5285 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005286 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00005287
Evan Chengace3c172008-07-22 21:13:36 +00005288 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00005289 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00005290 SmallVector<int, 8> Mask1(4U, -1);
5291 SmallVector<int, 8> PermMask;
5292 SVOp->getMask(PermMask);
5293
Evan Chengace3c172008-07-22 21:13:36 +00005294 unsigned NumHi = 0;
5295 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00005296 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005297 int Idx = PermMask[i];
5298 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00005299 Locs[i] = std::make_pair(-1, -1);
5300 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005301 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
5302 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00005303 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00005304 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005305 NumLo++;
5306 } else {
5307 Locs[i] = std::make_pair(1, NumHi);
5308 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00005309 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005310 NumHi++;
5311 }
5312 }
5313 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005314
Evan Chengace3c172008-07-22 21:13:36 +00005315 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005316 // If no more than two elements come from either vector. This can be
5317 // implemented with two shuffles. First shuffle gather the elements.
5318 // The second shuffle, which takes the first shuffle as both of its
5319 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00005320 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005321
Nate Begeman9008ca62009-04-27 18:41:29 +00005322 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopherfd179292009-08-27 18:07:15 +00005323
Evan Chengace3c172008-07-22 21:13:36 +00005324 for (unsigned i = 0; i != 4; ++i) {
5325 if (Locs[i].first == -1)
5326 continue;
5327 else {
5328 unsigned Idx = (i < 2) ? 0 : 4;
5329 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00005330 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005331 }
5332 }
5333
Nate Begeman9008ca62009-04-27 18:41:29 +00005334 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005335 } else if (NumLo == 3 || NumHi == 3) {
5336 // Otherwise, we must have three elements from one vector, call it X, and
5337 // one element from the other, call it Y. First, use a shufps to build an
5338 // intermediate vector with the one element from Y and the element from X
5339 // that will be in the same half in the final destination (the indexes don't
5340 // matter). Then, use a shufps to build the final vector, taking the half
5341 // containing the element from Y from the intermediate, and the other half
5342 // from X.
5343 if (NumHi == 3) {
5344 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00005345 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005346 std::swap(V1, V2);
5347 }
5348
5349 // Find the element from V2.
5350 unsigned HiIndex;
5351 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005352 int Val = PermMask[HiIndex];
5353 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005354 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005355 if (Val >= 4)
5356 break;
5357 }
5358
Nate Begeman9008ca62009-04-27 18:41:29 +00005359 Mask1[0] = PermMask[HiIndex];
5360 Mask1[1] = -1;
5361 Mask1[2] = PermMask[HiIndex^1];
5362 Mask1[3] = -1;
5363 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005364
5365 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005366 Mask1[0] = PermMask[0];
5367 Mask1[1] = PermMask[1];
5368 Mask1[2] = HiIndex & 1 ? 6 : 4;
5369 Mask1[3] = HiIndex & 1 ? 4 : 6;
5370 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005371 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005372 Mask1[0] = HiIndex & 1 ? 2 : 0;
5373 Mask1[1] = HiIndex & 1 ? 0 : 2;
5374 Mask1[2] = PermMask[2];
5375 Mask1[3] = PermMask[3];
5376 if (Mask1[2] >= 0)
5377 Mask1[2] += 4;
5378 if (Mask1[3] >= 0)
5379 Mask1[3] += 4;
5380 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005381 }
Evan Chengace3c172008-07-22 21:13:36 +00005382 }
5383
5384 // Break it into (shuffle shuffle_hi, shuffle_lo).
5385 Locs.clear();
David Greenec4db4e52011-02-28 19:06:56 +00005386 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00005387 SmallVector<int,8> LoMask(4U, -1);
5388 SmallVector<int,8> HiMask(4U, -1);
5389
5390 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00005391 unsigned MaskIdx = 0;
5392 unsigned LoIdx = 0;
5393 unsigned HiIdx = 2;
5394 for (unsigned i = 0; i != 4; ++i) {
5395 if (i == 2) {
5396 MaskPtr = &HiMask;
5397 MaskIdx = 1;
5398 LoIdx = 0;
5399 HiIdx = 2;
5400 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005401 int Idx = PermMask[i];
5402 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00005403 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00005404 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00005405 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00005406 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005407 LoIdx++;
5408 } else {
5409 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00005410 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005411 HiIdx++;
5412 }
5413 }
5414
Nate Begeman9008ca62009-04-27 18:41:29 +00005415 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
5416 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
5417 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00005418 for (unsigned i = 0; i != 4; ++i) {
5419 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005420 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00005421 } else {
5422 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00005423 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00005424 }
5425 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005426 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00005427}
5428
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005429static bool MayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005430 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005431 V = V.getOperand(0);
5432 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
5433 V = V.getOperand(0);
5434 if (MayFoldLoad(V))
5435 return true;
5436 return false;
5437}
5438
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005439// FIXME: the version above should always be used. Since there's
5440// a bug where several vector shuffles can't be folded because the
5441// DAG is not updated during lowering and a node claims to have two
5442// uses while it only has one, use this version, and let isel match
5443// another instruction if the load really happens to have more than
5444// one use. Remove this version after this bug get fixed.
Evan Cheng835580f2010-10-07 20:50:20 +00005445// rdar://8434668, PR8156
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005446static bool RelaxedMayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005447 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005448 V = V.getOperand(0);
5449 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
5450 V = V.getOperand(0);
5451 if (ISD::isNormalLoad(V.getNode()))
5452 return true;
5453 return false;
5454}
5455
5456/// CanFoldShuffleIntoVExtract - Check if the current shuffle is used by
5457/// a vector extract, and if both can be later optimized into a single load.
5458/// This is done in visitEXTRACT_VECTOR_ELT and the conditions are checked
5459/// here because otherwise a target specific shuffle node is going to be
5460/// emitted for this shuffle, and the optimization not done.
5461/// FIXME: This is probably not the best approach, but fix the problem
5462/// until the right path is decided.
5463static
5464bool CanXFormVExtractWithShuffleIntoLoad(SDValue V, SelectionDAG &DAG,
5465 const TargetLowering &TLI) {
5466 EVT VT = V.getValueType();
5467 ShuffleVectorSDNode *SVOp = dyn_cast<ShuffleVectorSDNode>(V);
5468
5469 // Be sure that the vector shuffle is present in a pattern like this:
5470 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), c) -> (f32 load $addr)
5471 if (!V.hasOneUse())
5472 return false;
5473
5474 SDNode *N = *V.getNode()->use_begin();
5475 if (N->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
5476 return false;
5477
5478 SDValue EltNo = N->getOperand(1);
5479 if (!isa<ConstantSDNode>(EltNo))
5480 return false;
5481
5482 // If the bit convert changed the number of elements, it is unsafe
5483 // to examine the mask.
5484 bool HasShuffleIntoBitcast = false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005485 if (V.getOpcode() == ISD::BITCAST) {
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005486 EVT SrcVT = V.getOperand(0).getValueType();
5487 if (SrcVT.getVectorNumElements() != VT.getVectorNumElements())
5488 return false;
5489 V = V.getOperand(0);
5490 HasShuffleIntoBitcast = true;
5491 }
5492
5493 // Select the input vector, guarding against out of range extract vector.
5494 unsigned NumElems = VT.getVectorNumElements();
5495 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
5496 int Idx = (Elt > NumElems) ? -1 : SVOp->getMaskElt(Elt);
5497 V = (Idx < (int)NumElems) ? V.getOperand(0) : V.getOperand(1);
5498
5499 // Skip one more bit_convert if necessary
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005500 if (V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005501 V = V.getOperand(0);
5502
5503 if (ISD::isNormalLoad(V.getNode())) {
5504 // Is the original load suitable?
5505 LoadSDNode *LN0 = cast<LoadSDNode>(V);
5506
5507 // FIXME: avoid the multi-use bug that is preventing lots of
5508 // of foldings to be detected, this is still wrong of course, but
5509 // give the temporary desired behavior, and if it happens that
5510 // the load has real more uses, during isel it will not fold, and
5511 // will generate poor code.
5512 if (!LN0 || LN0->isVolatile()) // || !LN0->hasOneUse()
5513 return false;
5514
5515 if (!HasShuffleIntoBitcast)
5516 return true;
5517
5518 // If there's a bitcast before the shuffle, check if the load type and
5519 // alignment is valid.
5520 unsigned Align = LN0->getAlignment();
5521 unsigned NewAlign =
5522 TLI.getTargetData()->getABITypeAlignment(
5523 VT.getTypeForEVT(*DAG.getContext()));
5524
5525 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
5526 return false;
5527 }
5528
5529 return true;
5530}
5531
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005532static
Evan Cheng835580f2010-10-07 20:50:20 +00005533SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
5534 EVT VT = Op.getValueType();
5535
5536 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005537 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
5538 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00005539 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
5540 V1, DAG));
5541}
5542
5543static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005544SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
5545 bool HasSSE2) {
5546 SDValue V1 = Op.getOperand(0);
5547 SDValue V2 = Op.getOperand(1);
5548 EVT VT = Op.getValueType();
5549
5550 assert(VT != MVT::v2i64 && "unsupported shuffle type");
5551
5552 if (HasSSE2 && VT == MVT::v2f64)
5553 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
5554
5555 // v4f32 or v4i32
5556 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V2, DAG);
5557}
5558
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00005559static
5560SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
5561 SDValue V1 = Op.getOperand(0);
5562 SDValue V2 = Op.getOperand(1);
5563 EVT VT = Op.getValueType();
5564
5565 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
5566 "unsupported shuffle type");
5567
5568 if (V2.getOpcode() == ISD::UNDEF)
5569 V2 = V1;
5570
5571 // v4i32 or v4f32
5572 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
5573}
5574
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005575static
5576SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasSSE2) {
5577 SDValue V1 = Op.getOperand(0);
5578 SDValue V2 = Op.getOperand(1);
5579 EVT VT = Op.getValueType();
5580 unsigned NumElems = VT.getVectorNumElements();
5581
5582 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
5583 // operand of these instructions is only memory, so check if there's a
5584 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
5585 // same masks.
5586 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005587
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00005588 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005589 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005590 CanFoldLoad = true;
5591
5592 // When V1 is a load, it can be folded later into a store in isel, example:
5593 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
5594 // turns into:
5595 // (MOVLPSmr addr:$src1, VR128:$src2)
5596 // So, recognize this potential and also use MOVLPS or MOVLPD
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005597 if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005598 CanFoldLoad = true;
5599
Eric Christopher893a8822011-02-20 05:04:42 +00005600 // Both of them can't be memory operations though.
5601 if (MayFoldVectorLoad(V1) && MayFoldVectorLoad(V2))
5602 CanFoldLoad = false;
Owen Anderson95771af2011-02-25 21:41:48 +00005603
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005604 if (CanFoldLoad) {
5605 if (HasSSE2 && NumElems == 2)
5606 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
5607
5608 if (NumElems == 4)
5609 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
5610 }
5611
5612 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5613 // movl and movlp will both match v2i64, but v2i64 is never matched by
5614 // movl earlier because we make it strict to avoid messing with the movlp load
5615 // folding logic (see the code above getMOVLP call). Match it here then,
5616 // this is horrible, but will stay like this until we move all shuffle
5617 // matching to x86 specific nodes. Note that for the 1st condition all
5618 // types are matched with movsd.
5619 if ((HasSSE2 && NumElems == 2) || !X86::isMOVLMask(SVOp))
5620 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
5621 else if (HasSSE2)
5622 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
5623
5624
5625 assert(VT != MVT::v4i32 && "unsupported shuffle type");
5626
5627 // Invert the operand order and use SHUFPS to match it.
5628 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V2, V1,
5629 X86::getShuffleSHUFImmediate(SVOp), DAG);
5630}
5631
David Greenec4db4e52011-02-28 19:06:56 +00005632static inline unsigned getUNPCKLOpcode(EVT VT, const X86Subtarget *Subtarget) {
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005633 switch(VT.getSimpleVT().SimpleTy) {
5634 case MVT::v4i32: return X86ISD::PUNPCKLDQ;
5635 case MVT::v2i64: return X86ISD::PUNPCKLQDQ;
David Greenec4db4e52011-02-28 19:06:56 +00005636 case MVT::v4f32:
5637 return Subtarget->hasAVX() ? X86ISD::VUNPCKLPS : X86ISD::UNPCKLPS;
5638 case MVT::v2f64:
5639 return Subtarget->hasAVX() ? X86ISD::VUNPCKLPD : X86ISD::UNPCKLPD;
5640 case MVT::v8f32: return X86ISD::VUNPCKLPSY;
5641 case MVT::v4f64: return X86ISD::VUNPCKLPDY;
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005642 case MVT::v16i8: return X86ISD::PUNPCKLBW;
5643 case MVT::v8i16: return X86ISD::PUNPCKLWD;
5644 default:
Eric Christopherdd6e40a2011-02-19 03:19:09 +00005645 llvm_unreachable("Unknown type for unpckl");
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005646 }
5647 return 0;
5648}
5649
5650static inline unsigned getUNPCKHOpcode(EVT VT) {
5651 switch(VT.getSimpleVT().SimpleTy) {
5652 case MVT::v4i32: return X86ISD::PUNPCKHDQ;
5653 case MVT::v2i64: return X86ISD::PUNPCKHQDQ;
5654 case MVT::v4f32: return X86ISD::UNPCKHPS;
5655 case MVT::v2f64: return X86ISD::UNPCKHPD;
5656 case MVT::v16i8: return X86ISD::PUNPCKHBW;
5657 case MVT::v8i16: return X86ISD::PUNPCKHWD;
5658 default:
Eric Christopherdd6e40a2011-02-19 03:19:09 +00005659 llvm_unreachable("Unknown type for unpckh");
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005660 }
5661 return 0;
5662}
5663
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005664static
5665SDValue NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG,
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005666 const TargetLowering &TLI,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005667 const X86Subtarget *Subtarget) {
5668 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5669 EVT VT = Op.getValueType();
5670 DebugLoc dl = Op.getDebugLoc();
5671 SDValue V1 = Op.getOperand(0);
5672 SDValue V2 = Op.getOperand(1);
5673
5674 if (isZeroShuffle(SVOp))
5675 return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
5676
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005677 // Handle splat operations
5678 if (SVOp->isSplat()) {
5679 // Special case, this is the only place now where it's
5680 // allowed to return a vector_shuffle operation without
5681 // using a target specific node, because *hopefully* it
5682 // will be optimized away by the dag combiner.
5683 if (VT.getVectorNumElements() <= 4 &&
5684 CanXFormVExtractWithShuffleIntoLoad(Op, DAG, TLI))
5685 return Op;
5686
5687 // Handle splats by matching through known masks
5688 if (VT.getVectorNumElements() <= 4)
5689 return SDValue();
5690
Evan Cheng835580f2010-10-07 20:50:20 +00005691 // Canonicalize all of the remaining to v4f32.
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005692 return PromoteSplat(SVOp, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005693 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005694
5695 // If the shuffle can be profitably rewritten as a narrower shuffle, then
5696 // do it!
5697 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
5698 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5699 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005700 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005701 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
5702 // FIXME: Figure out a cleaner way to do this.
5703 // Try to make use of movq to zero out the top part.
5704 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
5705 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5706 if (NewOp.getNode()) {
5707 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
5708 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
5709 DAG, Subtarget, dl);
5710 }
5711 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
5712 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5713 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
5714 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
5715 DAG, Subtarget, dl);
5716 }
5717 }
5718 return SDValue();
5719}
5720
Dan Gohman475871a2008-07-27 21:46:04 +00005721SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005722X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00005723 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00005724 SDValue V1 = Op.getOperand(0);
5725 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00005726 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005727 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00005728 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005729 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005730 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
5731 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00005732 bool V1IsSplat = false;
5733 bool V2IsSplat = false;
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005734 bool HasSSE2 = Subtarget->hasSSE2() || Subtarget->hasAVX();
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00005735 bool HasSSE3 = Subtarget->hasSSE3() || Subtarget->hasAVX();
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00005736 bool HasSSSE3 = Subtarget->hasSSSE3() || Subtarget->hasAVX();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005737 MachineFunction &MF = DAG.getMachineFunction();
5738 bool OptForSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005739
Dale Johannesen0488fb62010-09-30 23:57:10 +00005740 // Shuffle operations on MMX not supported.
5741 if (isMMX)
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00005742 return Op;
5743
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005744 // Vector shuffle lowering takes 3 steps:
5745 //
5746 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
5747 // narrowing and commutation of operands should be handled.
5748 // 2) Matching of shuffles with known shuffle masks to x86 target specific
5749 // shuffle nodes.
5750 // 3) Rewriting of unmatched masks into new generic shuffle operations,
5751 // so the shuffle can be broken into other shuffles and the legalizer can
5752 // try the lowering again.
5753 //
5754 // The general ideia is that no vector_shuffle operation should be left to
5755 // be matched during isel, all of them must be converted to a target specific
5756 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00005757
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005758 // Normalize the input vectors. Here splats, zeroed vectors, profitable
5759 // narrowing and commutation of operands should be handled. The actual code
5760 // doesn't include all of those, work in progress...
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005761 SDValue NewOp = NormalizeVectorShuffle(Op, DAG, *this, Subtarget);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005762 if (NewOp.getNode())
5763 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00005764
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005765 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
5766 // unpckh_undef). Only use pshufd if speed is more important than size.
5767 if (OptForSize && X86::isUNPCKL_v_undef_Mask(SVOp))
5768 if (VT != MVT::v2i64 && VT != MVT::v2f64)
David Greenec4db4e52011-02-28 19:06:56 +00005769 return getTargetShuffleNode(getUNPCKLOpcode(VT, getSubtarget()), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005770 if (OptForSize && X86::isUNPCKH_v_undef_Mask(SVOp))
5771 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5772 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00005773
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005774 if (X86::isMOVDDUPMask(SVOp) && HasSSE3 && V2IsUndef &&
Dale Johannesen0488fb62010-09-30 23:57:10 +00005775 RelaxedMayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00005776 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005777
Dale Johannesen0488fb62010-09-30 23:57:10 +00005778 if (X86::isMOVHLPS_v_undef_Mask(SVOp))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005779 return getMOVHighToLow(Op, dl, DAG);
5780
5781 // Use to match splats
5782 if (HasSSE2 && X86::isUNPCKHMask(SVOp) && V2IsUndef &&
5783 (VT == MVT::v2f64 || VT == MVT::v2i64))
5784 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
5785
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005786 if (X86::isPSHUFDMask(SVOp)) {
5787 // The actual implementation will match the mask in the if above and then
5788 // during isel it can match several different instructions, not only pshufd
5789 // as its name says, sad but true, emulate the behavior for now...
5790 if (X86::isMOVDDUPMask(SVOp) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
5791 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
5792
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005793 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
5794
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005795 if (HasSSE2 && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005796 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
5797
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005798 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005799 return getTargetShuffleNode(X86ISD::SHUFPD, dl, VT, V1, V1,
5800 TargetMask, DAG);
5801
5802 if (VT == MVT::v4f32)
5803 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V1, V1,
5804 TargetMask, DAG);
5805 }
Eric Christopherfd179292009-08-27 18:07:15 +00005806
Evan Chengf26ffe92008-05-29 08:22:04 +00005807 // Check if this can be converted into a logical shift.
5808 bool isLeft = false;
5809 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00005810 SDValue ShVal;
Nate Begeman9008ca62009-04-27 18:41:29 +00005811 bool isShift = getSubtarget()->hasSSE2() &&
Evan Chengc3630942009-12-09 21:00:30 +00005812 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00005813 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00005814 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00005815 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00005816 EVT EltVT = VT.getVectorElementType();
5817 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00005818 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005819 }
Eric Christopherfd179292009-08-27 18:07:15 +00005820
Nate Begeman9008ca62009-04-27 18:41:29 +00005821 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005822 if (V1IsUndef)
5823 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00005824 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00005825 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Dale Johannesen0488fb62010-09-30 23:57:10 +00005826 if (!X86::isMOVLPMask(SVOp)) {
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005827 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00005828 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
5829
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005830 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00005831 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
5832 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00005833 }
Eric Christopherfd179292009-08-27 18:07:15 +00005834
Nate Begeman9008ca62009-04-27 18:41:29 +00005835 // FIXME: fold these into legal mask.
Dale Johannesen0488fb62010-09-30 23:57:10 +00005836 if (X86::isMOVLHPSMask(SVOp) && !X86::isUNPCKLMask(SVOp))
5837 return getMOVLowToHigh(Op, dl, DAG, HasSSE2);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005838
Dale Johannesen0488fb62010-09-30 23:57:10 +00005839 if (X86::isMOVHLPSMask(SVOp))
5840 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00005841
Dale Johannesen0488fb62010-09-30 23:57:10 +00005842 if (X86::isMOVSHDUPMask(SVOp) && HasSSE3 && V2IsUndef && NumElems == 4)
5843 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00005844
Dale Johannesen0488fb62010-09-30 23:57:10 +00005845 if (X86::isMOVSLDUPMask(SVOp) && HasSSE3 && V2IsUndef && NumElems == 4)
5846 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00005847
Dale Johannesen0488fb62010-09-30 23:57:10 +00005848 if (X86::isMOVLPMask(SVOp))
5849 return getMOVLP(Op, dl, DAG, HasSSE2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005850
Nate Begeman9008ca62009-04-27 18:41:29 +00005851 if (ShouldXformToMOVHLPS(SVOp) ||
5852 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
5853 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005854
Evan Chengf26ffe92008-05-29 08:22:04 +00005855 if (isShift) {
5856 // No better options. Use a vshl / vsrl.
Dan Gohman8a55ce42009-09-23 21:02:20 +00005857 EVT EltVT = VT.getVectorElementType();
5858 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00005859 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005860 }
Eric Christopherfd179292009-08-27 18:07:15 +00005861
Evan Cheng9eca5e82006-10-25 21:49:50 +00005862 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00005863 // FIXME: This should also accept a bitcast of a splat? Be careful, not
5864 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00005865 V1IsSplat = isSplatVector(V1.getNode());
5866 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00005867
Chris Lattner8a594482007-11-25 00:24:49 +00005868 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00005869 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005870 Op = CommuteVectorShuffle(SVOp, DAG);
5871 SVOp = cast<ShuffleVectorSDNode>(Op);
5872 V1 = SVOp->getOperand(0);
5873 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00005874 std::swap(V1IsSplat, V2IsSplat);
5875 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00005876 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00005877 }
5878
Nate Begeman9008ca62009-04-27 18:41:29 +00005879 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
5880 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00005881 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00005882 return V1;
5883 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
5884 // the instruction selector will not match, so get a canonical MOVL with
5885 // swapped operands to undo the commute.
5886 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00005887 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005888
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005889 if (X86::isUNPCKLMask(SVOp))
David Greenec4db4e52011-02-28 19:06:56 +00005890 return getTargetShuffleNode(getUNPCKLOpcode(VT, getSubtarget()),
5891 dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005892
5893 if (X86::isUNPCKHMask(SVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005894 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00005895
Evan Cheng9bbbb982006-10-25 20:48:19 +00005896 if (V2IsSplat) {
5897 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005898 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00005899 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00005900 SDValue NewMask = NormalizeMask(SVOp, DAG);
5901 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
5902 if (NSVOp != SVOp) {
5903 if (X86::isUNPCKLMask(NSVOp, true)) {
5904 return NewMask;
5905 } else if (X86::isUNPCKHMask(NSVOp, true)) {
5906 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005907 }
5908 }
5909 }
5910
Evan Cheng9eca5e82006-10-25 21:49:50 +00005911 if (Commuted) {
5912 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00005913 // FIXME: this seems wrong.
5914 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
5915 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005916
5917 if (X86::isUNPCKLMask(NewSVOp))
David Greenec4db4e52011-02-28 19:06:56 +00005918 return getTargetShuffleNode(getUNPCKLOpcode(VT, getSubtarget()),
5919 dl, VT, V2, V1, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005920
5921 if (X86::isUNPCKHMask(NewSVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005922 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V2, V1, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00005923 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005924
Nate Begeman9008ca62009-04-27 18:41:29 +00005925 // Normalize the node to match x86 shuffle ops if needed
Dale Johannesen0488fb62010-09-30 23:57:10 +00005926 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
Nate Begeman9008ca62009-04-27 18:41:29 +00005927 return CommuteVectorShuffle(SVOp, DAG);
5928
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00005929 // The checks below are all present in isShuffleMaskLegal, but they are
5930 // inlined here right now to enable us to directly emit target specific
5931 // nodes, and remove one by one until they don't return Op anymore.
5932 SmallVector<int, 16> M;
5933 SVOp->getMask(M);
5934
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00005935 if (isPALIGNRMask(M, VT, HasSSSE3))
5936 return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
5937 X86::getShufflePALIGNRImmediate(SVOp),
5938 DAG);
5939
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00005940 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
5941 SVOp->getSplatIndex() == 0 && V2IsUndef) {
David Greenec4db4e52011-02-28 19:06:56 +00005942 if (VT == MVT::v2f64) {
5943 X86ISD::NodeType Opcode =
5944 getSubtarget()->hasAVX() ? X86ISD::VUNPCKLPD : X86ISD::UNPCKLPD;
5945 return getTargetShuffleNode(Opcode, dl, VT, V1, V1, DAG);
5946 }
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00005947 if (VT == MVT::v2i64)
5948 return getTargetShuffleNode(X86ISD::PUNPCKLQDQ, dl, VT, V1, V1, DAG);
5949 }
5950
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00005951 if (isPSHUFHWMask(M, VT))
5952 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
5953 X86::getShufflePSHUFHWImmediate(SVOp),
5954 DAG);
5955
5956 if (isPSHUFLWMask(M, VT))
5957 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
5958 X86::getShufflePSHUFLWImmediate(SVOp),
5959 DAG);
5960
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00005961 if (isSHUFPMask(M, VT)) {
5962 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
5963 if (VT == MVT::v4f32 || VT == MVT::v4i32)
5964 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V1, V2,
5965 TargetMask, DAG);
5966 if (VT == MVT::v2f64 || VT == MVT::v2i64)
5967 return getTargetShuffleNode(X86ISD::SHUFPD, dl, VT, V1, V2,
5968 TargetMask, DAG);
5969 }
5970
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005971 if (X86::isUNPCKL_v_undef_Mask(SVOp))
5972 if (VT != MVT::v2i64 && VT != MVT::v2f64)
David Greenec4db4e52011-02-28 19:06:56 +00005973 return getTargetShuffleNode(getUNPCKLOpcode(VT, getSubtarget()),
5974 dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005975 if (X86::isUNPCKH_v_undef_Mask(SVOp))
5976 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5977 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
5978
Evan Cheng14b32e12007-12-11 01:46:18 +00005979 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
Owen Anderson825b72b2009-08-11 20:47:22 +00005980 if (VT == MVT::v8i16) {
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005981 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00005982 if (NewOp.getNode())
Evan Cheng14b32e12007-12-11 01:46:18 +00005983 return NewOp;
5984 }
5985
Owen Anderson825b72b2009-08-11 20:47:22 +00005986 if (VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005987 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005988 if (NewOp.getNode())
5989 return NewOp;
5990 }
Eric Christopherfd179292009-08-27 18:07:15 +00005991
Dale Johannesen0488fb62010-09-30 23:57:10 +00005992 // Handle all 4 wide cases with a number of shuffles.
5993 if (NumElems == 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00005994 return LowerVECTOR_SHUFFLE_4wide(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005995
Dan Gohman475871a2008-07-27 21:46:04 +00005996 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005997}
5998
Dan Gohman475871a2008-07-27 21:46:04 +00005999SDValue
6000X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00006001 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006002 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006003 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006004 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006005 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006006 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006007 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006008 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006009 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006010 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00006011 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
6012 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
6013 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006014 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6015 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006016 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006017 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00006018 Op.getOperand(0)),
6019 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006020 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006021 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006022 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006023 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006024 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Anderson825b72b2009-08-11 20:47:22 +00006025 } else if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00006026 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
6027 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006028 // result has a single use which is a store or a bitcast to i32. And in
6029 // the case of a store, it's not worth it if the index is a constant 0,
6030 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00006031 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00006032 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00006033 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006034 if ((User->getOpcode() != ISD::STORE ||
6035 (isa<ConstantSDNode>(Op.getOperand(1)) &&
6036 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006037 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00006038 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00006039 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00006040 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006041 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00006042 Op.getOperand(0)),
6043 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006044 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Owen Anderson825b72b2009-08-11 20:47:22 +00006045 } else if (VT == MVT::i32) {
Mon P Wangf0fcdd82009-01-15 21:10:20 +00006046 // ExtractPS works with constant index.
6047 if (isa<ConstantSDNode>(Op.getOperand(1)))
6048 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00006049 }
Dan Gohman475871a2008-07-27 21:46:04 +00006050 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006051}
6052
6053
Dan Gohman475871a2008-07-27 21:46:04 +00006054SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006055X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
6056 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006057 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00006058 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006059
David Greene74a579d2011-02-10 16:57:36 +00006060 SDValue Vec = Op.getOperand(0);
6061 EVT VecVT = Vec.getValueType();
6062
6063 // If this is a 256-bit vector result, first extract the 128-bit
6064 // vector and then extract from the 128-bit vector.
6065 if (VecVT.getSizeInBits() > 128) {
6066 DebugLoc dl = Op.getNode()->getDebugLoc();
6067 unsigned NumElems = VecVT.getVectorNumElements();
6068 SDValue Idx = Op.getOperand(1);
6069
6070 if (!isa<ConstantSDNode>(Idx))
6071 return SDValue();
6072
6073 unsigned ExtractNumElems = NumElems / (VecVT.getSizeInBits() / 128);
6074 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
6075
6076 // Get the 128-bit vector.
6077 bool Upper = IdxVal >= ExtractNumElems;
6078 Vec = Extract128BitVector(Vec, Idx, DAG, dl);
6079
6080 // Extract from it.
6081 SDValue ScaledIdx = Idx;
6082 if (Upper)
6083 ScaledIdx = DAG.getNode(ISD::SUB, dl, Idx.getValueType(), Idx,
6084 DAG.getConstant(ExtractNumElems,
6085 Idx.getValueType()));
6086 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, Op.getValueType(), Vec,
6087 ScaledIdx);
6088 }
6089
6090 assert(Vec.getValueSizeInBits() <= 128 && "Unexpected vector length");
6091
Evan Cheng62a3f152008-03-24 21:52:23 +00006092 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006093 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006094 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00006095 return Res;
6096 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00006097
Owen Andersone50ed302009-08-10 22:56:29 +00006098 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006099 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006100 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006101 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00006102 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006103 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00006104 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006105 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6106 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006107 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006108 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00006109 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006110 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00006111 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00006112 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00006113 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00006114 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00006115 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006116 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006117 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006118 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006119 if (Idx == 0)
6120 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00006121
Evan Cheng0db9fe62006-04-25 20:13:52 +00006122 // SHUFPS the element to the lowest double word, then movss.
Nate Begeman9008ca62009-04-27 18:41:29 +00006123 int Mask[4] = { Idx, -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006124 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006125 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00006126 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00006127 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00006128 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00006129 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00006130 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
6131 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
6132 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006133 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006134 if (Idx == 0)
6135 return Op;
6136
6137 // UNPCKHPD the element to the lowest double word, then movsd.
6138 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
6139 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00006140 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006141 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006142 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00006143 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00006144 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00006145 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006146 }
6147
Dan Gohman475871a2008-07-27 21:46:04 +00006148 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006149}
6150
Dan Gohman475871a2008-07-27 21:46:04 +00006151SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006152X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
6153 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006154 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006155 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006156 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006157
Dan Gohman475871a2008-07-27 21:46:04 +00006158 SDValue N0 = Op.getOperand(0);
6159 SDValue N1 = Op.getOperand(1);
6160 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00006161
Dan Gohman8a55ce42009-09-23 21:02:20 +00006162 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00006163 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00006164 unsigned Opc;
6165 if (VT == MVT::v8i16)
6166 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00006167 else if (VT == MVT::v16i8)
6168 Opc = X86ISD::PINSRB;
6169 else
6170 Opc = X86ISD::PINSRB;
6171
Nate Begeman14d12ca2008-02-11 04:19:36 +00006172 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
6173 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00006174 if (N1.getValueType() != MVT::i32)
6175 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
6176 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006177 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00006178 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00006179 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00006180 // Bits [7:6] of the constant are the source select. This will always be
6181 // zero here. The DAG Combiner may combine an extract_elt index into these
6182 // bits. For example (insert (extract, 3), 2) could be matched by putting
6183 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00006184 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00006185 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00006186 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00006187 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006188 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00006189 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00006190 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00006191 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00006192 } else if (EltVT == MVT::i32 && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00006193 // PINSR* works with constant index.
6194 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00006195 }
Dan Gohman475871a2008-07-27 21:46:04 +00006196 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006197}
6198
Dan Gohman475871a2008-07-27 21:46:04 +00006199SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006200X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006201 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006202 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006203
David Greene6b381262011-02-09 15:32:06 +00006204 DebugLoc dl = Op.getDebugLoc();
6205 SDValue N0 = Op.getOperand(0);
6206 SDValue N1 = Op.getOperand(1);
6207 SDValue N2 = Op.getOperand(2);
6208
6209 // If this is a 256-bit vector result, first insert into a 128-bit
6210 // vector and then insert into the 256-bit vector.
6211 if (VT.getSizeInBits() > 128) {
6212 if (!isa<ConstantSDNode>(N2))
6213 return SDValue();
6214
6215 // Get the 128-bit vector.
6216 unsigned NumElems = VT.getVectorNumElements();
6217 unsigned IdxVal = cast<ConstantSDNode>(N2)->getZExtValue();
6218 bool Upper = IdxVal >= NumElems / 2;
6219
6220 SDValue SubN0 = Extract128BitVector(N0, N2, DAG, dl);
6221
6222 // Insert into it.
6223 SDValue ScaledN2 = N2;
6224 if (Upper)
6225 ScaledN2 = DAG.getNode(ISD::SUB, dl, N2.getValueType(), N2,
Owen Anderson95771af2011-02-25 21:41:48 +00006226 DAG.getConstant(NumElems /
David Greene6b381262011-02-09 15:32:06 +00006227 (VT.getSizeInBits() / 128),
6228 N2.getValueType()));
6229 Op = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, SubN0.getValueType(), SubN0,
6230 N1, ScaledN2);
6231
6232 // Insert the 128-bit vector
6233 // FIXME: Why UNDEF?
6234 return Insert128BitVector(N0, Op, N2, DAG, dl);
6235 }
6236
Nate Begeman14d12ca2008-02-11 04:19:36 +00006237 if (Subtarget->hasSSE41())
6238 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
6239
Dan Gohman8a55ce42009-09-23 21:02:20 +00006240 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00006241 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00006242
Dan Gohman8a55ce42009-09-23 21:02:20 +00006243 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00006244 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
6245 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00006246 if (N1.getValueType() != MVT::i32)
6247 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
6248 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006249 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00006250 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006251 }
Dan Gohman475871a2008-07-27 21:46:04 +00006252 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006253}
6254
Dan Gohman475871a2008-07-27 21:46:04 +00006255SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006256X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) const {
David Greene2fcdfb42011-02-10 23:11:29 +00006257 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006258 DebugLoc dl = Op.getDebugLoc();
David Greene2fcdfb42011-02-10 23:11:29 +00006259 EVT OpVT = Op.getValueType();
6260
6261 // If this is a 256-bit vector result, first insert into a 128-bit
6262 // vector and then insert into the 256-bit vector.
6263 if (OpVT.getSizeInBits() > 128) {
6264 // Insert into a 128-bit vector.
6265 EVT VT128 = EVT::getVectorVT(*Context,
6266 OpVT.getVectorElementType(),
6267 OpVT.getVectorNumElements() / 2);
6268
6269 Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT128, Op.getOperand(0));
6270
6271 // Insert the 128-bit vector.
6272 return Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, OpVT), Op,
6273 DAG.getConstant(0, MVT::i32),
6274 DAG, dl);
6275 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00006276
Chris Lattnerf172ecd2010-07-04 23:07:25 +00006277 if (Op.getValueType() == MVT::v1i64 &&
6278 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00006279 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00006280
Owen Anderson825b72b2009-08-11 20:47:22 +00006281 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Dale Johannesen0488fb62010-09-30 23:57:10 +00006282 assert(Op.getValueType().getSimpleVT().getSizeInBits() == 128 &&
6283 "Expected an SSE type!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006284 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(),
Dale Johannesen0488fb62010-09-30 23:57:10 +00006285 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006286}
6287
David Greene91585092011-01-26 15:38:49 +00006288// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
6289// a simple subregister reference or explicit instructions to grab
6290// upper bits of a vector.
6291SDValue
6292X86TargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
6293 if (Subtarget->hasAVX()) {
David Greenea5f26012011-02-07 19:36:54 +00006294 DebugLoc dl = Op.getNode()->getDebugLoc();
6295 SDValue Vec = Op.getNode()->getOperand(0);
6296 SDValue Idx = Op.getNode()->getOperand(1);
6297
6298 if (Op.getNode()->getValueType(0).getSizeInBits() == 128
6299 && Vec.getNode()->getValueType(0).getSizeInBits() == 256) {
6300 return Extract128BitVector(Vec, Idx, DAG, dl);
6301 }
David Greene91585092011-01-26 15:38:49 +00006302 }
6303 return SDValue();
6304}
6305
David Greenecfe33c42011-01-26 19:13:22 +00006306// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
6307// simple superregister reference or explicit instructions to insert
6308// the upper bits of a vector.
6309SDValue
6310X86TargetLowering::LowerINSERT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
6311 if (Subtarget->hasAVX()) {
6312 DebugLoc dl = Op.getNode()->getDebugLoc();
6313 SDValue Vec = Op.getNode()->getOperand(0);
6314 SDValue SubVec = Op.getNode()->getOperand(1);
6315 SDValue Idx = Op.getNode()->getOperand(2);
6316
6317 if (Op.getNode()->getValueType(0).getSizeInBits() == 256
6318 && SubVec.getNode()->getValueType(0).getSizeInBits() == 128) {
David Greenea5f26012011-02-07 19:36:54 +00006319 return Insert128BitVector(Vec, SubVec, Idx, DAG, dl);
David Greenecfe33c42011-01-26 19:13:22 +00006320 }
6321 }
6322 return SDValue();
6323}
6324
Bill Wendling056292f2008-09-16 21:48:12 +00006325// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
6326// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
6327// one of the above mentioned nodes. It has to be wrapped because otherwise
6328// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
6329// be used to form addressing mode. These wrapped nodes will be selected
6330// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00006331SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006332X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006333 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00006334
Chris Lattner41621a22009-06-26 19:22:52 +00006335 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6336 // global base reg.
6337 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006338 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006339 CodeModel::Model M = getTargetMachine().getCodeModel();
6340
Chris Lattner4f066492009-07-11 20:29:19 +00006341 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006342 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00006343 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00006344 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006345 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00006346 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006347 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00006348
Evan Cheng1606e8e2009-03-13 07:51:59 +00006349 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00006350 CP->getAlignment(),
6351 CP->getOffset(), OpFlag);
6352 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00006353 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006354 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00006355 if (OpFlag) {
6356 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00006357 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006358 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006359 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006360 }
6361
6362 return Result;
6363}
6364
Dan Gohmand858e902010-04-17 15:26:15 +00006365SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00006366 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00006367
Chris Lattner18c59872009-06-27 04:16:01 +00006368 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6369 // global base reg.
6370 unsigned char OpFlag = 0;
6371 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006372 CodeModel::Model M = getTargetMachine().getCodeModel();
6373
Chris Lattner4f066492009-07-11 20:29:19 +00006374 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006375 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00006376 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00006377 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006378 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00006379 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006380 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00006381
Chris Lattner18c59872009-06-27 04:16:01 +00006382 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
6383 OpFlag);
6384 DebugLoc DL = JT->getDebugLoc();
6385 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00006386
Chris Lattner18c59872009-06-27 04:16:01 +00006387 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00006388 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00006389 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6390 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006391 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00006392 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00006393
Chris Lattner18c59872009-06-27 04:16:01 +00006394 return Result;
6395}
6396
6397SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006398X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00006399 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00006400
Chris Lattner18c59872009-06-27 04:16:01 +00006401 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6402 // global base reg.
6403 unsigned char OpFlag = 0;
6404 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006405 CodeModel::Model M = getTargetMachine().getCodeModel();
6406
Chris Lattner4f066492009-07-11 20:29:19 +00006407 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006408 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00006409 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00006410 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006411 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00006412 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006413 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00006414
Chris Lattner18c59872009-06-27 04:16:01 +00006415 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00006416
Chris Lattner18c59872009-06-27 04:16:01 +00006417 DebugLoc DL = Op.getDebugLoc();
6418 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00006419
6420
Chris Lattner18c59872009-06-27 04:16:01 +00006421 // With PIC, the address is actually $g + Offset.
6422 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00006423 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00006424 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6425 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006426 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00006427 Result);
6428 }
Eric Christopherfd179292009-08-27 18:07:15 +00006429
Chris Lattner18c59872009-06-27 04:16:01 +00006430 return Result;
6431}
6432
Dan Gohman475871a2008-07-27 21:46:04 +00006433SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006434X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00006435 // Create the TargetBlockAddressAddress node.
6436 unsigned char OpFlags =
6437 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00006438 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00006439 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +00006440 DebugLoc dl = Op.getDebugLoc();
6441 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
6442 /*isTarget=*/true, OpFlags);
6443
Dan Gohmanf705adb2009-10-30 01:28:02 +00006444 if (Subtarget->isPICStyleRIPRel() &&
6445 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00006446 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
6447 else
6448 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00006449
Dan Gohman29cbade2009-11-20 23:18:13 +00006450 // With PIC, the address is actually $g + Offset.
6451 if (isGlobalRelativeToPICBase(OpFlags)) {
6452 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
6453 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
6454 Result);
6455 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00006456
6457 return Result;
6458}
6459
6460SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00006461X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00006462 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00006463 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00006464 // Create the TargetGlobalAddress node, folding in the constant
6465 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00006466 unsigned char OpFlags =
6467 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006468 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00006469 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006470 if (OpFlags == X86II::MO_NO_FLAG &&
6471 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00006472 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00006473 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00006474 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006475 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00006476 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00006477 }
Eric Christopherfd179292009-08-27 18:07:15 +00006478
Chris Lattner4f066492009-07-11 20:29:19 +00006479 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006480 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00006481 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
6482 else
6483 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00006484
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006485 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00006486 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00006487 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
6488 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006489 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006490 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006491
Chris Lattner36c25012009-07-10 07:34:39 +00006492 // For globals that require a load from a stub to get the address, emit the
6493 // load.
6494 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00006495 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00006496 MachinePointerInfo::getGOT(), false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006497
Dan Gohman6520e202008-10-18 02:06:02 +00006498 // If there was a non-zero offset that we didn't fold, create an explicit
6499 // addition for it.
6500 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00006501 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00006502 DAG.getConstant(Offset, getPointerTy()));
6503
Evan Cheng0db9fe62006-04-25 20:13:52 +00006504 return Result;
6505}
6506
Evan Chengda43bcf2008-09-24 00:05:32 +00006507SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006508X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00006509 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00006510 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006511 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00006512}
6513
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006514static SDValue
6515GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00006516 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerb903bed2009-06-26 21:20:29 +00006517 unsigned char OperandFlags) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006518 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006519 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006520 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00006521 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006522 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00006523 GA->getOffset(),
6524 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006525 if (InFlag) {
6526 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00006527 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006528 } else {
6529 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00006530 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006531 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006532
6533 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00006534 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006535
Rafael Espindola15f1b662009-04-24 12:59:40 +00006536 SDValue Flag = Chain.getValue(1);
6537 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006538}
6539
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006540// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00006541static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006542LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006543 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00006544 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00006545 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
6546 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006547 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006548 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006549 InFlag = Chain.getValue(1);
6550
Chris Lattnerb903bed2009-06-26 21:20:29 +00006551 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006552}
6553
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006554// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00006555static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006556LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006557 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00006558 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
6559 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006560}
6561
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006562// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
6563// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00006564static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006565 const EVT PtrVT, TLSModel::Model model,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00006566 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00006567 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00006568
Chris Lattnerf93b90c2010-09-22 04:39:11 +00006569 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
6570 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
6571 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00006572
Michael J. Spencerec38de22010-10-10 22:04:20 +00006573 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00006574 DAG.getIntPtrConstant(0),
6575 MachinePointerInfo(Ptr), false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00006576
Chris Lattnerb903bed2009-06-26 21:20:29 +00006577 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006578 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
6579 // initialexec.
6580 unsigned WrapperKind = X86ISD::Wrapper;
6581 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00006582 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00006583 } else if (is64Bit) {
6584 assert(model == TLSModel::InitialExec);
6585 OperandFlags = X86II::MO_GOTTPOFF;
6586 WrapperKind = X86ISD::WrapperRIP;
6587 } else {
6588 assert(model == TLSModel::InitialExec);
6589 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00006590 }
Eric Christopherfd179292009-08-27 18:07:15 +00006591
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006592 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
6593 // exec)
Michael J. Spencerec38de22010-10-10 22:04:20 +00006594 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00006595 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00006596 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00006597 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00006598
Rafael Espindola9a580232009-02-27 13:37:18 +00006599 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00006600 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00006601 MachinePointerInfo::getGOT(), false, false, 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00006602
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006603 // The address of the thread local variable is the add of the thread
6604 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00006605 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006606}
6607
Dan Gohman475871a2008-07-27 21:46:04 +00006608SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006609X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00006610
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006611 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00006612 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00006613
Eric Christopher30ef0e52010-06-03 04:07:48 +00006614 if (Subtarget->isTargetELF()) {
6615 // TODO: implement the "local dynamic" model
6616 // TODO: implement the "initial exec"model for pic executables
Michael J. Spencerec38de22010-10-10 22:04:20 +00006617
Eric Christopher30ef0e52010-06-03 04:07:48 +00006618 // If GV is an alias then use the aliasee for determining
6619 // thread-localness.
6620 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
6621 GV = GA->resolveAliasedGlobal(false);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006622
6623 TLSModel::Model model
Eric Christopher30ef0e52010-06-03 04:07:48 +00006624 = getTLSModel(GV, getTargetMachine().getRelocationModel());
Michael J. Spencerec38de22010-10-10 22:04:20 +00006625
Eric Christopher30ef0e52010-06-03 04:07:48 +00006626 switch (model) {
6627 case TLSModel::GeneralDynamic:
6628 case TLSModel::LocalDynamic: // not implemented
6629 if (Subtarget->is64Bit())
6630 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
6631 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Michael J. Spencerec38de22010-10-10 22:04:20 +00006632
Eric Christopher30ef0e52010-06-03 04:07:48 +00006633 case TLSModel::InitialExec:
6634 case TLSModel::LocalExec:
6635 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
6636 Subtarget->is64Bit());
6637 }
6638 } else if (Subtarget->isTargetDarwin()) {
6639 // Darwin only has one model of TLS. Lower to that.
6640 unsigned char OpFlag = 0;
6641 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
6642 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006643
Eric Christopher30ef0e52010-06-03 04:07:48 +00006644 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6645 // global base reg.
6646 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
6647 !Subtarget->is64Bit();
6648 if (PIC32)
6649 OpFlag = X86II::MO_TLVP_PIC_BASE;
6650 else
6651 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006652 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00006653 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00006654 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00006655 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00006656 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006657
Eric Christopher30ef0e52010-06-03 04:07:48 +00006658 // With PIC32, the address is actually $g + Offset.
6659 if (PIC32)
6660 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6661 DAG.getNode(X86ISD::GlobalBaseReg,
6662 DebugLoc(), getPointerTy()),
6663 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006664
Eric Christopher30ef0e52010-06-03 04:07:48 +00006665 // Lowering the machine isd will make sure everything is in the right
6666 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00006667 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006668 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00006669 SDValue Args[] = { Chain, Offset };
6670 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006671
Eric Christopher30ef0e52010-06-03 04:07:48 +00006672 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
6673 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
6674 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00006675
Eric Christopher30ef0e52010-06-03 04:07:48 +00006676 // And our return value (tls address) is in the standard call return value
6677 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00006678 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
6679 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy());
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006680 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00006681
Eric Christopher30ef0e52010-06-03 04:07:48 +00006682 assert(false &&
6683 "TLS not implemented for this target.");
Eric Christopherfd179292009-08-27 18:07:15 +00006684
Torok Edwinc23197a2009-07-14 16:55:14 +00006685 llvm_unreachable("Unreachable");
Chris Lattner5867de12009-04-01 22:14:45 +00006686 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006687}
6688
Evan Cheng0db9fe62006-04-25 20:13:52 +00006689
Nadav Rotem43012222011-05-11 08:12:09 +00006690/// LowerShiftParts - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00006691/// take a 2 x i32 value to shift plus a shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +00006692SDValue X86TargetLowering::LowerShiftParts(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman4c1fa612008-03-03 22:22:09 +00006693 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00006694 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006695 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006696 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006697 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00006698 SDValue ShOpLo = Op.getOperand(0);
6699 SDValue ShOpHi = Op.getOperand(1);
6700 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00006701 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00006702 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00006703 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00006704
Dan Gohman475871a2008-07-27 21:46:04 +00006705 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006706 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00006707 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
6708 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006709 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006710 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
6711 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006712 }
Evan Chenge3413162006-01-09 18:33:28 +00006713
Owen Anderson825b72b2009-08-11 20:47:22 +00006714 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
6715 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00006716 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00006717 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00006718
Dan Gohman475871a2008-07-27 21:46:04 +00006719 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00006720 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00006721 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
6722 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00006723
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006724 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00006725 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
6726 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006727 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006728 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
6729 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006730 }
6731
Dan Gohman475871a2008-07-27 21:46:04 +00006732 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00006733 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006734}
Evan Chenga3195e82006-01-12 22:54:21 +00006735
Dan Gohmand858e902010-04-17 15:26:15 +00006736SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
6737 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006738 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00006739
Dale Johannesen0488fb62010-09-30 23:57:10 +00006740 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00006741 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00006742
Owen Anderson825b72b2009-08-11 20:47:22 +00006743 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00006744 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00006745
Eli Friedman36df4992009-05-27 00:47:34 +00006746 // These are really Legal; return the operand so the caller accepts it as
6747 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00006748 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00006749 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00006750 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00006751 Subtarget->is64Bit()) {
6752 return Op;
6753 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006754
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006755 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006756 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006757 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00006758 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006759 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00006760 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00006761 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006762 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006763 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00006764 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
6765}
Evan Cheng0db9fe62006-04-25 20:13:52 +00006766
Owen Andersone50ed302009-08-10 22:56:29 +00006767SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00006768 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00006769 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006770 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00006771 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00006772 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00006773 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006774 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006775 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00006776 else
Owen Anderson825b72b2009-08-11 20:47:22 +00006777 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006778
Chris Lattner492a43e2010-09-22 01:28:21 +00006779 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006780
Stuart Hastings84be9582011-06-02 15:57:11 +00006781 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(StackSlot);
6782 MachineMemOperand *MMO;
6783 if (FI) {
6784 int SSFI = FI->getIndex();
6785 MMO =
6786 DAG.getMachineFunction()
6787 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6788 MachineMemOperand::MOLoad, ByteSize, ByteSize);
6789 } else {
6790 MMO = cast<LoadSDNode>(StackSlot)->getMemOperand();
6791 StackSlot = StackSlot.getOperand(1);
6792 }
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006793 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00006794 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
6795 X86ISD::FILD, DL,
6796 Tys, Ops, array_lengthof(Ops),
6797 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006798
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006799 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006800 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00006801 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006802
6803 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
6804 // shouldn't be necessary except that RFP cannot be live across
6805 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006806 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00006807 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
6808 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006809 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00006810 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006811 SDValue Ops[] = {
6812 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
6813 };
Chris Lattner492a43e2010-09-22 01:28:21 +00006814 MachineMemOperand *MMO =
6815 DAG.getMachineFunction()
6816 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00006817 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006818
Chris Lattner492a43e2010-09-22 01:28:21 +00006819 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
6820 Ops, array_lengthof(Ops),
6821 Op.getValueType(), MMO);
6822 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006823 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006824 false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006825 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006826
Evan Cheng0db9fe62006-04-25 20:13:52 +00006827 return Result;
6828}
6829
Bill Wendling8b8a6362009-01-17 03:56:04 +00006830// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00006831SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
6832 SelectionDAG &DAG) const {
Bill Wendling8b8a6362009-01-17 03:56:04 +00006833 // This algorithm is not obvious. Here it is in C code, more or less:
6834 /*
6835 double uint64_to_double( uint32_t hi, uint32_t lo ) {
6836 static const __m128i exp = { 0x4330000045300000ULL, 0 };
6837 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00006838
Bill Wendling8b8a6362009-01-17 03:56:04 +00006839 // Copy ints to xmm registers.
6840 __m128i xh = _mm_cvtsi32_si128( hi );
6841 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00006842
Bill Wendling8b8a6362009-01-17 03:56:04 +00006843 // Combine into low half of a single xmm register.
6844 __m128i x = _mm_unpacklo_epi32( xh, xl );
6845 __m128d d;
6846 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00006847
Bill Wendling8b8a6362009-01-17 03:56:04 +00006848 // Merge in appropriate exponents to give the integer bits the right
6849 // magnitude.
6850 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00006851
Bill Wendling8b8a6362009-01-17 03:56:04 +00006852 // Subtract away the biases to deal with the IEEE-754 double precision
6853 // implicit 1.
6854 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00006855
Bill Wendling8b8a6362009-01-17 03:56:04 +00006856 // All conversions up to here are exact. The correctly rounded result is
6857 // calculated using the current rounding mode using the following
6858 // horizontal add.
6859 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
6860 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
6861 // store doesn't really need to be here (except
6862 // maybe to zero the other double)
6863 return sd;
6864 }
6865 */
Dale Johannesen040225f2008-10-21 23:07:49 +00006866
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006867 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00006868 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00006869
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006870 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00006871 std::vector<Constant*> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00006872 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
6873 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
6874 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
6875 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00006876 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006877 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006878
Bill Wendling8b8a6362009-01-17 03:56:04 +00006879 std::vector<Constant*> CV1;
Owen Andersona90b3dc2009-07-15 21:51:10 +00006880 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006881 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
Owen Andersona90b3dc2009-07-15 21:51:10 +00006882 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006883 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00006884 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006885 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006886
Owen Anderson825b72b2009-08-11 20:47:22 +00006887 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6888 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00006889 Op.getOperand(0),
6890 DAG.getIntPtrConstant(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006891 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6892 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00006893 Op.getOperand(0),
6894 DAG.getIntPtrConstant(0)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006895 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
6896 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00006897 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006898 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00006899 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006900 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck2);
Owen Anderson825b72b2009-08-11 20:47:22 +00006901 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00006902 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006903 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00006904 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006905
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006906 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00006907 int ShufMask[2] = { 1, -1 };
Owen Anderson825b72b2009-08-11 20:47:22 +00006908 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
6909 DAG.getUNDEF(MVT::v2f64), ShufMask);
6910 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
6911 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006912 DAG.getIntPtrConstant(0));
6913}
6914
Bill Wendling8b8a6362009-01-17 03:56:04 +00006915// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00006916SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
6917 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006918 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00006919 // FP constant to bias correct the final result.
6920 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00006921 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006922
6923 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00006924 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6925 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Bill Wendling8b8a6362009-01-17 03:56:04 +00006926 Op.getOperand(0),
6927 DAG.getIntPtrConstant(0)));
6928
Owen Anderson825b72b2009-08-11 20:47:22 +00006929 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006930 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00006931 DAG.getIntPtrConstant(0));
6932
6933 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00006934 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006935 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00006936 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006937 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006938 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00006939 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006940 MVT::v2f64, Bias)));
6941 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006942 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00006943 DAG.getIntPtrConstant(0));
6944
6945 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00006946 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006947
6948 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00006949 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00006950
Owen Anderson825b72b2009-08-11 20:47:22 +00006951 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006952 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00006953 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00006954 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006955 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00006956 }
6957
6958 // Handle final rounding.
6959 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00006960}
6961
Dan Gohmand858e902010-04-17 15:26:15 +00006962SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
6963 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00006964 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006965 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00006966
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006967 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00006968 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
6969 // the optimization here.
6970 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00006971 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00006972
Owen Andersone50ed302009-08-10 22:56:29 +00006973 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006974 EVT DstVT = Op.getValueType();
6975 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00006976 return LowerUINT_TO_FP_i64(Op, DAG);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006977 else if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00006978 return LowerUINT_TO_FP_i32(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00006979
6980 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00006981 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006982 if (SrcVT == MVT::i32) {
6983 SDValue WordOff = DAG.getConstant(4, getPointerTy());
6984 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
6985 getPointerTy(), StackSlot, WordOff);
6986 SDValue Store1 = 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 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006990 OffsetSlot, MachinePointerInfo(),
6991 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006992 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
6993 return Fild;
6994 }
6995
6996 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
6997 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006998 StackSlot, MachinePointerInfo(),
6999 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007000 // For i64 source, we need to add the appropriate power of 2 if the input
7001 // was negative. This is the same as the optimization in
7002 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
7003 // we must be careful to do the computation in x87 extended precision, not
7004 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00007005 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
7006 MachineMemOperand *MMO =
7007 DAG.getMachineFunction()
7008 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7009 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007010
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007011 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
7012 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007013 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
7014 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007015
7016 APInt FF(32, 0x5F800000ULL);
7017
7018 // Check whether the sign bit is set.
7019 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
7020 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
7021 ISD::SETLT);
7022
7023 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
7024 SDValue FudgePtr = DAG.getConstantPool(
7025 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
7026 getPointerTy());
7027
7028 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
7029 SDValue Zero = DAG.getIntPtrConstant(0);
7030 SDValue Four = DAG.getIntPtrConstant(4);
7031 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
7032 Zero, Four);
7033 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
7034
7035 // Load the value out, extending it from f32 to f80.
7036 // FIXME: Avoid the extend by constructing the right constant pool?
Stuart Hastingsa9011292011-02-16 16:23:55 +00007037 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00007038 FudgePtr, MachinePointerInfo::getConstantPool(),
7039 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007040 // Extend everything to 80 bits to force it to be done on x87.
7041 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
7042 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00007043}
7044
Dan Gohman475871a2008-07-27 21:46:04 +00007045std::pair<SDValue,SDValue> X86TargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00007046FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) const {
Chris Lattner07290932010-09-22 01:05:16 +00007047 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00007048
Owen Andersone50ed302009-08-10 22:56:29 +00007049 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00007050
7051 if (!IsSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007052 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
7053 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00007054 }
7055
Owen Anderson825b72b2009-08-11 20:47:22 +00007056 assert(DstTy.getSimpleVT() <= MVT::i64 &&
7057 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00007058 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00007059
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007060 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007061 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00007062 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00007063 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00007064 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00007065 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00007066 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00007067 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007068
Evan Cheng87c89352007-10-15 20:11:21 +00007069 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
7070 // stack slot.
7071 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00007072 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00007073 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007074 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00007075
Michael J. Spencerec38de22010-10-10 22:04:20 +00007076
7077
Evan Cheng0db9fe62006-04-25 20:13:52 +00007078 unsigned Opc;
Owen Anderson825b72b2009-08-11 20:47:22 +00007079 switch (DstTy.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007080 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson825b72b2009-08-11 20:47:22 +00007081 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
7082 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
7083 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007084 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007085
Dan Gohman475871a2008-07-27 21:46:04 +00007086 SDValue Chain = DAG.getEntryNode();
7087 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00007088 EVT TheVT = Op.getOperand(0).getValueType();
7089 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007090 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00007091 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007092 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007093 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007094 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00007095 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00007096 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00007097 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00007098
Chris Lattner492a43e2010-09-22 01:28:21 +00007099 MachineMemOperand *MMO =
7100 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7101 MachineMemOperand::MOLoad, MemSize, MemSize);
7102 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
7103 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007104 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00007105 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007106 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
7107 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007108
Chris Lattner07290932010-09-22 01:05:16 +00007109 MachineMemOperand *MMO =
7110 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7111 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007112
Evan Cheng0db9fe62006-04-25 20:13:52 +00007113 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00007114 SDValue Ops[] = { Chain, Value, StackSlot };
Chris Lattner07290932010-09-22 01:05:16 +00007115 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
7116 Ops, 3, DstTy, MMO);
Evan Chengd9558e02006-01-06 00:43:03 +00007117
Chris Lattner27a6c732007-11-24 07:07:01 +00007118 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007119}
7120
Dan Gohmand858e902010-04-17 15:26:15 +00007121SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
7122 SelectionDAG &DAG) const {
Dale Johannesen0488fb62010-09-30 23:57:10 +00007123 if (Op.getValueType().isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007124 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007125
Eli Friedman948e95a2009-05-23 09:59:16 +00007126 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00007127 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00007128 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
7129 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00007130
Chris Lattner27a6c732007-11-24 07:07:01 +00007131 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007132 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00007133 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00007134}
7135
Dan Gohmand858e902010-04-17 15:26:15 +00007136SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
7137 SelectionDAG &DAG) const {
Eli Friedman948e95a2009-05-23 09:59:16 +00007138 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
7139 SDValue FIST = Vals.first, StackSlot = Vals.second;
7140 assert(FIST.getNode() && "Unexpected failure");
7141
7142 // Load the result.
7143 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00007144 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007145}
7146
Dan Gohmand858e902010-04-17 15:26:15 +00007147SDValue X86TargetLowering::LowerFABS(SDValue Op,
7148 SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007149 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007150 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007151 EVT VT = Op.getValueType();
7152 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007153 if (VT.isVector())
7154 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007155 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007156 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007157 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00007158 CV.push_back(C);
7159 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007160 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007161 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00007162 CV.push_back(C);
7163 CV.push_back(C);
7164 CV.push_back(C);
7165 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007166 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007167 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007168 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007169 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007170 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007171 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007172 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007173}
7174
Dan Gohmand858e902010-04-17 15:26:15 +00007175SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007176 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007177 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007178 EVT VT = Op.getValueType();
7179 EVT EltVT = VT;
Duncan Sandsda9ad382009-09-06 19:29:07 +00007180 if (VT.isVector())
Duncan Sands83ec4b62008-06-06 12:08:01 +00007181 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007182 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007183 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007184 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00007185 CV.push_back(C);
7186 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007187 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007188 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00007189 CV.push_back(C);
7190 CV.push_back(C);
7191 CV.push_back(C);
7192 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007193 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007194 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007195 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007196 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007197 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007198 false, false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00007199 if (VT.isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007200 return DAG.getNode(ISD::BITCAST, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007201 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007202 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007203 Op.getOperand(0)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007204 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00007205 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007206 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00007207 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00007208}
7209
Dan Gohmand858e902010-04-17 15:26:15 +00007210SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007211 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00007212 SDValue Op0 = Op.getOperand(0);
7213 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007214 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007215 EVT VT = Op.getValueType();
7216 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00007217
7218 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007219 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007220 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00007221 SrcVT = VT;
7222 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007223 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007224 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007225 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007226 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007227 }
7228
7229 // At this point the operands and the result should have the same
7230 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00007231
Evan Cheng68c47cb2007-01-05 07:55:56 +00007232 // First get the sign bit of second operand.
7233 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007234 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007235 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
7236 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007237 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007238 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
7239 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7240 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7241 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007242 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007243 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007244 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007245 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007246 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007247 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007248 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00007249
7250 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007251 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007252 // Op0 is MVT::f32, Op1 is MVT::f64.
7253 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
7254 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
7255 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007256 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00007257 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00007258 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007259 }
7260
Evan Cheng73d6cf12007-01-05 21:37:56 +00007261 // Clear first operand sign bit.
7262 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00007263 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007264 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
7265 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00007266 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007267 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
7268 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7269 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7270 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00007271 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007272 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007273 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007274 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007275 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007276 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007277 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00007278
7279 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00007280 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00007281}
7282
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00007283SDValue X86TargetLowering::LowerFGETSIGN(SDValue Op, SelectionDAG &DAG) const {
7284 SDValue N0 = Op.getOperand(0);
7285 DebugLoc dl = Op.getDebugLoc();
7286 EVT VT = Op.getValueType();
7287
7288 // Lower ISD::FGETSIGN to (AND (X86ISD::FGETSIGNx86 ...) 1).
7289 SDValue xFGETSIGN = DAG.getNode(X86ISD::FGETSIGNx86, dl, VT, N0,
7290 DAG.getConstant(1, VT));
7291 return DAG.getNode(ISD::AND, dl, VT, xFGETSIGN, DAG.getConstant(1, VT));
7292}
7293
Dan Gohman076aee32009-03-04 19:44:21 +00007294/// Emit nodes that will be selected as "test Op0,Op0", or something
7295/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00007296SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00007297 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00007298 DebugLoc dl = Op.getDebugLoc();
7299
Dan Gohman31125812009-03-07 01:58:32 +00007300 // CF and OF aren't always set the way we want. Determine which
7301 // of these we need.
7302 bool NeedCF = false;
7303 bool NeedOF = false;
7304 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007305 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00007306 case X86::COND_A: case X86::COND_AE:
7307 case X86::COND_B: case X86::COND_BE:
7308 NeedCF = true;
7309 break;
7310 case X86::COND_G: case X86::COND_GE:
7311 case X86::COND_L: case X86::COND_LE:
7312 case X86::COND_O: case X86::COND_NO:
7313 NeedOF = true;
7314 break;
Dan Gohman31125812009-03-07 01:58:32 +00007315 }
7316
Dan Gohman076aee32009-03-04 19:44:21 +00007317 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00007318 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
7319 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007320 if (Op.getResNo() != 0 || NeedOF || NeedCF)
7321 // Emit a CMP with 0, which is the TEST pattern.
7322 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
7323 DAG.getConstant(0, Op.getValueType()));
7324
7325 unsigned Opcode = 0;
7326 unsigned NumOperands = 0;
7327 switch (Op.getNode()->getOpcode()) {
7328 case ISD::ADD:
7329 // Due to an isel shortcoming, be conservative if this add is likely to be
7330 // selected as part of a load-modify-store instruction. When the root node
7331 // in a match is a store, isel doesn't know how to remap non-chain non-flag
7332 // uses of other nodes in the match, such as the ADD in this case. This
7333 // leads to the ADD being left around and reselected, with the result being
7334 // two adds in the output. Alas, even if none our users are stores, that
7335 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
7336 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
7337 // climbing the DAG back to the root, and it doesn't seem to be worth the
7338 // effort.
7339 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Dan Gohman076aee32009-03-04 19:44:21 +00007340 UE = Op.getNode()->use_end(); UI != UE; ++UI)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007341 if (UI->getOpcode() != ISD::CopyToReg && UI->getOpcode() != ISD::SETCC)
7342 goto default_case;
7343
7344 if (ConstantSDNode *C =
7345 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
7346 // An add of one will be selected as an INC.
7347 if (C->getAPIntValue() == 1) {
7348 Opcode = X86ISD::INC;
7349 NumOperands = 1;
7350 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00007351 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007352
7353 // An add of negative one (subtract of one) will be selected as a DEC.
7354 if (C->getAPIntValue().isAllOnesValue()) {
7355 Opcode = X86ISD::DEC;
7356 NumOperands = 1;
7357 break;
7358 }
Dan Gohman076aee32009-03-04 19:44:21 +00007359 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007360
7361 // Otherwise use a regular EFLAGS-setting add.
7362 Opcode = X86ISD::ADD;
7363 NumOperands = 2;
7364 break;
7365 case ISD::AND: {
7366 // If the primary and result isn't used, don't bother using X86ISD::AND,
7367 // because a TEST instruction will be better.
7368 bool NonFlagUse = false;
7369 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
7370 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
7371 SDNode *User = *UI;
7372 unsigned UOpNo = UI.getOperandNo();
7373 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
7374 // Look pass truncate.
7375 UOpNo = User->use_begin().getOperandNo();
7376 User = *User->use_begin();
7377 }
7378
7379 if (User->getOpcode() != ISD::BRCOND &&
7380 User->getOpcode() != ISD::SETCC &&
7381 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
7382 NonFlagUse = true;
7383 break;
7384 }
Dan Gohman076aee32009-03-04 19:44:21 +00007385 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007386
7387 if (!NonFlagUse)
7388 break;
7389 }
7390 // FALL THROUGH
7391 case ISD::SUB:
7392 case ISD::OR:
7393 case ISD::XOR:
7394 // Due to the ISEL shortcoming noted above, be conservative if this op is
7395 // likely to be selected as part of a load-modify-store instruction.
7396 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
7397 UE = Op.getNode()->use_end(); UI != UE; ++UI)
7398 if (UI->getOpcode() == ISD::STORE)
7399 goto default_case;
7400
7401 // Otherwise use a regular EFLAGS-setting instruction.
7402 switch (Op.getNode()->getOpcode()) {
7403 default: llvm_unreachable("unexpected operator!");
7404 case ISD::SUB: Opcode = X86ISD::SUB; break;
7405 case ISD::OR: Opcode = X86ISD::OR; break;
7406 case ISD::XOR: Opcode = X86ISD::XOR; break;
7407 case ISD::AND: Opcode = X86ISD::AND; break;
7408 }
7409
7410 NumOperands = 2;
7411 break;
7412 case X86ISD::ADD:
7413 case X86ISD::SUB:
7414 case X86ISD::INC:
7415 case X86ISD::DEC:
7416 case X86ISD::OR:
7417 case X86ISD::XOR:
7418 case X86ISD::AND:
7419 return SDValue(Op.getNode(), 1);
7420 default:
7421 default_case:
7422 break;
Dan Gohman076aee32009-03-04 19:44:21 +00007423 }
7424
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007425 if (Opcode == 0)
7426 // Emit a CMP with 0, which is the TEST pattern.
7427 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
7428 DAG.getConstant(0, Op.getValueType()));
7429
7430 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
7431 SmallVector<SDValue, 4> Ops;
7432 for (unsigned i = 0; i != NumOperands; ++i)
7433 Ops.push_back(Op.getOperand(i));
7434
7435 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
7436 DAG.ReplaceAllUsesWith(Op, New);
7437 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00007438}
7439
7440/// Emit nodes that will be selected as "cmp Op0,Op1", or something
7441/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00007442SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00007443 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00007444 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
7445 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00007446 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00007447
7448 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00007449 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00007450}
7451
Evan Chengd40d03e2010-01-06 19:38:29 +00007452/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
7453/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00007454SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
7455 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00007456 SDValue Op0 = And.getOperand(0);
7457 SDValue Op1 = And.getOperand(1);
7458 if (Op0.getOpcode() == ISD::TRUNCATE)
7459 Op0 = Op0.getOperand(0);
7460 if (Op1.getOpcode() == ISD::TRUNCATE)
7461 Op1 = Op1.getOperand(0);
7462
Evan Chengd40d03e2010-01-06 19:38:29 +00007463 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00007464 if (Op1.getOpcode() == ISD::SHL)
7465 std::swap(Op0, Op1);
7466 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00007467 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
7468 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00007469 // If we looked past a truncate, check that it's only truncating away
7470 // known zeros.
7471 unsigned BitWidth = Op0.getValueSizeInBits();
7472 unsigned AndBitWidth = And.getValueSizeInBits();
7473 if (BitWidth > AndBitWidth) {
7474 APInt Mask = APInt::getAllOnesValue(BitWidth), Zeros, Ones;
7475 DAG.ComputeMaskedBits(Op0, Mask, Zeros, Ones);
7476 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
7477 return SDValue();
7478 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007479 LHS = Op1;
7480 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00007481 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007482 } else if (Op1.getOpcode() == ISD::Constant) {
7483 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
7484 SDValue AndLHS = Op0;
Evan Chengd40d03e2010-01-06 19:38:29 +00007485 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
7486 LHS = AndLHS.getOperand(0);
7487 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00007488 }
Evan Chengd40d03e2010-01-06 19:38:29 +00007489 }
Evan Cheng0488db92007-09-25 01:57:46 +00007490
Evan Chengd40d03e2010-01-06 19:38:29 +00007491 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00007492 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00007493 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00007494 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00007495 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00007496 // Also promote i16 to i32 for performance / code size reason.
7497 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00007498 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00007499 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00007500
Evan Chengd40d03e2010-01-06 19:38:29 +00007501 // If the operand types disagree, extend the shift amount to match. Since
7502 // BT ignores high bits (like shifts) we can use anyextend.
7503 if (LHS.getValueType() != RHS.getValueType())
7504 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00007505
Evan Chengd40d03e2010-01-06 19:38:29 +00007506 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
7507 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
7508 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7509 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00007510 }
7511
Evan Cheng54de3ea2010-01-05 06:52:31 +00007512 return SDValue();
7513}
7514
Dan Gohmand858e902010-04-17 15:26:15 +00007515SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng54de3ea2010-01-05 06:52:31 +00007516 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
7517 SDValue Op0 = Op.getOperand(0);
7518 SDValue Op1 = Op.getOperand(1);
7519 DebugLoc dl = Op.getDebugLoc();
7520 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
7521
7522 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00007523 // Lower (X & (1 << N)) == 0 to BT(X, N).
7524 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
7525 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Andrew Trickf6c39412011-03-23 23:11:02 +00007526 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00007527 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00007528 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00007529 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
7530 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
7531 if (NewSetCC.getNode())
7532 return NewSetCC;
7533 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00007534
Chris Lattner481eebc2010-12-19 21:23:48 +00007535 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
7536 // these.
7537 if (Op1.getOpcode() == ISD::Constant &&
Andrew Trickf6c39412011-03-23 23:11:02 +00007538 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
Evan Cheng2c755ba2010-02-27 07:36:59 +00007539 cast<ConstantSDNode>(Op1)->isNullValue()) &&
7540 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007541
Chris Lattner481eebc2010-12-19 21:23:48 +00007542 // If the input is a setcc, then reuse the input setcc or use a new one with
7543 // the inverted condition.
7544 if (Op0.getOpcode() == X86ISD::SETCC) {
7545 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
7546 bool Invert = (CC == ISD::SETNE) ^
7547 cast<ConstantSDNode>(Op1)->isNullValue();
7548 if (!Invert) return Op0;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007549
Evan Cheng2c755ba2010-02-27 07:36:59 +00007550 CCode = X86::GetOppositeBranchCondition(CCode);
Chris Lattner481eebc2010-12-19 21:23:48 +00007551 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7552 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
7553 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007554 }
7555
Evan Chenge5b51ac2010-04-17 06:13:15 +00007556 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00007557 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00007558 if (X86CC == X86::COND_INVALID)
7559 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00007560
Chris Lattnerc19d1c32010-12-19 22:08:31 +00007561 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00007562 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnerc19d1c32010-12-19 22:08:31 +00007563 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
Evan Cheng0488db92007-09-25 01:57:46 +00007564}
7565
Dan Gohmand858e902010-04-17 15:26:15 +00007566SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00007567 SDValue Cond;
7568 SDValue Op0 = Op.getOperand(0);
7569 SDValue Op1 = Op.getOperand(1);
7570 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00007571 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00007572 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
7573 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007574 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00007575
7576 if (isFP) {
7577 unsigned SSECC = 8;
Owen Andersone50ed302009-08-10 22:56:29 +00007578 EVT VT0 = Op0.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00007579 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
7580 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00007581 bool Swap = false;
7582
7583 switch (SetCCOpcode) {
7584 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00007585 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00007586 case ISD::SETEQ: SSECC = 0; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00007587 case ISD::SETOGT:
Nate Begeman30a0de92008-07-17 16:51:19 +00007588 case ISD::SETGT: Swap = true; // Fallthrough
7589 case ISD::SETLT:
7590 case ISD::SETOLT: SSECC = 1; break;
7591 case ISD::SETOGE:
7592 case ISD::SETGE: Swap = true; // Fallthrough
7593 case ISD::SETLE:
7594 case ISD::SETOLE: SSECC = 2; break;
7595 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00007596 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00007597 case ISD::SETNE: SSECC = 4; break;
7598 case ISD::SETULE: Swap = true;
7599 case ISD::SETUGE: SSECC = 5; break;
7600 case ISD::SETULT: Swap = true;
7601 case ISD::SETUGT: SSECC = 6; break;
7602 case ISD::SETO: SSECC = 7; break;
7603 }
7604 if (Swap)
7605 std::swap(Op0, Op1);
7606
Nate Begemanfb8ead02008-07-25 19:05:58 +00007607 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00007608 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00007609 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00007610 SDValue UNORD, EQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00007611 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
7612 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00007613 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00007614 }
7615 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00007616 SDValue ORD, NEQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00007617 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
7618 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00007619 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00007620 }
Torok Edwinc23197a2009-07-14 16:55:14 +00007621 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00007622 }
7623 // Handle all other FP comparisons here.
Owen Anderson825b72b2009-08-11 20:47:22 +00007624 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00007625 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007626
Nate Begeman30a0de92008-07-17 16:51:19 +00007627 // We are handling one of the integer comparisons here. Since SSE only has
7628 // GT and EQ comparisons for integer, swapping operands and multiple
7629 // operations may be required for some comparisons.
7630 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
7631 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007632
Owen Anderson825b72b2009-08-11 20:47:22 +00007633 switch (VT.getSimpleVT().SimpleTy) {
Nate Begeman30a0de92008-07-17 16:51:19 +00007634 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007635 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007636 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007637 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
7638 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00007639 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007640
Nate Begeman30a0de92008-07-17 16:51:19 +00007641 switch (SetCCOpcode) {
7642 default: break;
7643 case ISD::SETNE: Invert = true;
7644 case ISD::SETEQ: Opc = EQOpc; break;
7645 case ISD::SETLT: Swap = true;
7646 case ISD::SETGT: Opc = GTOpc; break;
7647 case ISD::SETGE: Swap = true;
7648 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
7649 case ISD::SETULT: Swap = true;
7650 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
7651 case ISD::SETUGE: Swap = true;
7652 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
7653 }
7654 if (Swap)
7655 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007656
Nate Begeman30a0de92008-07-17 16:51:19 +00007657 // Since SSE has no unsigned integer comparisons, we need to flip the sign
7658 // bits of the inputs before performing those operations.
7659 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00007660 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00007661 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
7662 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00007663 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00007664 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
7665 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00007666 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
7667 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00007668 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007669
Dale Johannesenace16102009-02-03 19:33:06 +00007670 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00007671
7672 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00007673 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00007674 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00007675
Nate Begeman30a0de92008-07-17 16:51:19 +00007676 return Result;
7677}
Evan Cheng0488db92007-09-25 01:57:46 +00007678
Evan Cheng370e5342008-12-03 08:38:43 +00007679// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00007680static bool isX86LogicalCmp(SDValue Op) {
7681 unsigned Opc = Op.getNode()->getOpcode();
7682 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
7683 return true;
7684 if (Op.getResNo() == 1 &&
7685 (Opc == X86ISD::ADD ||
7686 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00007687 Opc == X86ISD::ADC ||
7688 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00007689 Opc == X86ISD::SMUL ||
7690 Opc == X86ISD::UMUL ||
7691 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00007692 Opc == X86ISD::DEC ||
7693 Opc == X86ISD::OR ||
7694 Opc == X86ISD::XOR ||
7695 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00007696 return true;
7697
Chris Lattner9637d5b2010-12-05 07:49:54 +00007698 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
7699 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007700
Dan Gohman076aee32009-03-04 19:44:21 +00007701 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00007702}
7703
Chris Lattnera2b56002010-12-05 01:23:24 +00007704static bool isZero(SDValue V) {
7705 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
7706 return C && C->isNullValue();
7707}
7708
Chris Lattner96908b12010-12-05 02:00:51 +00007709static bool isAllOnes(SDValue V) {
7710 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
7711 return C && C->isAllOnesValue();
7712}
7713
Dan Gohmand858e902010-04-17 15:26:15 +00007714SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00007715 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007716 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00007717 SDValue Op1 = Op.getOperand(1);
7718 SDValue Op2 = Op.getOperand(2);
7719 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00007720 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00007721
Dan Gohman1a492952009-10-20 16:22:37 +00007722 if (Cond.getOpcode() == ISD::SETCC) {
7723 SDValue NewCond = LowerSETCC(Cond, DAG);
7724 if (NewCond.getNode())
7725 Cond = NewCond;
7726 }
Evan Cheng734503b2006-09-11 02:19:56 +00007727
Chris Lattnera2b56002010-12-05 01:23:24 +00007728 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00007729 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00007730 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00007731 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007732 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00007733 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
7734 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007735 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007736
Chris Lattnera2b56002010-12-05 01:23:24 +00007737 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007738
7739 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00007740 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
7741 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00007742
7743 SDValue CmpOp0 = Cmp.getOperand(0);
7744 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
7745 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007746
Chris Lattner96908b12010-12-05 02:00:51 +00007747 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00007748 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
7749 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007750
Chris Lattner96908b12010-12-05 02:00:51 +00007751 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
7752 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007753
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007754 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00007755 if (N2C == 0 || !N2C->isNullValue())
7756 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
7757 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007758 }
7759 }
7760
Chris Lattnera2b56002010-12-05 01:23:24 +00007761 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00007762 if (Cond.getOpcode() == ISD::AND &&
7763 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
7764 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00007765 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00007766 Cond = Cond.getOperand(0);
7767 }
7768
Evan Cheng3f41d662007-10-08 22:16:29 +00007769 // If condition flag is set by a X86ISD::CMP, then use it as the condition
7770 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00007771 if (Cond.getOpcode() == X86ISD::SETCC ||
7772 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00007773 CC = Cond.getOperand(0);
7774
Dan Gohman475871a2008-07-27 21:46:04 +00007775 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00007776 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00007777 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00007778
Evan Cheng3f41d662007-10-08 22:16:29 +00007779 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007780 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00007781 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00007782 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00007783
Chris Lattnerd1980a52009-03-12 06:52:53 +00007784 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
7785 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00007786 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00007787 addTest = false;
7788 }
7789 }
7790
7791 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007792 // Look pass the truncate.
7793 if (Cond.getOpcode() == ISD::TRUNCATE)
7794 Cond = Cond.getOperand(0);
7795
7796 // We know the result of AND is compared against zero. Try to match
7797 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00007798 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00007799 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00007800 if (NewSetCC.getNode()) {
7801 CC = NewSetCC.getOperand(0);
7802 Cond = NewSetCC.getOperand(1);
7803 addTest = false;
7804 }
7805 }
7806 }
7807
7808 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007809 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00007810 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00007811 }
7812
Benjamin Kramere915ff32010-12-22 23:09:28 +00007813 // a < b ? -1 : 0 -> RES = ~setcc_carry
7814 // a < b ? 0 : -1 -> RES = setcc_carry
7815 // a >= b ? -1 : 0 -> RES = setcc_carry
7816 // a >= b ? 0 : -1 -> RES = ~setcc_carry
7817 if (Cond.getOpcode() == X86ISD::CMP) {
7818 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
7819
7820 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
7821 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
7822 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
7823 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
7824 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
7825 return DAG.getNOT(DL, Res, Res.getValueType());
7826 return Res;
7827 }
7828 }
7829
Evan Cheng0488db92007-09-25 01:57:46 +00007830 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
7831 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007832 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007833 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00007834 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00007835}
7836
Evan Cheng370e5342008-12-03 08:38:43 +00007837// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
7838// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
7839// from the AND / OR.
7840static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
7841 Opc = Op.getOpcode();
7842 if (Opc != ISD::OR && Opc != ISD::AND)
7843 return false;
7844 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
7845 Op.getOperand(0).hasOneUse() &&
7846 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
7847 Op.getOperand(1).hasOneUse());
7848}
7849
Evan Cheng961d6d42009-02-02 08:19:07 +00007850// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
7851// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00007852static bool isXor1OfSetCC(SDValue Op) {
7853 if (Op.getOpcode() != ISD::XOR)
7854 return false;
7855 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
7856 if (N1C && N1C->getAPIntValue() == 1) {
7857 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
7858 Op.getOperand(0).hasOneUse();
7859 }
7860 return false;
7861}
7862
Dan Gohmand858e902010-04-17 15:26:15 +00007863SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00007864 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007865 SDValue Chain = Op.getOperand(0);
7866 SDValue Cond = Op.getOperand(1);
7867 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007868 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00007869 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00007870
Dan Gohman1a492952009-10-20 16:22:37 +00007871 if (Cond.getOpcode() == ISD::SETCC) {
7872 SDValue NewCond = LowerSETCC(Cond, DAG);
7873 if (NewCond.getNode())
7874 Cond = NewCond;
7875 }
Chris Lattnere55484e2008-12-25 05:34:37 +00007876#if 0
7877 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00007878 else if (Cond.getOpcode() == X86ISD::ADD ||
7879 Cond.getOpcode() == X86ISD::SUB ||
7880 Cond.getOpcode() == X86ISD::SMUL ||
7881 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00007882 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00007883#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00007884
Evan Chengad9c0a32009-12-15 00:53:42 +00007885 // Look pass (and (setcc_carry (cmp ...)), 1).
7886 if (Cond.getOpcode() == ISD::AND &&
7887 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
7888 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00007889 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00007890 Cond = Cond.getOperand(0);
7891 }
7892
Evan Cheng3f41d662007-10-08 22:16:29 +00007893 // If condition flag is set by a X86ISD::CMP, then use it as the condition
7894 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00007895 if (Cond.getOpcode() == X86ISD::SETCC ||
7896 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00007897 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007898
Dan Gohman475871a2008-07-27 21:46:04 +00007899 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00007900 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00007901 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00007902 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00007903 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00007904 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00007905 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00007906 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00007907 default: break;
7908 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00007909 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00007910 // These can only come from an arithmetic instruction with overflow,
7911 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00007912 Cond = Cond.getNode()->getOperand(1);
7913 addTest = false;
7914 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00007915 }
Evan Cheng0488db92007-09-25 01:57:46 +00007916 }
Evan Cheng370e5342008-12-03 08:38:43 +00007917 } else {
7918 unsigned CondOpc;
7919 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
7920 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00007921 if (CondOpc == ISD::OR) {
7922 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
7923 // two branches instead of an explicit OR instruction with a
7924 // separate test.
7925 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00007926 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00007927 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007928 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00007929 Chain, Dest, CC, Cmp);
7930 CC = Cond.getOperand(1).getOperand(0);
7931 Cond = Cmp;
7932 addTest = false;
7933 }
7934 } else { // ISD::AND
7935 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
7936 // two branches instead of an explicit AND instruction with a
7937 // separate test. However, we only do this if this block doesn't
7938 // have a fall-through edge, because this requires an explicit
7939 // jmp when the condition is false.
7940 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00007941 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00007942 Op.getNode()->hasOneUse()) {
7943 X86::CondCode CCode =
7944 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
7945 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007946 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00007947 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00007948 // Look for an unconditional branch following this conditional branch.
7949 // We need this because we need to reverse the successors in order
7950 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00007951 if (User->getOpcode() == ISD::BR) {
7952 SDValue FalseBB = User->getOperand(1);
7953 SDNode *NewBR =
7954 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00007955 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00007956 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00007957 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00007958
Dale Johannesene4d209d2009-02-03 20:21:25 +00007959 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00007960 Chain, Dest, CC, Cmp);
7961 X86::CondCode CCode =
7962 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
7963 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007964 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00007965 Cond = Cmp;
7966 addTest = false;
7967 }
7968 }
Dan Gohman279c22e2008-10-21 03:29:32 +00007969 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00007970 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
7971 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
7972 // It should be transformed during dag combiner except when the condition
7973 // is set by a arithmetics with overflow node.
7974 X86::CondCode CCode =
7975 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
7976 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007977 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00007978 Cond = Cond.getOperand(0).getOperand(1);
7979 addTest = false;
Dan Gohman279c22e2008-10-21 03:29:32 +00007980 }
Evan Cheng0488db92007-09-25 01:57:46 +00007981 }
7982
7983 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007984 // Look pass the truncate.
7985 if (Cond.getOpcode() == ISD::TRUNCATE)
7986 Cond = Cond.getOperand(0);
7987
7988 // We know the result of AND is compared against zero. Try to match
7989 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00007990 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007991 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
7992 if (NewSetCC.getNode()) {
7993 CC = NewSetCC.getOperand(0);
7994 Cond = NewSetCC.getOperand(1);
7995 addTest = false;
7996 }
7997 }
7998 }
7999
8000 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008001 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00008002 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00008003 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00008004 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00008005 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00008006}
8007
Anton Korobeynikove060b532007-04-17 19:34:00 +00008008
8009// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
8010// Calls to _alloca is needed to probe the stack when allocating more than 4k
8011// bytes in one go. Touching the stack at 4K increments is necessary to ensure
8012// that the guard pages used by the OS virtual memory manager are allocated in
8013// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00008014SDValue
8015X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008016 SelectionDAG &DAG) const {
Duncan Sands1e1ca0b2010-10-21 16:02:12 +00008017 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows()) &&
Michael J. Spencere9c253e2010-10-21 01:41:01 +00008018 "This should be used only on Windows targets");
NAKAMURA Takumia2e07622011-03-24 07:07:00 +00008019 assert(!Subtarget->isTargetEnvMacho());
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008020 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008021
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008022 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00008023 SDValue Chain = Op.getOperand(0);
8024 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008025 // FIXME: Ensure alignment here
8026
Dan Gohman475871a2008-07-27 21:46:04 +00008027 SDValue Flag;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008028
Owen Anderson825b72b2009-08-11 20:47:22 +00008029 EVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
NAKAMURA Takumia2e07622011-03-24 07:07:00 +00008030 unsigned Reg = (Subtarget->is64Bit() ? X86::RAX : X86::EAX);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008031
NAKAMURA Takumia2e07622011-03-24 07:07:00 +00008032 Chain = DAG.getCopyToReg(Chain, dl, Reg, Size, Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00008033 Flag = Chain.getValue(1);
8034
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008035 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00008036
Michael J. Spencere9c253e2010-10-21 01:41:01 +00008037 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00008038 Flag = Chain.getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008039
Dale Johannesendd64c412009-02-04 00:33:20 +00008040 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008041
Dan Gohman475871a2008-07-27 21:46:04 +00008042 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008043 return DAG.getMergeValues(Ops1, 2, dl);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008044}
8045
Dan Gohmand858e902010-04-17 15:26:15 +00008046SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00008047 MachineFunction &MF = DAG.getMachineFunction();
8048 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
8049
Dan Gohman69de1932008-02-06 22:27:42 +00008050 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00008051 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00008052
Anton Korobeynikove7beda12010-10-03 22:52:07 +00008053 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00008054 // vastart just stores the address of the VarArgsFrameIndex slot into the
8055 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00008056 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
8057 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00008058 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
8059 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008060 }
8061
8062 // __va_list_tag:
8063 // gp_offset (0 - 6 * 8)
8064 // fp_offset (48 - 48 + 8 * 16)
8065 // overflow_arg_area (point to parameters coming in memory).
8066 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00008067 SmallVector<SDValue, 8> MemOps;
8068 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00008069 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00008070 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00008071 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
8072 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008073 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008074 MemOps.push_back(Store);
8075
8076 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00008077 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008078 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008079 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00008080 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
8081 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008082 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008083 MemOps.push_back(Store);
8084
8085 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +00008086 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008087 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00008088 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
8089 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00008090 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
8091 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +00008092 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008093 MemOps.push_back(Store);
8094
8095 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +00008096 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008097 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00008098 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
8099 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00008100 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
8101 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008102 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +00008103 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00008104 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00008105}
8106
Dan Gohmand858e902010-04-17 15:26:15 +00008107SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +00008108 assert(Subtarget->is64Bit() &&
8109 "LowerVAARG only handles 64-bit va_arg!");
8110 assert((Subtarget->isTargetLinux() ||
8111 Subtarget->isTargetDarwin()) &&
8112 "Unhandled target in LowerVAARG");
8113 assert(Op.getNode()->getNumOperands() == 4);
8114 SDValue Chain = Op.getOperand(0);
8115 SDValue SrcPtr = Op.getOperand(1);
8116 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
8117 unsigned Align = Op.getConstantOperandVal(3);
8118 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +00008119
Dan Gohman320afb82010-10-12 18:00:49 +00008120 EVT ArgVT = Op.getNode()->getValueType(0);
8121 const Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
8122 uint32_t ArgSize = getTargetData()->getTypeAllocSize(ArgTy);
8123 uint8_t ArgMode;
8124
8125 // Decide which area this value should be read from.
8126 // TODO: Implement the AMD64 ABI in its entirety. This simple
8127 // selection mechanism works only for the basic types.
8128 if (ArgVT == MVT::f80) {
8129 llvm_unreachable("va_arg for f80 not yet implemented");
8130 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
8131 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
8132 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
8133 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
8134 } else {
8135 llvm_unreachable("Unhandled argument type in LowerVAARG");
8136 }
8137
8138 if (ArgMode == 2) {
8139 // Sanity Check: Make sure using fp_offset makes sense.
Michael J. Spencer87b86652010-10-19 07:32:42 +00008140 assert(!UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +00008141 !(DAG.getMachineFunction()
8142 .getFunction()->hasFnAttr(Attribute::NoImplicitFloat)) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00008143 Subtarget->hasXMM());
Dan Gohman320afb82010-10-12 18:00:49 +00008144 }
8145
8146 // Insert VAARG_64 node into the DAG
8147 // VAARG_64 returns two values: Variable Argument Address, Chain
8148 SmallVector<SDValue, 11> InstOps;
8149 InstOps.push_back(Chain);
8150 InstOps.push_back(SrcPtr);
8151 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
8152 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
8153 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
8154 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
8155 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
8156 VTs, &InstOps[0], InstOps.size(),
8157 MVT::i64,
8158 MachinePointerInfo(SV),
8159 /*Align=*/0,
8160 /*Volatile=*/false,
8161 /*ReadMem=*/true,
8162 /*WriteMem=*/true);
8163 Chain = VAARG.getValue(1);
8164
8165 // Load the next argument and return it
8166 return DAG.getLoad(ArgVT, dl,
8167 Chain,
8168 VAARG,
8169 MachinePointerInfo(),
8170 false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +00008171}
8172
Dan Gohmand858e902010-04-17 15:26:15 +00008173SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) const {
Evan Chengae642192007-03-02 23:16:35 +00008174 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00008175 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00008176 SDValue Chain = Op.getOperand(0);
8177 SDValue DstPtr = Op.getOperand(1);
8178 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00008179 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
8180 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +00008181 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00008182
Chris Lattnere72f2022010-09-21 05:40:29 +00008183 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00008184 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +00008185 false,
Chris Lattnere72f2022010-09-21 05:40:29 +00008186 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +00008187}
8188
Dan Gohman475871a2008-07-27 21:46:04 +00008189SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00008190X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008191 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00008192 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00008193 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00008194 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00008195 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00008196 case Intrinsic::x86_sse_comieq_ss:
8197 case Intrinsic::x86_sse_comilt_ss:
8198 case Intrinsic::x86_sse_comile_ss:
8199 case Intrinsic::x86_sse_comigt_ss:
8200 case Intrinsic::x86_sse_comige_ss:
8201 case Intrinsic::x86_sse_comineq_ss:
8202 case Intrinsic::x86_sse_ucomieq_ss:
8203 case Intrinsic::x86_sse_ucomilt_ss:
8204 case Intrinsic::x86_sse_ucomile_ss:
8205 case Intrinsic::x86_sse_ucomigt_ss:
8206 case Intrinsic::x86_sse_ucomige_ss:
8207 case Intrinsic::x86_sse_ucomineq_ss:
8208 case Intrinsic::x86_sse2_comieq_sd:
8209 case Intrinsic::x86_sse2_comilt_sd:
8210 case Intrinsic::x86_sse2_comile_sd:
8211 case Intrinsic::x86_sse2_comigt_sd:
8212 case Intrinsic::x86_sse2_comige_sd:
8213 case Intrinsic::x86_sse2_comineq_sd:
8214 case Intrinsic::x86_sse2_ucomieq_sd:
8215 case Intrinsic::x86_sse2_ucomilt_sd:
8216 case Intrinsic::x86_sse2_ucomile_sd:
8217 case Intrinsic::x86_sse2_ucomigt_sd:
8218 case Intrinsic::x86_sse2_ucomige_sd:
8219 case Intrinsic::x86_sse2_ucomineq_sd: {
8220 unsigned Opc = 0;
8221 ISD::CondCode CC = ISD::SETCC_INVALID;
8222 switch (IntNo) {
8223 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008224 case Intrinsic::x86_sse_comieq_ss:
8225 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008226 Opc = X86ISD::COMI;
8227 CC = ISD::SETEQ;
8228 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00008229 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008230 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008231 Opc = X86ISD::COMI;
8232 CC = ISD::SETLT;
8233 break;
8234 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008235 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008236 Opc = X86ISD::COMI;
8237 CC = ISD::SETLE;
8238 break;
8239 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008240 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008241 Opc = X86ISD::COMI;
8242 CC = ISD::SETGT;
8243 break;
8244 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008245 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008246 Opc = X86ISD::COMI;
8247 CC = ISD::SETGE;
8248 break;
8249 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008250 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008251 Opc = X86ISD::COMI;
8252 CC = ISD::SETNE;
8253 break;
8254 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008255 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008256 Opc = X86ISD::UCOMI;
8257 CC = ISD::SETEQ;
8258 break;
8259 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008260 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008261 Opc = X86ISD::UCOMI;
8262 CC = ISD::SETLT;
8263 break;
8264 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008265 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008266 Opc = X86ISD::UCOMI;
8267 CC = ISD::SETLE;
8268 break;
8269 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008270 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008271 Opc = X86ISD::UCOMI;
8272 CC = ISD::SETGT;
8273 break;
8274 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008275 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008276 Opc = X86ISD::UCOMI;
8277 CC = ISD::SETGE;
8278 break;
8279 case Intrinsic::x86_sse_ucomineq_ss:
8280 case Intrinsic::x86_sse2_ucomineq_sd:
8281 Opc = X86ISD::UCOMI;
8282 CC = ISD::SETNE;
8283 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00008284 }
Evan Cheng734503b2006-09-11 02:19:56 +00008285
Dan Gohman475871a2008-07-27 21:46:04 +00008286 SDValue LHS = Op.getOperand(1);
8287 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00008288 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00008289 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00008290 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
8291 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8292 DAG.getConstant(X86CC, MVT::i8), Cond);
8293 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00008294 }
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008295 // ptest and testp intrinsics. The intrinsic these come from are designed to
8296 // return an integer value, not just an instruction so lower it to the ptest
8297 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00008298 case Intrinsic::x86_sse41_ptestz:
8299 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008300 case Intrinsic::x86_sse41_ptestnzc:
8301 case Intrinsic::x86_avx_ptestz_256:
8302 case Intrinsic::x86_avx_ptestc_256:
8303 case Intrinsic::x86_avx_ptestnzc_256:
8304 case Intrinsic::x86_avx_vtestz_ps:
8305 case Intrinsic::x86_avx_vtestc_ps:
8306 case Intrinsic::x86_avx_vtestnzc_ps:
8307 case Intrinsic::x86_avx_vtestz_pd:
8308 case Intrinsic::x86_avx_vtestc_pd:
8309 case Intrinsic::x86_avx_vtestnzc_pd:
8310 case Intrinsic::x86_avx_vtestz_ps_256:
8311 case Intrinsic::x86_avx_vtestc_ps_256:
8312 case Intrinsic::x86_avx_vtestnzc_ps_256:
8313 case Intrinsic::x86_avx_vtestz_pd_256:
8314 case Intrinsic::x86_avx_vtestc_pd_256:
8315 case Intrinsic::x86_avx_vtestnzc_pd_256: {
8316 bool IsTestPacked = false;
Eric Christopher71c67532009-07-29 00:28:05 +00008317 unsigned X86CC = 0;
8318 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00008319 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008320 case Intrinsic::x86_avx_vtestz_ps:
8321 case Intrinsic::x86_avx_vtestz_pd:
8322 case Intrinsic::x86_avx_vtestz_ps_256:
8323 case Intrinsic::x86_avx_vtestz_pd_256:
8324 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00008325 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008326 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +00008327 // ZF = 1
8328 X86CC = X86::COND_E;
8329 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008330 case Intrinsic::x86_avx_vtestc_ps:
8331 case Intrinsic::x86_avx_vtestc_pd:
8332 case Intrinsic::x86_avx_vtestc_ps_256:
8333 case Intrinsic::x86_avx_vtestc_pd_256:
8334 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00008335 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008336 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00008337 // CF = 1
8338 X86CC = X86::COND_B;
8339 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008340 case Intrinsic::x86_avx_vtestnzc_ps:
8341 case Intrinsic::x86_avx_vtestnzc_pd:
8342 case Intrinsic::x86_avx_vtestnzc_ps_256:
8343 case Intrinsic::x86_avx_vtestnzc_pd_256:
8344 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +00008345 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008346 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00008347 // ZF and CF = 0
8348 X86CC = X86::COND_A;
8349 break;
8350 }
Eric Christopherfd179292009-08-27 18:07:15 +00008351
Eric Christopher71c67532009-07-29 00:28:05 +00008352 SDValue LHS = Op.getOperand(1);
8353 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008354 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
8355 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00008356 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
8357 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
8358 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00008359 }
Evan Cheng5759f972008-05-04 09:15:50 +00008360
8361 // Fix vector shift instructions where the last operand is a non-immediate
8362 // i32 value.
8363 case Intrinsic::x86_sse2_pslli_w:
8364 case Intrinsic::x86_sse2_pslli_d:
8365 case Intrinsic::x86_sse2_pslli_q:
8366 case Intrinsic::x86_sse2_psrli_w:
8367 case Intrinsic::x86_sse2_psrli_d:
8368 case Intrinsic::x86_sse2_psrli_q:
8369 case Intrinsic::x86_sse2_psrai_w:
8370 case Intrinsic::x86_sse2_psrai_d:
8371 case Intrinsic::x86_mmx_pslli_w:
8372 case Intrinsic::x86_mmx_pslli_d:
8373 case Intrinsic::x86_mmx_pslli_q:
8374 case Intrinsic::x86_mmx_psrli_w:
8375 case Intrinsic::x86_mmx_psrli_d:
8376 case Intrinsic::x86_mmx_psrli_q:
8377 case Intrinsic::x86_mmx_psrai_w:
8378 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00008379 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00008380 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00008381 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00008382
8383 unsigned NewIntNo = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00008384 EVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00008385 switch (IntNo) {
8386 case Intrinsic::x86_sse2_pslli_w:
8387 NewIntNo = Intrinsic::x86_sse2_psll_w;
8388 break;
8389 case Intrinsic::x86_sse2_pslli_d:
8390 NewIntNo = Intrinsic::x86_sse2_psll_d;
8391 break;
8392 case Intrinsic::x86_sse2_pslli_q:
8393 NewIntNo = Intrinsic::x86_sse2_psll_q;
8394 break;
8395 case Intrinsic::x86_sse2_psrli_w:
8396 NewIntNo = Intrinsic::x86_sse2_psrl_w;
8397 break;
8398 case Intrinsic::x86_sse2_psrli_d:
8399 NewIntNo = Intrinsic::x86_sse2_psrl_d;
8400 break;
8401 case Intrinsic::x86_sse2_psrli_q:
8402 NewIntNo = Intrinsic::x86_sse2_psrl_q;
8403 break;
8404 case Intrinsic::x86_sse2_psrai_w:
8405 NewIntNo = Intrinsic::x86_sse2_psra_w;
8406 break;
8407 case Intrinsic::x86_sse2_psrai_d:
8408 NewIntNo = Intrinsic::x86_sse2_psra_d;
8409 break;
8410 default: {
Owen Anderson825b72b2009-08-11 20:47:22 +00008411 ShAmtVT = MVT::v2i32;
Evan Cheng5759f972008-05-04 09:15:50 +00008412 switch (IntNo) {
8413 case Intrinsic::x86_mmx_pslli_w:
8414 NewIntNo = Intrinsic::x86_mmx_psll_w;
8415 break;
8416 case Intrinsic::x86_mmx_pslli_d:
8417 NewIntNo = Intrinsic::x86_mmx_psll_d;
8418 break;
8419 case Intrinsic::x86_mmx_pslli_q:
8420 NewIntNo = Intrinsic::x86_mmx_psll_q;
8421 break;
8422 case Intrinsic::x86_mmx_psrli_w:
8423 NewIntNo = Intrinsic::x86_mmx_psrl_w;
8424 break;
8425 case Intrinsic::x86_mmx_psrli_d:
8426 NewIntNo = Intrinsic::x86_mmx_psrl_d;
8427 break;
8428 case Intrinsic::x86_mmx_psrli_q:
8429 NewIntNo = Intrinsic::x86_mmx_psrl_q;
8430 break;
8431 case Intrinsic::x86_mmx_psrai_w:
8432 NewIntNo = Intrinsic::x86_mmx_psra_w;
8433 break;
8434 case Intrinsic::x86_mmx_psrai_d:
8435 NewIntNo = Intrinsic::x86_mmx_psra_d;
8436 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00008437 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00008438 }
8439 break;
8440 }
8441 }
Mon P Wangefa42202009-09-03 19:56:25 +00008442
8443 // The vector shift intrinsics with scalars uses 32b shift amounts but
8444 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
8445 // to be zero.
8446 SDValue ShOps[4];
8447 ShOps[0] = ShAmt;
8448 ShOps[1] = DAG.getConstant(0, MVT::i32);
8449 if (ShAmtVT == MVT::v4i32) {
8450 ShOps[2] = DAG.getUNDEF(MVT::i32);
8451 ShOps[3] = DAG.getUNDEF(MVT::i32);
8452 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
8453 } else {
8454 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
Dale Johannesen0488fb62010-09-30 23:57:10 +00008455// FIXME this must be lowered to get rid of the invalid type.
Mon P Wangefa42202009-09-03 19:56:25 +00008456 }
8457
Owen Andersone50ed302009-08-10 22:56:29 +00008458 EVT VT = Op.getValueType();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008459 ShAmt = DAG.getNode(ISD::BITCAST, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008460 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008461 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00008462 Op.getOperand(1), ShAmt);
8463 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00008464 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008465}
Evan Cheng72261582005-12-20 06:22:03 +00008466
Dan Gohmand858e902010-04-17 15:26:15 +00008467SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
8468 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +00008469 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
8470 MFI->setReturnAddressIsTaken(true);
8471
Bill Wendling64e87322009-01-16 19:25:27 +00008472 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008473 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00008474
8475 if (Depth > 0) {
8476 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
8477 SDValue Offset =
8478 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00008479 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008480 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00008481 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008482 FrameAddr, Offset),
Chris Lattner51abfe42010-09-21 06:02:19 +00008483 MachinePointerInfo(), false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +00008484 }
8485
8486 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00008487 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00008488 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Chris Lattner51abfe42010-09-21 06:02:19 +00008489 RetAddrFI, MachinePointerInfo(), false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00008490}
8491
Dan Gohmand858e902010-04-17 15:26:15 +00008492SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +00008493 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
8494 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00008495
Owen Andersone50ed302009-08-10 22:56:29 +00008496 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008497 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00008498 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
8499 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00008500 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00008501 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +00008502 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
8503 MachinePointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +00008504 false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00008505 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00008506}
8507
Dan Gohman475871a2008-07-27 21:46:04 +00008508SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008509 SelectionDAG &DAG) const {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00008510 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008511}
8512
Dan Gohmand858e902010-04-17 15:26:15 +00008513SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008514 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00008515 SDValue Chain = Op.getOperand(0);
8516 SDValue Offset = Op.getOperand(1);
8517 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008518 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008519
Dan Gohmand8816272010-08-11 18:14:00 +00008520 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
8521 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
8522 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008523 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008524
Dan Gohmand8816272010-08-11 18:14:00 +00008525 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
8526 DAG.getIntPtrConstant(TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008527 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +00008528 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
8529 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00008530 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008531 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008532
Dale Johannesene4d209d2009-02-03 20:21:25 +00008533 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008534 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008535 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008536}
8537
Dan Gohman475871a2008-07-27 21:46:04 +00008538SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008539 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008540 SDValue Root = Op.getOperand(0);
8541 SDValue Trmp = Op.getOperand(1); // trampoline
8542 SDValue FPtr = Op.getOperand(2); // nested function
8543 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008544 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008545
Dan Gohman69de1932008-02-06 22:27:42 +00008546 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008547
8548 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00008549 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00008550
8551 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +00008552 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
8553 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +00008554
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00008555 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
8556 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00008557
8558 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
8559
8560 // Load the pointer to the nested function into R11.
8561 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00008562 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00008563 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008564 Addr, MachinePointerInfo(TrmpAddr),
8565 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008566
Owen Anderson825b72b2009-08-11 20:47:22 +00008567 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8568 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008569 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
8570 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +00008571 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00008572
8573 // Load the 'nest' parameter value into R10.
8574 // R10 is specified in X86CallingConv.td
8575 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +00008576 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8577 DAG.getConstant(10, MVT::i64));
8578 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008579 Addr, MachinePointerInfo(TrmpAddr, 10),
8580 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008581
Owen Anderson825b72b2009-08-11 20:47:22 +00008582 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8583 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008584 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
8585 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +00008586 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00008587
8588 // Jump to the nested function.
8589 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +00008590 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8591 DAG.getConstant(20, MVT::i64));
8592 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008593 Addr, MachinePointerInfo(TrmpAddr, 20),
8594 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008595
8596 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +00008597 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8598 DAG.getConstant(22, MVT::i64));
8599 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00008600 MachinePointerInfo(TrmpAddr, 22),
8601 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008602
Dan Gohman475871a2008-07-27 21:46:04 +00008603 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00008604 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008605 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008606 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00008607 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00008608 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00008609 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00008610 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008611
8612 switch (CC) {
8613 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00008614 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00008615 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00008616 case CallingConv::X86_StdCall: {
8617 // Pass 'nest' parameter in ECX.
8618 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00008619 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008620
8621 // Check that ECX wasn't needed by an 'inreg' parameter.
8622 const FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00008623 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008624
Chris Lattner58d74912008-03-12 17:45:29 +00008625 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00008626 unsigned InRegCount = 0;
8627 unsigned Idx = 1;
8628
8629 for (FunctionType::param_iterator I = FTy->param_begin(),
8630 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00008631 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00008632 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00008633 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008634
8635 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +00008636 report_fatal_error("Nest register in use - reduce number of inreg"
8637 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00008638 }
8639 }
8640 break;
8641 }
8642 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +00008643 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00008644 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00008645 // Pass 'nest' parameter in EAX.
8646 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00008647 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008648 break;
8649 }
8650
Dan Gohman475871a2008-07-27 21:46:04 +00008651 SDValue OutChains[4];
8652 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008653
Owen Anderson825b72b2009-08-11 20:47:22 +00008654 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8655 DAG.getConstant(10, MVT::i32));
8656 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008657
Chris Lattnera62fe662010-02-05 19:20:30 +00008658 // This is storing the opcode for MOV32ri.
8659 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00008660 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00008661 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008662 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008663 Trmp, MachinePointerInfo(TrmpAddr),
8664 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008665
Owen Anderson825b72b2009-08-11 20:47:22 +00008666 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8667 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008668 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
8669 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +00008670 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008671
Chris Lattnera62fe662010-02-05 19:20:30 +00008672 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +00008673 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8674 DAG.getConstant(5, MVT::i32));
8675 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00008676 MachinePointerInfo(TrmpAddr, 5),
8677 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008678
Owen Anderson825b72b2009-08-11 20:47:22 +00008679 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8680 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008681 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
8682 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +00008683 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008684
Dan Gohman475871a2008-07-27 21:46:04 +00008685 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00008686 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008687 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008688 }
8689}
8690
Dan Gohmand858e902010-04-17 15:26:15 +00008691SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
8692 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008693 /*
8694 The rounding mode is in bits 11:10 of FPSR, and has the following
8695 settings:
8696 00 Round to nearest
8697 01 Round to -inf
8698 10 Round to +inf
8699 11 Round to 0
8700
8701 FLT_ROUNDS, on the other hand, expects the following:
8702 -1 Undefined
8703 0 Round to 0
8704 1 Round to nearest
8705 2 Round to +inf
8706 3 Round to -inf
8707
8708 To perform the conversion, we do:
8709 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
8710 */
8711
8712 MachineFunction &MF = DAG.getMachineFunction();
8713 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00008714 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008715 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00008716 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +00008717 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008718
8719 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00008720 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +00008721 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008722
Michael J. Spencerec38de22010-10-10 22:04:20 +00008723
Chris Lattner2156b792010-09-22 01:11:26 +00008724 MachineMemOperand *MMO =
8725 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8726 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008727
Chris Lattner2156b792010-09-22 01:11:26 +00008728 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
8729 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
8730 DAG.getVTList(MVT::Other),
8731 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008732
8733 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +00008734 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Chris Lattner51abfe42010-09-21 06:02:19 +00008735 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008736
8737 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00008738 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +00008739 DAG.getNode(ISD::SRL, DL, MVT::i16,
8740 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00008741 CWD, DAG.getConstant(0x800, MVT::i16)),
8742 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00008743 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +00008744 DAG.getNode(ISD::SRL, DL, MVT::i16,
8745 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00008746 CWD, DAG.getConstant(0x400, MVT::i16)),
8747 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008748
Dan Gohman475871a2008-07-27 21:46:04 +00008749 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +00008750 DAG.getNode(ISD::AND, DL, MVT::i16,
8751 DAG.getNode(ISD::ADD, DL, MVT::i16,
8752 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +00008753 DAG.getConstant(1, MVT::i16)),
8754 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008755
8756
Duncan Sands83ec4b62008-06-06 12:08:01 +00008757 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +00008758 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008759}
8760
Dan Gohmand858e902010-04-17 15:26:15 +00008761SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008762 EVT VT = Op.getValueType();
8763 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008764 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008765 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00008766
8767 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008768 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00008769 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +00008770 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00008771 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008772 }
Evan Cheng18efe262007-12-14 02:13:44 +00008773
Evan Cheng152804e2007-12-14 08:30:15 +00008774 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008775 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008776 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00008777
8778 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00008779 SDValue Ops[] = {
8780 Op,
8781 DAG.getConstant(NumBits+NumBits-1, OpVT),
8782 DAG.getConstant(X86::COND_E, MVT::i8),
8783 Op.getValue(1)
8784 };
8785 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00008786
8787 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00008788 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00008789
Owen Anderson825b72b2009-08-11 20:47:22 +00008790 if (VT == MVT::i8)
8791 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008792 return Op;
8793}
8794
Dan Gohmand858e902010-04-17 15:26:15 +00008795SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008796 EVT VT = Op.getValueType();
8797 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008798 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008799 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00008800
8801 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008802 if (VT == MVT::i8) {
8803 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00008804 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008805 }
Evan Cheng152804e2007-12-14 08:30:15 +00008806
8807 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008808 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008809 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00008810
8811 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00008812 SDValue Ops[] = {
8813 Op,
8814 DAG.getConstant(NumBits, OpVT),
8815 DAG.getConstant(X86::COND_E, MVT::i8),
8816 Op.getValue(1)
8817 };
8818 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00008819
Owen Anderson825b72b2009-08-11 20:47:22 +00008820 if (VT == MVT::i8)
8821 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008822 return Op;
8823}
8824
Dan Gohmand858e902010-04-17 15:26:15 +00008825SDValue X86TargetLowering::LowerMUL_V2I64(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008826 EVT VT = Op.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00008827 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008828 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00008829
Mon P Wangaf9b9522008-12-18 21:42:19 +00008830 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
8831 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
8832 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
8833 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
8834 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
8835 //
8836 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
8837 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
8838 // return AloBlo + AloBhi + AhiBlo;
8839
8840 SDValue A = Op.getOperand(0);
8841 SDValue B = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008842
Dale Johannesene4d209d2009-02-03 20:21:25 +00008843 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008844 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8845 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008846 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008847 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8848 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008849 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008850 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008851 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008852 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008853 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008854 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008855 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008856 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008857 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008858 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008859 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8860 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008861 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008862 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8863 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008864 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
8865 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +00008866 return Res;
8867}
8868
Nadav Rotem43012222011-05-11 08:12:09 +00008869SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
8870
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008871 EVT VT = Op.getValueType();
8872 DebugLoc dl = Op.getDebugLoc();
8873 SDValue R = Op.getOperand(0);
Nadav Rotem43012222011-05-11 08:12:09 +00008874 SDValue Amt = Op.getOperand(1);
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008875
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008876 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008877
Nadav Rotem43012222011-05-11 08:12:09 +00008878 // Must have SSE2.
8879 if (!Subtarget->hasSSE2()) return SDValue();
Nate Begeman51409212010-07-28 00:21:48 +00008880
Nadav Rotem43012222011-05-11 08:12:09 +00008881 // Optimize shl/srl/sra with constant shift amount.
8882 if (isSplatVector(Amt.getNode())) {
8883 SDValue SclrAmt = Amt->getOperand(0);
8884 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SclrAmt)) {
8885 uint64_t ShiftAmt = C->getZExtValue();
8886
8887 if (VT == MVT::v2i64 && Op.getOpcode() == ISD::SHL)
8888 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8889 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8890 R, DAG.getConstant(ShiftAmt, MVT::i32));
8891
8892 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SHL)
8893 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8894 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
8895 R, DAG.getConstant(ShiftAmt, MVT::i32));
8896
8897 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SHL)
8898 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8899 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
8900 R, DAG.getConstant(ShiftAmt, MVT::i32));
8901
8902 if (VT == MVT::v2i64 && Op.getOpcode() == ISD::SRL)
8903 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8904 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8905 R, DAG.getConstant(ShiftAmt, MVT::i32));
8906
8907 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SRL)
8908 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8909 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
8910 R, DAG.getConstant(ShiftAmt, MVT::i32));
8911
8912 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SRL)
8913 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8914 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
8915 R, DAG.getConstant(ShiftAmt, MVT::i32));
8916
8917 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SRA)
8918 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8919 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
8920 R, DAG.getConstant(ShiftAmt, MVT::i32));
8921
8922 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SRA)
8923 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8924 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
8925 R, DAG.getConstant(ShiftAmt, MVT::i32));
8926 }
8927 }
8928
8929 // Lower SHL with variable shift amount.
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00008930 // Cannot lower SHL without SSE2 or later.
8931 if (!Subtarget->hasSSE2()) return SDValue();
Nadav Rotem43012222011-05-11 08:12:09 +00008932
8933 if (VT == MVT::v4i32 && Op->getOpcode() == ISD::SHL) {
Nate Begeman51409212010-07-28 00:21:48 +00008934 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8935 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
8936 Op.getOperand(1), DAG.getConstant(23, MVT::i32));
8937
8938 ConstantInt *CI = ConstantInt::get(*Context, APInt(32, 0x3f800000U));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008939
Nate Begeman51409212010-07-28 00:21:48 +00008940 std::vector<Constant*> CV(4, CI);
8941 Constant *C = ConstantVector::get(CV);
8942 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8943 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008944 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00008945 false, false, 16);
8946
8947 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008948 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +00008949 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
8950 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
8951 }
Nadav Rotem43012222011-05-11 08:12:09 +00008952 if (VT == MVT::v16i8 && Op->getOpcode() == ISD::SHL) {
Nate Begeman51409212010-07-28 00:21:48 +00008953 // a = a << 5;
8954 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8955 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
8956 Op.getOperand(1), DAG.getConstant(5, MVT::i32));
8957
8958 ConstantInt *CM1 = ConstantInt::get(*Context, APInt(8, 15));
8959 ConstantInt *CM2 = ConstantInt::get(*Context, APInt(8, 63));
8960
8961 std::vector<Constant*> CVM1(16, CM1);
8962 std::vector<Constant*> CVM2(16, CM2);
8963 Constant *C = ConstantVector::get(CVM1);
8964 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8965 SDValue M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008966 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00008967 false, false, 16);
8968
8969 // r = pblendv(r, psllw(r & (char16)15, 4), a);
8970 M = DAG.getNode(ISD::AND, dl, VT, R, M);
8971 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8972 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
8973 DAG.getConstant(4, MVT::i32));
Nate Begeman672fb622010-12-20 22:04:24 +00008974 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT, R, M, Op);
Nate Begeman51409212010-07-28 00:21:48 +00008975 // a += a
8976 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008977
Nate Begeman51409212010-07-28 00:21:48 +00008978 C = ConstantVector::get(CVM2);
8979 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8980 M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008981 MachinePointerInfo::getConstantPool(),
Chris Lattner51abfe42010-09-21 06:02:19 +00008982 false, false, 16);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008983
Nate Begeman51409212010-07-28 00:21:48 +00008984 // r = pblendv(r, psllw(r & (char16)63, 2), a);
8985 M = DAG.getNode(ISD::AND, dl, VT, R, M);
8986 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8987 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
8988 DAG.getConstant(2, MVT::i32));
Nate Begeman672fb622010-12-20 22:04:24 +00008989 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT, R, M, Op);
Nate Begeman51409212010-07-28 00:21:48 +00008990 // a += a
8991 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008992
Nate Begeman51409212010-07-28 00:21:48 +00008993 // return pblendv(r, r+r, a);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008994 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT,
Nate Begeman51409212010-07-28 00:21:48 +00008995 R, DAG.getNode(ISD::ADD, dl, VT, R, R), Op);
8996 return R;
8997 }
8998 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008999}
Mon P Wangaf9b9522008-12-18 21:42:19 +00009000
Dan Gohmand858e902010-04-17 15:26:15 +00009001SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
Bill Wendling74c37652008-12-09 22:08:41 +00009002 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
9003 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +00009004 // looks for this combo and may remove the "setcc" instruction if the "setcc"
9005 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +00009006 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +00009007 SDValue LHS = N->getOperand(0);
9008 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +00009009 unsigned BaseOp = 0;
9010 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +00009011 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +00009012 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00009013 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +00009014 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +00009015 // A subtract of one will be selected as a INC. Note that INC doesn't
9016 // set CF, so we can't do this for UADDO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +00009017 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
9018 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +00009019 BaseOp = X86ISD::INC;
9020 Cond = X86::COND_O;
9021 break;
9022 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00009023 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +00009024 Cond = X86::COND_O;
9025 break;
9026 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00009027 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +00009028 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00009029 break;
9030 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +00009031 // A subtract of one will be selected as a DEC. Note that DEC doesn't
9032 // set CF, so we can't do this for USUBO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +00009033 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
9034 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +00009035 BaseOp = X86ISD::DEC;
9036 Cond = X86::COND_O;
9037 break;
9038 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00009039 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +00009040 Cond = X86::COND_O;
9041 break;
9042 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00009043 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +00009044 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00009045 break;
9046 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00009047 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +00009048 Cond = X86::COND_O;
9049 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +00009050 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
9051 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
9052 MVT::i32);
9053 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009054
Chris Lattnerb20e0b12010-12-05 07:30:36 +00009055 SDValue SetCC =
9056 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
9057 DAG.getConstant(X86::COND_O, MVT::i32),
9058 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009059
Chris Lattnerb20e0b12010-12-05 07:30:36 +00009060 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
9061 return Sum;
9062 }
Bill Wendling74c37652008-12-09 22:08:41 +00009063 }
Bill Wendling3fafd932008-11-26 22:37:40 +00009064
Bill Wendling61edeb52008-12-02 01:06:39 +00009065 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00009066 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +00009067 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +00009068
Bill Wendling61edeb52008-12-02 01:06:39 +00009069 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +00009070 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
9071 DAG.getConstant(Cond, MVT::i32),
9072 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +00009073
Bill Wendling61edeb52008-12-02 01:06:39 +00009074 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
9075 return Sum;
Bill Wendling41ea7e72008-11-24 19:21:46 +00009076}
9077
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00009078SDValue X86TargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op, SelectionDAG &DAG) const{
9079 DebugLoc dl = Op.getDebugLoc();
9080 SDNode* Node = Op.getNode();
9081 EVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
9082 EVT VT = Node->getValueType(0);
9083
9084 if (Subtarget->hasSSE2() && VT.isVector()) {
9085 unsigned BitsDiff = VT.getScalarType().getSizeInBits() -
9086 ExtraVT.getScalarType().getSizeInBits();
9087 SDValue ShAmt = DAG.getConstant(BitsDiff, MVT::i32);
9088
9089 unsigned SHLIntrinsicsID = 0;
9090 unsigned SRAIntrinsicsID = 0;
9091 switch (VT.getSimpleVT().SimpleTy) {
9092 default:
9093 return SDValue();
9094 case MVT::v2i64: {
9095 SHLIntrinsicsID = Intrinsic::x86_sse2_pslli_q;
9096 SRAIntrinsicsID = 0;
9097 break;
9098 }
9099 case MVT::v4i32: {
9100 SHLIntrinsicsID = Intrinsic::x86_sse2_pslli_d;
9101 SRAIntrinsicsID = Intrinsic::x86_sse2_psrai_d;
9102 break;
9103 }
9104 case MVT::v8i16: {
9105 SHLIntrinsicsID = Intrinsic::x86_sse2_pslli_w;
9106 SRAIntrinsicsID = Intrinsic::x86_sse2_psrai_w;
9107 break;
9108 }
9109 }
9110
9111 SDValue Tmp1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9112 DAG.getConstant(SHLIntrinsicsID, MVT::i32),
9113 Node->getOperand(0), ShAmt);
9114
9115 // In case of 1 bit sext, no need to shr
9116 if (ExtraVT.getScalarType().getSizeInBits() == 1) return Tmp1;
9117
9118 if (SRAIntrinsicsID) {
9119 Tmp1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9120 DAG.getConstant(SRAIntrinsicsID, MVT::i32),
9121 Tmp1, ShAmt);
9122 }
9123 return Tmp1;
9124 }
9125
9126 return SDValue();
9127}
9128
9129
Eric Christopher9a9d2752010-07-22 02:48:34 +00009130SDValue X86TargetLowering::LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG) const{
9131 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00009132
Eric Christopher77ed1352011-07-08 00:04:56 +00009133 // Go ahead and emit the fence on x86-64 even if we asked for no-sse2.
9134 // There isn't any reason to disable it if the target processor supports it.
9135 if (!Subtarget->hasSSE2() && !Subtarget->is64Bit()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +00009136 SDValue Chain = Op.getOperand(0);
Eric Christopher77ed1352011-07-08 00:04:56 +00009137 SDValue Zero = DAG.getConstant(0, MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +00009138 SDValue Ops[] = {
9139 DAG.getRegister(X86::ESP, MVT::i32), // Base
9140 DAG.getTargetConstant(1, MVT::i8), // Scale
9141 DAG.getRegister(0, MVT::i32), // Index
9142 DAG.getTargetConstant(0, MVT::i32), // Disp
9143 DAG.getRegister(0, MVT::i32), // Segment.
9144 Zero,
9145 Chain
9146 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00009147 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +00009148 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
9149 array_lengthof(Ops));
9150 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +00009151 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00009152
Eric Christopher9a9d2752010-07-22 02:48:34 +00009153 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +00009154 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +00009155 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009156
Chris Lattner132929a2010-08-14 17:26:09 +00009157 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
9158 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
9159 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
9160 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +00009161
Chris Lattner132929a2010-08-14 17:26:09 +00009162 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
9163 if (!Op1 && !Op2 && !Op3 && Op4)
9164 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009165
Chris Lattner132929a2010-08-14 17:26:09 +00009166 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
9167 if (Op1 && !Op2 && !Op3 && !Op4)
9168 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009169
9170 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +00009171 // (MFENCE)>;
9172 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +00009173}
9174
Dan Gohmand858e902010-04-17 15:26:15 +00009175SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009176 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +00009177 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +00009178 unsigned Reg = 0;
9179 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00009180 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00009181 default:
9182 assert(false && "Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +00009183 case MVT::i8: Reg = X86::AL; size = 1; break;
9184 case MVT::i16: Reg = X86::AX; size = 2; break;
9185 case MVT::i32: Reg = X86::EAX; size = 4; break;
9186 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +00009187 assert(Subtarget->is64Bit() && "Node not type legal!");
9188 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00009189 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00009190 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +00009191 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +00009192 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +00009193 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +00009194 Op.getOperand(1),
9195 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +00009196 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +00009197 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009198 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +00009199 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
9200 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
9201 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +00009202 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +00009203 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +00009204 return cpOut;
9205}
9206
Duncan Sands1607f052008-12-01 11:39:25 +00009207SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00009208 SelectionDAG &DAG) const {
Duncan Sands1607f052008-12-01 11:39:25 +00009209 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009210 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +00009211 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009212 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +00009213 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00009214 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
9215 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +00009216 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +00009217 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
9218 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +00009219 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +00009220 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +00009221 rdx.getValue(1)
9222 };
Dale Johannesene4d209d2009-02-03 20:21:25 +00009223 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009224}
9225
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009226SDValue X86TargetLowering::LowerBITCAST(SDValue Op,
Dale Johannesen7d07b482010-05-21 00:52:33 +00009227 SelectionDAG &DAG) const {
9228 EVT SrcVT = Op.getOperand(0).getValueType();
9229 EVT DstVT = Op.getValueType();
Chris Lattner2a786eb2010-12-19 20:19:20 +00009230 assert(Subtarget->is64Bit() && !Subtarget->hasSSE2() &&
9231 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +00009232 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +00009233 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009234 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +00009235 // i64 <=> MMX conversions are Legal.
9236 if (SrcVT==MVT::i64 && DstVT.isVector())
9237 return Op;
9238 if (DstVT==MVT::i64 && SrcVT.isVector())
9239 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +00009240 // MMX <=> MMX conversions are Legal.
9241 if (SrcVT.isVector() && DstVT.isVector())
9242 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +00009243 // All other conversions need to be expanded.
9244 return SDValue();
9245}
Chris Lattner5b856542010-12-20 00:59:46 +00009246
Dan Gohmand858e902010-04-17 15:26:15 +00009247SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen71d1bf52008-09-29 22:25:26 +00009248 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +00009249 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00009250 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009251 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +00009252 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009253 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009254 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +00009255 Node->getOperand(0),
9256 Node->getOperand(1), negOp,
9257 cast<AtomicSDNode>(Node)->getSrcValue(),
9258 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang63307c32008-05-05 19:05:59 +00009259}
9260
Chris Lattner5b856542010-12-20 00:59:46 +00009261static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
9262 EVT VT = Op.getNode()->getValueType(0);
9263
9264 // Let legalize expand this if it isn't a legal type yet.
9265 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
9266 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009267
Chris Lattner5b856542010-12-20 00:59:46 +00009268 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009269
Chris Lattner5b856542010-12-20 00:59:46 +00009270 unsigned Opc;
9271 bool ExtraOp = false;
9272 switch (Op.getOpcode()) {
9273 default: assert(0 && "Invalid code");
9274 case ISD::ADDC: Opc = X86ISD::ADD; break;
9275 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
9276 case ISD::SUBC: Opc = X86ISD::SUB; break;
9277 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
9278 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009279
Chris Lattner5b856542010-12-20 00:59:46 +00009280 if (!ExtraOp)
9281 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
9282 Op.getOperand(1));
9283 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
9284 Op.getOperand(1), Op.getOperand(2));
9285}
9286
Evan Cheng0db9fe62006-04-25 20:13:52 +00009287/// LowerOperation - Provide custom lowering hooks for some operations.
9288///
Dan Gohmand858e902010-04-17 15:26:15 +00009289SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00009290 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00009291 default: llvm_unreachable("Should not custom lower this!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00009292 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op,DAG);
Eric Christopher9a9d2752010-07-22 02:48:34 +00009293 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op,DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009294 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
9295 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009296 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00009297 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009298 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
9299 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
9300 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
David Greene91585092011-01-26 15:38:49 +00009301 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
David Greenecfe33c42011-01-26 19:13:22 +00009302 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009303 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
9304 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
9305 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00009306 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +00009307 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +00009308 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009309 case ISD::SHL_PARTS:
9310 case ISD::SRA_PARTS:
Nadav Rotem43012222011-05-11 08:12:09 +00009311 case ISD::SRL_PARTS: return LowerShiftParts(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009312 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00009313 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009314 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00009315 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009316 case ISD::FABS: return LowerFABS(Op, DAG);
9317 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00009318 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00009319 case ISD::FGETSIGN: return LowerFGETSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00009320 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman30a0de92008-07-17 16:51:19 +00009321 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00009322 case ISD::SELECT: return LowerSELECT(Op, DAG);
9323 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009324 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009325 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +00009326 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +00009327 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009328 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00009329 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
9330 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009331 case ISD::FRAME_TO_ARGS_OFFSET:
9332 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009333 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009334 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009335 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00009336 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +00009337 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
9338 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Mon P Wangaf9b9522008-12-18 21:42:19 +00009339 case ISD::MUL: return LowerMUL_V2I64(Op, DAG);
Nadav Rotem43012222011-05-11 08:12:09 +00009340 case ISD::SRA:
9341 case ISD::SRL:
9342 case ISD::SHL: return LowerShift(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +00009343 case ISD::SADDO:
9344 case ISD::UADDO:
9345 case ISD::SSUBO:
9346 case ISD::USUBO:
9347 case ISD::SMULO:
9348 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00009349 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009350 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +00009351 case ISD::ADDC:
9352 case ISD::ADDE:
9353 case ISD::SUBC:
9354 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009355 }
Chris Lattner27a6c732007-11-24 07:07:01 +00009356}
9357
Duncan Sands1607f052008-12-01 11:39:25 +00009358void X86TargetLowering::
9359ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00009360 SelectionDAG &DAG, unsigned NewOp) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009361 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009362 DebugLoc dl = Node->getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00009363 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +00009364
9365 SDValue Chain = Node->getOperand(0);
9366 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00009367 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00009368 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00009369 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00009370 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +00009371 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +00009372 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +00009373 SDValue Result =
9374 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
9375 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +00009376 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +00009377 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00009378 Results.push_back(Result.getValue(2));
9379}
9380
Duncan Sands126d9072008-07-04 11:47:58 +00009381/// ReplaceNodeResults - Replace a node with an illegal result type
9382/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00009383void X86TargetLowering::ReplaceNodeResults(SDNode *N,
9384 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00009385 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +00009386 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +00009387 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +00009388 default:
Duncan Sands1607f052008-12-01 11:39:25 +00009389 assert(false && "Do not know how to custom type legalize this operation!");
9390 return;
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00009391 case ISD::SIGN_EXTEND_INREG:
Chris Lattner5b856542010-12-20 00:59:46 +00009392 case ISD::ADDC:
9393 case ISD::ADDE:
9394 case ISD::SUBC:
9395 case ISD::SUBE:
9396 // We don't want to expand or promote these.
9397 return;
Duncan Sands1607f052008-12-01 11:39:25 +00009398 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +00009399 std::pair<SDValue,SDValue> Vals =
9400 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +00009401 SDValue FIST = Vals.first, StackSlot = Vals.second;
9402 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +00009403 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +00009404 // Return a load from the stack slot.
Chris Lattner51abfe42010-09-21 06:02:19 +00009405 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
9406 MachinePointerInfo(), false, false, 0));
Duncan Sands1607f052008-12-01 11:39:25 +00009407 }
9408 return;
9409 }
9410 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009411 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +00009412 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009413 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00009414 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +00009415 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00009416 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00009417 eax.getValue(2));
9418 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
9419 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +00009420 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00009421 Results.push_back(edx.getValue(1));
9422 return;
9423 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009424 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +00009425 EVT T = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009426 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Duncan Sands1607f052008-12-01 11:39:25 +00009427 SDValue cpInL, cpInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00009428 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
9429 DAG.getConstant(0, MVT::i32));
9430 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
9431 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00009432 cpInL = DAG.getCopyToReg(N->getOperand(0), dl, X86::EAX, cpInL, SDValue());
9433 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl, X86::EDX, cpInH,
Duncan Sands1607f052008-12-01 11:39:25 +00009434 cpInL.getValue(1));
9435 SDValue swapInL, swapInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00009436 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
9437 DAG.getConstant(0, MVT::i32));
9438 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
9439 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00009440 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl, X86::EBX, swapInL,
Duncan Sands1607f052008-12-01 11:39:25 +00009441 cpInH.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00009442 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl, X86::ECX, swapInH,
Duncan Sands1607f052008-12-01 11:39:25 +00009443 swapInL.getValue(1));
9444 SDValue Ops[] = { swapInH.getValue(0),
9445 N->getOperand(1),
9446 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009447 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +00009448 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
9449 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG8_DAG, dl, Tys,
9450 Ops, 3, T, MMO);
Dale Johannesendd64c412009-02-04 00:33:20 +00009451 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl, X86::EAX,
Owen Anderson825b72b2009-08-11 20:47:22 +00009452 MVT::i32, Result.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00009453 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl, X86::EDX,
Owen Anderson825b72b2009-08-11 20:47:22 +00009454 MVT::i32, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +00009455 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Owen Anderson825b72b2009-08-11 20:47:22 +00009456 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00009457 Results.push_back(cpOutH.getValue(1));
9458 return;
9459 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009460 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +00009461 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
9462 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009463 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +00009464 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
9465 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009466 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +00009467 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
9468 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009469 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +00009470 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
9471 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009472 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +00009473 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
9474 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009475 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +00009476 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
9477 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009478 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +00009479 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
9480 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00009481 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00009482}
9483
Evan Cheng72261582005-12-20 06:22:03 +00009484const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
9485 switch (Opcode) {
9486 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +00009487 case X86ISD::BSF: return "X86ISD::BSF";
9488 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +00009489 case X86ISD::SHLD: return "X86ISD::SHLD";
9490 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00009491 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00009492 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00009493 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00009494 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00009495 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00009496 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00009497 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
9498 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
9499 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00009500 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00009501 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +00009502 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +00009503 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +00009504 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +00009505 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00009506 case X86ISD::COMI: return "X86ISD::COMI";
9507 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00009508 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +00009509 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Stuart Hastings865f0932011-06-03 23:53:54 +00009510 case X86ISD::FSETCCsd: return "X86ISD::FSETCCsd";
9511 case X86ISD::FSETCCss: return "X86ISD::FSETCCss";
Evan Cheng72261582005-12-20 06:22:03 +00009512 case X86ISD::CMOV: return "X86ISD::CMOV";
9513 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00009514 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00009515 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
9516 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +00009517 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00009518 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +00009519 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +00009520 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +00009521 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +00009522 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
9523 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +00009524 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +00009525 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +00009526 case X86ISD::ANDNP: return "X86ISD::ANDNP";
Nate Begemanb65c1752010-12-17 22:55:37 +00009527 case X86ISD::PSIGNB: return "X86ISD::PSIGNB";
9528 case X86ISD::PSIGNW: return "X86ISD::PSIGNW";
9529 case X86ISD::PSIGND: return "X86ISD::PSIGND";
Nate Begeman672fb622010-12-20 22:04:24 +00009530 case X86ISD::PBLENDVB: return "X86ISD::PBLENDVB";
Evan Cheng8ca29322006-11-10 21:43:37 +00009531 case X86ISD::FMAX: return "X86ISD::FMAX";
9532 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +00009533 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
9534 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00009535 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +00009536 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009537 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00009538 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009539 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +00009540 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
9541 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009542 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
9543 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
9544 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
9545 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
9546 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
9547 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +00009548 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
9549 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +00009550 case X86ISD::VSHL: return "X86ISD::VSHL";
9551 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +00009552 case X86ISD::CMPPD: return "X86ISD::CMPPD";
9553 case X86ISD::CMPPS: return "X86ISD::CMPPS";
9554 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
9555 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
9556 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
9557 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
9558 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
9559 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
9560 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
9561 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +00009562 case X86ISD::ADD: return "X86ISD::ADD";
9563 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +00009564 case X86ISD::ADC: return "X86ISD::ADC";
9565 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +00009566 case X86ISD::SMUL: return "X86ISD::SMUL";
9567 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +00009568 case X86ISD::INC: return "X86ISD::INC";
9569 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +00009570 case X86ISD::OR: return "X86ISD::OR";
9571 case X86ISD::XOR: return "X86ISD::XOR";
9572 case X86ISD::AND: return "X86ISD::AND";
Evan Cheng73f24c92009-03-30 21:36:47 +00009573 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +00009574 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009575 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009576 case X86ISD::PALIGN: return "X86ISD::PALIGN";
9577 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
9578 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
9579 case X86ISD::PSHUFHW_LD: return "X86ISD::PSHUFHW_LD";
9580 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
9581 case X86ISD::PSHUFLW_LD: return "X86ISD::PSHUFLW_LD";
9582 case X86ISD::SHUFPS: return "X86ISD::SHUFPS";
9583 case X86ISD::SHUFPD: return "X86ISD::SHUFPD";
9584 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009585 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00009586 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009587 case X86ISD::MOVHLPD: return "X86ISD::MOVHLPD";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00009588 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
9589 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009590 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
9591 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
9592 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
9593 case X86ISD::MOVSHDUP_LD: return "X86ISD::MOVSHDUP_LD";
9594 case X86ISD::MOVSLDUP_LD: return "X86ISD::MOVSLDUP_LD";
9595 case X86ISD::MOVSD: return "X86ISD::MOVSD";
9596 case X86ISD::MOVSS: return "X86ISD::MOVSS";
9597 case X86ISD::UNPCKLPS: return "X86ISD::UNPCKLPS";
9598 case X86ISD::UNPCKLPD: return "X86ISD::UNPCKLPD";
David Greenefbf05d32011-02-22 23:31:46 +00009599 case X86ISD::VUNPCKLPS: return "X86ISD::VUNPCKLPS";
9600 case X86ISD::VUNPCKLPD: return "X86ISD::VUNPCKLPD";
9601 case X86ISD::VUNPCKLPSY: return "X86ISD::VUNPCKLPSY";
9602 case X86ISD::VUNPCKLPDY: return "X86ISD::VUNPCKLPDY";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009603 case X86ISD::UNPCKHPS: return "X86ISD::UNPCKHPS";
9604 case X86ISD::UNPCKHPD: return "X86ISD::UNPCKHPD";
9605 case X86ISD::PUNPCKLBW: return "X86ISD::PUNPCKLBW";
9606 case X86ISD::PUNPCKLWD: return "X86ISD::PUNPCKLWD";
9607 case X86ISD::PUNPCKLDQ: return "X86ISD::PUNPCKLDQ";
9608 case X86ISD::PUNPCKLQDQ: return "X86ISD::PUNPCKLQDQ";
9609 case X86ISD::PUNPCKHBW: return "X86ISD::PUNPCKHBW";
9610 case X86ISD::PUNPCKHWD: return "X86ISD::PUNPCKHWD";
9611 case X86ISD::PUNPCKHDQ: return "X86ISD::PUNPCKHDQ";
9612 case X86ISD::PUNPCKHQDQ: return "X86ISD::PUNPCKHQDQ";
Dan Gohmand6708ea2009-08-15 01:38:56 +00009613 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +00009614 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +00009615 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Evan Cheng72261582005-12-20 06:22:03 +00009616 }
9617}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00009618
Chris Lattnerc9addb72007-03-30 23:15:24 +00009619// isLegalAddressingMode - Return true if the addressing mode represented
9620// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +00009621bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerc9addb72007-03-30 23:15:24 +00009622 const Type *Ty) const {
9623 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009624 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +00009625 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +00009626
Chris Lattnerc9addb72007-03-30 23:15:24 +00009627 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009628 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +00009629 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00009630
Chris Lattnerc9addb72007-03-30 23:15:24 +00009631 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +00009632 unsigned GVFlags =
9633 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009634
Chris Lattnerdfed4132009-07-10 07:38:24 +00009635 // If a reference to this global requires an extra load, we can't fold it.
9636 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +00009637 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009638
Chris Lattnerdfed4132009-07-10 07:38:24 +00009639 // If BaseGV requires a register for the PIC base, we cannot also have a
9640 // BaseReg specified.
9641 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +00009642 return false;
Evan Cheng52787842007-08-01 23:46:47 +00009643
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009644 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +00009645 if ((M != CodeModel::Small || R != Reloc::Static) &&
9646 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009647 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +00009648 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009649
Chris Lattnerc9addb72007-03-30 23:15:24 +00009650 switch (AM.Scale) {
9651 case 0:
9652 case 1:
9653 case 2:
9654 case 4:
9655 case 8:
9656 // These scales always work.
9657 break;
9658 case 3:
9659 case 5:
9660 case 9:
9661 // These scales are formed with basereg+scalereg. Only accept if there is
9662 // no basereg yet.
9663 if (AM.HasBaseReg)
9664 return false;
9665 break;
9666 default: // Other stuff never works.
9667 return false;
9668 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009669
Chris Lattnerc9addb72007-03-30 23:15:24 +00009670 return true;
9671}
9672
9673
Evan Cheng2bd122c2007-10-26 01:56:11 +00009674bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009675 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +00009676 return false;
Evan Chenge127a732007-10-29 07:57:50 +00009677 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
9678 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00009679 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +00009680 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00009681 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +00009682}
9683
Owen Andersone50ed302009-08-10 22:56:29 +00009684bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +00009685 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009686 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009687 unsigned NumBits1 = VT1.getSizeInBits();
9688 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00009689 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009690 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00009691 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009692}
Evan Cheng2bd122c2007-10-26 01:56:11 +00009693
Dan Gohman97121ba2009-04-08 00:15:30 +00009694bool X86TargetLowering::isZExtFree(const Type *Ty1, const Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00009695 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009696 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00009697}
9698
Owen Andersone50ed302009-08-10 22:56:29 +00009699bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00009700 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00009701 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00009702}
9703
Owen Andersone50ed302009-08-10 22:56:29 +00009704bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +00009705 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +00009706 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +00009707}
9708
Evan Cheng60c07e12006-07-05 22:17:51 +00009709/// isShuffleMaskLegal - Targets can use this to indicate that they only
9710/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
9711/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
9712/// are assumed to be legal.
9713bool
Eric Christopherfd179292009-08-27 18:07:15 +00009714X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +00009715 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +00009716 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +00009717 if (VT.getSizeInBits() == 64)
Eric Christophercff6f852010-04-15 01:40:20 +00009718 return isPALIGNRMask(M, VT, Subtarget->hasSSSE3());
Nate Begeman9008ca62009-04-27 18:41:29 +00009719
Nate Begemana09008b2009-10-19 02:17:23 +00009720 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +00009721 return (VT.getVectorNumElements() == 2 ||
9722 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
9723 isMOVLMask(M, VT) ||
9724 isSHUFPMask(M, VT) ||
9725 isPSHUFDMask(M, VT) ||
9726 isPSHUFHWMask(M, VT) ||
9727 isPSHUFLWMask(M, VT) ||
Nate Begemana09008b2009-10-19 02:17:23 +00009728 isPALIGNRMask(M, VT, Subtarget->hasSSSE3()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +00009729 isUNPCKLMask(M, VT) ||
9730 isUNPCKHMask(M, VT) ||
9731 isUNPCKL_v_undef_Mask(M, VT) ||
9732 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00009733}
9734
Dan Gohman7d8143f2008-04-09 20:09:42 +00009735bool
Nate Begeman5a5ca152009-04-29 05:20:52 +00009736X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +00009737 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00009738 unsigned NumElts = VT.getVectorNumElements();
9739 // FIXME: This collection of masks seems suspect.
9740 if (NumElts == 2)
9741 return true;
9742 if (NumElts == 4 && VT.getSizeInBits() == 128) {
9743 return (isMOVLMask(Mask, VT) ||
9744 isCommutedMOVLMask(Mask, VT, true) ||
9745 isSHUFPMask(Mask, VT) ||
9746 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00009747 }
9748 return false;
9749}
9750
9751//===----------------------------------------------------------------------===//
9752// X86 Scheduler Hooks
9753//===----------------------------------------------------------------------===//
9754
Mon P Wang63307c32008-05-05 19:05:59 +00009755// private utility function
9756MachineBasicBlock *
9757X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
9758 MachineBasicBlock *MBB,
9759 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009760 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009761 unsigned LoadOpc,
9762 unsigned CXchgOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009763 unsigned notOpc,
9764 unsigned EAXreg,
9765 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009766 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00009767 // For the atomic bitwise operator, we generate
9768 // thisMBB:
9769 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00009770 // ld t1 = [bitinstr.addr]
9771 // op t2 = t1, [bitinstr.val]
9772 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00009773 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
9774 // bz newMBB
9775 // fallthrough -->nextMBB
9776 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9777 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009778 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00009779 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009780
Mon P Wang63307c32008-05-05 19:05:59 +00009781 /// First build the CFG
9782 MachineFunction *F = MBB->getParent();
9783 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009784 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9785 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9786 F->insert(MBBIter, newMBB);
9787 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009788
Dan Gohman14152b42010-07-06 20:24:04 +00009789 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9790 nextMBB->splice(nextMBB->begin(), thisMBB,
9791 llvm::next(MachineBasicBlock::iterator(bInstr)),
9792 thisMBB->end());
9793 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009794
Mon P Wang63307c32008-05-05 19:05:59 +00009795 // Update thisMBB to fall through to newMBB
9796 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009797
Mon P Wang63307c32008-05-05 19:05:59 +00009798 // newMBB jumps to itself and fall through to nextMBB
9799 newMBB->addSuccessor(nextMBB);
9800 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009801
Mon P Wang63307c32008-05-05 19:05:59 +00009802 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009803 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009804 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +00009805 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00009806 MachineOperand& destOper = bInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009807 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00009808 int numArgs = bInstr->getNumOperands() - 1;
9809 for (int i=0; i < numArgs; ++i)
9810 argOpers[i] = &bInstr->getOperand(i+1);
9811
9812 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009813 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009814 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00009815
Dale Johannesen140be2d2008-08-19 18:47:28 +00009816 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009817 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00009818 for (int i=0; i <= lastAddrIndx; ++i)
9819 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009820
Dale Johannesen140be2d2008-08-19 18:47:28 +00009821 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009822 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00009823 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009824 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009825 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009826 tt = t1;
9827
Dale Johannesen140be2d2008-08-19 18:47:28 +00009828 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +00009829 assert((argOpers[valArgIndx]->isReg() ||
9830 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00009831 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +00009832 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009833 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00009834 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009835 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009836 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +00009837 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009838
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009839 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +00009840 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009841
Dale Johannesene4d209d2009-02-03 20:21:25 +00009842 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +00009843 for (int i=0; i <= lastAddrIndx; ++i)
9844 (*MIB).addOperand(*argOpers[i]);
9845 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +00009846 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009847 (*MIB).setMemRefs(bInstr->memoperands_begin(),
9848 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +00009849
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009850 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +00009851 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +00009852
Mon P Wang63307c32008-05-05 19:05:59 +00009853 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009854 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00009855
Dan Gohman14152b42010-07-06 20:24:04 +00009856 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00009857 return nextMBB;
9858}
9859
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00009860// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +00009861MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009862X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
9863 MachineBasicBlock *MBB,
9864 unsigned regOpcL,
9865 unsigned regOpcH,
9866 unsigned immOpcL,
9867 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009868 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009869 // For the atomic bitwise operator, we generate
9870 // thisMBB (instructions are in pairs, except cmpxchg8b)
9871 // ld t1,t2 = [bitinstr.addr]
9872 // newMBB:
9873 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
9874 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +00009875 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009876 // mov ECX, EBX <- t5, t6
9877 // mov EAX, EDX <- t1, t2
9878 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
9879 // mov t3, t4 <- EAX, EDX
9880 // bz newMBB
9881 // result in out1, out2
9882 // fallthrough -->nextMBB
9883
9884 const TargetRegisterClass *RC = X86::GR32RegisterClass;
9885 const unsigned LoadOpc = X86::MOV32rm;
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009886 const unsigned NotOpc = X86::NOT32r;
9887 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9888 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
9889 MachineFunction::iterator MBBIter = MBB;
9890 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009891
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009892 /// First build the CFG
9893 MachineFunction *F = MBB->getParent();
9894 MachineBasicBlock *thisMBB = MBB;
9895 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9896 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9897 F->insert(MBBIter, newMBB);
9898 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009899
Dan Gohman14152b42010-07-06 20:24:04 +00009900 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9901 nextMBB->splice(nextMBB->begin(), thisMBB,
9902 llvm::next(MachineBasicBlock::iterator(bInstr)),
9903 thisMBB->end());
9904 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009905
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009906 // Update thisMBB to fall through to newMBB
9907 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009908
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009909 // newMBB jumps to itself and fall through to nextMBB
9910 newMBB->addSuccessor(nextMBB);
9911 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009912
Dale Johannesene4d209d2009-02-03 20:21:25 +00009913 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009914 // Insert instructions into newMBB based on incoming instruction
9915 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009916 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009917 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009918 MachineOperand& dest1Oper = bInstr->getOperand(0);
9919 MachineOperand& dest2Oper = bInstr->getOperand(1);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009920 MachineOperand* argOpers[2 + X86::AddrNumOperands];
9921 for (int i=0; i < 2 + X86::AddrNumOperands; ++i) {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009922 argOpers[i] = &bInstr->getOperand(i+2);
9923
Dan Gohman71ea4e52010-05-14 21:01:44 +00009924 // We use some of the operands multiple times, so conservatively just
9925 // clear any kill flags that might be present.
9926 if (argOpers[i]->isReg() && argOpers[i]->isUse())
9927 argOpers[i]->setIsKill(false);
9928 }
9929
Evan Chengad5b52f2010-01-08 19:14:57 +00009930 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009931 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +00009932
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009933 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009934 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009935 for (int i=0; i <= lastAddrIndx; ++i)
9936 (*MIB).addOperand(*argOpers[i]);
9937 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009938 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +00009939 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +00009940 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009941 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +00009942 MachineOperand newOp3 = *(argOpers[3]);
9943 if (newOp3.isImm())
9944 newOp3.setImm(newOp3.getImm()+4);
9945 else
9946 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009947 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +00009948 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009949
9950 // t3/4 are defined later, at the bottom of the loop
9951 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
9952 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009953 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009954 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009955 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009956 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
9957
Evan Cheng306b4ca2010-01-08 23:41:50 +00009958 // The subsequent operations should be using the destination registers of
9959 //the PHI instructions.
Scott Michelfdc40a02009-02-17 22:15:04 +00009960 if (invSrc) {
Evan Cheng306b4ca2010-01-08 23:41:50 +00009961 t1 = F->getRegInfo().createVirtualRegister(RC);
9962 t2 = F->getRegInfo().createVirtualRegister(RC);
9963 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
9964 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009965 } else {
Evan Cheng306b4ca2010-01-08 23:41:50 +00009966 t1 = dest1Oper.getReg();
9967 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009968 }
9969
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009970 int valArgIndx = lastAddrIndx + 1;
9971 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +00009972 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009973 "invalid operand");
9974 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
9975 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009976 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009977 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009978 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009979 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +00009980 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00009981 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009982 (*MIB).addOperand(*argOpers[valArgIndx]);
9983 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00009984 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009985 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00009986 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009987 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009988 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009989 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009990 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +00009991 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00009992 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009993 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009994
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009995 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009996 MIB.addReg(t1);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009997 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009998 MIB.addReg(t2);
9999
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010000 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010001 MIB.addReg(t5);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010002 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010003 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +000010004
Dale Johannesene4d209d2009-02-03 20:21:25 +000010005 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010006 for (int i=0; i <= lastAddrIndx; ++i)
10007 (*MIB).addOperand(*argOpers[i]);
10008
10009 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000010010 (*MIB).setMemRefs(bInstr->memoperands_begin(),
10011 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010012
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010013 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010014 MIB.addReg(X86::EAX);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010015 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010016 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +000010017
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010018 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000010019 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010020
Dan Gohman14152b42010-07-06 20:24:04 +000010021 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010022 return nextMBB;
10023}
10024
10025// private utility function
10026MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +000010027X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
10028 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000010029 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +000010030 // For the atomic min/max operator, we generate
10031 // thisMBB:
10032 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +000010033 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +000010034 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +000010035 // cmp t1, t2
10036 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +000010037 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +000010038 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
10039 // bz newMBB
10040 // fallthrough -->nextMBB
10041 //
10042 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10043 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +000010044 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +000010045 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000010046
Mon P Wang63307c32008-05-05 19:05:59 +000010047 /// First build the CFG
10048 MachineFunction *F = MBB->getParent();
10049 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +000010050 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
10051 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
10052 F->insert(MBBIter, newMBB);
10053 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010054
Dan Gohman14152b42010-07-06 20:24:04 +000010055 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
10056 nextMBB->splice(nextMBB->begin(), thisMBB,
10057 llvm::next(MachineBasicBlock::iterator(mInstr)),
10058 thisMBB->end());
10059 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010060
Mon P Wang63307c32008-05-05 19:05:59 +000010061 // Update thisMBB to fall through to newMBB
10062 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010063
Mon P Wang63307c32008-05-05 19:05:59 +000010064 // newMBB jumps to newMBB and fall through to nextMBB
10065 newMBB->addSuccessor(nextMBB);
10066 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010067
Dale Johannesene4d209d2009-02-03 20:21:25 +000010068 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +000010069 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010070 assert(mInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000010071 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +000010072 MachineOperand& destOper = mInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010073 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +000010074 int numArgs = mInstr->getNumOperands() - 1;
10075 for (int i=0; i < numArgs; ++i)
10076 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +000010077
Mon P Wang63307c32008-05-05 19:05:59 +000010078 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010079 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000010080 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +000010081
Mon P Wangab3e7472008-05-05 22:56:23 +000010082 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010083 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +000010084 for (int i=0; i <= lastAddrIndx; ++i)
10085 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +000010086
Mon P Wang63307c32008-05-05 19:05:59 +000010087 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +000010088 assert((argOpers[valArgIndx]->isReg() ||
10089 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +000010090 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +000010091
10092 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +000010093 if (argOpers[valArgIndx]->isReg())
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010094 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +000010095 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000010096 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +000010097 (*MIB).addOperand(*argOpers[valArgIndx]);
10098
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010099 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +000010100 MIB.addReg(t1);
10101
Dale Johannesene4d209d2009-02-03 20:21:25 +000010102 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +000010103 MIB.addReg(t1);
10104 MIB.addReg(t2);
10105
10106 // Generate movc
10107 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010108 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +000010109 MIB.addReg(t2);
10110 MIB.addReg(t1);
10111
10112 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +000010113 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +000010114 for (int i=0; i <= lastAddrIndx; ++i)
10115 (*MIB).addOperand(*argOpers[i]);
10116 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +000010117 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000010118 (*MIB).setMemRefs(mInstr->memoperands_begin(),
10119 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +000010120
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010121 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +000010122 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +000010123
Mon P Wang63307c32008-05-05 19:05:59 +000010124 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000010125 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000010126
Dan Gohman14152b42010-07-06 20:24:04 +000010127 mInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +000010128 return nextMBB;
10129}
10130
Eric Christopherf83a5de2009-08-27 18:08:16 +000010131// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010132// or XMM0_V32I8 in AVX all of this code can be replaced with that
10133// in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +000010134MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +000010135X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +000010136 unsigned numArgs, bool memArg) const {
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010137 assert((Subtarget->hasSSE42() || Subtarget->hasAVX()) &&
10138 "Target must have SSE4.2 or AVX features enabled");
10139
Eric Christopherb120ab42009-08-18 22:50:32 +000010140 DebugLoc dl = MI->getDebugLoc();
10141 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Eric Christopherb120ab42009-08-18 22:50:32 +000010142 unsigned Opc;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010143 if (!Subtarget->hasAVX()) {
10144 if (memArg)
10145 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
10146 else
10147 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
10148 } else {
10149 if (memArg)
10150 Opc = numArgs == 3 ? X86::VPCMPISTRM128rm : X86::VPCMPESTRM128rm;
10151 else
10152 Opc = numArgs == 3 ? X86::VPCMPISTRM128rr : X86::VPCMPESTRM128rr;
10153 }
Eric Christopherb120ab42009-08-18 22:50:32 +000010154
Eric Christopher41c902f2010-11-30 08:20:21 +000010155 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Eric Christopherb120ab42009-08-18 22:50:32 +000010156 for (unsigned i = 0; i < numArgs; ++i) {
10157 MachineOperand &Op = MI->getOperand(i+1);
Eric Christopherb120ab42009-08-18 22:50:32 +000010158 if (!(Op.isReg() && Op.isImplicit()))
10159 MIB.addOperand(Op);
10160 }
Eric Christopher41c902f2010-11-30 08:20:21 +000010161 BuildMI(*BB, MI, dl, TII->get(X86::MOVAPSrr), MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +000010162 .addReg(X86::XMM0);
10163
Dan Gohman14152b42010-07-06 20:24:04 +000010164 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +000010165 return BB;
10166}
10167
10168MachineBasicBlock *
Eric Christopher228232b2010-11-30 07:20:12 +000010169X86TargetLowering::EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000010170 DebugLoc dl = MI->getDebugLoc();
10171 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010172
Eric Christopher228232b2010-11-30 07:20:12 +000010173 // Address into RAX/EAX, other two args into ECX, EDX.
10174 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
10175 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
10176 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
10177 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +000010178 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010179
Eric Christopher228232b2010-11-30 07:20:12 +000010180 unsigned ValOps = X86::AddrNumOperands;
10181 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
10182 .addReg(MI->getOperand(ValOps).getReg());
10183 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
10184 .addReg(MI->getOperand(ValOps+1).getReg());
10185
10186 // The instruction doesn't actually take any operands though.
10187 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010188
Eric Christopher228232b2010-11-30 07:20:12 +000010189 MI->eraseFromParent(); // The pseudo is gone now.
10190 return BB;
10191}
10192
10193MachineBasicBlock *
10194X86TargetLowering::EmitMwait(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000010195 DebugLoc dl = MI->getDebugLoc();
10196 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010197
Eric Christopher228232b2010-11-30 07:20:12 +000010198 // First arg in ECX, the second in EAX.
10199 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
10200 .addReg(MI->getOperand(0).getReg());
10201 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EAX)
10202 .addReg(MI->getOperand(1).getReg());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010203
Eric Christopher228232b2010-11-30 07:20:12 +000010204 // The instruction doesn't actually take any operands though.
10205 BuildMI(*BB, MI, dl, TII->get(X86::MWAITrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010206
Eric Christopher228232b2010-11-30 07:20:12 +000010207 MI->eraseFromParent(); // The pseudo is gone now.
10208 return BB;
10209}
10210
10211MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +000010212X86TargetLowering::EmitVAARG64WithCustomInserter(
10213 MachineInstr *MI,
10214 MachineBasicBlock *MBB) const {
10215 // Emit va_arg instruction on X86-64.
10216
10217 // Operands to this pseudo-instruction:
10218 // 0 ) Output : destination address (reg)
10219 // 1-5) Input : va_list address (addr, i64mem)
10220 // 6 ) ArgSize : Size (in bytes) of vararg type
10221 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
10222 // 8 ) Align : Alignment of type
10223 // 9 ) EFLAGS (implicit-def)
10224
10225 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
10226 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
10227
10228 unsigned DestReg = MI->getOperand(0).getReg();
10229 MachineOperand &Base = MI->getOperand(1);
10230 MachineOperand &Scale = MI->getOperand(2);
10231 MachineOperand &Index = MI->getOperand(3);
10232 MachineOperand &Disp = MI->getOperand(4);
10233 MachineOperand &Segment = MI->getOperand(5);
10234 unsigned ArgSize = MI->getOperand(6).getImm();
10235 unsigned ArgMode = MI->getOperand(7).getImm();
10236 unsigned Align = MI->getOperand(8).getImm();
10237
10238 // Memory Reference
10239 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
10240 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
10241 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
10242
10243 // Machine Information
10244 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10245 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
10246 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
10247 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
10248 DebugLoc DL = MI->getDebugLoc();
10249
10250 // struct va_list {
10251 // i32 gp_offset
10252 // i32 fp_offset
10253 // i64 overflow_area (address)
10254 // i64 reg_save_area (address)
10255 // }
10256 // sizeof(va_list) = 24
10257 // alignment(va_list) = 8
10258
10259 unsigned TotalNumIntRegs = 6;
10260 unsigned TotalNumXMMRegs = 8;
10261 bool UseGPOffset = (ArgMode == 1);
10262 bool UseFPOffset = (ArgMode == 2);
10263 unsigned MaxOffset = TotalNumIntRegs * 8 +
10264 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
10265
10266 /* Align ArgSize to a multiple of 8 */
10267 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
10268 bool NeedsAlign = (Align > 8);
10269
10270 MachineBasicBlock *thisMBB = MBB;
10271 MachineBasicBlock *overflowMBB;
10272 MachineBasicBlock *offsetMBB;
10273 MachineBasicBlock *endMBB;
10274
10275 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
10276 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
10277 unsigned OffsetReg = 0;
10278
10279 if (!UseGPOffset && !UseFPOffset) {
10280 // If we only pull from the overflow region, we don't create a branch.
10281 // We don't need to alter control flow.
10282 OffsetDestReg = 0; // unused
10283 OverflowDestReg = DestReg;
10284
10285 offsetMBB = NULL;
10286 overflowMBB = thisMBB;
10287 endMBB = thisMBB;
10288 } else {
10289 // First emit code to check if gp_offset (or fp_offset) is below the bound.
10290 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
10291 // If not, pull from overflow_area. (branch to overflowMBB)
10292 //
10293 // thisMBB
10294 // | .
10295 // | .
10296 // offsetMBB overflowMBB
10297 // | .
10298 // | .
10299 // endMBB
10300
10301 // Registers for the PHI in endMBB
10302 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
10303 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
10304
10305 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
10306 MachineFunction *MF = MBB->getParent();
10307 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
10308 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
10309 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
10310
10311 MachineFunction::iterator MBBIter = MBB;
10312 ++MBBIter;
10313
10314 // Insert the new basic blocks
10315 MF->insert(MBBIter, offsetMBB);
10316 MF->insert(MBBIter, overflowMBB);
10317 MF->insert(MBBIter, endMBB);
10318
10319 // Transfer the remainder of MBB and its successor edges to endMBB.
10320 endMBB->splice(endMBB->begin(), thisMBB,
10321 llvm::next(MachineBasicBlock::iterator(MI)),
10322 thisMBB->end());
10323 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
10324
10325 // Make offsetMBB and overflowMBB successors of thisMBB
10326 thisMBB->addSuccessor(offsetMBB);
10327 thisMBB->addSuccessor(overflowMBB);
10328
10329 // endMBB is a successor of both offsetMBB and overflowMBB
10330 offsetMBB->addSuccessor(endMBB);
10331 overflowMBB->addSuccessor(endMBB);
10332
10333 // Load the offset value into a register
10334 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
10335 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
10336 .addOperand(Base)
10337 .addOperand(Scale)
10338 .addOperand(Index)
10339 .addDisp(Disp, UseFPOffset ? 4 : 0)
10340 .addOperand(Segment)
10341 .setMemRefs(MMOBegin, MMOEnd);
10342
10343 // Check if there is enough room left to pull this argument.
10344 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
10345 .addReg(OffsetReg)
10346 .addImm(MaxOffset + 8 - ArgSizeA8);
10347
10348 // Branch to "overflowMBB" if offset >= max
10349 // Fall through to "offsetMBB" otherwise
10350 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
10351 .addMBB(overflowMBB);
10352 }
10353
10354 // In offsetMBB, emit code to use the reg_save_area.
10355 if (offsetMBB) {
10356 assert(OffsetReg != 0);
10357
10358 // Read the reg_save_area address.
10359 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
10360 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
10361 .addOperand(Base)
10362 .addOperand(Scale)
10363 .addOperand(Index)
10364 .addDisp(Disp, 16)
10365 .addOperand(Segment)
10366 .setMemRefs(MMOBegin, MMOEnd);
10367
10368 // Zero-extend the offset
10369 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
10370 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
10371 .addImm(0)
10372 .addReg(OffsetReg)
10373 .addImm(X86::sub_32bit);
10374
10375 // Add the offset to the reg_save_area to get the final address.
10376 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
10377 .addReg(OffsetReg64)
10378 .addReg(RegSaveReg);
10379
10380 // Compute the offset for the next argument
10381 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
10382 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
10383 .addReg(OffsetReg)
10384 .addImm(UseFPOffset ? 16 : 8);
10385
10386 // Store it back into the va_list.
10387 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
10388 .addOperand(Base)
10389 .addOperand(Scale)
10390 .addOperand(Index)
10391 .addDisp(Disp, UseFPOffset ? 4 : 0)
10392 .addOperand(Segment)
10393 .addReg(NextOffsetReg)
10394 .setMemRefs(MMOBegin, MMOEnd);
10395
10396 // Jump to endMBB
10397 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
10398 .addMBB(endMBB);
10399 }
10400
10401 //
10402 // Emit code to use overflow area
10403 //
10404
10405 // Load the overflow_area address into a register.
10406 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
10407 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
10408 .addOperand(Base)
10409 .addOperand(Scale)
10410 .addOperand(Index)
10411 .addDisp(Disp, 8)
10412 .addOperand(Segment)
10413 .setMemRefs(MMOBegin, MMOEnd);
10414
10415 // If we need to align it, do so. Otherwise, just copy the address
10416 // to OverflowDestReg.
10417 if (NeedsAlign) {
10418 // Align the overflow address
10419 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
10420 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
10421
10422 // aligned_addr = (addr + (align-1)) & ~(align-1)
10423 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
10424 .addReg(OverflowAddrReg)
10425 .addImm(Align-1);
10426
10427 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
10428 .addReg(TmpReg)
10429 .addImm(~(uint64_t)(Align-1));
10430 } else {
10431 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
10432 .addReg(OverflowAddrReg);
10433 }
10434
10435 // Compute the next overflow address after this argument.
10436 // (the overflow address should be kept 8-byte aligned)
10437 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
10438 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
10439 .addReg(OverflowDestReg)
10440 .addImm(ArgSizeA8);
10441
10442 // Store the new overflow address.
10443 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
10444 .addOperand(Base)
10445 .addOperand(Scale)
10446 .addOperand(Index)
10447 .addDisp(Disp, 8)
10448 .addOperand(Segment)
10449 .addReg(NextAddrReg)
10450 .setMemRefs(MMOBegin, MMOEnd);
10451
10452 // If we branched, emit the PHI to the front of endMBB.
10453 if (offsetMBB) {
10454 BuildMI(*endMBB, endMBB->begin(), DL,
10455 TII->get(X86::PHI), DestReg)
10456 .addReg(OffsetDestReg).addMBB(offsetMBB)
10457 .addReg(OverflowDestReg).addMBB(overflowMBB);
10458 }
10459
10460 // Erase the pseudo instruction
10461 MI->eraseFromParent();
10462
10463 return endMBB;
10464}
10465
10466MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +000010467X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
10468 MachineInstr *MI,
10469 MachineBasicBlock *MBB) const {
10470 // Emit code to save XMM registers to the stack. The ABI says that the
10471 // number of registers to save is given in %al, so it's theoretically
10472 // possible to do an indirect jump trick to avoid saving all of them,
10473 // however this code takes a simpler approach and just executes all
10474 // of the stores if %al is non-zero. It's less code, and it's probably
10475 // easier on the hardware branch predictor, and stores aren't all that
10476 // expensive anyway.
10477
10478 // Create the new basic blocks. One block contains all the XMM stores,
10479 // and one block is the final destination regardless of whether any
10480 // stores were performed.
10481 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
10482 MachineFunction *F = MBB->getParent();
10483 MachineFunction::iterator MBBIter = MBB;
10484 ++MBBIter;
10485 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
10486 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
10487 F->insert(MBBIter, XMMSaveMBB);
10488 F->insert(MBBIter, EndMBB);
10489
Dan Gohman14152b42010-07-06 20:24:04 +000010490 // Transfer the remainder of MBB and its successor edges to EndMBB.
10491 EndMBB->splice(EndMBB->begin(), MBB,
10492 llvm::next(MachineBasicBlock::iterator(MI)),
10493 MBB->end());
10494 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
10495
Dan Gohmand6708ea2009-08-15 01:38:56 +000010496 // The original block will now fall through to the XMM save block.
10497 MBB->addSuccessor(XMMSaveMBB);
10498 // The XMMSaveMBB will fall through to the end block.
10499 XMMSaveMBB->addSuccessor(EndMBB);
10500
10501 // Now add the instructions.
10502 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10503 DebugLoc DL = MI->getDebugLoc();
10504
10505 unsigned CountReg = MI->getOperand(0).getReg();
10506 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
10507 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
10508
10509 if (!Subtarget->isTargetWin64()) {
10510 // If %al is 0, branch around the XMM save block.
10511 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +000010512 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010513 MBB->addSuccessor(EndMBB);
10514 }
10515
10516 // In the XMM save block, save all the XMM argument registers.
10517 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
10518 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +000010519 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +000010520 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +000010521 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +000010522 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +000010523 /*Size=*/16, /*Align=*/16);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010524 BuildMI(XMMSaveMBB, DL, TII->get(X86::MOVAPSmr))
10525 .addFrameIndex(RegSaveFrameIndex)
10526 .addImm(/*Scale=*/1)
10527 .addReg(/*IndexReg=*/0)
10528 .addImm(/*Disp=*/Offset)
10529 .addReg(/*Segment=*/0)
10530 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +000010531 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010532 }
10533
Dan Gohman14152b42010-07-06 20:24:04 +000010534 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +000010535
10536 return EndMBB;
10537}
Mon P Wang63307c32008-05-05 19:05:59 +000010538
Evan Cheng60c07e12006-07-05 22:17:51 +000010539MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +000010540X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010541 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +000010542 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10543 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +000010544
Chris Lattner52600972009-09-02 05:57:00 +000010545 // To "insert" a SELECT_CC instruction, we actually have to insert the
10546 // diamond control-flow pattern. The incoming instruction knows the
10547 // destination vreg to set, the condition code register to branch on, the
10548 // true/false values to select between, and a branch opcode to use.
10549 const BasicBlock *LLVM_BB = BB->getBasicBlock();
10550 MachineFunction::iterator It = BB;
10551 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +000010552
Chris Lattner52600972009-09-02 05:57:00 +000010553 // thisMBB:
10554 // ...
10555 // TrueVal = ...
10556 // cmpTY ccX, r1, r2
10557 // bCC copy1MBB
10558 // fallthrough --> copy0MBB
10559 MachineBasicBlock *thisMBB = BB;
10560 MachineFunction *F = BB->getParent();
10561 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
10562 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +000010563 F->insert(It, copy0MBB);
10564 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +000010565
Bill Wendling730c07e2010-06-25 20:48:10 +000010566 // If the EFLAGS register isn't dead in the terminator, then claim that it's
10567 // live into the sink and copy blocks.
10568 const MachineFunction *MF = BB->getParent();
10569 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
10570 BitVector ReservedRegs = TRI->getReservedRegs(*MF);
Bill Wendling730c07e2010-06-25 20:48:10 +000010571
Dan Gohman14152b42010-07-06 20:24:04 +000010572 for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) {
10573 const MachineOperand &MO = MI->getOperand(I);
10574 if (!MO.isReg() || !MO.isUse() || MO.isKill()) continue;
Bill Wendling730c07e2010-06-25 20:48:10 +000010575 unsigned Reg = MO.getReg();
10576 if (Reg != X86::EFLAGS) continue;
10577 copy0MBB->addLiveIn(Reg);
10578 sinkMBB->addLiveIn(Reg);
10579 }
10580
Dan Gohman14152b42010-07-06 20:24:04 +000010581 // Transfer the remainder of BB and its successor edges to sinkMBB.
10582 sinkMBB->splice(sinkMBB->begin(), BB,
10583 llvm::next(MachineBasicBlock::iterator(MI)),
10584 BB->end());
10585 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
10586
10587 // Add the true and fallthrough blocks as its successors.
10588 BB->addSuccessor(copy0MBB);
10589 BB->addSuccessor(sinkMBB);
10590
10591 // Create the conditional branch instruction.
10592 unsigned Opc =
10593 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
10594 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
10595
Chris Lattner52600972009-09-02 05:57:00 +000010596 // copy0MBB:
10597 // %FalseValue = ...
10598 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000010599 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000010600
Chris Lattner52600972009-09-02 05:57:00 +000010601 // sinkMBB:
10602 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
10603 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000010604 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
10605 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000010606 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
10607 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
10608
Dan Gohman14152b42010-07-06 20:24:04 +000010609 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000010610 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000010611}
10612
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010613MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010614X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010615 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010616 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10617 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010618
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000010619 assert(!Subtarget->isTargetEnvMacho());
10620
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010621 // The lowering is pretty easy: we're just emitting the call to _alloca. The
10622 // non-trivial part is impdef of ESP.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010623
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000010624 if (Subtarget->isTargetWin64()) {
10625 if (Subtarget->isTargetCygMing()) {
10626 // ___chkstk(Mingw64):
10627 // Clobbers R10, R11, RAX and EFLAGS.
10628 // Updates RSP.
10629 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
10630 .addExternalSymbol("___chkstk")
10631 .addReg(X86::RAX, RegState::Implicit)
10632 .addReg(X86::RSP, RegState::Implicit)
10633 .addReg(X86::RAX, RegState::Define | RegState::Implicit)
10634 .addReg(X86::RSP, RegState::Define | RegState::Implicit)
10635 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
10636 } else {
10637 // __chkstk(MSVCRT): does not update stack pointer.
10638 // Clobbers R10, R11 and EFLAGS.
10639 // FIXME: RAX(allocated size) might be reused and not killed.
10640 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
10641 .addExternalSymbol("__chkstk")
10642 .addReg(X86::RAX, RegState::Implicit)
10643 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
10644 // RAX has the offset to subtracted from RSP.
10645 BuildMI(*BB, MI, DL, TII->get(X86::SUB64rr), X86::RSP)
10646 .addReg(X86::RSP)
10647 .addReg(X86::RAX);
10648 }
10649 } else {
10650 const char *StackProbeSymbol =
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010651 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
10652
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000010653 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
10654 .addExternalSymbol(StackProbeSymbol)
10655 .addReg(X86::EAX, RegState::Implicit)
10656 .addReg(X86::ESP, RegState::Implicit)
10657 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
10658 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
10659 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
10660 }
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010661
Dan Gohman14152b42010-07-06 20:24:04 +000010662 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010663 return BB;
10664}
Chris Lattner52600972009-09-02 05:57:00 +000010665
10666MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000010667X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
10668 MachineBasicBlock *BB) const {
10669 // This is pretty easy. We're taking the value that we received from
10670 // our load from the relocation, sticking it in either RDI (x86-64)
10671 // or EAX and doing an indirect call. The return value will then
10672 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000010673 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000010674 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000010675 DebugLoc DL = MI->getDebugLoc();
10676 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000010677
10678 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000010679 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000010680
Eric Christopher30ef0e52010-06-03 04:07:48 +000010681 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000010682 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10683 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000010684 .addReg(X86::RIP)
10685 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010686 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000010687 MI->getOperand(3).getTargetFlags())
10688 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000010689 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000010690 addDirectMem(MIB, X86::RDI);
Eric Christopher61025492010-06-15 23:08:42 +000010691 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000010692 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10693 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000010694 .addReg(0)
10695 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010696 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000010697 MI->getOperand(3).getTargetFlags())
10698 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000010699 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000010700 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010701 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000010702 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10703 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000010704 .addReg(TII->getGlobalBaseReg(F))
10705 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010706 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000010707 MI->getOperand(3).getTargetFlags())
10708 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000010709 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000010710 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010711 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000010712
Dan Gohman14152b42010-07-06 20:24:04 +000010713 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000010714 return BB;
10715}
10716
10717MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000010718X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010719 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000010720 switch (MI->getOpcode()) {
10721 default: assert(false && "Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000010722 case X86::TAILJMPd64:
10723 case X86::TAILJMPr64:
10724 case X86::TAILJMPm64:
10725 assert(!"TAILJMP64 would not be touched here.");
10726 case X86::TCRETURNdi64:
10727 case X86::TCRETURNri64:
10728 case X86::TCRETURNmi64:
10729 // Defs of TCRETURNxx64 has Win64's callee-saved registers, as subset.
10730 // On AMD64, additional defs should be added before register allocation.
10731 if (!Subtarget->isTargetWin64()) {
10732 MI->addRegisterDefined(X86::RSI);
10733 MI->addRegisterDefined(X86::RDI);
10734 MI->addRegisterDefined(X86::XMM6);
10735 MI->addRegisterDefined(X86::XMM7);
10736 MI->addRegisterDefined(X86::XMM8);
10737 MI->addRegisterDefined(X86::XMM9);
10738 MI->addRegisterDefined(X86::XMM10);
10739 MI->addRegisterDefined(X86::XMM11);
10740 MI->addRegisterDefined(X86::XMM12);
10741 MI->addRegisterDefined(X86::XMM13);
10742 MI->addRegisterDefined(X86::XMM14);
10743 MI->addRegisterDefined(X86::XMM15);
10744 }
10745 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010746 case X86::WIN_ALLOCA:
10747 return EmitLoweredWinAlloca(MI, BB);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010748 case X86::TLSCall_32:
10749 case X86::TLSCall_64:
10750 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000010751 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000010752 case X86::CMOV_FR32:
10753 case X86::CMOV_FR64:
10754 case X86::CMOV_V4F32:
10755 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000010756 case X86::CMOV_V2I64:
Chris Lattner314a1132010-03-14 18:31:44 +000010757 case X86::CMOV_GR16:
10758 case X86::CMOV_GR32:
10759 case X86::CMOV_RFP32:
10760 case X86::CMOV_RFP64:
10761 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010762 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000010763
Dale Johannesen849f2142007-07-03 00:53:03 +000010764 case X86::FP32_TO_INT16_IN_MEM:
10765 case X86::FP32_TO_INT32_IN_MEM:
10766 case X86::FP32_TO_INT64_IN_MEM:
10767 case X86::FP64_TO_INT16_IN_MEM:
10768 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000010769 case X86::FP64_TO_INT64_IN_MEM:
10770 case X86::FP80_TO_INT16_IN_MEM:
10771 case X86::FP80_TO_INT32_IN_MEM:
10772 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000010773 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10774 DebugLoc DL = MI->getDebugLoc();
10775
Evan Cheng60c07e12006-07-05 22:17:51 +000010776 // Change the floating point control register to use "round towards zero"
10777 // mode when truncating to an integer value.
10778 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000010779 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000010780 addFrameReference(BuildMI(*BB, MI, DL,
10781 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010782
10783 // Load the old value of the high byte of the control word...
10784 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +000010785 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohman14152b42010-07-06 20:24:04 +000010786 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000010787 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010788
10789 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000010790 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000010791 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000010792
10793 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000010794 addFrameReference(BuildMI(*BB, MI, DL,
10795 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010796
10797 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000010798 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000010799 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000010800
10801 // Get the X86 opcode to use.
10802 unsigned Opc;
10803 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010804 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000010805 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
10806 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
10807 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
10808 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
10809 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
10810 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000010811 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
10812 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
10813 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000010814 }
10815
10816 X86AddressMode AM;
10817 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000010818 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000010819 AM.BaseType = X86AddressMode::RegBase;
10820 AM.Base.Reg = Op.getReg();
10821 } else {
10822 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000010823 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000010824 }
10825 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000010826 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000010827 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010828 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000010829 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000010830 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010831 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000010832 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000010833 AM.GV = Op.getGlobal();
10834 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000010835 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010836 }
Dan Gohman14152b42010-07-06 20:24:04 +000010837 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010838 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000010839
10840 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000010841 addFrameReference(BuildMI(*BB, MI, DL,
10842 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010843
Dan Gohman14152b42010-07-06 20:24:04 +000010844 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000010845 return BB;
10846 }
Eric Christopherb120ab42009-08-18 22:50:32 +000010847 // String/text processing lowering.
10848 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010849 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000010850 return EmitPCMP(MI, BB, 3, false /* in-mem */);
10851 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010852 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000010853 return EmitPCMP(MI, BB, 3, true /* in-mem */);
10854 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010855 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000010856 return EmitPCMP(MI, BB, 5, false /* in mem */);
10857 case X86::PCMPESTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010858 case X86::VPCMPESTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000010859 return EmitPCMP(MI, BB, 5, true /* in mem */);
10860
Eric Christopher228232b2010-11-30 07:20:12 +000010861 // Thread synchronization.
10862 case X86::MONITOR:
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010863 return EmitMonitor(MI, BB);
Eric Christopher228232b2010-11-30 07:20:12 +000010864 case X86::MWAIT:
10865 return EmitMwait(MI, BB);
10866
Eric Christopherb120ab42009-08-18 22:50:32 +000010867 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +000010868 case X86::ATOMAND32:
10869 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010870 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010871 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010872 X86::NOT32r, X86::EAX,
10873 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000010874 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +000010875 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
10876 X86::OR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010877 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010878 X86::NOT32r, X86::EAX,
10879 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000010880 case X86::ATOMXOR32:
10881 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010882 X86::XOR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010883 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010884 X86::NOT32r, X86::EAX,
10885 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010886 case X86::ATOMNAND32:
10887 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010888 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010889 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010890 X86::NOT32r, X86::EAX,
10891 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +000010892 case X86::ATOMMIN32:
10893 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
10894 case X86::ATOMMAX32:
10895 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
10896 case X86::ATOMUMIN32:
10897 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
10898 case X86::ATOMUMAX32:
10899 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +000010900
10901 case X86::ATOMAND16:
10902 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
10903 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010904 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010905 X86::NOT16r, X86::AX,
10906 X86::GR16RegisterClass);
10907 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +000010908 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010909 X86::OR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010910 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010911 X86::NOT16r, X86::AX,
10912 X86::GR16RegisterClass);
10913 case X86::ATOMXOR16:
10914 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
10915 X86::XOR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010916 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010917 X86::NOT16r, X86::AX,
10918 X86::GR16RegisterClass);
10919 case X86::ATOMNAND16:
10920 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
10921 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010922 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010923 X86::NOT16r, X86::AX,
10924 X86::GR16RegisterClass, true);
10925 case X86::ATOMMIN16:
10926 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
10927 case X86::ATOMMAX16:
10928 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
10929 case X86::ATOMUMIN16:
10930 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
10931 case X86::ATOMUMAX16:
10932 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
10933
10934 case X86::ATOMAND8:
10935 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
10936 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010937 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010938 X86::NOT8r, X86::AL,
10939 X86::GR8RegisterClass);
10940 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +000010941 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010942 X86::OR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010943 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010944 X86::NOT8r, X86::AL,
10945 X86::GR8RegisterClass);
10946 case X86::ATOMXOR8:
10947 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
10948 X86::XOR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010949 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010950 X86::NOT8r, X86::AL,
10951 X86::GR8RegisterClass);
10952 case X86::ATOMNAND8:
10953 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
10954 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010955 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010956 X86::NOT8r, X86::AL,
10957 X86::GR8RegisterClass, true);
10958 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010959 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +000010960 case X86::ATOMAND64:
10961 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010962 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010963 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010964 X86::NOT64r, X86::RAX,
10965 X86::GR64RegisterClass);
10966 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +000010967 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
10968 X86::OR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010969 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010970 X86::NOT64r, X86::RAX,
10971 X86::GR64RegisterClass);
10972 case X86::ATOMXOR64:
10973 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010974 X86::XOR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010975 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010976 X86::NOT64r, X86::RAX,
10977 X86::GR64RegisterClass);
10978 case X86::ATOMNAND64:
10979 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
10980 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010981 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010982 X86::NOT64r, X86::RAX,
10983 X86::GR64RegisterClass, true);
10984 case X86::ATOMMIN64:
10985 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
10986 case X86::ATOMMAX64:
10987 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
10988 case X86::ATOMUMIN64:
10989 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
10990 case X86::ATOMUMAX64:
10991 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010992
10993 // This group does 64-bit operations on a 32-bit host.
10994 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010995 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010996 X86::AND32rr, X86::AND32rr,
10997 X86::AND32ri, X86::AND32ri,
10998 false);
10999 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000011000 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011001 X86::OR32rr, X86::OR32rr,
11002 X86::OR32ri, X86::OR32ri,
11003 false);
11004 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000011005 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011006 X86::XOR32rr, X86::XOR32rr,
11007 X86::XOR32ri, X86::XOR32ri,
11008 false);
11009 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000011010 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011011 X86::AND32rr, X86::AND32rr,
11012 X86::AND32ri, X86::AND32ri,
11013 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011014 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000011015 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011016 X86::ADD32rr, X86::ADC32rr,
11017 X86::ADD32ri, X86::ADC32ri,
11018 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011019 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000011020 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011021 X86::SUB32rr, X86::SBB32rr,
11022 X86::SUB32ri, X86::SBB32ri,
11023 false);
Dale Johannesen880ae362008-10-03 22:25:52 +000011024 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000011025 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +000011026 X86::MOV32rr, X86::MOV32rr,
11027 X86::MOV32ri, X86::MOV32ri,
11028 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +000011029 case X86::VASTART_SAVE_XMM_REGS:
11030 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000011031
11032 case X86::VAARG_64:
11033 return EmitVAARG64WithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000011034 }
11035}
11036
11037//===----------------------------------------------------------------------===//
11038// X86 Optimization Hooks
11039//===----------------------------------------------------------------------===//
11040
Dan Gohman475871a2008-07-27 21:46:04 +000011041void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +000011042 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000011043 APInt &KnownZero,
11044 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000011045 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000011046 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +000011047 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000011048 assert((Opc >= ISD::BUILTIN_OP_END ||
11049 Opc == ISD::INTRINSIC_WO_CHAIN ||
11050 Opc == ISD::INTRINSIC_W_CHAIN ||
11051 Opc == ISD::INTRINSIC_VOID) &&
11052 "Should use MaskedValueIsZero if you don't know whether Op"
11053 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000011054
Dan Gohmanf4f92f52008-02-13 23:07:24 +000011055 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000011056 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000011057 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000011058 case X86ISD::ADD:
11059 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000011060 case X86ISD::ADC:
11061 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000011062 case X86ISD::SMUL:
11063 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000011064 case X86ISD::INC:
11065 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000011066 case X86ISD::OR:
11067 case X86ISD::XOR:
11068 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000011069 // These nodes' second result is a boolean.
11070 if (Op.getResNo() == 0)
11071 break;
11072 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011073 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000011074 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
11075 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000011076 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +000011077 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000011078}
Chris Lattner259e97c2006-01-31 19:43:35 +000011079
Owen Andersonbc146b02010-09-21 20:42:50 +000011080unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
11081 unsigned Depth) const {
11082 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
11083 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
11084 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000011085
Owen Andersonbc146b02010-09-21 20:42:50 +000011086 // Fallback case.
11087 return 1;
11088}
11089
Evan Cheng206ee9d2006-07-07 08:33:52 +000011090/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000011091/// node is a GlobalAddress + offset.
11092bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000011093 const GlobalValue* &GA,
11094 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000011095 if (N->getOpcode() == X86ISD::Wrapper) {
11096 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000011097 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000011098 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000011099 return true;
11100 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000011101 }
Evan Chengad4196b2008-05-12 19:56:52 +000011102 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000011103}
11104
Evan Cheng206ee9d2006-07-07 08:33:52 +000011105/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
11106/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
11107/// if the load addresses are consecutive, non-overlapping, and in the right
Nate Begemanfdea31a2010-03-24 20:49:50 +000011108/// order.
Dan Gohman475871a2008-07-27 21:46:04 +000011109static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Mon P Wanga0fd0d52010-12-19 23:55:53 +000011110 TargetLowering::DAGCombinerInfo &DCI) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000011111 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000011112 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000011113
Eli Friedman7a5e5552009-06-07 06:52:44 +000011114 if (VT.getSizeInBits() != 128)
11115 return SDValue();
11116
Mon P Wanga0fd0d52010-12-19 23:55:53 +000011117 // Don't create instructions with illegal types after legalize types has run.
11118 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
11119 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
11120 return SDValue();
11121
Nate Begemanfdea31a2010-03-24 20:49:50 +000011122 SmallVector<SDValue, 16> Elts;
11123 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000011124 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000011125
Nate Begemanfdea31a2010-03-24 20:49:50 +000011126 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000011127}
Evan Chengd880b972008-05-09 21:53:03 +000011128
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000011129/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
11130/// generation and convert it from being a bunch of shuffles and extracts
11131/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000011132static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
11133 const TargetLowering &TLI) {
11134 SDValue InputVector = N->getOperand(0);
11135
11136 // Only operate on vectors of 4 elements, where the alternative shuffling
11137 // gets to be more expensive.
11138 if (InputVector.getValueType() != MVT::v4i32)
11139 return SDValue();
11140
11141 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
11142 // single use which is a sign-extend or zero-extend, and all elements are
11143 // used.
11144 SmallVector<SDNode *, 4> Uses;
11145 unsigned ExtractedElements = 0;
11146 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
11147 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
11148 if (UI.getUse().getResNo() != InputVector.getResNo())
11149 return SDValue();
11150
11151 SDNode *Extract = *UI;
11152 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
11153 return SDValue();
11154
11155 if (Extract->getValueType(0) != MVT::i32)
11156 return SDValue();
11157 if (!Extract->hasOneUse())
11158 return SDValue();
11159 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
11160 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
11161 return SDValue();
11162 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
11163 return SDValue();
11164
11165 // Record which element was extracted.
11166 ExtractedElements |=
11167 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
11168
11169 Uses.push_back(Extract);
11170 }
11171
11172 // If not all the elements were used, this may not be worthwhile.
11173 if (ExtractedElements != 15)
11174 return SDValue();
11175
11176 // Ok, we've now decided to do the transformation.
11177 DebugLoc dl = InputVector.getDebugLoc();
11178
11179 // Store the value to a temporary stack slot.
11180 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000011181 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
11182 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000011183
11184 // Replace each use (extract) with a load of the appropriate element.
11185 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
11186 UE = Uses.end(); UI != UE; ++UI) {
11187 SDNode *Extract = *UI;
11188
Nadav Rotem86694292011-05-17 08:31:57 +000011189 // cOMpute the element's address.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000011190 SDValue Idx = Extract->getOperand(1);
11191 unsigned EltSize =
11192 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
11193 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
11194 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
11195
Nadav Rotem86694292011-05-17 08:31:57 +000011196 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
Chris Lattner51abfe42010-09-21 06:02:19 +000011197 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000011198
11199 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000011200 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000011201 ScalarAddr, MachinePointerInfo(),
11202 false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000011203
11204 // Replace the exact with the load.
11205 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
11206 }
11207
11208 // The replacement was made in place; don't return anything.
11209 return SDValue();
11210}
11211
Chris Lattner83e6c992006-10-04 06:57:07 +000011212/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000011213static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +000011214 const X86Subtarget *Subtarget) {
11215 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000011216 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000011217 // Get the LHS/RHS of the select.
11218 SDValue LHS = N->getOperand(1);
11219 SDValue RHS = N->getOperand(2);
Eric Christopherfd179292009-08-27 18:07:15 +000011220
Dan Gohman670e5392009-09-21 18:03:22 +000011221 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000011222 // instructions match the semantics of the common C idiom x<y?x:y but not
11223 // x<=y?x:y, because of how they handle negative zero (which can be
11224 // ignored in unsafe-math mode).
Chris Lattner83e6c992006-10-04 06:57:07 +000011225 if (Subtarget->hasSSE2() &&
Owen Anderson825b72b2009-08-11 20:47:22 +000011226 (LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64) &&
Chris Lattner47b4ce82009-03-11 05:48:52 +000011227 Cond.getOpcode() == ISD::SETCC) {
11228 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011229
Chris Lattner47b4ce82009-03-11 05:48:52 +000011230 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000011231 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000011232 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
11233 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000011234 switch (CC) {
11235 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000011236 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000011237 // Converting this to a min would handle NaNs incorrectly, and swapping
11238 // the operands would cause it to handle comparisons between positive
11239 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000011240 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000011241 if (!UnsafeFPMath &&
11242 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
11243 break;
11244 std::swap(LHS, RHS);
11245 }
Dan Gohman670e5392009-09-21 18:03:22 +000011246 Opcode = X86ISD::FMIN;
11247 break;
11248 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000011249 // Converting this to a min would handle comparisons between positive
11250 // and negative zero incorrectly.
11251 if (!UnsafeFPMath &&
11252 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
11253 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011254 Opcode = X86ISD::FMIN;
11255 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000011256 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000011257 // Converting this to a min would handle both negative zeros and NaNs
11258 // incorrectly, but we can swap the operands to fix both.
11259 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000011260 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011261 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000011262 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011263 Opcode = X86ISD::FMIN;
11264 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011265
Dan Gohman670e5392009-09-21 18:03:22 +000011266 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000011267 // Converting this to a max would handle comparisons between positive
11268 // and negative zero incorrectly.
11269 if (!UnsafeFPMath &&
11270 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(LHS))
11271 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011272 Opcode = X86ISD::FMAX;
11273 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000011274 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000011275 // Converting this to a max would handle NaNs incorrectly, and swapping
11276 // the operands would cause it to handle comparisons between positive
11277 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000011278 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000011279 if (!UnsafeFPMath &&
11280 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
11281 break;
11282 std::swap(LHS, RHS);
11283 }
Dan Gohman670e5392009-09-21 18:03:22 +000011284 Opcode = X86ISD::FMAX;
11285 break;
11286 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000011287 // Converting this to a max would handle both negative zeros and NaNs
11288 // incorrectly, but we can swap the operands to fix both.
11289 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000011290 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011291 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011292 case ISD::SETGE:
11293 Opcode = X86ISD::FMAX;
11294 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000011295 }
Dan Gohman670e5392009-09-21 18:03:22 +000011296 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000011297 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
11298 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000011299 switch (CC) {
11300 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000011301 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000011302 // Converting this to a min would handle comparisons between positive
11303 // and negative zero incorrectly, and swapping the operands would
11304 // cause it to handle NaNs incorrectly.
11305 if (!UnsafeFPMath &&
11306 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000011307 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000011308 break;
11309 std::swap(LHS, RHS);
11310 }
Dan Gohman670e5392009-09-21 18:03:22 +000011311 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000011312 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011313 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000011314 // Converting this to a min would handle NaNs incorrectly.
11315 if (!UnsafeFPMath &&
11316 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
11317 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011318 Opcode = X86ISD::FMIN;
11319 break;
11320 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000011321 // Converting this to a min would handle both negative zeros and NaNs
11322 // incorrectly, but we can swap the operands to fix both.
11323 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000011324 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011325 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011326 case ISD::SETGE:
11327 Opcode = X86ISD::FMIN;
11328 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011329
Dan Gohman670e5392009-09-21 18:03:22 +000011330 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000011331 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000011332 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000011333 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011334 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000011335 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011336 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000011337 // Converting this to a max would handle comparisons between positive
11338 // and negative zero incorrectly, and swapping the operands would
11339 // cause it to handle NaNs incorrectly.
11340 if (!UnsafeFPMath &&
11341 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000011342 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000011343 break;
11344 std::swap(LHS, RHS);
11345 }
Dan Gohman670e5392009-09-21 18:03:22 +000011346 Opcode = X86ISD::FMAX;
11347 break;
11348 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000011349 // Converting this to a max would handle both negative zeros and NaNs
11350 // incorrectly, but we can swap the operands to fix both.
11351 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000011352 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011353 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000011354 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011355 Opcode = X86ISD::FMAX;
11356 break;
11357 }
Chris Lattner83e6c992006-10-04 06:57:07 +000011358 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011359
Chris Lattner47b4ce82009-03-11 05:48:52 +000011360 if (Opcode)
11361 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000011362 }
Eric Christopherfd179292009-08-27 18:07:15 +000011363
Chris Lattnerd1980a52009-03-12 06:52:53 +000011364 // If this is a select between two integer constants, try to do some
11365 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000011366 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
11367 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000011368 // Don't do this for crazy integer types.
11369 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
11370 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000011371 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000011372 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000011373
Chris Lattnercee56e72009-03-13 05:53:31 +000011374 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000011375 // Efficiently invertible.
11376 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
11377 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
11378 isa<ConstantSDNode>(Cond.getOperand(1))))) {
11379 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000011380 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000011381 }
Eric Christopherfd179292009-08-27 18:07:15 +000011382
Chris Lattnerd1980a52009-03-12 06:52:53 +000011383 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000011384 if (FalseC->getAPIntValue() == 0 &&
11385 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000011386 if (NeedsCondInvert) // Invert the condition if needed.
11387 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
11388 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011389
Chris Lattnerd1980a52009-03-12 06:52:53 +000011390 // Zero extend the condition if needed.
11391 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011392
Chris Lattnercee56e72009-03-13 05:53:31 +000011393 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000011394 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000011395 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000011396 }
Eric Christopherfd179292009-08-27 18:07:15 +000011397
Chris Lattner97a29a52009-03-13 05:22:11 +000011398 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000011399 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000011400 if (NeedsCondInvert) // Invert the condition if needed.
11401 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
11402 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011403
Chris Lattner97a29a52009-03-13 05:22:11 +000011404 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000011405 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
11406 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000011407 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000011408 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000011409 }
Eric Christopherfd179292009-08-27 18:07:15 +000011410
Chris Lattnercee56e72009-03-13 05:53:31 +000011411 // Optimize cases that will turn into an LEA instruction. This requires
11412 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000011413 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000011414 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000011415 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000011416
Chris Lattnercee56e72009-03-13 05:53:31 +000011417 bool isFastMultiplier = false;
11418 if (Diff < 10) {
11419 switch ((unsigned char)Diff) {
11420 default: break;
11421 case 1: // result = add base, cond
11422 case 2: // result = lea base( , cond*2)
11423 case 3: // result = lea base(cond, cond*2)
11424 case 4: // result = lea base( , cond*4)
11425 case 5: // result = lea base(cond, cond*4)
11426 case 8: // result = lea base( , cond*8)
11427 case 9: // result = lea base(cond, cond*8)
11428 isFastMultiplier = true;
11429 break;
11430 }
11431 }
Eric Christopherfd179292009-08-27 18:07:15 +000011432
Chris Lattnercee56e72009-03-13 05:53:31 +000011433 if (isFastMultiplier) {
11434 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
11435 if (NeedsCondInvert) // Invert the condition if needed.
11436 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
11437 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011438
Chris Lattnercee56e72009-03-13 05:53:31 +000011439 // Zero extend the condition if needed.
11440 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
11441 Cond);
11442 // Scale the condition by the difference.
11443 if (Diff != 1)
11444 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
11445 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011446
Chris Lattnercee56e72009-03-13 05:53:31 +000011447 // Add the base if non-zero.
11448 if (FalseC->getAPIntValue() != 0)
11449 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
11450 SDValue(FalseC, 0));
11451 return Cond;
11452 }
Eric Christopherfd179292009-08-27 18:07:15 +000011453 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000011454 }
11455 }
Eric Christopherfd179292009-08-27 18:07:15 +000011456
Dan Gohman475871a2008-07-27 21:46:04 +000011457 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000011458}
11459
Chris Lattnerd1980a52009-03-12 06:52:53 +000011460/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
11461static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
11462 TargetLowering::DAGCombinerInfo &DCI) {
11463 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000011464
Chris Lattnerd1980a52009-03-12 06:52:53 +000011465 // If the flag operand isn't dead, don't touch this CMOV.
11466 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
11467 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000011468
Evan Chengb5a55d92011-05-24 01:48:22 +000011469 SDValue FalseOp = N->getOperand(0);
11470 SDValue TrueOp = N->getOperand(1);
11471 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
11472 SDValue Cond = N->getOperand(3);
11473 if (CC == X86::COND_E || CC == X86::COND_NE) {
11474 switch (Cond.getOpcode()) {
11475 default: break;
11476 case X86ISD::BSR:
11477 case X86ISD::BSF:
11478 // If operand of BSR / BSF are proven never zero, then ZF cannot be set.
11479 if (DAG.isKnownNeverZero(Cond.getOperand(0)))
11480 return (CC == X86::COND_E) ? FalseOp : TrueOp;
11481 }
11482 }
11483
Chris Lattnerd1980a52009-03-12 06:52:53 +000011484 // If this is a select between two integer constants, try to do some
11485 // optimizations. Note that the operands are ordered the opposite of SELECT
11486 // operands.
Evan Chengb5a55d92011-05-24 01:48:22 +000011487 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(TrueOp)) {
11488 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(FalseOp)) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000011489 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
11490 // larger than FalseC (the false value).
Chris Lattnerd1980a52009-03-12 06:52:53 +000011491 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
11492 CC = X86::GetOppositeBranchCondition(CC);
11493 std::swap(TrueC, FalseC);
11494 }
Eric Christopherfd179292009-08-27 18:07:15 +000011495
Chris Lattnerd1980a52009-03-12 06:52:53 +000011496 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000011497 // This is efficient for any integer data type (including i8/i16) and
11498 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000011499 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000011500 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11501 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011502
Chris Lattnerd1980a52009-03-12 06:52:53 +000011503 // Zero extend the condition if needed.
11504 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011505
Chris Lattnerd1980a52009-03-12 06:52:53 +000011506 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
11507 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000011508 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000011509 if (N->getNumValues() == 2) // Dead flag value?
11510 return DCI.CombineTo(N, Cond, SDValue());
11511 return Cond;
11512 }
Eric Christopherfd179292009-08-27 18:07:15 +000011513
Chris Lattnercee56e72009-03-13 05:53:31 +000011514 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
11515 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000011516 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000011517 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11518 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011519
Chris Lattner97a29a52009-03-13 05:22:11 +000011520 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000011521 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
11522 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000011523 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
11524 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000011525
Chris Lattner97a29a52009-03-13 05:22:11 +000011526 if (N->getNumValues() == 2) // Dead flag value?
11527 return DCI.CombineTo(N, Cond, SDValue());
11528 return Cond;
11529 }
Eric Christopherfd179292009-08-27 18:07:15 +000011530
Chris Lattnercee56e72009-03-13 05:53:31 +000011531 // Optimize cases that will turn into an LEA instruction. This requires
11532 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000011533 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000011534 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000011535 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000011536
Chris Lattnercee56e72009-03-13 05:53:31 +000011537 bool isFastMultiplier = false;
11538 if (Diff < 10) {
11539 switch ((unsigned char)Diff) {
11540 default: break;
11541 case 1: // result = add base, cond
11542 case 2: // result = lea base( , cond*2)
11543 case 3: // result = lea base(cond, cond*2)
11544 case 4: // result = lea base( , cond*4)
11545 case 5: // result = lea base(cond, cond*4)
11546 case 8: // result = lea base( , cond*8)
11547 case 9: // result = lea base(cond, cond*8)
11548 isFastMultiplier = true;
11549 break;
11550 }
11551 }
Eric Christopherfd179292009-08-27 18:07:15 +000011552
Chris Lattnercee56e72009-03-13 05:53:31 +000011553 if (isFastMultiplier) {
11554 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000011555 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11556 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000011557 // Zero extend the condition if needed.
11558 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
11559 Cond);
11560 // Scale the condition by the difference.
11561 if (Diff != 1)
11562 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
11563 DAG.getConstant(Diff, Cond.getValueType()));
11564
11565 // Add the base if non-zero.
11566 if (FalseC->getAPIntValue() != 0)
11567 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
11568 SDValue(FalseC, 0));
11569 if (N->getNumValues() == 2) // Dead flag value?
11570 return DCI.CombineTo(N, Cond, SDValue());
11571 return Cond;
11572 }
Eric Christopherfd179292009-08-27 18:07:15 +000011573 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000011574 }
11575 }
11576 return SDValue();
11577}
11578
11579
Evan Cheng0b0cd912009-03-28 05:57:29 +000011580/// PerformMulCombine - Optimize a single multiply with constant into two
11581/// in order to implement it with two cheaper instructions, e.g.
11582/// LEA + SHL, LEA + LEA.
11583static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
11584 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000011585 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
11586 return SDValue();
11587
Owen Andersone50ed302009-08-10 22:56:29 +000011588 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000011589 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000011590 return SDValue();
11591
11592 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
11593 if (!C)
11594 return SDValue();
11595 uint64_t MulAmt = C->getZExtValue();
11596 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
11597 return SDValue();
11598
11599 uint64_t MulAmt1 = 0;
11600 uint64_t MulAmt2 = 0;
11601 if ((MulAmt % 9) == 0) {
11602 MulAmt1 = 9;
11603 MulAmt2 = MulAmt / 9;
11604 } else if ((MulAmt % 5) == 0) {
11605 MulAmt1 = 5;
11606 MulAmt2 = MulAmt / 5;
11607 } else if ((MulAmt % 3) == 0) {
11608 MulAmt1 = 3;
11609 MulAmt2 = MulAmt / 3;
11610 }
11611 if (MulAmt2 &&
11612 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
11613 DebugLoc DL = N->getDebugLoc();
11614
11615 if (isPowerOf2_64(MulAmt2) &&
11616 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
11617 // If second multiplifer is pow2, issue it first. We want the multiply by
11618 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
11619 // is an add.
11620 std::swap(MulAmt1, MulAmt2);
11621
11622 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000011623 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000011624 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000011625 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000011626 else
Evan Cheng73f24c92009-03-30 21:36:47 +000011627 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000011628 DAG.getConstant(MulAmt1, VT));
11629
Eric Christopherfd179292009-08-27 18:07:15 +000011630 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000011631 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000011632 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000011633 else
Evan Cheng73f24c92009-03-30 21:36:47 +000011634 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000011635 DAG.getConstant(MulAmt2, VT));
11636
11637 // Do not add new nodes to DAG combiner worklist.
11638 DCI.CombineTo(N, NewMul, false);
11639 }
11640 return SDValue();
11641}
11642
Evan Chengad9c0a32009-12-15 00:53:42 +000011643static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
11644 SDValue N0 = N->getOperand(0);
11645 SDValue N1 = N->getOperand(1);
11646 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
11647 EVT VT = N0.getValueType();
11648
11649 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
11650 // since the result of setcc_c is all zero's or all ones.
11651 if (N1C && N0.getOpcode() == ISD::AND &&
11652 N0.getOperand(1).getOpcode() == ISD::Constant) {
11653 SDValue N00 = N0.getOperand(0);
11654 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
11655 ((N00.getOpcode() == ISD::ANY_EXTEND ||
11656 N00.getOpcode() == ISD::ZERO_EXTEND) &&
11657 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
11658 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
11659 APInt ShAmt = N1C->getAPIntValue();
11660 Mask = Mask.shl(ShAmt);
11661 if (Mask != 0)
11662 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
11663 N00, DAG.getConstant(Mask, VT));
11664 }
11665 }
11666
11667 return SDValue();
11668}
Evan Cheng0b0cd912009-03-28 05:57:29 +000011669
Nate Begeman740ab032009-01-26 00:52:55 +000011670/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
11671/// when possible.
11672static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
11673 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000011674 EVT VT = N->getValueType(0);
11675 if (!VT.isVector() && VT.isInteger() &&
11676 N->getOpcode() == ISD::SHL)
11677 return PerformSHLCombine(N, DAG);
11678
Nate Begeman740ab032009-01-26 00:52:55 +000011679 // On X86 with SSE2 support, we can transform this to a vector shift if
11680 // all elements are shifted by the same amount. We can't do this in legalize
11681 // because the a constant vector is typically transformed to a constant pool
11682 // so we have no knowledge of the shift amount.
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011683 if (!Subtarget->hasSSE2())
11684 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000011685
Owen Anderson825b72b2009-08-11 20:47:22 +000011686 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011687 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000011688
Mon P Wang3becd092009-01-28 08:12:05 +000011689 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000011690 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000011691 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000011692 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000011693 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
11694 unsigned NumElts = VT.getVectorNumElements();
11695 unsigned i = 0;
11696 for (; i != NumElts; ++i) {
11697 SDValue Arg = ShAmtOp.getOperand(i);
11698 if (Arg.getOpcode() == ISD::UNDEF) continue;
11699 BaseShAmt = Arg;
11700 break;
11701 }
11702 for (; i != NumElts; ++i) {
11703 SDValue Arg = ShAmtOp.getOperand(i);
11704 if (Arg.getOpcode() == ISD::UNDEF) continue;
11705 if (Arg != BaseShAmt) {
11706 return SDValue();
11707 }
11708 }
11709 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000011710 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000011711 SDValue InVec = ShAmtOp.getOperand(0);
11712 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
11713 unsigned NumElts = InVec.getValueType().getVectorNumElements();
11714 unsigned i = 0;
11715 for (; i != NumElts; ++i) {
11716 SDValue Arg = InVec.getOperand(i);
11717 if (Arg.getOpcode() == ISD::UNDEF) continue;
11718 BaseShAmt = Arg;
11719 break;
11720 }
11721 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
11722 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000011723 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000011724 if (C->getZExtValue() == SplatIdx)
11725 BaseShAmt = InVec.getOperand(1);
11726 }
11727 }
11728 if (BaseShAmt.getNode() == 0)
11729 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
11730 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +000011731 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011732 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000011733
Mon P Wangefa42202009-09-03 19:56:25 +000011734 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000011735 if (EltVT.bitsGT(MVT::i32))
11736 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
11737 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000011738 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000011739
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011740 // The shift amount is identical so we can do a vector shift.
11741 SDValue ValOp = N->getOperand(0);
11742 switch (N->getOpcode()) {
11743 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000011744 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011745 break;
11746 case ISD::SHL:
Owen Anderson825b72b2009-08-11 20:47:22 +000011747 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011748 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011749 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011750 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011751 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011752 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011753 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011754 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011755 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011756 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011757 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011758 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011759 break;
11760 case ISD::SRA:
Owen Anderson825b72b2009-08-11 20:47:22 +000011761 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011762 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011763 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011764 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011765 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011766 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011767 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011768 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011769 break;
11770 case ISD::SRL:
Owen Anderson825b72b2009-08-11 20:47:22 +000011771 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011772 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011773 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011774 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011775 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011776 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011777 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011778 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011779 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011780 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011781 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011782 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011783 break;
Nate Begeman740ab032009-01-26 00:52:55 +000011784 }
11785 return SDValue();
11786}
11787
Nate Begemanb65c1752010-12-17 22:55:37 +000011788
Stuart Hastings865f0932011-06-03 23:53:54 +000011789// CMPEQCombine - Recognize the distinctive (AND (setcc ...) (setcc ..))
11790// where both setccs reference the same FP CMP, and rewrite for CMPEQSS
11791// and friends. Likewise for OR -> CMPNEQSS.
11792static SDValue CMPEQCombine(SDNode *N, SelectionDAG &DAG,
11793 TargetLowering::DAGCombinerInfo &DCI,
11794 const X86Subtarget *Subtarget) {
11795 unsigned opcode;
11796
11797 // SSE1 supports CMP{eq|ne}SS, and SSE2 added CMP{eq|ne}SD, but
11798 // we're requiring SSE2 for both.
11799 if (Subtarget->hasSSE2() && isAndOrOfSetCCs(SDValue(N, 0U), opcode)) {
11800 SDValue N0 = N->getOperand(0);
11801 SDValue N1 = N->getOperand(1);
11802 SDValue CMP0 = N0->getOperand(1);
11803 SDValue CMP1 = N1->getOperand(1);
11804 DebugLoc DL = N->getDebugLoc();
11805
11806 // The SETCCs should both refer to the same CMP.
11807 if (CMP0.getOpcode() != X86ISD::CMP || CMP0 != CMP1)
11808 return SDValue();
11809
11810 SDValue CMP00 = CMP0->getOperand(0);
11811 SDValue CMP01 = CMP0->getOperand(1);
11812 EVT VT = CMP00.getValueType();
11813
11814 if (VT == MVT::f32 || VT == MVT::f64) {
11815 bool ExpectingFlags = false;
11816 // Check for any users that want flags:
11817 for (SDNode::use_iterator UI = N->use_begin(),
11818 UE = N->use_end();
11819 !ExpectingFlags && UI != UE; ++UI)
11820 switch (UI->getOpcode()) {
11821 default:
11822 case ISD::BR_CC:
11823 case ISD::BRCOND:
11824 case ISD::SELECT:
11825 ExpectingFlags = true;
11826 break;
11827 case ISD::CopyToReg:
11828 case ISD::SIGN_EXTEND:
11829 case ISD::ZERO_EXTEND:
11830 case ISD::ANY_EXTEND:
11831 break;
11832 }
11833
11834 if (!ExpectingFlags) {
11835 enum X86::CondCode cc0 = (enum X86::CondCode)N0.getConstantOperandVal(0);
11836 enum X86::CondCode cc1 = (enum X86::CondCode)N1.getConstantOperandVal(0);
11837
11838 if (cc1 == X86::COND_E || cc1 == X86::COND_NE) {
11839 X86::CondCode tmp = cc0;
11840 cc0 = cc1;
11841 cc1 = tmp;
11842 }
11843
11844 if ((cc0 == X86::COND_E && cc1 == X86::COND_NP) ||
11845 (cc0 == X86::COND_NE && cc1 == X86::COND_P)) {
11846 bool is64BitFP = (CMP00.getValueType() == MVT::f64);
11847 X86ISD::NodeType NTOperator = is64BitFP ?
11848 X86ISD::FSETCCsd : X86ISD::FSETCCss;
11849 // FIXME: need symbolic constants for these magic numbers.
11850 // See X86ATTInstPrinter.cpp:printSSECC().
11851 unsigned x86cc = (cc0 == X86::COND_E) ? 0 : 4;
11852 SDValue OnesOrZeroesF = DAG.getNode(NTOperator, DL, MVT::f32, CMP00, CMP01,
11853 DAG.getConstant(x86cc, MVT::i8));
11854 SDValue OnesOrZeroesI = DAG.getNode(ISD::BITCAST, DL, MVT::i32,
11855 OnesOrZeroesF);
11856 SDValue ANDed = DAG.getNode(ISD::AND, DL, MVT::i32, OnesOrZeroesI,
11857 DAG.getConstant(1, MVT::i32));
11858 SDValue OneBitOfTruth = DAG.getNode(ISD::TRUNCATE, DL, MVT::i8, ANDed);
11859 return OneBitOfTruth;
11860 }
11861 }
11862 }
11863 }
11864 return SDValue();
11865}
11866
Nate Begemanb65c1752010-12-17 22:55:37 +000011867static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
11868 TargetLowering::DAGCombinerInfo &DCI,
11869 const X86Subtarget *Subtarget) {
11870 if (DCI.isBeforeLegalizeOps())
11871 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011872
Stuart Hastings865f0932011-06-03 23:53:54 +000011873 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
11874 if (R.getNode())
11875 return R;
11876
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000011877 // Want to form ANDNP nodes:
11878 // 1) In the hopes of then easily combining them with OR and AND nodes
11879 // to form PBLEND/PSIGN.
11880 // 2) To match ANDN packed intrinsics
Nate Begemanb65c1752010-12-17 22:55:37 +000011881 EVT VT = N->getValueType(0);
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000011882 if (VT != MVT::v2i64 && VT != MVT::v4i64)
Nate Begemanb65c1752010-12-17 22:55:37 +000011883 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011884
Nate Begemanb65c1752010-12-17 22:55:37 +000011885 SDValue N0 = N->getOperand(0);
11886 SDValue N1 = N->getOperand(1);
11887 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011888
Nate Begemanb65c1752010-12-17 22:55:37 +000011889 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011890 if (N0.getOpcode() == ISD::XOR &&
Nate Begemanb65c1752010-12-17 22:55:37 +000011891 ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000011892 return DAG.getNode(X86ISD::ANDNP, DL, VT, N0.getOperand(0), N1);
Nate Begemanb65c1752010-12-17 22:55:37 +000011893
11894 // Check RHS for vnot
11895 if (N1.getOpcode() == ISD::XOR &&
11896 ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000011897 return DAG.getNode(X86ISD::ANDNP, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011898
Nate Begemanb65c1752010-12-17 22:55:37 +000011899 return SDValue();
11900}
11901
Evan Cheng760d1942010-01-04 21:22:48 +000011902static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000011903 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000011904 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +000011905 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000011906 return SDValue();
11907
Stuart Hastings865f0932011-06-03 23:53:54 +000011908 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
11909 if (R.getNode())
11910 return R;
11911
Evan Cheng760d1942010-01-04 21:22:48 +000011912 EVT VT = N->getValueType(0);
Nate Begemanb65c1752010-12-17 22:55:37 +000011913 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64 && VT != MVT::v2i64)
Evan Cheng760d1942010-01-04 21:22:48 +000011914 return SDValue();
11915
Evan Cheng760d1942010-01-04 21:22:48 +000011916 SDValue N0 = N->getOperand(0);
11917 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011918
Nate Begemanb65c1752010-12-17 22:55:37 +000011919 // look for psign/blend
11920 if (Subtarget->hasSSSE3()) {
11921 if (VT == MVT::v2i64) {
11922 // Canonicalize pandn to RHS
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000011923 if (N0.getOpcode() == X86ISD::ANDNP)
Nate Begemanb65c1752010-12-17 22:55:37 +000011924 std::swap(N0, N1);
11925 // or (and (m, x), (pandn m, y))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000011926 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::ANDNP) {
Nate Begemanb65c1752010-12-17 22:55:37 +000011927 SDValue Mask = N1.getOperand(0);
11928 SDValue X = N1.getOperand(1);
11929 SDValue Y;
11930 if (N0.getOperand(0) == Mask)
11931 Y = N0.getOperand(1);
11932 if (N0.getOperand(1) == Mask)
11933 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011934
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000011935 // Check to see if the mask appeared in both the AND and ANDNP and
Nate Begemanb65c1752010-12-17 22:55:37 +000011936 if (!Y.getNode())
11937 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011938
Nate Begemanb65c1752010-12-17 22:55:37 +000011939 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
11940 if (Mask.getOpcode() != ISD::BITCAST ||
11941 X.getOpcode() != ISD::BITCAST ||
11942 Y.getOpcode() != ISD::BITCAST)
11943 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011944
Nate Begemanb65c1752010-12-17 22:55:37 +000011945 // Look through mask bitcast.
11946 Mask = Mask.getOperand(0);
11947 EVT MaskVT = Mask.getValueType();
11948
11949 // Validate that the Mask operand is a vector sra node. The sra node
11950 // will be an intrinsic.
11951 if (Mask.getOpcode() != ISD::INTRINSIC_WO_CHAIN)
11952 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011953
Nate Begemanb65c1752010-12-17 22:55:37 +000011954 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
11955 // there is no psrai.b
11956 switch (cast<ConstantSDNode>(Mask.getOperand(0))->getZExtValue()) {
11957 case Intrinsic::x86_sse2_psrai_w:
11958 case Intrinsic::x86_sse2_psrai_d:
11959 break;
11960 default: return SDValue();
11961 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011962
Nate Begemanb65c1752010-12-17 22:55:37 +000011963 // Check that the SRA is all signbits.
11964 SDValue SraC = Mask.getOperand(2);
11965 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
11966 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
11967 if ((SraAmt + 1) != EltBits)
11968 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011969
Nate Begemanb65c1752010-12-17 22:55:37 +000011970 DebugLoc DL = N->getDebugLoc();
11971
11972 // Now we know we at least have a plendvb with the mask val. See if
11973 // we can form a psignb/w/d.
11974 // psign = x.type == y.type == mask.type && y = sub(0, x);
11975 X = X.getOperand(0);
11976 Y = Y.getOperand(0);
11977 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
11978 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
11979 X.getValueType() == MaskVT && X.getValueType() == Y.getValueType()){
11980 unsigned Opc = 0;
11981 switch (EltBits) {
11982 case 8: Opc = X86ISD::PSIGNB; break;
11983 case 16: Opc = X86ISD::PSIGNW; break;
11984 case 32: Opc = X86ISD::PSIGND; break;
11985 default: break;
11986 }
11987 if (Opc) {
11988 SDValue Sign = DAG.getNode(Opc, DL, MaskVT, X, Mask.getOperand(1));
11989 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Sign);
11990 }
11991 }
11992 // PBLENDVB only available on SSE 4.1
11993 if (!Subtarget->hasSSE41())
11994 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011995
Nate Begemanb65c1752010-12-17 22:55:37 +000011996 X = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, X);
11997 Y = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Y);
11998 Mask = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Mask);
Nate Begeman672fb622010-12-20 22:04:24 +000011999 Mask = DAG.getNode(X86ISD::PBLENDVB, DL, MVT::v16i8, X, Y, Mask);
Nate Begemanb65c1752010-12-17 22:55:37 +000012000 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Mask);
12001 }
12002 }
12003 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012004
Nate Begemanb65c1752010-12-17 22:55:37 +000012005 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000012006 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
12007 std::swap(N0, N1);
12008 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
12009 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000012010 if (!N0.hasOneUse() || !N1.hasOneUse())
12011 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000012012
12013 SDValue ShAmt0 = N0.getOperand(1);
12014 if (ShAmt0.getValueType() != MVT::i8)
12015 return SDValue();
12016 SDValue ShAmt1 = N1.getOperand(1);
12017 if (ShAmt1.getValueType() != MVT::i8)
12018 return SDValue();
12019 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
12020 ShAmt0 = ShAmt0.getOperand(0);
12021 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
12022 ShAmt1 = ShAmt1.getOperand(0);
12023
12024 DebugLoc DL = N->getDebugLoc();
12025 unsigned Opc = X86ISD::SHLD;
12026 SDValue Op0 = N0.getOperand(0);
12027 SDValue Op1 = N1.getOperand(0);
12028 if (ShAmt0.getOpcode() == ISD::SUB) {
12029 Opc = X86ISD::SHRD;
12030 std::swap(Op0, Op1);
12031 std::swap(ShAmt0, ShAmt1);
12032 }
12033
Evan Cheng8b1190a2010-04-28 01:18:01 +000012034 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000012035 if (ShAmt1.getOpcode() == ISD::SUB) {
12036 SDValue Sum = ShAmt1.getOperand(0);
12037 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000012038 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
12039 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
12040 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
12041 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000012042 return DAG.getNode(Opc, DL, VT,
12043 Op0, Op1,
12044 DAG.getNode(ISD::TRUNCATE, DL,
12045 MVT::i8, ShAmt0));
12046 }
12047 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
12048 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
12049 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000012050 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000012051 return DAG.getNode(Opc, DL, VT,
12052 N0.getOperand(0), N1.getOperand(0),
12053 DAG.getNode(ISD::TRUNCATE, DL,
12054 MVT::i8, ShAmt0));
12055 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012056
Evan Cheng760d1942010-01-04 21:22:48 +000012057 return SDValue();
12058}
12059
Chris Lattner149a4e52008-02-22 02:09:43 +000012060/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000012061static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000012062 const X86Subtarget *Subtarget) {
Chris Lattner149a4e52008-02-22 02:09:43 +000012063 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
12064 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000012065 // A preferable solution to the general problem is to figure out the right
12066 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000012067
12068 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng7e2ff772008-05-08 00:57:18 +000012069 StoreSDNode *St = cast<StoreSDNode>(N);
Owen Andersone50ed302009-08-10 22:56:29 +000012070 EVT VT = St->getValue().getValueType();
Evan Cheng536e6672009-03-12 05:59:15 +000012071 if (VT.getSizeInBits() != 64)
12072 return SDValue();
12073
Devang Patel578efa92009-06-05 21:57:13 +000012074 const Function *F = DAG.getMachineFunction().getFunction();
12075 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Eric Christopherfd179292009-08-27 18:07:15 +000012076 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
Devang Patel578efa92009-06-05 21:57:13 +000012077 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +000012078 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000012079 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000012080 isa<LoadSDNode>(St->getValue()) &&
12081 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
12082 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000012083 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000012084 LoadSDNode *Ld = 0;
12085 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000012086 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000012087 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000012088 // Must be a store of a load. We currently handle two cases: the load
12089 // is a direct child, and it's under an intervening TokenFactor. It is
12090 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000012091 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000012092 Ld = cast<LoadSDNode>(St->getChain());
12093 else if (St->getValue().hasOneUse() &&
12094 ChainVal->getOpcode() == ISD::TokenFactor) {
12095 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000012096 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000012097 TokenFactorIndex = i;
12098 Ld = cast<LoadSDNode>(St->getValue());
12099 } else
12100 Ops.push_back(ChainVal->getOperand(i));
12101 }
12102 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000012103
Evan Cheng536e6672009-03-12 05:59:15 +000012104 if (!Ld || !ISD::isNormalLoad(Ld))
12105 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000012106
Evan Cheng536e6672009-03-12 05:59:15 +000012107 // If this is not the MMX case, i.e. we are just turning i64 load/store
12108 // into f64 load/store, avoid the transformation if there are multiple
12109 // uses of the loaded value.
12110 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
12111 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000012112
Evan Cheng536e6672009-03-12 05:59:15 +000012113 DebugLoc LdDL = Ld->getDebugLoc();
12114 DebugLoc StDL = N->getDebugLoc();
12115 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
12116 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
12117 // pair instead.
12118 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000012119 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000012120 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
12121 Ld->getPointerInfo(), Ld->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000012122 Ld->isNonTemporal(), Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000012123 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000012124 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000012125 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000012126 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000012127 Ops.size());
12128 }
Evan Cheng536e6672009-03-12 05:59:15 +000012129 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000012130 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000012131 St->isVolatile(), St->isNonTemporal(),
12132 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000012133 }
Evan Cheng536e6672009-03-12 05:59:15 +000012134
12135 // Otherwise, lower to two pairs of 32-bit loads / stores.
12136 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000012137 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
12138 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000012139
Owen Anderson825b72b2009-08-11 20:47:22 +000012140 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000012141 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000012142 Ld->isVolatile(), Ld->isNonTemporal(),
12143 Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000012144 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000012145 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000012146 Ld->isVolatile(), Ld->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000012147 MinAlign(Ld->getAlignment(), 4));
12148
12149 SDValue NewChain = LoLd.getValue(1);
12150 if (TokenFactorIndex != -1) {
12151 Ops.push_back(LoLd);
12152 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000012153 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000012154 Ops.size());
12155 }
12156
12157 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000012158 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
12159 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000012160
12161 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000012162 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000012163 St->isVolatile(), St->isNonTemporal(),
12164 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000012165 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000012166 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000012167 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000012168 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000012169 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000012170 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000012171 }
Dan Gohman475871a2008-07-27 21:46:04 +000012172 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000012173}
12174
Chris Lattner6cf73262008-01-25 06:14:17 +000012175/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
12176/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000012177static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000012178 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
12179 // F[X]OR(0.0, x) -> x
12180 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000012181 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
12182 if (C->getValueAPF().isPosZero())
12183 return N->getOperand(1);
12184 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
12185 if (C->getValueAPF().isPosZero())
12186 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000012187 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000012188}
12189
12190/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000012191static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000012192 // FAND(0.0, x) -> 0.0
12193 // FAND(x, 0.0) -> 0.0
12194 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
12195 if (C->getValueAPF().isPosZero())
12196 return N->getOperand(0);
12197 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
12198 if (C->getValueAPF().isPosZero())
12199 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000012200 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000012201}
12202
Dan Gohmane5af2d32009-01-29 01:59:02 +000012203static SDValue PerformBTCombine(SDNode *N,
12204 SelectionDAG &DAG,
12205 TargetLowering::DAGCombinerInfo &DCI) {
12206 // BT ignores high bits in the bit index operand.
12207 SDValue Op1 = N->getOperand(1);
12208 if (Op1.hasOneUse()) {
12209 unsigned BitWidth = Op1.getValueSizeInBits();
12210 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
12211 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000012212 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
12213 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000012214 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000012215 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
12216 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
12217 DCI.CommitTargetLoweringOpt(TLO);
12218 }
12219 return SDValue();
12220}
Chris Lattner83e6c992006-10-04 06:57:07 +000012221
Eli Friedman7a5e5552009-06-07 06:52:44 +000012222static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
12223 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000012224 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000012225 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000012226 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000012227 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000012228 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000012229 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000012230 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000012231 }
12232 return SDValue();
12233}
12234
Evan Cheng2e489c42009-12-16 00:53:11 +000012235static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
12236 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
12237 // (and (i32 x86isd::setcc_carry), 1)
12238 // This eliminates the zext. This transformation is necessary because
12239 // ISD::SETCC is always legalized to i8.
12240 DebugLoc dl = N->getDebugLoc();
12241 SDValue N0 = N->getOperand(0);
12242 EVT VT = N->getValueType(0);
12243 if (N0.getOpcode() == ISD::AND &&
12244 N0.hasOneUse() &&
12245 N0.getOperand(0).hasOneUse()) {
12246 SDValue N00 = N0.getOperand(0);
12247 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
12248 return SDValue();
12249 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
12250 if (!C || C->getZExtValue() != 1)
12251 return SDValue();
12252 return DAG.getNode(ISD::AND, dl, VT,
12253 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
12254 N00.getOperand(0), N00.getOperand(1)),
12255 DAG.getConstant(1, VT));
12256 }
12257
12258 return SDValue();
12259}
12260
Chris Lattnerc19d1c32010-12-19 22:08:31 +000012261// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
12262static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG) {
12263 unsigned X86CC = N->getConstantOperandVal(0);
12264 SDValue EFLAG = N->getOperand(1);
12265 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012266
Chris Lattnerc19d1c32010-12-19 22:08:31 +000012267 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
12268 // a zext and produces an all-ones bit which is more useful than 0/1 in some
12269 // cases.
12270 if (X86CC == X86::COND_B)
12271 return DAG.getNode(ISD::AND, DL, MVT::i8,
12272 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
12273 DAG.getConstant(X86CC, MVT::i8), EFLAG),
12274 DAG.getConstant(1, MVT::i8));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012275
Chris Lattnerc19d1c32010-12-19 22:08:31 +000012276 return SDValue();
12277}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012278
Benjamin Kramer1396c402011-06-18 11:09:41 +000012279static SDValue PerformSINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG,
12280 const X86TargetLowering *XTLI) {
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000012281 SDValue Op0 = N->getOperand(0);
12282 // Transform (SINT_TO_FP (i64 ...)) into an x87 operation if we have
12283 // a 32-bit target where SSE doesn't support i64->FP operations.
12284 if (Op0.getOpcode() == ISD::LOAD) {
12285 LoadSDNode *Ld = cast<LoadSDNode>(Op0.getNode());
12286 EVT VT = Ld->getValueType(0);
12287 if (!Ld->isVolatile() && !N->getValueType(0).isVector() &&
12288 ISD::isNON_EXTLoad(Op0.getNode()) && Op0.hasOneUse() &&
12289 !XTLI->getSubtarget()->is64Bit() &&
12290 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Benjamin Kramer1396c402011-06-18 11:09:41 +000012291 SDValue FILDChain = XTLI->BuildFILD(SDValue(N, 0), Ld->getValueType(0),
12292 Ld->getChain(), Op0, DAG);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000012293 DAG.ReplaceAllUsesOfValueWith(Op0.getValue(1), FILDChain.getValue(1));
12294 return FILDChain;
12295 }
12296 }
12297 return SDValue();
12298}
12299
Chris Lattner23a01992010-12-20 01:37:09 +000012300// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
12301static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
12302 X86TargetLowering::DAGCombinerInfo &DCI) {
12303 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
12304 // the result is either zero or one (depending on the input carry bit).
12305 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
12306 if (X86::isZeroNode(N->getOperand(0)) &&
12307 X86::isZeroNode(N->getOperand(1)) &&
12308 // We don't have a good way to replace an EFLAGS use, so only do this when
12309 // dead right now.
12310 SDValue(N, 1).use_empty()) {
12311 DebugLoc DL = N->getDebugLoc();
12312 EVT VT = N->getValueType(0);
12313 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
12314 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
12315 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
12316 DAG.getConstant(X86::COND_B,MVT::i8),
12317 N->getOperand(2)),
12318 DAG.getConstant(1, VT));
12319 return DCI.CombineTo(N, Res1, CarryOut);
12320 }
12321
12322 return SDValue();
12323}
12324
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000012325// fold (add Y, (sete X, 0)) -> adc 0, Y
12326// (add Y, (setne X, 0)) -> sbb -1, Y
12327// (sub (sete X, 0), Y) -> sbb 0, Y
12328// (sub (setne X, 0), Y) -> adc -1, Y
12329static SDValue OptimizeConditonalInDecrement(SDNode *N, SelectionDAG &DAG) {
12330 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012331
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000012332 // Look through ZExts.
12333 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
12334 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
12335 return SDValue();
12336
12337 SDValue SetCC = Ext.getOperand(0);
12338 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
12339 return SDValue();
12340
12341 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
12342 if (CC != X86::COND_E && CC != X86::COND_NE)
12343 return SDValue();
12344
12345 SDValue Cmp = SetCC.getOperand(1);
12346 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000012347 !X86::isZeroNode(Cmp.getOperand(1)) ||
12348 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000012349 return SDValue();
12350
12351 SDValue CmpOp0 = Cmp.getOperand(0);
12352 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
12353 DAG.getConstant(1, CmpOp0.getValueType()));
12354
12355 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
12356 if (CC == X86::COND_NE)
12357 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
12358 DL, OtherVal.getValueType(), OtherVal,
12359 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
12360 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
12361 DL, OtherVal.getValueType(), OtherVal,
12362 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
12363}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000012364
Dan Gohman475871a2008-07-27 21:46:04 +000012365SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000012366 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000012367 SelectionDAG &DAG = DCI.DAG;
12368 switch (N->getOpcode()) {
12369 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012370 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerc19d1c32010-12-19 22:08:31 +000012371 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, *this);
Chris Lattneraf723b92008-01-25 05:46:26 +000012372 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +000012373 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000012374 case ISD::ADD:
12375 case ISD::SUB: return OptimizeConditonalInDecrement(N, DAG);
Chris Lattner23a01992010-12-20 01:37:09 +000012376 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000012377 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000012378 case ISD::SHL:
12379 case ISD::SRA:
12380 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000012381 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000012382 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000012383 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000012384 case ISD::SINT_TO_FP: return PerformSINT_TO_FPCombine(N, DAG, this);
Chris Lattner6cf73262008-01-25 06:14:17 +000012385 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000012386 case X86ISD::FOR: return PerformFORCombine(N, DAG);
12387 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000012388 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000012389 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Evan Cheng2e489c42009-12-16 00:53:11 +000012390 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG);
Chris Lattnerc19d1c32010-12-19 22:08:31 +000012391 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000012392 case X86ISD::SHUFPS: // Handle all target specific shuffles
12393 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000012394 case X86ISD::PALIGN:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000012395 case X86ISD::PUNPCKHBW:
12396 case X86ISD::PUNPCKHWD:
12397 case X86ISD::PUNPCKHDQ:
12398 case X86ISD::PUNPCKHQDQ:
12399 case X86ISD::UNPCKHPS:
12400 case X86ISD::UNPCKHPD:
12401 case X86ISD::PUNPCKLBW:
12402 case X86ISD::PUNPCKLWD:
12403 case X86ISD::PUNPCKLDQ:
12404 case X86ISD::PUNPCKLQDQ:
12405 case X86ISD::UNPCKLPS:
12406 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +000012407 case X86ISD::VUNPCKLPS:
12408 case X86ISD::VUNPCKLPD:
12409 case X86ISD::VUNPCKLPSY:
12410 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000012411 case X86ISD::MOVHLPS:
12412 case X86ISD::MOVLHPS:
12413 case X86ISD::PSHUFD:
12414 case X86ISD::PSHUFHW:
12415 case X86ISD::PSHUFLW:
12416 case X86ISD::MOVSS:
12417 case X86ISD::MOVSD:
Mon P Wanga0fd0d52010-12-19 23:55:53 +000012418 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI);
Evan Cheng206ee9d2006-07-07 08:33:52 +000012419 }
12420
Dan Gohman475871a2008-07-27 21:46:04 +000012421 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000012422}
12423
Evan Chenge5b51ac2010-04-17 06:13:15 +000012424/// isTypeDesirableForOp - Return true if the target has native support for
12425/// the specified value type and it is 'desirable' to use the type for the
12426/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
12427/// instruction encodings are longer and some i16 instructions are slow.
12428bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
12429 if (!isTypeLegal(VT))
12430 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000012431 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000012432 return true;
12433
12434 switch (Opc) {
12435 default:
12436 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000012437 case ISD::LOAD:
12438 case ISD::SIGN_EXTEND:
12439 case ISD::ZERO_EXTEND:
12440 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000012441 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000012442 case ISD::SRL:
12443 case ISD::SUB:
12444 case ISD::ADD:
12445 case ISD::MUL:
12446 case ISD::AND:
12447 case ISD::OR:
12448 case ISD::XOR:
12449 return false;
12450 }
12451}
12452
12453/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000012454/// beneficial for dag combiner to promote the specified node. If true, it
12455/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000012456bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000012457 EVT VT = Op.getValueType();
12458 if (VT != MVT::i16)
12459 return false;
12460
Evan Cheng4c26e932010-04-19 19:29:22 +000012461 bool Promote = false;
12462 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000012463 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000012464 default: break;
12465 case ISD::LOAD: {
12466 LoadSDNode *LD = cast<LoadSDNode>(Op);
12467 // If the non-extending load has a single use and it's not live out, then it
12468 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000012469 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
12470 Op.hasOneUse()*/) {
12471 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
12472 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
12473 // The only case where we'd want to promote LOAD (rather then it being
12474 // promoted as an operand is when it's only use is liveout.
12475 if (UI->getOpcode() != ISD::CopyToReg)
12476 return false;
12477 }
12478 }
Evan Cheng4c26e932010-04-19 19:29:22 +000012479 Promote = true;
12480 break;
12481 }
12482 case ISD::SIGN_EXTEND:
12483 case ISD::ZERO_EXTEND:
12484 case ISD::ANY_EXTEND:
12485 Promote = true;
12486 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000012487 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000012488 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000012489 SDValue N0 = Op.getOperand(0);
12490 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000012491 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000012492 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000012493 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000012494 break;
12495 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000012496 case ISD::ADD:
12497 case ISD::MUL:
12498 case ISD::AND:
12499 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000012500 case ISD::XOR:
12501 Commute = true;
12502 // fallthrough
12503 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000012504 SDValue N0 = Op.getOperand(0);
12505 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000012506 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000012507 return false;
12508 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000012509 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000012510 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000012511 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000012512 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000012513 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000012514 }
12515 }
12516
12517 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000012518 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000012519}
12520
Evan Cheng60c07e12006-07-05 22:17:51 +000012521//===----------------------------------------------------------------------===//
12522// X86 Inline Assembly Support
12523//===----------------------------------------------------------------------===//
12524
Chris Lattnerb8105652009-07-20 17:51:36 +000012525bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
12526 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000012527
12528 std::string AsmStr = IA->getAsmString();
12529
12530 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000012531 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000012532 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000012533
12534 switch (AsmPieces.size()) {
12535 default: return false;
12536 case 1:
12537 AsmStr = AsmPieces[0];
12538 AsmPieces.clear();
12539 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
12540
Chris Lattner7a2bdde2011-04-15 05:18:47 +000012541 // FIXME: this should verify that we are targeting a 486 or better. If not,
Evan Cheng55d42002011-01-08 01:24:27 +000012542 // we will turn this bswap into something that will be lowered to logical ops
12543 // instead of emitting the bswap asm. For now, we don't support 486 or lower
12544 // so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000012545 // bswap $0
12546 if (AsmPieces.size() == 2 &&
12547 (AsmPieces[0] == "bswap" ||
12548 AsmPieces[0] == "bswapq" ||
12549 AsmPieces[0] == "bswapl") &&
12550 (AsmPieces[1] == "$0" ||
12551 AsmPieces[1] == "${0:q}")) {
12552 // No need to check constraints, nothing other than the equivalent of
12553 // "=r,0" would be valid here.
Evan Cheng55d42002011-01-08 01:24:27 +000012554 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
12555 if (!Ty || Ty->getBitWidth() % 16 != 0)
12556 return false;
12557 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000012558 }
12559 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000012560 if (CI->getType()->isIntegerTy(16) &&
Chris Lattnerb8105652009-07-20 17:51:36 +000012561 AsmPieces.size() == 3 &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000012562 (AsmPieces[0] == "rorw" || AsmPieces[0] == "rolw") &&
Chris Lattnerb8105652009-07-20 17:51:36 +000012563 AsmPieces[1] == "$$8," &&
12564 AsmPieces[2] == "${0:w}" &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000012565 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
12566 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000012567 const std::string &ConstraintsStr = IA->getConstraintString();
12568 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000012569 std::sort(AsmPieces.begin(), AsmPieces.end());
12570 if (AsmPieces.size() == 4 &&
12571 AsmPieces[0] == "~{cc}" &&
12572 AsmPieces[1] == "~{dirflag}" &&
12573 AsmPieces[2] == "~{flags}" &&
12574 AsmPieces[3] == "~{fpsr}") {
Evan Cheng55d42002011-01-08 01:24:27 +000012575 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
12576 if (!Ty || Ty->getBitWidth() % 16 != 0)
12577 return false;
12578 return IntrinsicLowering::LowerToByteSwap(CI);
Dan Gohman0ef701e2010-03-04 19:58:08 +000012579 }
Chris Lattnerb8105652009-07-20 17:51:36 +000012580 }
12581 break;
12582 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000012583 if (CI->getType()->isIntegerTy(32) &&
12584 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
12585 SmallVector<StringRef, 4> Words;
12586 SplitString(AsmPieces[0], Words, " \t,");
12587 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
12588 Words[2] == "${0:w}") {
12589 Words.clear();
12590 SplitString(AsmPieces[1], Words, " \t,");
12591 if (Words.size() == 3 && Words[0] == "rorl" && Words[1] == "$$16" &&
12592 Words[2] == "$0") {
12593 Words.clear();
12594 SplitString(AsmPieces[2], Words, " \t,");
12595 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
12596 Words[2] == "${0:w}") {
12597 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000012598 const std::string &ConstraintsStr = IA->getConstraintString();
12599 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Peter Collingbourne948cf022010-11-13 19:54:30 +000012600 std::sort(AsmPieces.begin(), AsmPieces.end());
12601 if (AsmPieces.size() == 4 &&
12602 AsmPieces[0] == "~{cc}" &&
12603 AsmPieces[1] == "~{dirflag}" &&
12604 AsmPieces[2] == "~{flags}" &&
12605 AsmPieces[3] == "~{fpsr}") {
Evan Cheng55d42002011-01-08 01:24:27 +000012606 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
12607 if (!Ty || Ty->getBitWidth() % 16 != 0)
12608 return false;
12609 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000012610 }
12611 }
12612 }
12613 }
12614 }
Evan Cheng55d42002011-01-08 01:24:27 +000012615
12616 if (CI->getType()->isIntegerTy(64)) {
12617 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
12618 if (Constraints.size() >= 2 &&
12619 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
12620 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
12621 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
12622 SmallVector<StringRef, 4> Words;
12623 SplitString(AsmPieces[0], Words, " \t");
12624 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
Chris Lattnerb8105652009-07-20 17:51:36 +000012625 Words.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000012626 SplitString(AsmPieces[1], Words, " \t");
12627 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
12628 Words.clear();
12629 SplitString(AsmPieces[2], Words, " \t,");
12630 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
12631 Words[2] == "%edx") {
12632 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
12633 if (!Ty || Ty->getBitWidth() % 16 != 0)
12634 return false;
12635 return IntrinsicLowering::LowerToByteSwap(CI);
12636 }
Chris Lattnerb8105652009-07-20 17:51:36 +000012637 }
12638 }
12639 }
12640 }
12641 break;
12642 }
12643 return false;
12644}
12645
12646
12647
Chris Lattnerf4dff842006-07-11 02:54:03 +000012648/// getConstraintType - Given a constraint letter, return the type of
12649/// constraint it is for this target.
12650X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000012651X86TargetLowering::getConstraintType(const std::string &Constraint) const {
12652 if (Constraint.size() == 1) {
12653 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000012654 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000012655 case 'q':
12656 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000012657 case 'f':
12658 case 't':
12659 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000012660 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000012661 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000012662 case 'Y':
Eric Christopher31b5f002011-07-07 22:29:07 +000012663 case 'l':
Chris Lattner4234f572007-03-25 02:14:49 +000012664 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000012665 case 'a':
12666 case 'b':
12667 case 'c':
12668 case 'd':
12669 case 'S':
12670 case 'D':
12671 case 'A':
12672 return C_Register;
12673 case 'I':
12674 case 'J':
12675 case 'K':
12676 case 'L':
12677 case 'M':
12678 case 'N':
12679 case 'G':
12680 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000012681 case 'e':
12682 case 'Z':
12683 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000012684 default:
12685 break;
12686 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000012687 }
Chris Lattner4234f572007-03-25 02:14:49 +000012688 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000012689}
12690
John Thompson44ab89e2010-10-29 17:29:13 +000012691/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000012692/// This object must already have been set up with the operand type
12693/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000012694TargetLowering::ConstraintWeight
12695 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000012696 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000012697 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012698 Value *CallOperandVal = info.CallOperandVal;
12699 // If we don't have a value, we can't do a match,
12700 // but allow it at the lowest weight.
12701 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000012702 return CW_Default;
12703 const Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000012704 // Look at the constraint type.
12705 switch (*constraint) {
12706 default:
John Thompson44ab89e2010-10-29 17:29:13 +000012707 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
12708 case 'R':
12709 case 'q':
12710 case 'Q':
12711 case 'a':
12712 case 'b':
12713 case 'c':
12714 case 'd':
12715 case 'S':
12716 case 'D':
12717 case 'A':
12718 if (CallOperandVal->getType()->isIntegerTy())
12719 weight = CW_SpecificReg;
12720 break;
12721 case 'f':
12722 case 't':
12723 case 'u':
12724 if (type->isFloatingPointTy())
12725 weight = CW_SpecificReg;
12726 break;
12727 case 'y':
Chris Lattner2a786eb2010-12-19 20:19:20 +000012728 if (type->isX86_MMXTy() && Subtarget->hasMMX())
John Thompson44ab89e2010-10-29 17:29:13 +000012729 weight = CW_SpecificReg;
12730 break;
12731 case 'x':
12732 case 'Y':
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012733 if ((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasXMM())
John Thompson44ab89e2010-10-29 17:29:13 +000012734 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012735 break;
12736 case 'I':
12737 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
12738 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000012739 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012740 }
12741 break;
John Thompson44ab89e2010-10-29 17:29:13 +000012742 case 'J':
12743 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12744 if (C->getZExtValue() <= 63)
12745 weight = CW_Constant;
12746 }
12747 break;
12748 case 'K':
12749 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12750 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
12751 weight = CW_Constant;
12752 }
12753 break;
12754 case 'L':
12755 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12756 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
12757 weight = CW_Constant;
12758 }
12759 break;
12760 case 'M':
12761 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12762 if (C->getZExtValue() <= 3)
12763 weight = CW_Constant;
12764 }
12765 break;
12766 case 'N':
12767 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12768 if (C->getZExtValue() <= 0xff)
12769 weight = CW_Constant;
12770 }
12771 break;
12772 case 'G':
12773 case 'C':
12774 if (dyn_cast<ConstantFP>(CallOperandVal)) {
12775 weight = CW_Constant;
12776 }
12777 break;
12778 case 'e':
12779 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12780 if ((C->getSExtValue() >= -0x80000000LL) &&
12781 (C->getSExtValue() <= 0x7fffffffLL))
12782 weight = CW_Constant;
12783 }
12784 break;
12785 case 'Z':
12786 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12787 if (C->getZExtValue() <= 0xffffffff)
12788 weight = CW_Constant;
12789 }
12790 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012791 }
12792 return weight;
12793}
12794
Dale Johannesenba2a0b92008-01-29 02:21:21 +000012795/// LowerXConstraint - try to replace an X constraint, which matches anything,
12796/// with another that has more specific requirements based on the type of the
12797/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000012798const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000012799LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000012800 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
12801 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000012802 if (ConstraintVT.isFloatingPoint()) {
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012803 if (Subtarget->hasXMMInt())
Chris Lattner5e764232008-04-26 23:02:14 +000012804 return "Y";
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012805 if (Subtarget->hasXMM())
Chris Lattner5e764232008-04-26 23:02:14 +000012806 return "x";
12807 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012808
Chris Lattner5e764232008-04-26 23:02:14 +000012809 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000012810}
12811
Chris Lattner48884cd2007-08-25 00:47:38 +000012812/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
12813/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000012814void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +000012815 std::string &Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000012816 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000012817 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000012818 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000012819
Eric Christopher100c8332011-06-02 23:16:42 +000012820 // Only support length 1 constraints for now.
12821 if (Constraint.length() > 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +000012822
Eric Christopher100c8332011-06-02 23:16:42 +000012823 char ConstraintLetter = Constraint[0];
12824 switch (ConstraintLetter) {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012825 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000012826 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000012827 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000012828 if (C->getZExtValue() <= 31) {
12829 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000012830 break;
12831 }
Devang Patel84f7fd22007-03-17 00:13:28 +000012832 }
Chris Lattner48884cd2007-08-25 00:47:38 +000012833 return;
Evan Cheng364091e2008-09-22 23:57:37 +000012834 case 'J':
12835 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000012836 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000012837 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
12838 break;
12839 }
12840 }
12841 return;
12842 case 'K':
12843 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000012844 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000012845 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
12846 break;
12847 }
12848 }
12849 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000012850 case 'N':
12851 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000012852 if (C->getZExtValue() <= 255) {
12853 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000012854 break;
12855 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000012856 }
Chris Lattner48884cd2007-08-25 00:47:38 +000012857 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000012858 case 'e': {
12859 // 32-bit signed value
12860 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000012861 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
12862 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000012863 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000012864 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000012865 break;
12866 }
12867 // FIXME gcc accepts some relocatable values here too, but only in certain
12868 // memory models; it's complicated.
12869 }
12870 return;
12871 }
12872 case 'Z': {
12873 // 32-bit unsigned value
12874 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000012875 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
12876 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000012877 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
12878 break;
12879 }
12880 }
12881 // FIXME gcc accepts some relocatable values here too, but only in certain
12882 // memory models; it's complicated.
12883 return;
12884 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012885 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012886 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000012887 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000012888 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000012889 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000012890 break;
12891 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012892
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000012893 // In any sort of PIC mode addresses need to be computed at runtime by
12894 // adding in a register or some sort of table lookup. These can't
12895 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000012896 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000012897 return;
12898
Chris Lattnerdc43a882007-05-03 16:52:29 +000012899 // If we are in non-pic codegen mode, we allow the address of a global (with
12900 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000012901 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000012902 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000012903
Chris Lattner49921962009-05-08 18:23:14 +000012904 // Match either (GA), (GA+C), (GA+C1+C2), etc.
12905 while (1) {
12906 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
12907 Offset += GA->getOffset();
12908 break;
12909 } else if (Op.getOpcode() == ISD::ADD) {
12910 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
12911 Offset += C->getZExtValue();
12912 Op = Op.getOperand(0);
12913 continue;
12914 }
12915 } else if (Op.getOpcode() == ISD::SUB) {
12916 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
12917 Offset += -C->getZExtValue();
12918 Op = Op.getOperand(0);
12919 continue;
12920 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012921 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012922
Chris Lattner49921962009-05-08 18:23:14 +000012923 // Otherwise, this isn't something we can handle, reject it.
12924 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000012925 }
Eric Christopherfd179292009-08-27 18:07:15 +000012926
Dan Gohman46510a72010-04-15 01:51:59 +000012927 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012928 // If we require an extra load to get this address, as in PIC mode, we
12929 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000012930 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
12931 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012932 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000012933
Devang Patel0d881da2010-07-06 22:08:15 +000012934 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
12935 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000012936 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012937 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012938 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012939
Gabor Greifba36cb52008-08-28 21:40:38 +000012940 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000012941 Ops.push_back(Result);
12942 return;
12943 }
Dale Johannesen1784d162010-06-25 21:55:36 +000012944 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012945}
12946
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012947std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000012948X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000012949 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000012950 // First, see if this is a constraint that directly corresponds to an LLVM
12951 // register class.
12952 if (Constraint.size() == 1) {
12953 // GCC Constraint Letters
12954 switch (Constraint[0]) {
12955 default: break;
Eric Christopherd176af82011-06-29 17:23:50 +000012956 // TODO: Slight differences here in allocation order and leaving
12957 // RIP in the class. Do they matter any more here than they do
12958 // in the normal allocation?
12959 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
12960 if (Subtarget->is64Bit()) {
Nick Lewycky9bf45d02011-07-08 00:19:27 +000012961 if (VT == MVT::i32 || VT == MVT::f32)
Eric Christopherd176af82011-06-29 17:23:50 +000012962 return std::make_pair(0U, X86::GR32RegisterClass);
12963 else if (VT == MVT::i16)
12964 return std::make_pair(0U, X86::GR16RegisterClass);
Eric Christopher5427ede2011-07-14 20:13:52 +000012965 else if (VT == MVT::i8 || VT == MVT::i1)
Eric Christopherd176af82011-06-29 17:23:50 +000012966 return std::make_pair(0U, X86::GR8RegisterClass);
Nick Lewycky9bf45d02011-07-08 00:19:27 +000012967 else if (VT == MVT::i64 || VT == MVT::f64)
Eric Christopherd176af82011-06-29 17:23:50 +000012968 return std::make_pair(0U, X86::GR64RegisterClass);
12969 break;
12970 }
12971 // 32-bit fallthrough
12972 case 'Q': // Q_REGS
Nick Lewycky9bf45d02011-07-08 00:19:27 +000012973 if (VT == MVT::i32 || VT == MVT::f32)
Eric Christopherd176af82011-06-29 17:23:50 +000012974 return std::make_pair(0U, X86::GR32_ABCDRegisterClass);
12975 else if (VT == MVT::i16)
12976 return std::make_pair(0U, X86::GR16_ABCDRegisterClass);
Eric Christopher5427ede2011-07-14 20:13:52 +000012977 else if (VT == MVT::i8 || VT == MVT::i1)
Eric Christopherd176af82011-06-29 17:23:50 +000012978 return std::make_pair(0U, X86::GR8_ABCD_LRegisterClass);
12979 else if (VT == MVT::i64)
12980 return std::make_pair(0U, X86::GR64_ABCDRegisterClass);
12981 break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000012982 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000012983 case 'l': // INDEX_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000012984 if (VT == MVT::i8 || VT == MVT::i1)
Chris Lattner0f65cad2007-04-09 05:49:22 +000012985 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012986 if (VT == MVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +000012987 return std::make_pair(0U, X86::GR16RegisterClass);
Eric Christopher2bbecd82011-05-19 21:33:47 +000012988 if (VT == MVT::i32 || VT == MVT::f32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +000012989 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +000012990 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000012991 case 'R': // LEGACY_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000012992 if (VT == MVT::i8 || VT == MVT::i1)
Dale Johannesen5f3663e2009-10-07 22:47:20 +000012993 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
12994 if (VT == MVT::i16)
12995 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
12996 if (VT == MVT::i32 || !Subtarget->is64Bit())
12997 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
12998 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000012999 case 'f': // FP Stack registers.
13000 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
13001 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000013002 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000013003 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000013004 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000013005 return std::make_pair(0U, X86::RFP64RegisterClass);
13006 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000013007 case 'y': // MMX_REGS if MMX allowed.
13008 if (!Subtarget->hasMMX()) break;
13009 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000013010 case 'Y': // SSE_REGS if SSE2 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000013011 if (!Subtarget->hasXMMInt()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000013012 // FALL THROUGH.
13013 case 'x': // SSE_REGS if SSE1 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000013014 if (!Subtarget->hasXMM()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000013015
Owen Anderson825b72b2009-08-11 20:47:22 +000013016 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000013017 default: break;
13018 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000013019 case MVT::f32:
13020 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +000013021 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000013022 case MVT::f64:
13023 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +000013024 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000013025 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000013026 case MVT::v16i8:
13027 case MVT::v8i16:
13028 case MVT::v4i32:
13029 case MVT::v2i64:
13030 case MVT::v4f32:
13031 case MVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +000013032 return std::make_pair(0U, X86::VR128RegisterClass);
13033 }
Chris Lattnerad043e82007-04-09 05:11:28 +000013034 break;
13035 }
13036 }
Scott Michelfdc40a02009-02-17 22:15:04 +000013037
Chris Lattnerf76d1802006-07-31 23:26:50 +000013038 // Use the default implementation in TargetLowering to convert the register
13039 // constraint into a member of a register class.
13040 std::pair<unsigned, const TargetRegisterClass*> Res;
13041 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000013042
13043 // Not found as a standard register?
13044 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000013045 // Map st(0) -> st(7) -> ST0
13046 if (Constraint.size() == 7 && Constraint[0] == '{' &&
13047 tolower(Constraint[1]) == 's' &&
13048 tolower(Constraint[2]) == 't' &&
13049 Constraint[3] == '(' &&
13050 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
13051 Constraint[5] == ')' &&
13052 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000013053
Chris Lattner56d77c72009-09-13 22:41:48 +000013054 Res.first = X86::ST0+Constraint[4]-'0';
13055 Res.second = X86::RFP80RegisterClass;
13056 return Res;
13057 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000013058
Chris Lattner56d77c72009-09-13 22:41:48 +000013059 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000013060 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000013061 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +000013062 Res.second = X86::RFP80RegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000013063 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000013064 }
Chris Lattner56d77c72009-09-13 22:41:48 +000013065
13066 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000013067 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000013068 Res.first = X86::EFLAGS;
13069 Res.second = X86::CCRRegisterClass;
13070 return Res;
13071 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000013072
Dale Johannesen330169f2008-11-13 21:52:36 +000013073 // 'A' means EAX + EDX.
13074 if (Constraint == "A") {
13075 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +000013076 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000013077 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000013078 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000013079 return Res;
13080 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013081
Chris Lattnerf76d1802006-07-31 23:26:50 +000013082 // Otherwise, check to see if this is a register class of the wrong value
13083 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
13084 // turn into {ax},{dx}.
13085 if (Res.second->hasType(VT))
13086 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013087
Chris Lattnerf76d1802006-07-31 23:26:50 +000013088 // All of the single-register GCC register classes map their values onto
13089 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
13090 // really want an 8-bit or 32-bit register, map to the appropriate register
13091 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +000013092 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000013093 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000013094 unsigned DestReg = 0;
13095 switch (Res.first) {
13096 default: break;
13097 case X86::AX: DestReg = X86::AL; break;
13098 case X86::DX: DestReg = X86::DL; break;
13099 case X86::CX: DestReg = X86::CL; break;
13100 case X86::BX: DestReg = X86::BL; break;
13101 }
13102 if (DestReg) {
13103 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000013104 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000013105 }
Owen Anderson825b72b2009-08-11 20:47:22 +000013106 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000013107 unsigned DestReg = 0;
13108 switch (Res.first) {
13109 default: break;
13110 case X86::AX: DestReg = X86::EAX; break;
13111 case X86::DX: DestReg = X86::EDX; break;
13112 case X86::CX: DestReg = X86::ECX; break;
13113 case X86::BX: DestReg = X86::EBX; break;
13114 case X86::SI: DestReg = X86::ESI; break;
13115 case X86::DI: DestReg = X86::EDI; break;
13116 case X86::BP: DestReg = X86::EBP; break;
13117 case X86::SP: DestReg = X86::ESP; break;
13118 }
13119 if (DestReg) {
13120 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000013121 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000013122 }
Owen Anderson825b72b2009-08-11 20:47:22 +000013123 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000013124 unsigned DestReg = 0;
13125 switch (Res.first) {
13126 default: break;
13127 case X86::AX: DestReg = X86::RAX; break;
13128 case X86::DX: DestReg = X86::RDX; break;
13129 case X86::CX: DestReg = X86::RCX; break;
13130 case X86::BX: DestReg = X86::RBX; break;
13131 case X86::SI: DestReg = X86::RSI; break;
13132 case X86::DI: DestReg = X86::RDI; break;
13133 case X86::BP: DestReg = X86::RBP; break;
13134 case X86::SP: DestReg = X86::RSP; break;
13135 }
13136 if (DestReg) {
13137 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000013138 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000013139 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000013140 }
Chris Lattner6ba50a92008-08-26 06:19:02 +000013141 } else if (Res.second == X86::FR32RegisterClass ||
13142 Res.second == X86::FR64RegisterClass ||
13143 Res.second == X86::VR128RegisterClass) {
13144 // Handle references to XMM physical registers that got mapped into the
13145 // wrong class. This can happen with constraints like {xmm0} where the
13146 // target independent register mapper will just pick the first match it can
13147 // find, ignoring the required type.
Owen Anderson825b72b2009-08-11 20:47:22 +000013148 if (VT == MVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +000013149 Res.second = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +000013150 else if (VT == MVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +000013151 Res.second = X86::FR64RegisterClass;
13152 else if (X86::VR128RegisterClass->hasType(VT))
13153 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000013154 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013155
Chris Lattnerf76d1802006-07-31 23:26:50 +000013156 return Res;
13157}