blob: fa9bf9aa28e8671077aa9ae27470c6c4bbf80bf4 [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);
991 setOperationAction(ISD::LOAD, MVT::v8i32, Legal);
992 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
993 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
David Greene54d8eba2011-01-27 22:38:56 +0000994
Owen Anderson825b72b2009-08-11 20:47:22 +0000995 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
996 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
997 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
998 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
999 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
1000 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001001
Owen Anderson825b72b2009-08-11 20:47:22 +00001002 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
1003 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
1004 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
1005 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
1006 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
1007 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001008
David Greene54d8eba2011-01-27 22:38:56 +00001009 // Custom lower build_vector, vector_shuffle, scalar_to_vector,
1010 // insert_vector_elt extract_subvector and extract_vector_elt for
1011 // 256-bit types.
1012 for (unsigned i = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
1013 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE;
1014 ++i) {
1015 MVT::SimpleValueType VT = (MVT::SimpleValueType)i;
1016 // Do not attempt to custom lower non-256-bit vectors
1017 if (!isPowerOf2_32(MVT(VT).getVectorNumElements())
1018 || (MVT(VT).getSizeInBits() < 256))
David Greene9b9838d2009-06-29 16:47:10 +00001019 continue;
David Greene9b9838d2009-06-29 16:47:10 +00001020 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
1021 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
David Greene54d8eba2011-01-27 22:38:56 +00001022 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001023 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
David Greene54d8eba2011-01-27 22:38:56 +00001024 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001025 }
David Greene54d8eba2011-01-27 22:38:56 +00001026 // Custom-lower insert_subvector and extract_subvector based on
1027 // the result type.
1028 for (unsigned i = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
1029 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE;
1030 ++i) {
1031 MVT::SimpleValueType VT = (MVT::SimpleValueType)i;
1032 // Do not attempt to custom lower non-256-bit vectors
1033 if (!isPowerOf2_32(MVT(VT).getVectorNumElements()))
David Greene9b9838d2009-06-29 16:47:10 +00001034 continue;
David Greene54d8eba2011-01-27 22:38:56 +00001035
1036 if (MVT(VT).getSizeInBits() == 128) {
1037 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001038 }
David Greene54d8eba2011-01-27 22:38:56 +00001039 else if (MVT(VT).getSizeInBits() == 256) {
1040 setOperationAction(ISD::INSERT_SUBVECTOR, VT, Custom);
1041 }
David Greene9b9838d2009-06-29 16:47:10 +00001042 }
1043
David Greene54d8eba2011-01-27 22:38:56 +00001044 // Promote v32i8, v16i16, v8i32 select, and, or, xor to v4i64.
1045 // Don't promote loads because we need them for VPERM vector index versions.
1046
1047 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
1048 VT != (unsigned)MVT::LAST_VECTOR_VALUETYPE;
1049 VT++) {
1050 if (!isPowerOf2_32(MVT((MVT::SimpleValueType)VT).getVectorNumElements())
1051 || (MVT((MVT::SimpleValueType)VT).getSizeInBits() < 256))
1052 continue;
1053 setOperationAction(ISD::AND, (MVT::SimpleValueType)VT, Promote);
1054 AddPromotedToType (ISD::AND, (MVT::SimpleValueType)VT, MVT::v4i64);
1055 setOperationAction(ISD::OR, (MVT::SimpleValueType)VT, Promote);
1056 AddPromotedToType (ISD::OR, (MVT::SimpleValueType)VT, MVT::v4i64);
1057 setOperationAction(ISD::XOR, (MVT::SimpleValueType)VT, Promote);
1058 AddPromotedToType (ISD::XOR, (MVT::SimpleValueType)VT, MVT::v4i64);
1059 //setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Promote);
1060 //AddPromotedToType (ISD::LOAD, (MVT::SimpleValueType)VT, MVT::v4i64);
1061 setOperationAction(ISD::SELECT, (MVT::SimpleValueType)VT, Promote);
1062 AddPromotedToType (ISD::SELECT, (MVT::SimpleValueType)VT, MVT::v4i64);
1063 }
David Greene9b9838d2009-06-29 16:47:10 +00001064 }
1065
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001066
1067 // SIGN_EXTEND_INREGs are evaluated by the extend type. Handle the expansion
1068 // of this type with custom code.
1069 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
1070 VT != (unsigned)MVT::LAST_VECTOR_VALUETYPE; VT++) {
1071 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT, Custom);
1072 }
1073
Evan Cheng6be2c582006-04-05 23:38:46 +00001074 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +00001075 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +00001076
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001077
Eli Friedman962f5492010-06-02 19:35:46 +00001078 // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
1079 // handle type legalization for these operations here.
Dan Gohman71c62a22010-06-02 19:13:40 +00001080 //
Eli Friedman962f5492010-06-02 19:35:46 +00001081 // FIXME: We really should do custom legalization for addition and
1082 // subtraction on x86-32 once PR3203 is fixed. We really can't do much better
1083 // than generic legalization for 64-bit multiplication-with-overflow, though.
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001084 for (unsigned i = 0, e = 3+Subtarget->is64Bit(); i != e; ++i) {
1085 // Add/Sub/Mul with overflow operations are custom lowered.
1086 MVT VT = IntVTs[i];
1087 setOperationAction(ISD::SADDO, VT, Custom);
1088 setOperationAction(ISD::UADDO, VT, Custom);
1089 setOperationAction(ISD::SSUBO, VT, Custom);
1090 setOperationAction(ISD::USUBO, VT, Custom);
1091 setOperationAction(ISD::SMULO, VT, Custom);
1092 setOperationAction(ISD::UMULO, VT, Custom);
Eli Friedmana993f0a2010-06-02 00:27:18 +00001093 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001094
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001095 // There are no 8-bit 3-address imul/mul instructions
1096 setOperationAction(ISD::SMULO, MVT::i8, Expand);
1097 setOperationAction(ISD::UMULO, MVT::i8, Expand);
Bill Wendling41ea7e72008-11-24 19:21:46 +00001098
Evan Chengd54f2d52009-03-31 19:38:51 +00001099 if (!Subtarget->is64Bit()) {
1100 // These libcalls are not available in 32-bit.
1101 setLibcallName(RTLIB::SHL_I128, 0);
1102 setLibcallName(RTLIB::SRL_I128, 0);
1103 setLibcallName(RTLIB::SRA_I128, 0);
1104 }
1105
Evan Cheng206ee9d2006-07-07 08:33:52 +00001106 // We have target-specific dag combine patterns for the following nodes:
1107 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00001108 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Evan Chengd880b972008-05-09 21:53:03 +00001109 setTargetDAGCombine(ISD::BUILD_VECTOR);
Chris Lattner83e6c992006-10-04 06:57:07 +00001110 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +00001111 setTargetDAGCombine(ISD::SHL);
1112 setTargetDAGCombine(ISD::SRA);
1113 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +00001114 setTargetDAGCombine(ISD::OR);
Nate Begemanb65c1752010-12-17 22:55:37 +00001115 setTargetDAGCombine(ISD::AND);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001116 setTargetDAGCombine(ISD::ADD);
1117 setTargetDAGCombine(ISD::SUB);
Chris Lattner149a4e52008-02-22 02:09:43 +00001118 setTargetDAGCombine(ISD::STORE);
Evan Cheng2e489c42009-12-16 00:53:11 +00001119 setTargetDAGCombine(ISD::ZERO_EXTEND);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +00001120 setTargetDAGCombine(ISD::SINT_TO_FP);
Evan Cheng0b0cd912009-03-28 05:57:29 +00001121 if (Subtarget->is64Bit())
1122 setTargetDAGCombine(ISD::MUL);
Evan Cheng206ee9d2006-07-07 08:33:52 +00001123
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001124 computeRegisterProperties();
1125
Evan Cheng05219282011-01-06 06:52:41 +00001126 // On Darwin, -Os means optimize for size without hurting performance,
1127 // do not reduce the limit.
Dan Gohman87060f52008-06-30 21:00:56 +00001128 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001129 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 16 : 8;
Evan Cheng255f20f2010-04-01 06:04:33 +00001130 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001131 maxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
1132 maxStoresPerMemmove = 8; // For @llvm.memmove -> sequence of stores
1133 maxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Evan Chengfb8075d2008-02-28 00:43:03 +00001134 setPrefLoopAlignment(16);
Evan Cheng6ebf7bc2009-05-13 21:42:09 +00001135 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +00001136
1137 setPrefFunctionAlignment(4);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001138}
1139
Scott Michel5b8f82e2008-03-10 15:42:14 +00001140
Owen Anderson825b72b2009-08-11 20:47:22 +00001141MVT::SimpleValueType X86TargetLowering::getSetCCResultType(EVT VT) const {
1142 return MVT::i8;
Scott Michel5b8f82e2008-03-10 15:42:14 +00001143}
1144
1145
Evan Cheng29286502008-01-23 23:17:41 +00001146/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1147/// the desired ByVal argument alignment.
1148static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
1149 if (MaxAlign == 16)
1150 return;
1151 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
1152 if (VTy->getBitWidth() == 128)
1153 MaxAlign = 16;
Evan Cheng29286502008-01-23 23:17:41 +00001154 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
1155 unsigned EltAlign = 0;
1156 getMaxByValAlign(ATy->getElementType(), EltAlign);
1157 if (EltAlign > MaxAlign)
1158 MaxAlign = EltAlign;
1159 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
1160 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1161 unsigned EltAlign = 0;
1162 getMaxByValAlign(STy->getElementType(i), EltAlign);
1163 if (EltAlign > MaxAlign)
1164 MaxAlign = EltAlign;
1165 if (MaxAlign == 16)
1166 break;
1167 }
1168 }
1169 return;
1170}
1171
1172/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1173/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001174/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1175/// are at 4-byte boundaries.
Evan Cheng29286502008-01-23 23:17:41 +00001176unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001177 if (Subtarget->is64Bit()) {
1178 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001179 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001180 if (TyAlign > 8)
1181 return TyAlign;
1182 return 8;
1183 }
1184
Evan Cheng29286502008-01-23 23:17:41 +00001185 unsigned Align = 4;
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001186 if (Subtarget->hasXMM())
Dale Johannesen0c191872008-02-08 19:48:20 +00001187 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001188 return Align;
1189}
Chris Lattner2b02a442007-02-25 08:29:00 +00001190
Evan Chengf0df0312008-05-15 08:39:06 +00001191/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001192/// and store operations as a result of memset, memcpy, and memmove
1193/// lowering. If DstAlign is zero that means it's safe to destination
1194/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1195/// means there isn't a need to check it against alignment requirement,
1196/// probably because the source does not need to be loaded. If
1197/// 'NonScalarIntSafe' is true, that means it's safe to return a
1198/// non-scalar-integer type, e.g. empty string source, constant, or loaded
1199/// from memory. 'MemcpyStrSrc' indicates whether the memcpy source is
1200/// constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001201/// It returns EVT::Other if the type should be determined using generic
1202/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001203EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001204X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1205 unsigned DstAlign, unsigned SrcAlign,
Evan Chengf28f8bc2010-04-02 19:36:14 +00001206 bool NonScalarIntSafe,
Evan Chengc3b0c342010-04-08 07:37:57 +00001207 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001208 MachineFunction &MF) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001209 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1210 // linux. This is because the stack realignment code can't handle certain
1211 // cases like PR2962. This should be removed when PR2962 is fixed.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001212 const Function *F = MF.getFunction();
Evan Chenga5e13622011-01-07 19:35:30 +00001213 if (NonScalarIntSafe &&
1214 !F->hasFnAttr(Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001215 if (Size >= 16 &&
Evan Chenga5e13622011-01-07 19:35:30 +00001216 (Subtarget->isUnalignedMemAccessFast() ||
1217 ((DstAlign == 0 || DstAlign >= 16) &&
1218 (SrcAlign == 0 || SrcAlign >= 16))) &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001219 Subtarget->getStackAlignment() >= 16) {
1220 if (Subtarget->hasSSE2())
1221 return MVT::v4i32;
Evan Chengf28f8bc2010-04-02 19:36:14 +00001222 if (Subtarget->hasSSE1())
Evan Cheng255f20f2010-04-01 06:04:33 +00001223 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001224 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001225 !Subtarget->is64Bit() &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001226 Subtarget->getStackAlignment() >= 8 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001227 Subtarget->hasXMMInt()) {
Evan Chengc3b0c342010-04-08 07:37:57 +00001228 // Do not use f64 to lower memcpy if source is string constant. It's
1229 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001230 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001231 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001232 }
Evan Chengf0df0312008-05-15 08:39:06 +00001233 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001234 return MVT::i64;
1235 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001236}
1237
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001238/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1239/// current function. The returned value is a member of the
1240/// MachineJumpTableInfo::JTEntryKind enum.
1241unsigned X86TargetLowering::getJumpTableEncoding() const {
1242 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1243 // symbol.
1244 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1245 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001246 return MachineJumpTableInfo::EK_Custom32;
Michael J. Spencerec38de22010-10-10 22:04:20 +00001247
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001248 // Otherwise, use the normal jump table encoding heuristics.
1249 return TargetLowering::getJumpTableEncoding();
1250}
1251
Chris Lattnerc64daab2010-01-26 05:02:42 +00001252const MCExpr *
1253X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1254 const MachineBasicBlock *MBB,
1255 unsigned uid,MCContext &Ctx) const{
1256 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1257 Subtarget->isPICStyleGOT());
1258 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1259 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001260 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1261 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001262}
1263
Evan Chengcc415862007-11-09 01:32:10 +00001264/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1265/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001266SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001267 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001268 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001269 // This doesn't have DebugLoc associated with it, but is not really the
1270 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001271 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001272 return Table;
1273}
1274
Chris Lattner589c6f62010-01-26 06:28:43 +00001275/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1276/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1277/// MCExpr.
1278const MCExpr *X86TargetLowering::
1279getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1280 MCContext &Ctx) const {
1281 // X86-64 uses RIP relative addressing based on the jump table label.
1282 if (Subtarget->isPICStyleRIPRel())
1283 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1284
1285 // Otherwise, the reference is relative to the PIC base.
Chris Lattner142b5312010-11-14 22:48:15 +00001286 return MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
Chris Lattner589c6f62010-01-26 06:28:43 +00001287}
1288
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001289// FIXME: Why this routine is here? Move to RegInfo!
Evan Chengdee81012010-07-26 21:50:05 +00001290std::pair<const TargetRegisterClass*, uint8_t>
1291X86TargetLowering::findRepresentativeClass(EVT VT) const{
1292 const TargetRegisterClass *RRC = 0;
1293 uint8_t Cost = 1;
1294 switch (VT.getSimpleVT().SimpleTy) {
1295 default:
1296 return TargetLowering::findRepresentativeClass(VT);
1297 case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
1298 RRC = (Subtarget->is64Bit()
1299 ? X86::GR64RegisterClass : X86::GR32RegisterClass);
1300 break;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001301 case MVT::x86mmx:
Evan Chengdee81012010-07-26 21:50:05 +00001302 RRC = X86::VR64RegisterClass;
1303 break;
1304 case MVT::f32: case MVT::f64:
1305 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1306 case MVT::v4f32: case MVT::v2f64:
1307 case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1308 case MVT::v4f64:
1309 RRC = X86::VR128RegisterClass;
1310 break;
1311 }
1312 return std::make_pair(RRC, Cost);
1313}
1314
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001315bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1316 unsigned &Offset) const {
1317 if (!Subtarget->isTargetLinux())
1318 return false;
1319
1320 if (Subtarget->is64Bit()) {
1321 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1322 Offset = 0x28;
1323 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1324 AddressSpace = 256;
1325 else
1326 AddressSpace = 257;
1327 } else {
1328 // %gs:0x14 on i386
1329 Offset = 0x14;
1330 AddressSpace = 256;
1331 }
1332 return true;
1333}
1334
1335
Chris Lattner2b02a442007-02-25 08:29:00 +00001336//===----------------------------------------------------------------------===//
1337// Return Value Calling Convention Implementation
1338//===----------------------------------------------------------------------===//
1339
Chris Lattner59ed56b2007-02-28 04:55:35 +00001340#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001341
Michael J. Spencerec38de22010-10-10 22:04:20 +00001342bool
Eric Christopher471e4222011-06-08 23:55:35 +00001343X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv,
1344 MachineFunction &MF, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001345 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001346 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001347 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001348 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001349 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001350 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001351}
1352
Dan Gohman98ca4f22009-08-05 01:29:28 +00001353SDValue
1354X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001355 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001356 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001357 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001358 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001359 MachineFunction &MF = DAG.getMachineFunction();
1360 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001361
Chris Lattner9774c912007-02-27 05:28:59 +00001362 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001363 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001364 RVLocs, *DAG.getContext());
1365 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001366
Evan Chengdcea1632010-02-04 02:40:39 +00001367 // Add the regs to the liveout set for the function.
1368 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1369 for (unsigned i = 0; i != RVLocs.size(); ++i)
1370 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1371 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001372
Dan Gohman475871a2008-07-27 21:46:04 +00001373 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001374
Dan Gohman475871a2008-07-27 21:46:04 +00001375 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001376 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1377 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001378 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1379 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001380
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001381 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001382 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1383 CCValAssign &VA = RVLocs[i];
1384 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001385 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001386 EVT ValVT = ValToCopy.getValueType();
1387
Dale Johannesenc4510512010-09-24 19:05:48 +00001388 // If this is x86-64, and we disabled SSE, we can't return FP values,
1389 // or SSE or MMX vectors.
1390 if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
1391 VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001392 (Subtarget->is64Bit() && !Subtarget->hasXMM())) {
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001393 report_fatal_error("SSE register return with SSE disabled");
1394 }
1395 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1396 // llvm-gcc has never done it right and no one has noticed, so this
1397 // should be OK for now.
1398 if (ValVT == MVT::f64 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001399 (Subtarget->is64Bit() && !Subtarget->hasXMMInt()))
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001400 report_fatal_error("SSE2 register return with SSE2 disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001401
Chris Lattner447ff682008-03-11 03:23:40 +00001402 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1403 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001404 if (VA.getLocReg() == X86::ST0 ||
1405 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001406 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1407 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001408 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001409 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001410 RetOps.push_back(ValToCopy);
1411 // Don't emit a copytoreg.
1412 continue;
1413 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001414
Evan Cheng242b38b2009-02-23 09:03:22 +00001415 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1416 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001417 if (Subtarget->is64Bit()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001418 if (ValVT == MVT::x86mmx) {
Chris Lattner97a2a562010-08-26 05:24:29 +00001419 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001420 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ValToCopy);
Eric Christopher90eb4022010-07-22 00:26:08 +00001421 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1422 ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001423 // If we don't have SSE2 available, convert to v4f32 so the generated
1424 // register is legal.
1425 if (!Subtarget->hasSSE2())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001426 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32,ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001427 }
Evan Cheng242b38b2009-02-23 09:03:22 +00001428 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001429 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00001430
Dale Johannesendd64c412009-02-04 00:33:20 +00001431 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001432 Flag = Chain.getValue(1);
1433 }
Dan Gohman61a92132008-04-21 23:59:07 +00001434
1435 // The x86-64 ABI for returning structs by value requires that we copy
1436 // the sret argument into %rax for the return. We saved the argument into
1437 // a virtual register in the entry block, so now we copy the value out
1438 // and into %rax.
1439 if (Subtarget->is64Bit() &&
1440 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1441 MachineFunction &MF = DAG.getMachineFunction();
1442 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1443 unsigned Reg = FuncInfo->getSRetReturnReg();
Michael J. Spencerec38de22010-10-10 22:04:20 +00001444 assert(Reg &&
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001445 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001446 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001447
Dale Johannesendd64c412009-02-04 00:33:20 +00001448 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001449 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001450
1451 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001452 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001453 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001454
Chris Lattner447ff682008-03-11 03:23:40 +00001455 RetOps[0] = Chain; // Update chain.
1456
1457 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001458 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001459 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001460
1461 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001462 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001463}
1464
Evan Cheng3d2125c2010-11-30 23:55:39 +00001465bool X86TargetLowering::isUsedByReturnOnly(SDNode *N) const {
1466 if (N->getNumValues() != 1)
1467 return false;
1468 if (!N->hasNUsesOfValue(1, 0))
1469 return false;
1470
1471 SDNode *Copy = *N->use_begin();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001472 if (Copy->getOpcode() != ISD::CopyToReg &&
1473 Copy->getOpcode() != ISD::FP_EXTEND)
Evan Cheng3d2125c2010-11-30 23:55:39 +00001474 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001475
1476 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001477 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001478 UI != UE; ++UI) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001479 if (UI->getOpcode() != X86ISD::RET_FLAG)
1480 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001481 HasRet = true;
1482 }
Evan Cheng3d2125c2010-11-30 23:55:39 +00001483
Evan Cheng1bf891a2010-12-01 22:59:46 +00001484 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001485}
1486
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001487EVT
1488X86TargetLowering::getTypeForExtArgOrReturn(LLVMContext &Context, EVT VT,
Cameron Zwarich44579682011-03-17 14:21:56 +00001489 ISD::NodeType ExtendKind) const {
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001490 MVT ReturnMVT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001491 // TODO: Is this also valid on 32-bit?
1492 if (Subtarget->is64Bit() && VT == MVT::i1 && ExtendKind == ISD::ZERO_EXTEND)
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001493 ReturnMVT = MVT::i8;
1494 else
1495 ReturnMVT = MVT::i32;
1496
1497 EVT MinVT = getRegisterType(Context, ReturnMVT);
1498 return VT.bitsLT(MinVT) ? MinVT : VT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001499}
1500
Dan Gohman98ca4f22009-08-05 01:29:28 +00001501/// LowerCallResult - Lower the result values of a call into the
1502/// appropriate copies out of appropriate physical registers.
1503///
1504SDValue
1505X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001506 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001507 const SmallVectorImpl<ISD::InputArg> &Ins,
1508 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001509 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001510
Chris Lattnere32bbf62007-02-28 07:09:55 +00001511 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001512 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001513 bool Is64Bit = Subtarget->is64Bit();
Eric Christopher471e4222011-06-08 23:55:35 +00001514 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1515 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001516 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001517
Chris Lattner3085e152007-02-25 08:59:22 +00001518 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001519 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001520 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001521 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001522
Torok Edwin3f142c32009-02-01 18:15:56 +00001523 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001524 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001525 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasXMM())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001526 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001527 }
1528
Evan Cheng79fb3b42009-02-20 20:43:02 +00001529 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001530
1531 // If this is a call to a function that returns an fp value on the floating
1532 // point stack, we must guarantee the the value is popped from the stack, so
1533 // a CopyFromReg is not good enough - the copy instruction may be eliminated
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001534 // if the return value is not used. We use the FpPOP_RETVAL instruction
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001535 // instead.
1536 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1537 // If we prefer to use the value in xmm registers, copy it out as f80 and
1538 // use a truncate to move it from fp stack reg to xmm reg.
1539 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001540 SDValue Ops[] = { Chain, InFlag };
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001541 Chain = SDValue(DAG.getMachineNode(X86::FpPOP_RETVAL, dl, CopyVT,
1542 MVT::Other, MVT::Glue, Ops, 2), 1);
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001543 Val = Chain.getValue(0);
1544
1545 // Round the f80 to the right size, which also moves it to the appropriate
1546 // xmm register.
1547 if (CopyVT != VA.getValVT())
1548 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1549 // This truncation won't change the value.
1550 DAG.getIntPtrConstant(1));
Evan Cheng79fb3b42009-02-20 20:43:02 +00001551 } else {
1552 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1553 CopyVT, InFlag).getValue(1);
1554 Val = Chain.getValue(0);
1555 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001556 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001557 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001558 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001559
Dan Gohman98ca4f22009-08-05 01:29:28 +00001560 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001561}
1562
1563
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001564//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001565// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001566//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001567// StdCall calling convention seems to be standard for many Windows' API
1568// routines and around. It differs from C calling convention just a little:
1569// callee should clean up the stack, not caller. Symbols should be also
1570// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001571// For info on fast calling convention see Fast Calling Convention (tail call)
1572// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001573
Dan Gohman98ca4f22009-08-05 01:29:28 +00001574/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001575/// semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001576static bool CallIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
1577 if (Outs.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001578 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001579
Dan Gohman98ca4f22009-08-05 01:29:28 +00001580 return Outs[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001581}
1582
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001583/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001584/// return semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001585static bool
1586ArgsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
1587 if (Ins.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001588 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001589
Dan Gohman98ca4f22009-08-05 01:29:28 +00001590 return Ins[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001591}
1592
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001593/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1594/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001595/// the specific parameter attribute. The copy will be passed as a byval
1596/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001597static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001598CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001599 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1600 DebugLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00001601 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Michael J. Spencerec38de22010-10-10 22:04:20 +00001602
Dale Johannesendd64c412009-02-04 00:33:20 +00001603 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Stuart Hastings03d58262011-03-10 00:25:53 +00001604 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001605 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001606}
1607
Chris Lattner29689432010-03-11 00:22:57 +00001608/// IsTailCallConvention - Return true if the calling convention is one that
1609/// supports tail call optimization.
1610static bool IsTailCallConvention(CallingConv::ID CC) {
1611 return (CC == CallingConv::Fast || CC == CallingConv::GHC);
1612}
1613
Evan Cheng485fafc2011-03-21 01:19:09 +00001614bool X86TargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
1615 if (!CI->isTailCall())
1616 return false;
1617
1618 CallSite CS(CI);
1619 CallingConv::ID CalleeCC = CS.getCallingConv();
1620 if (!IsTailCallConvention(CalleeCC) && CalleeCC != CallingConv::C)
1621 return false;
1622
1623 return true;
1624}
1625
Evan Cheng0c439eb2010-01-27 00:07:07 +00001626/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1627/// a tailcall target by changing its ABI.
1628static bool FuncIsMadeTailCallSafe(CallingConv::ID CC) {
Chris Lattner29689432010-03-11 00:22:57 +00001629 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001630}
1631
Dan Gohman98ca4f22009-08-05 01:29:28 +00001632SDValue
1633X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001634 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001635 const SmallVectorImpl<ISD::InputArg> &Ins,
1636 DebugLoc dl, SelectionDAG &DAG,
1637 const CCValAssign &VA,
1638 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001639 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001640 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001641 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Evan Cheng0c439eb2010-01-27 00:07:07 +00001642 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001643 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001644 EVT ValVT;
1645
1646 // If value is passed by pointer we have address passed instead of the value
1647 // itself.
1648 if (VA.getLocInfo() == CCValAssign::Indirect)
1649 ValVT = VA.getLocVT();
1650 else
1651 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001652
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001653 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001654 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001655 // In case of tail call optimization mark all arguments mutable. Since they
1656 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001657 if (Flags.isByVal()) {
Evan Chengee2e0e32011-03-30 23:44:13 +00001658 unsigned Bytes = Flags.getByValSize();
1659 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
1660 int FI = MFI->CreateFixedObject(Bytes, VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001661 return DAG.getFrameIndex(FI, getPointerTy());
1662 } else {
1663 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001664 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001665 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1666 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001667 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00001668 false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001669 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001670}
1671
Dan Gohman475871a2008-07-27 21:46:04 +00001672SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001673X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001674 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001675 bool isVarArg,
1676 const SmallVectorImpl<ISD::InputArg> &Ins,
1677 DebugLoc dl,
1678 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001679 SmallVectorImpl<SDValue> &InVals)
1680 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001681 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001682 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001683
Gordon Henriksen86737662008-01-05 16:56:59 +00001684 const Function* Fn = MF.getFunction();
1685 if (Fn->hasExternalLinkage() &&
1686 Subtarget->isTargetCygMing() &&
1687 Fn->getName() == "main")
1688 FuncInfo->setForceFramePointer(true);
1689
Evan Cheng1bc78042006-04-26 01:20:17 +00001690 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001691 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001692 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001693
Chris Lattner29689432010-03-11 00:22:57 +00001694 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1695 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001696
Chris Lattner638402b2007-02-28 07:00:42 +00001697 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001698 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001699 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001700 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001701
1702 // Allocate shadow area for Win64
1703 if (IsWin64) {
1704 CCInfo.AllocateStack(32, 8);
1705 }
1706
Duncan Sands45907662010-10-31 13:21:44 +00001707 CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001708
Chris Lattnerf39f7712007-02-28 05:46:49 +00001709 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001710 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001711 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1712 CCValAssign &VA = ArgLocs[i];
1713 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1714 // places.
1715 assert(VA.getValNo() != LastVal &&
1716 "Don't support value assigned to multiple locs yet");
1717 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001718
Chris Lattnerf39f7712007-02-28 05:46:49 +00001719 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001720 EVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001721 TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001722 if (RegVT == MVT::i32)
Chris Lattnerf39f7712007-02-28 05:46:49 +00001723 RC = X86::GR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001724 else if (Is64Bit && RegVT == MVT::i64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001725 RC = X86::GR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001726 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001727 RC = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001728 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001729 RC = X86::FR64RegisterClass;
Bruno Cardoso Lopesac098352010-08-05 23:35:51 +00001730 else if (RegVT.isVector() && RegVT.getSizeInBits() == 256)
1731 RC = X86::VR256RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001732 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001733 RC = X86::VR128RegisterClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001734 else if (RegVT == MVT::x86mmx)
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001735 RC = X86::VR64RegisterClass;
1736 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001737 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001738
Devang Patel68e6bee2011-02-21 23:21:26 +00001739 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001740 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001741
Chris Lattnerf39f7712007-02-28 05:46:49 +00001742 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1743 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1744 // right size.
1745 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001746 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001747 DAG.getValueType(VA.getValVT()));
1748 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001749 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001750 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001751 else if (VA.getLocInfo() == CCValAssign::BCvt)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001752 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001753
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001754 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001755 // Handle MMX values passed in XMM regs.
1756 if (RegVT.isVector()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001757 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(),
1758 ArgValue);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001759 } else
1760 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001761 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001762 } else {
1763 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001764 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001765 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001766
1767 // If value is passed via pointer - do a load.
1768 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00001769 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
1770 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001771
Dan Gohman98ca4f22009-08-05 01:29:28 +00001772 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001773 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001774
Dan Gohman61a92132008-04-21 23:59:07 +00001775 // The x86-64 ABI for returning structs by value requires that we copy
1776 // the sret argument into %rax for the return. Save the argument into
1777 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001778 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001779 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1780 unsigned Reg = FuncInfo->getSRetReturnReg();
1781 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001782 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001783 FuncInfo->setSRetReturnReg(Reg);
1784 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001785 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001786 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001787 }
1788
Chris Lattnerf39f7712007-02-28 05:46:49 +00001789 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001790 // Align stack specially for tail calls.
1791 if (FuncIsMadeTailCallSafe(CallConv))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001792 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001793
Evan Cheng1bc78042006-04-26 01:20:17 +00001794 // If the function takes variable number of arguments, make a frame index for
1795 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001796 if (isVarArg) {
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001797 if (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
1798 CallConv != CallingConv::X86_ThisCall)) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00001799 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00001800 }
1801 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001802 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1803
1804 // FIXME: We should really autogenerate these arrays
1805 static const unsigned GPR64ArgRegsWin64[] = {
1806 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001807 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001808 static const unsigned GPR64ArgRegs64Bit[] = {
1809 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1810 };
1811 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001812 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1813 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1814 };
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001815 const unsigned *GPR64ArgRegs;
1816 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001817
1818 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001819 // The XMM registers which might contain var arg parameters are shadowed
1820 // in their paired GPR. So we only need to save the GPR to their home
1821 // slots.
1822 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001823 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001824 } else {
1825 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1826 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001827
1828 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit, TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001829 }
1830 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1831 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001832
Devang Patel578efa92009-06-05 21:57:13 +00001833 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001834 assert(!(NumXMMRegs && !Subtarget->hasXMM()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001835 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001836 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001837 "SSE register cannot be used when SSE is disabled!");
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001838 if (UseSoftFloat || NoImplicitFloatOps || !Subtarget->hasXMM())
Torok Edwin3f142c32009-02-01 18:15:56 +00001839 // Kernel mode asks for SSE to be disabled, so don't push them
1840 // on the stack.
1841 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001842
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001843 if (IsWin64) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001844 const TargetFrameLowering &TFI = *getTargetMachine().getFrameLowering();
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001845 // Get to the caller-allocated home save location. Add 8 to account
1846 // for the return address.
1847 int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001848 FuncInfo->setRegSaveFrameIndex(
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001849 MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001850 // Fixup to set vararg frame on shadow area (4 x i64).
1851 if (NumIntRegs < 4)
1852 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001853 } else {
1854 // For X86-64, if there are vararg parameters that are passed via
1855 // registers, then we must store them to their spots on the stack so they
1856 // may be loaded by deferencing the result of va_next.
1857 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
1858 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
1859 FuncInfo->setRegSaveFrameIndex(
1860 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00001861 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001862 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001863
Gordon Henriksen86737662008-01-05 16:56:59 +00001864 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001865 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00001866 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
1867 getPointerTy());
1868 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001869 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00001870 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
1871 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00001872 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00001873 X86::GR64RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00001874 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001875 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001876 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001877 MachinePointerInfo::getFixedStack(
1878 FuncInfo->getRegSaveFrameIndex(), Offset),
1879 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001880 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001881 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001882 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001883
Dan Gohmanface41a2009-08-16 21:24:25 +00001884 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
1885 // Now store the XMM (fp + vector) parameter registers.
1886 SmallVector<SDValue, 11> SaveXMMOps;
1887 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001888
Devang Patel68e6bee2011-02-21 23:21:26 +00001889 unsigned AL = MF.addLiveIn(X86::AL, X86::GR8RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00001890 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
1891 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001892
Dan Gohman1e93df62010-04-17 14:41:14 +00001893 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1894 FuncInfo->getRegSaveFrameIndex()));
1895 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1896 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00001897
Dan Gohmanface41a2009-08-16 21:24:25 +00001898 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001899 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00001900 X86::VR128RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00001901 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
1902 SaveXMMOps.push_back(Val);
1903 }
1904 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
1905 MVT::Other,
1906 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00001907 }
Dan Gohmanface41a2009-08-16 21:24:25 +00001908
1909 if (!MemOps.empty())
1910 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1911 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001912 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001913 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001914
Gordon Henriksen86737662008-01-05 16:56:59 +00001915 // Some CCs need callee pop.
Evan Chengef41ff62011-06-23 17:54:54 +00001916 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg, GuaranteedTailCallOpt)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001917 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001918 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00001919 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001920 // If this is an sret function, the return should pop the hidden pointer.
Chris Lattner29689432010-03-11 00:22:57 +00001921 if (!Is64Bit && !IsTailCallConvention(CallConv) && ArgsAreStructReturn(Ins))
Dan Gohman1e93df62010-04-17 14:41:14 +00001922 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001923 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001924
Gordon Henriksen86737662008-01-05 16:56:59 +00001925 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001926 // RegSaveFrameIndex is X86-64 only.
1927 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001928 if (CallConv == CallingConv::X86_FastCall ||
1929 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00001930 // fastcc functions can't have varargs.
1931 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00001932 }
Evan Cheng25caf632006-05-23 21:06:34 +00001933
Dan Gohman98ca4f22009-08-05 01:29:28 +00001934 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001935}
1936
Dan Gohman475871a2008-07-27 21:46:04 +00001937SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001938X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
1939 SDValue StackPtr, SDValue Arg,
1940 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00001941 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001942 ISD::ArgFlagsTy Flags) const {
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001943 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00001944 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00001945 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001946 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00001947 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001948
1949 return DAG.getStore(Chain, dl, Arg, PtrOff,
1950 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00001951 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00001952}
1953
Bill Wendling64e87322009-01-16 19:25:27 +00001954/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001955/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00001956SDValue
1957X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00001958 SDValue &OutRetAddr, SDValue Chain,
1959 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00001960 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001961 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00001962 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001963 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00001964
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001965 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00001966 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
1967 false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001968 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001969}
1970
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001971/// EmitTailCallStoreRetAddr - Emit a store of the return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001972/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00001973static SDValue
1974EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00001975 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00001976 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001977 // Store the return address to the appropriate stack slot.
1978 if (!FPDiff) return Chain;
1979 // Calculate the new stack slot for the return address.
1980 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00001981 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00001982 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00001983 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00001984 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001985 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00001986 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00001987 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001988 return Chain;
1989}
1990
Dan Gohman98ca4f22009-08-05 01:29:28 +00001991SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001992X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001993 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001994 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001995 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001996 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001997 const SmallVectorImpl<ISD::InputArg> &Ins,
1998 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001999 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00002000 MachineFunction &MF = DAG.getMachineFunction();
2001 bool Is64Bit = Subtarget->is64Bit();
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002002 bool IsWin64 = Subtarget->isTargetWin64();
Dan Gohman98ca4f22009-08-05 01:29:28 +00002003 bool IsStructRet = CallIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00002004 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002005
Evan Cheng5f941932010-02-05 02:21:12 +00002006 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00002007 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00002008 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
2009 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00002010 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00002011
2012 // Sibcalls are automatically detected tailcalls which do not require
2013 // ABI changes.
Dan Gohman1797ed52010-02-08 20:27:50 +00002014 if (!GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00002015 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00002016
2017 if (isTailCall)
2018 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00002019 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00002020
Chris Lattner29689432010-03-11 00:22:57 +00002021 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
2022 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00002023
Chris Lattner638402b2007-02-28 07:00:42 +00002024 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00002025 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002026 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00002027 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002028
2029 // Allocate shadow area for Win64
2030 if (IsWin64) {
2031 CCInfo.AllocateStack(32, 8);
2032 }
2033
Duncan Sands45907662010-10-31 13:21:44 +00002034 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00002035
Chris Lattner423c5f42007-02-28 05:31:48 +00002036 // Get a count of how many bytes are to be pushed on the stack.
2037 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00002038 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00002039 // This is a sibcall. The memory operands are available in caller's
2040 // own caller's stack.
2041 NumBytes = 0;
Chris Lattner29689432010-03-11 00:22:57 +00002042 else if (GuaranteedTailCallOpt && IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00002043 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002044
Gordon Henriksen86737662008-01-05 16:56:59 +00002045 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00002046 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002047 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00002048 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00002049 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
2050 FPDiff = NumBytesCallerPushed - NumBytes;
2051
2052 // Set the delta of movement of the returnaddr stackslot.
2053 // But only set if delta is greater than previous delta.
2054 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
2055 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
2056 }
2057
Evan Chengf22f9b32010-02-06 03:28:46 +00002058 if (!IsSibcall)
2059 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002060
Dan Gohman475871a2008-07-27 21:46:04 +00002061 SDValue RetAddrFrIdx;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002062 // Load return address for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00002063 if (isTailCall && FPDiff)
2064 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
2065 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002066
Dan Gohman475871a2008-07-27 21:46:04 +00002067 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
2068 SmallVector<SDValue, 8> MemOpChains;
2069 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00002070
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002071 // Walk the register/memloc assignments, inserting copies/loads. In the case
2072 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00002073 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2074 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00002075 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00002076 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002077 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00002078 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00002079
Chris Lattner423c5f42007-02-28 05:31:48 +00002080 // Promote the value if needed.
2081 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002082 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00002083 case CCValAssign::Full: break;
2084 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002085 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002086 break;
2087 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002088 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002089 break;
2090 case CCValAssign::AExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002091 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
2092 // Special case: passing MMX values in XMM registers.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002093 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00002094 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
2095 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002096 } else
2097 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
2098 break;
2099 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002100 Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002101 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002102 case CCValAssign::Indirect: {
2103 // Store the argument.
2104 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00002105 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002106 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00002107 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002108 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002109 Arg = SpillSlot;
2110 break;
2111 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00002112 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002113
Chris Lattner423c5f42007-02-28 05:31:48 +00002114 if (VA.isRegLoc()) {
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002115 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
2116 if (isVarArg && IsWin64) {
2117 // Win64 ABI requires argument XMM reg to be copied to the corresponding
2118 // shadow reg if callee is a varargs function.
2119 unsigned ShadowReg = 0;
2120 switch (VA.getLocReg()) {
2121 case X86::XMM0: ShadowReg = X86::RCX; break;
2122 case X86::XMM1: ShadowReg = X86::RDX; break;
2123 case X86::XMM2: ShadowReg = X86::R8; break;
2124 case X86::XMM3: ShadowReg = X86::R9; break;
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002125 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002126 if (ShadowReg)
2127 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002128 }
Evan Chengf22f9b32010-02-06 03:28:46 +00002129 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00002130 assert(VA.isMemLoc());
2131 if (StackPtr.getNode() == 0)
2132 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
2133 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
2134 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002135 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002136 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002137
Evan Cheng32fe1032006-05-25 00:59:30 +00002138 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002139 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002140 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002141
Evan Cheng347d5f72006-04-28 21:29:37 +00002142 // Build a sequence of copy-to-reg nodes chained together with token chain
2143 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002144 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002145 // Tail call byval lowering might overwrite argument registers so in case of
2146 // tail call optimization the copies to registers are lowered later.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002147 if (!isTailCall)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002148 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002149 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002150 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002151 InFlag = Chain.getValue(1);
2152 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002153
Chris Lattner88e1fd52009-07-09 04:24:46 +00002154 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002155 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2156 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002157 if (!isTailCall) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002158 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
2159 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00002160 DebugLoc(), getPointerTy()),
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002161 InFlag);
2162 InFlag = Chain.getValue(1);
2163 } else {
2164 // If we are tail calling and generating PIC/GOT style code load the
2165 // address of the callee into ECX. The value in ecx is used as target of
2166 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2167 // for tail calls on PIC/GOT architectures. Normally we would just put the
2168 // address of GOT into ebx and then call target@PLT. But for tail calls
2169 // ebx would be restored (since ebx is callee saved) before jumping to the
2170 // target@PLT.
2171
2172 // Note: The actual moving to ECX is done further down.
2173 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2174 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2175 !G->getGlobal()->hasProtectedVisibility())
2176 Callee = LowerGlobalAddress(Callee, DAG);
2177 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002178 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002179 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002180 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002181
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002182 if (Is64Bit && isVarArg && !IsWin64) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002183 // From AMD64 ABI document:
2184 // For calls that may call functions that use varargs or stdargs
2185 // (prototype-less calls or calls to functions containing ellipsis (...) in
2186 // the declaration) %al is used as hidden argument to specify the number
2187 // of SSE registers used. The contents of %al do not need to match exactly
2188 // the number of registers, but must be an ubound on the number of SSE
2189 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002190
Gordon Henriksen86737662008-01-05 16:56:59 +00002191 // Count the number of XMM registers allocated.
2192 static const unsigned XMMArgRegs[] = {
2193 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2194 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2195 };
2196 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00002197 assert((Subtarget->hasXMM() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002198 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002199
Dale Johannesendd64c412009-02-04 00:33:20 +00002200 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Owen Anderson825b72b2009-08-11 20:47:22 +00002201 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002202 InFlag = Chain.getValue(1);
2203 }
2204
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002205
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002206 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002207 if (isTailCall) {
2208 // Force all the incoming stack arguments to be loaded from the stack
2209 // before any new outgoing arguments are stored to the stack, because the
2210 // outgoing stack slots may alias the incoming argument stack slots, and
2211 // the alias isn't otherwise explicit. This is slightly more conservative
2212 // than necessary, because it means that each store effectively depends
2213 // on every argument instead of just those arguments it would clobber.
2214 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2215
Dan Gohman475871a2008-07-27 21:46:04 +00002216 SmallVector<SDValue, 8> MemOpChains2;
2217 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002218 int FI = 0;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002219 // Do not flag preceding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00002220 InFlag = SDValue();
Dan Gohman1797ed52010-02-08 20:27:50 +00002221 if (GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002222 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2223 CCValAssign &VA = ArgLocs[i];
2224 if (VA.isRegLoc())
2225 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002226 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002227 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002228 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002229 // Create frame index.
2230 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002231 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002232 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002233 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002234
Duncan Sands276dcbd2008-03-21 09:14:45 +00002235 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002236 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002237 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002238 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002239 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00002240 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002241 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002242
Dan Gohman98ca4f22009-08-05 01:29:28 +00002243 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2244 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002245 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002246 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002247 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002248 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002249 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002250 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002251 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002252 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002253 }
2254 }
2255
2256 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002257 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002258 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002259
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002260 // Copy arguments to their registers.
2261 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002262 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002263 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002264 InFlag = Chain.getValue(1);
2265 }
Dan Gohman475871a2008-07-27 21:46:04 +00002266 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002267
Gordon Henriksen86737662008-01-05 16:56:59 +00002268 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002269 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002270 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002271 }
2272
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002273 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2274 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2275 // In the 64-bit large code model, we have to make all calls
2276 // through a register, since the call instruction's 32-bit
2277 // pc-relative offset may not be large enough to hold the whole
2278 // address.
2279 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002280 // If the callee is a GlobalAddress node (quite common, every direct call
2281 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2282 // it.
2283
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002284 // We should use extra load for direct calls to dllimported functions in
2285 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002286 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002287 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002288 unsigned char OpFlags = 0;
John McCall3a3465b2011-06-15 20:36:13 +00002289 bool ExtraLoad = false;
2290 unsigned WrapperKind = ISD::DELETED_NODE;
Eric Christopherfd179292009-08-27 18:07:15 +00002291
Chris Lattner48a7d022009-07-09 05:02:21 +00002292 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2293 // external symbols most go through the PLT in PIC mode. If the symbol
2294 // has hidden or protected visibility, or if it is static or local, then
2295 // we don't need to use the PLT - we can directly call it.
2296 if (Subtarget->isTargetELF() &&
2297 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002298 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002299 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002300 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002301 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002302 (!Subtarget->getTargetTriple().isMacOSX() ||
2303 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002304 // PC-relative references to external symbols should go through $stub,
2305 // unless we're building with the leopard linker or later, which
2306 // automatically synthesizes these stubs.
2307 OpFlags = X86II::MO_DARWIN_STUB;
John McCall3a3465b2011-06-15 20:36:13 +00002308 } else if (Subtarget->isPICStyleRIPRel() &&
2309 isa<Function>(GV) &&
2310 cast<Function>(GV)->hasFnAttr(Attribute::NonLazyBind)) {
2311 // If the function is marked as non-lazy, generate an indirect call
2312 // which loads from the GOT directly. This avoids runtime overhead
2313 // at the cost of eager binding (and one extra byte of encoding).
2314 OpFlags = X86II::MO_GOTPCREL;
2315 WrapperKind = X86ISD::WrapperRIP;
2316 ExtraLoad = true;
Chris Lattner74e726e2009-07-09 05:27:35 +00002317 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002318
Devang Patel0d881da2010-07-06 22:08:15 +00002319 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002320 G->getOffset(), OpFlags);
John McCall3a3465b2011-06-15 20:36:13 +00002321
2322 // Add a wrapper if needed.
2323 if (WrapperKind != ISD::DELETED_NODE)
2324 Callee = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Callee);
2325 // Add extra indirection if needed.
2326 if (ExtraLoad)
2327 Callee = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Callee,
2328 MachinePointerInfo::getGOT(),
2329 false, false, 0);
Chris Lattner48a7d022009-07-09 05:02:21 +00002330 }
Bill Wendling056292f2008-09-16 21:48:12 +00002331 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002332 unsigned char OpFlags = 0;
2333
Evan Cheng1bf891a2010-12-01 22:59:46 +00002334 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2335 // external symbols should go through the PLT.
2336 if (Subtarget->isTargetELF() &&
2337 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2338 OpFlags = X86II::MO_PLT;
2339 } else if (Subtarget->isPICStyleStubAny() &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002340 (!Subtarget->getTargetTriple().isMacOSX() ||
2341 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Evan Cheng1bf891a2010-12-01 22:59:46 +00002342 // PC-relative references to external symbols should go through $stub,
2343 // unless we're building with the leopard linker or later, which
2344 // automatically synthesizes these stubs.
2345 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002346 }
Eric Christopherfd179292009-08-27 18:07:15 +00002347
Chris Lattner48a7d022009-07-09 05:02:21 +00002348 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2349 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002350 }
2351
Chris Lattnerd96d0722007-02-25 06:40:16 +00002352 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002353 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002354 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002355
Evan Chengf22f9b32010-02-06 03:28:46 +00002356 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002357 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2358 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002359 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002360 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002361
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002362 Ops.push_back(Chain);
2363 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002364
Dan Gohman98ca4f22009-08-05 01:29:28 +00002365 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002366 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002367
Gordon Henriksen86737662008-01-05 16:56:59 +00002368 // Add argument registers to the end of the list so that they are known live
2369 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002370 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2371 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2372 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002373
Evan Cheng586ccac2008-03-18 23:36:35 +00002374 // Add an implicit use GOT pointer in EBX.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002375 if (!isTailCall && Subtarget->isPICStyleGOT())
Evan Cheng586ccac2008-03-18 23:36:35 +00002376 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
2377
Anton Korobeynikov3a1e54a2010-08-17 21:06:07 +00002378 // Add an implicit use of AL for non-Windows x86 64-bit vararg functions.
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002379 if (Is64Bit && isVarArg && !IsWin64)
Owen Anderson825b72b2009-08-11 20:47:22 +00002380 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
Evan Cheng586ccac2008-03-18 23:36:35 +00002381
Gabor Greifba36cb52008-08-28 21:40:38 +00002382 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002383 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002384
Dan Gohman98ca4f22009-08-05 01:29:28 +00002385 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002386 // We used to do:
2387 //// If this is the first return lowered for this function, add the regs
2388 //// to the liveout set for the function.
2389 // This isn't right, although it's probably harmless on x86; liveouts
2390 // should be computed from returns not tail calls. Consider a void
2391 // function making a tail call to a function returning int.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002392 return DAG.getNode(X86ISD::TC_RETURN, dl,
2393 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002394 }
2395
Dale Johannesenace16102009-02-03 19:33:06 +00002396 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002397 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002398
Chris Lattner2d297092006-05-23 18:50:38 +00002399 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002400 unsigned NumBytesForCalleeToPush;
Evan Chengef41ff62011-06-23 17:54:54 +00002401 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg, GuaranteedTailCallOpt))
Gordon Henriksen86737662008-01-05 16:56:59 +00002402 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Chris Lattner29689432010-03-11 00:22:57 +00002403 else if (!Is64Bit && !IsTailCallConvention(CallConv) && IsStructRet)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002404 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002405 // pops the hidden struct pointer, so we have to push it back.
2406 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002407 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002408 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002409 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002410
Gordon Henriksenae636f82008-01-03 16:47:34 +00002411 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002412 if (!IsSibcall) {
2413 Chain = DAG.getCALLSEQ_END(Chain,
2414 DAG.getIntPtrConstant(NumBytes, true),
2415 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2416 true),
2417 InFlag);
2418 InFlag = Chain.getValue(1);
2419 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002420
Chris Lattner3085e152007-02-25 08:59:22 +00002421 // Handle result values, copying them out of physregs into vregs that we
2422 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002423 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2424 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002425}
2426
Evan Cheng25ab6902006-09-08 06:48:29 +00002427
2428//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002429// Fast Calling Convention (tail call) implementation
2430//===----------------------------------------------------------------------===//
2431
2432// Like std call, callee cleans arguments, convention except that ECX is
2433// reserved for storing the tail called function address. Only 2 registers are
2434// free for argument passing (inreg). Tail call optimization is performed
2435// provided:
2436// * tailcallopt is enabled
2437// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002438// On X86_64 architecture with GOT-style position independent code only local
2439// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002440// To keep the stack aligned according to platform abi the function
2441// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2442// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002443// If a tail called function callee has more arguments than the caller the
2444// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002445// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002446// original REtADDR, but before the saved framepointer or the spilled registers
2447// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2448// stack layout:
2449// arg1
2450// arg2
2451// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002452// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002453// move area ]
2454// (possible EBP)
2455// ESI
2456// EDI
2457// local1 ..
2458
2459/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2460/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002461unsigned
2462X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2463 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002464 MachineFunction &MF = DAG.getMachineFunction();
2465 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002466 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002467 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002468 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002469 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002470 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002471 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2472 // Number smaller than 12 so just add the difference.
2473 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2474 } else {
2475 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002476 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002477 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002478 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002479 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002480}
2481
Evan Cheng5f941932010-02-05 02:21:12 +00002482/// MatchingStackOffset - Return true if the given stack call argument is
2483/// already available in the same position (relatively) of the caller's
2484/// incoming argument stack.
2485static
2486bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2487 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2488 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002489 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2490 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002491 if (Arg.getOpcode() == ISD::CopyFromReg) {
2492 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00002493 if (!TargetRegisterInfo::isVirtualRegister(VR))
Evan Cheng5f941932010-02-05 02:21:12 +00002494 return false;
2495 MachineInstr *Def = MRI->getVRegDef(VR);
2496 if (!Def)
2497 return false;
2498 if (!Flags.isByVal()) {
2499 if (!TII->isLoadFromStackSlot(Def, FI))
2500 return false;
2501 } else {
2502 unsigned Opcode = Def->getOpcode();
2503 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2504 Def->getOperand(1).isFI()) {
2505 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002506 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002507 } else
2508 return false;
2509 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002510 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2511 if (Flags.isByVal())
2512 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002513 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002514 // define @foo(%struct.X* %A) {
2515 // tail call @bar(%struct.X* byval %A)
2516 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002517 return false;
2518 SDValue Ptr = Ld->getBasePtr();
2519 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2520 if (!FINode)
2521 return false;
2522 FI = FINode->getIndex();
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002523 } else if (Arg.getOpcode() == ISD::FrameIndex && Flags.isByVal()) {
Chad Rosier14d71aa2011-06-25 18:51:28 +00002524 FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Arg);
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002525 FI = FINode->getIndex();
2526 Bytes = Flags.getByValSize();
Evan Cheng4cae1332010-03-05 08:38:04 +00002527 } else
2528 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002529
Evan Cheng4cae1332010-03-05 08:38:04 +00002530 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002531 if (!MFI->isFixedObjectIndex(FI))
2532 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002533 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002534}
2535
Dan Gohman98ca4f22009-08-05 01:29:28 +00002536/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2537/// for tail call optimization. Targets which want to do tail call
2538/// optimization should implement this function.
2539bool
2540X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002541 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002542 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002543 bool isCalleeStructRet,
2544 bool isCallerStructRet,
Evan Chengb1712452010-01-27 06:25:16 +00002545 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002546 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002547 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002548 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002549 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002550 CalleeCC != CallingConv::C)
2551 return false;
2552
Evan Cheng7096ae42010-01-29 06:45:59 +00002553 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002554 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002555 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Cheng13617962010-04-30 01:12:32 +00002556 CallingConv::ID CallerCC = CallerF->getCallingConv();
2557 bool CCMatch = CallerCC == CalleeCC;
2558
Dan Gohman1797ed52010-02-08 20:27:50 +00002559 if (GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002560 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002561 return true;
2562 return false;
2563 }
2564
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002565 // Look for obvious safe cases to perform tail call optimization that do not
2566 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002567
Evan Cheng2c12cb42010-03-26 16:26:03 +00002568 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2569 // emit a special epilogue.
2570 if (RegInfo->needsStackRealignment(MF))
2571 return false;
2572
Evan Chenga375d472010-03-15 18:54:48 +00002573 // Also avoid sibcall optimization if either caller or callee uses struct
2574 // return semantics.
2575 if (isCalleeStructRet || isCallerStructRet)
2576 return false;
2577
Chad Rosier2416da32011-06-24 21:15:36 +00002578 // An stdcall caller is expected to clean up its arguments; the callee
2579 // isn't going to do that.
2580 if (!CCMatch && CallerCC==CallingConv::X86_StdCall)
2581 return false;
2582
Chad Rosier871f6642011-05-18 19:59:50 +00002583 // Do not sibcall optimize vararg calls unless all arguments are passed via
Chad Rosiera1660892011-05-20 00:59:28 +00002584 // registers.
Chad Rosier871f6642011-05-18 19:59:50 +00002585 if (isVarArg && !Outs.empty()) {
Chad Rosiera1660892011-05-20 00:59:28 +00002586
2587 // Optimizing for varargs on Win64 is unlikely to be safe without
2588 // additional testing.
2589 if (Subtarget->isTargetWin64())
2590 return false;
2591
Chad Rosier871f6642011-05-18 19:59:50 +00002592 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002593 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
2594 getTargetMachine(), ArgLocs, *DAG.getContext());
Chad Rosier871f6642011-05-18 19:59:50 +00002595
Chad Rosier871f6642011-05-18 19:59:50 +00002596 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
2597 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i)
2598 if (!ArgLocs[i].isRegLoc())
2599 return false;
2600 }
2601
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002602 // If the call result is in ST0 / ST1, it needs to be popped off the x87 stack.
2603 // Therefore if it's not used by the call it is not safe to optimize this into
2604 // a sibcall.
2605 bool Unused = false;
2606 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2607 if (!Ins[i].Used) {
2608 Unused = true;
2609 break;
2610 }
2611 }
2612 if (Unused) {
2613 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002614 CCState CCInfo(CalleeCC, false, DAG.getMachineFunction(),
2615 getTargetMachine(), RVLocs, *DAG.getContext());
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002616 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002617 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002618 CCValAssign &VA = RVLocs[i];
2619 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2620 return false;
2621 }
2622 }
2623
Evan Cheng13617962010-04-30 01:12:32 +00002624 // If the calling conventions do not match, then we'd better make sure the
2625 // results are returned in the same way as what the caller expects.
2626 if (!CCMatch) {
2627 SmallVector<CCValAssign, 16> RVLocs1;
Eric Christopher471e4222011-06-08 23:55:35 +00002628 CCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
2629 getTargetMachine(), RVLocs1, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002630 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2631
2632 SmallVector<CCValAssign, 16> RVLocs2;
Eric Christopher471e4222011-06-08 23:55:35 +00002633 CCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
2634 getTargetMachine(), RVLocs2, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002635 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2636
2637 if (RVLocs1.size() != RVLocs2.size())
2638 return false;
2639 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2640 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2641 return false;
2642 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2643 return false;
2644 if (RVLocs1[i].isRegLoc()) {
2645 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2646 return false;
2647 } else {
2648 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2649 return false;
2650 }
2651 }
2652 }
2653
Evan Chenga6bff982010-01-30 01:22:00 +00002654 // If the callee takes no arguments then go on to check the results of the
2655 // call.
2656 if (!Outs.empty()) {
2657 // Check if stack adjustment is needed. For now, do not do this if any
2658 // argument is passed on the stack.
2659 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002660 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
2661 getTargetMachine(), ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002662
2663 // Allocate shadow area for Win64
2664 if (Subtarget->isTargetWin64()) {
2665 CCInfo.AllocateStack(32, 8);
2666 }
2667
Duncan Sands45907662010-10-31 13:21:44 +00002668 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Stuart Hastings6db2c2f2011-05-17 16:59:46 +00002669 if (CCInfo.getNextStackOffset()) {
Evan Chengb2c92902010-02-02 02:22:50 +00002670 MachineFunction &MF = DAG.getMachineFunction();
2671 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2672 return false;
Evan Chengb2c92902010-02-02 02:22:50 +00002673
2674 // Check if the arguments are already laid out in the right way as
2675 // the caller's fixed stack objects.
2676 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002677 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2678 const X86InstrInfo *TII =
2679 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002680 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2681 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002682 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002683 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002684 if (VA.getLocInfo() == CCValAssign::Indirect)
2685 return false;
2686 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002687 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2688 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002689 return false;
2690 }
2691 }
2692 }
Evan Cheng9c044672010-05-29 01:35:22 +00002693
2694 // If the tailcall address may be in a register, then make sure it's
2695 // possible to register allocate for it. In 32-bit, the call address can
2696 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002697 // callee-saved registers are restored. These happen to be the same
2698 // registers used to pass 'inreg' arguments so watch out for those.
2699 if (!Subtarget->is64Bit() &&
2700 !isa<GlobalAddressSDNode>(Callee) &&
Evan Cheng9c044672010-05-29 01:35:22 +00002701 !isa<ExternalSymbolSDNode>(Callee)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002702 unsigned NumInRegs = 0;
2703 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2704 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002705 if (!VA.isRegLoc())
2706 continue;
2707 unsigned Reg = VA.getLocReg();
2708 switch (Reg) {
2709 default: break;
2710 case X86::EAX: case X86::EDX: case X86::ECX:
2711 if (++NumInRegs == 3)
Evan Cheng9c044672010-05-29 01:35:22 +00002712 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002713 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002714 }
2715 }
2716 }
Evan Chenga6bff982010-01-30 01:22:00 +00002717 }
Evan Chengb1712452010-01-27 06:25:16 +00002718
Evan Cheng86809cc2010-02-03 03:28:02 +00002719 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002720}
2721
Dan Gohman3df24e62008-09-03 23:12:08 +00002722FastISel *
Dan Gohmana4160c32010-07-07 16:29:44 +00002723X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
2724 return X86::createFastISel(funcInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00002725}
2726
2727
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002728//===----------------------------------------------------------------------===//
2729// Other Lowering Hooks
2730//===----------------------------------------------------------------------===//
2731
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00002732static bool MayFoldLoad(SDValue Op) {
2733 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
2734}
2735
2736static bool MayFoldIntoStore(SDValue Op) {
2737 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
2738}
2739
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002740static bool isTargetShuffle(unsigned Opcode) {
2741 switch(Opcode) {
2742 default: return false;
2743 case X86ISD::PSHUFD:
2744 case X86ISD::PSHUFHW:
2745 case X86ISD::PSHUFLW:
2746 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002747 case X86ISD::PALIGN:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002748 case X86ISD::SHUFPS:
2749 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002750 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002751 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002752 case X86ISD::MOVLPS:
2753 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002754 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002755 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002756 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002757 case X86ISD::MOVSS:
2758 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002759 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002760 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +00002761 case X86ISD::VUNPCKLPS:
2762 case X86ISD::VUNPCKLPD:
2763 case X86ISD::VUNPCKLPSY:
2764 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002765 case X86ISD::PUNPCKLWD:
2766 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002767 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002768 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002769 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002770 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002771 case X86ISD::PUNPCKHWD:
2772 case X86ISD::PUNPCKHBW:
2773 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002774 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002775 return true;
2776 }
2777 return false;
2778}
2779
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002780static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002781 SDValue V1, SelectionDAG &DAG) {
2782 switch(Opc) {
2783 default: llvm_unreachable("Unknown x86 shuffle node");
2784 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002785 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002786 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002787 return DAG.getNode(Opc, dl, VT, V1);
2788 }
2789
2790 return SDValue();
2791}
2792
2793static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00002794 SDValue V1, unsigned TargetMask, SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002795 switch(Opc) {
2796 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002797 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002798 case X86ISD::PSHUFHW:
2799 case X86ISD::PSHUFLW:
2800 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
2801 }
2802
2803 return SDValue();
2804}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002805
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002806static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2807 SDValue V1, SDValue V2, unsigned TargetMask, SelectionDAG &DAG) {
2808 switch(Opc) {
2809 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002810 case X86ISD::PALIGN:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002811 case X86ISD::SHUFPD:
2812 case X86ISD::SHUFPS:
2813 return DAG.getNode(Opc, dl, VT, V1, V2,
2814 DAG.getConstant(TargetMask, MVT::i8));
2815 }
2816 return SDValue();
2817}
2818
2819static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2820 SDValue V1, SDValue V2, SelectionDAG &DAG) {
2821 switch(Opc) {
2822 default: llvm_unreachable("Unknown x86 shuffle node");
2823 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00002824 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002825 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002826 case X86ISD::MOVLPS:
2827 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002828 case X86ISD::MOVSS:
2829 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002830 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002831 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +00002832 case X86ISD::VUNPCKLPS:
2833 case X86ISD::VUNPCKLPD:
2834 case X86ISD::VUNPCKLPSY:
2835 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002836 case X86ISD::PUNPCKLWD:
2837 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002838 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002839 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002840 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002841 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002842 case X86ISD::PUNPCKHWD:
2843 case X86ISD::PUNPCKHBW:
2844 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002845 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002846 return DAG.getNode(Opc, dl, VT, V1, V2);
2847 }
2848 return SDValue();
2849}
2850
Dan Gohmand858e902010-04-17 15:26:15 +00002851SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002852 MachineFunction &MF = DAG.getMachineFunction();
2853 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2854 int ReturnAddrIndex = FuncInfo->getRAIndex();
2855
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002856 if (ReturnAddrIndex == 0) {
2857 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002858 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002859 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00002860 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002861 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002862 }
2863
Evan Cheng25ab6902006-09-08 06:48:29 +00002864 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002865}
2866
2867
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002868bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2869 bool hasSymbolicDisplacement) {
2870 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00002871 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002872 return false;
2873
2874 // If we don't have a symbolic displacement - we don't have any extra
2875 // restrictions.
2876 if (!hasSymbolicDisplacement)
2877 return true;
2878
2879 // FIXME: Some tweaks might be needed for medium code model.
2880 if (M != CodeModel::Small && M != CodeModel::Kernel)
2881 return false;
2882
2883 // For small code model we assume that latest object is 16MB before end of 31
2884 // bits boundary. We may also accept pretty large negative constants knowing
2885 // that all objects are in the positive half of address space.
2886 if (M == CodeModel::Small && Offset < 16*1024*1024)
2887 return true;
2888
2889 // For kernel code model we know that all object resist in the negative half
2890 // of 32bits address space. We may not accept negative offsets, since they may
2891 // be just off and we may accept pretty large positive ones.
2892 if (M == CodeModel::Kernel && Offset > 0)
2893 return true;
2894
2895 return false;
2896}
2897
Evan Chengef41ff62011-06-23 17:54:54 +00002898/// isCalleePop - Determines whether the callee is required to pop its
2899/// own arguments. Callee pop is necessary to support tail calls.
2900bool X86::isCalleePop(CallingConv::ID CallingConv,
2901 bool is64Bit, bool IsVarArg, bool TailCallOpt) {
2902 if (IsVarArg)
2903 return false;
2904
2905 switch (CallingConv) {
2906 default:
2907 return false;
2908 case CallingConv::X86_StdCall:
2909 return !is64Bit;
2910 case CallingConv::X86_FastCall:
2911 return !is64Bit;
2912 case CallingConv::X86_ThisCall:
2913 return !is64Bit;
2914 case CallingConv::Fast:
2915 return TailCallOpt;
2916 case CallingConv::GHC:
2917 return TailCallOpt;
2918 }
2919}
2920
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002921/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2922/// specific condition code, returning the condition code and the LHS/RHS of the
2923/// comparison to make.
2924static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2925 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00002926 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002927 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2928 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2929 // X > -1 -> X == 0, jump !sign.
2930 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002931 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002932 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2933 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002934 return X86::COND_S;
Andrew Trickf6c39412011-03-23 23:11:02 +00002935 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00002936 // X < 1 -> X <= 0
2937 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002938 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002939 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002940 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002941
Evan Chengd9558e02006-01-06 00:43:03 +00002942 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002943 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002944 case ISD::SETEQ: return X86::COND_E;
2945 case ISD::SETGT: return X86::COND_G;
2946 case ISD::SETGE: return X86::COND_GE;
2947 case ISD::SETLT: return X86::COND_L;
2948 case ISD::SETLE: return X86::COND_LE;
2949 case ISD::SETNE: return X86::COND_NE;
2950 case ISD::SETULT: return X86::COND_B;
2951 case ISD::SETUGT: return X86::COND_A;
2952 case ISD::SETULE: return X86::COND_BE;
2953 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00002954 }
Chris Lattner4c78e022008-12-23 23:42:27 +00002955 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002956
Chris Lattner4c78e022008-12-23 23:42:27 +00002957 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00002958
Chris Lattner4c78e022008-12-23 23:42:27 +00002959 // If LHS is a foldable load, but RHS is not, flip the condition.
Rafael Espindolaf297c932011-02-03 03:58:05 +00002960 if (ISD::isNON_EXTLoad(LHS.getNode()) &&
2961 !ISD::isNON_EXTLoad(RHS.getNode())) {
Chris Lattner4c78e022008-12-23 23:42:27 +00002962 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
2963 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00002964 }
2965
Chris Lattner4c78e022008-12-23 23:42:27 +00002966 switch (SetCCOpcode) {
2967 default: break;
2968 case ISD::SETOLT:
2969 case ISD::SETOLE:
2970 case ISD::SETUGT:
2971 case ISD::SETUGE:
2972 std::swap(LHS, RHS);
2973 break;
2974 }
2975
2976 // On a floating point condition, the flags are set as follows:
2977 // ZF PF CF op
2978 // 0 | 0 | 0 | X > Y
2979 // 0 | 0 | 1 | X < Y
2980 // 1 | 0 | 0 | X == Y
2981 // 1 | 1 | 1 | unordered
2982 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002983 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00002984 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002985 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00002986 case ISD::SETOLT: // flipped
2987 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002988 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00002989 case ISD::SETOLE: // flipped
2990 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002991 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002992 case ISD::SETUGT: // flipped
2993 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002994 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00002995 case ISD::SETUGE: // flipped
2996 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002997 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002998 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002999 case ISD::SETNE: return X86::COND_NE;
3000 case ISD::SETUO: return X86::COND_P;
3001 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00003002 case ISD::SETOEQ:
3003 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00003004 }
Evan Chengd9558e02006-01-06 00:43:03 +00003005}
3006
Evan Cheng4a460802006-01-11 00:33:36 +00003007/// hasFPCMov - is there a floating point cmov for the specific X86 condition
3008/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00003009/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00003010static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00003011 switch (X86CC) {
3012 default:
3013 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00003014 case X86::COND_B:
3015 case X86::COND_BE:
3016 case X86::COND_E:
3017 case X86::COND_P:
3018 case X86::COND_A:
3019 case X86::COND_AE:
3020 case X86::COND_NE:
3021 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00003022 return true;
3023 }
3024}
3025
Evan Chengeb2f9692009-10-27 19:56:55 +00003026/// isFPImmLegal - Returns true if the target can instruction select the
3027/// specified FP immediate natively. If false, the legalizer will
3028/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00003029bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00003030 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
3031 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
3032 return true;
3033 }
3034 return false;
3035}
3036
Nate Begeman9008ca62009-04-27 18:41:29 +00003037/// isUndefOrInRange - Return true if Val is undef or if its value falls within
3038/// the specified range (L, H].
3039static bool isUndefOrInRange(int Val, int Low, int Hi) {
3040 return (Val < 0) || (Val >= Low && Val < Hi);
3041}
3042
3043/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
3044/// specified value.
3045static bool isUndefOrEqual(int Val, int CmpVal) {
3046 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003047 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00003048 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00003049}
3050
Nate Begeman9008ca62009-04-27 18:41:29 +00003051/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
3052/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
3053/// the second operand.
Owen Andersone50ed302009-08-10 22:56:29 +00003054static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00003055 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00003056 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00003057 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00003058 return (Mask[0] < 2 && Mask[1] < 2);
3059 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003060}
3061
Nate Begeman9008ca62009-04-27 18:41:29 +00003062bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003063 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003064 N->getMask(M);
3065 return ::isPSHUFDMask(M, N->getValueType(0));
3066}
Evan Cheng0188ecb2006-03-22 18:59:22 +00003067
Nate Begeman9008ca62009-04-27 18:41:29 +00003068/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
3069/// is suitable for input to PSHUFHW.
Owen Andersone50ed302009-08-10 22:56:29 +00003070static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003071 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00003072 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003073
Nate Begeman9008ca62009-04-27 18:41:29 +00003074 // Lower quadword copied in order or undef.
3075 for (int i = 0; i != 4; ++i)
3076 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00003077 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003078
Evan Cheng506d3df2006-03-29 23:07:14 +00003079 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00003080 for (int i = 4; i != 8; ++i)
3081 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00003082 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003083
Evan Cheng506d3df2006-03-29 23:07:14 +00003084 return true;
3085}
3086
Nate Begeman9008ca62009-04-27 18:41:29 +00003087bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003088 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003089 N->getMask(M);
3090 return ::isPSHUFHWMask(M, N->getValueType(0));
3091}
Evan Cheng506d3df2006-03-29 23:07:14 +00003092
Nate Begeman9008ca62009-04-27 18:41:29 +00003093/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
3094/// is suitable for input to PSHUFLW.
Owen Andersone50ed302009-08-10 22:56:29 +00003095static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003096 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00003097 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003098
Rafael Espindola15684b22009-04-24 12:40:33 +00003099 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00003100 for (int i = 4; i != 8; ++i)
3101 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00003102 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003103
Rafael Espindola15684b22009-04-24 12:40:33 +00003104 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00003105 for (int i = 0; i != 4; ++i)
3106 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00003107 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003108
Rafael Espindola15684b22009-04-24 12:40:33 +00003109 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003110}
3111
Nate Begeman9008ca62009-04-27 18:41:29 +00003112bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003113 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003114 N->getMask(M);
3115 return ::isPSHUFLWMask(M, N->getValueType(0));
3116}
3117
Nate Begemana09008b2009-10-19 02:17:23 +00003118/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
3119/// is suitable for input to PALIGNR.
3120static bool isPALIGNRMask(const SmallVectorImpl<int> &Mask, EVT VT,
3121 bool hasSSSE3) {
3122 int i, e = VT.getVectorNumElements();
Michael J. Spencerec38de22010-10-10 22:04:20 +00003123
Nate Begemana09008b2009-10-19 02:17:23 +00003124 // Do not handle v2i64 / v2f64 shuffles with palignr.
3125 if (e < 4 || !hasSSSE3)
3126 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003127
Nate Begemana09008b2009-10-19 02:17:23 +00003128 for (i = 0; i != e; ++i)
3129 if (Mask[i] >= 0)
3130 break;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003131
Nate Begemana09008b2009-10-19 02:17:23 +00003132 // All undef, not a palignr.
3133 if (i == e)
3134 return false;
3135
3136 // Determine if it's ok to perform a palignr with only the LHS, since we
3137 // don't have access to the actual shuffle elements to see if RHS is undef.
3138 bool Unary = Mask[i] < (int)e;
3139 bool NeedsUnary = false;
3140
3141 int s = Mask[i] - i;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003142
Nate Begemana09008b2009-10-19 02:17:23 +00003143 // Check the rest of the elements to see if they are consecutive.
3144 for (++i; i != e; ++i) {
3145 int m = Mask[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00003146 if (m < 0)
Nate Begemana09008b2009-10-19 02:17:23 +00003147 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003148
Nate Begemana09008b2009-10-19 02:17:23 +00003149 Unary = Unary && (m < (int)e);
3150 NeedsUnary = NeedsUnary || (m < s);
3151
3152 if (NeedsUnary && !Unary)
3153 return false;
3154 if (Unary && m != ((s+i) & (e-1)))
3155 return false;
3156 if (!Unary && m != (s+i))
3157 return false;
3158 }
3159 return true;
3160}
3161
3162bool X86::isPALIGNRMask(ShuffleVectorSDNode *N) {
3163 SmallVector<int, 8> M;
3164 N->getMask(M);
3165 return ::isPALIGNRMask(M, N->getValueType(0), true);
3166}
3167
Evan Cheng14aed5e2006-03-24 01:18:28 +00003168/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
3169/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Owen Andersone50ed302009-08-10 22:56:29 +00003170static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003171 int NumElems = VT.getVectorNumElements();
3172 if (NumElems != 2 && NumElems != 4)
3173 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003174
Nate Begeman9008ca62009-04-27 18:41:29 +00003175 int Half = NumElems / 2;
3176 for (int i = 0; i < Half; ++i)
3177 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003178 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003179 for (int i = Half; i < NumElems; ++i)
3180 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00003181 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003182
Evan Cheng14aed5e2006-03-24 01:18:28 +00003183 return true;
3184}
3185
Nate Begeman9008ca62009-04-27 18:41:29 +00003186bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
3187 SmallVector<int, 8> M;
3188 N->getMask(M);
3189 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003190}
3191
Evan Cheng213d2cf2007-05-17 18:45:50 +00003192/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00003193/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
3194/// half elements to come from vector 1 (which would equal the dest.) and
3195/// the upper half to come from vector 2.
Owen Andersone50ed302009-08-10 22:56:29 +00003196static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003197 int NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003198
3199 if (NumElems != 2 && NumElems != 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00003200 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003201
Nate Begeman9008ca62009-04-27 18:41:29 +00003202 int Half = NumElems / 2;
3203 for (int i = 0; i < Half; ++i)
3204 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00003205 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003206 for (int i = Half; i < NumElems; ++i)
3207 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003208 return false;
3209 return true;
3210}
3211
Nate Begeman9008ca62009-04-27 18:41:29 +00003212static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
3213 SmallVector<int, 8> M;
3214 N->getMask(M);
3215 return isCommutedSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003216}
3217
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003218/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
3219/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00003220bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
3221 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003222 return false;
3223
Evan Cheng2064a2b2006-03-28 06:50:32 +00003224 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00003225 return isUndefOrEqual(N->getMaskElt(0), 6) &&
3226 isUndefOrEqual(N->getMaskElt(1), 7) &&
3227 isUndefOrEqual(N->getMaskElt(2), 2) &&
3228 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003229}
3230
Nate Begeman0b10b912009-11-07 23:17:15 +00003231/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3232/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3233/// <2, 3, 2, 3>
3234bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
3235 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Michael J. Spencerec38de22010-10-10 22:04:20 +00003236
Nate Begeman0b10b912009-11-07 23:17:15 +00003237 if (NumElems != 4)
3238 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003239
Nate Begeman0b10b912009-11-07 23:17:15 +00003240 return isUndefOrEqual(N->getMaskElt(0), 2) &&
3241 isUndefOrEqual(N->getMaskElt(1), 3) &&
3242 isUndefOrEqual(N->getMaskElt(2), 2) &&
3243 isUndefOrEqual(N->getMaskElt(3), 3);
3244}
3245
Evan Cheng5ced1d82006-04-06 23:23:56 +00003246/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3247/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00003248bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
3249 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003250
Evan Cheng5ced1d82006-04-06 23:23:56 +00003251 if (NumElems != 2 && NumElems != 4)
3252 return false;
3253
Evan Chengc5cdff22006-04-07 21:53:05 +00003254 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003255 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003256 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003257
Evan Chengc5cdff22006-04-07 21:53:05 +00003258 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003259 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003260 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003261
3262 return true;
3263}
3264
Nate Begeman0b10b912009-11-07 23:17:15 +00003265/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3266/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
3267bool X86::isMOVLHPSMask(ShuffleVectorSDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003268 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003269
David Greenea20244d2011-03-02 17:23:43 +00003270 if ((NumElems != 2 && NumElems != 4)
3271 || N->getValueType(0).getSizeInBits() > 128)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003272 return false;
3273
Evan Chengc5cdff22006-04-07 21:53:05 +00003274 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003275 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003276 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003277
Nate Begeman9008ca62009-04-27 18:41:29 +00003278 for (unsigned i = 0; i < NumElems/2; ++i)
3279 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003280 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003281
3282 return true;
3283}
3284
Evan Cheng0038e592006-03-28 00:39:58 +00003285/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3286/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Owen Andersone50ed302009-08-10 22:56:29 +00003287static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003288 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003289 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003290 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng0038e592006-03-28 00:39:58 +00003291 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003292
David Greenea20244d2011-03-02 17:23:43 +00003293 // Handle vector lengths > 128 bits. Define a "section" as a set of
3294 // 128 bits. AVX defines UNPCK* to operate independently on 128-bit
3295 // sections.
3296 unsigned NumSections = VT.getSizeInBits() / 128;
3297 if (NumSections == 0 ) NumSections = 1; // Handle MMX
3298 unsigned NumSectionElts = NumElts / NumSections;
3299
3300 unsigned Start = 0;
3301 unsigned End = NumSectionElts;
3302 for (unsigned s = 0; s < NumSections; ++s) {
3303 for (unsigned i = Start, j = s * NumSectionElts;
3304 i != End;
3305 i += 2, ++j) {
3306 int BitI = Mask[i];
3307 int BitI1 = Mask[i+1];
3308 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003309 return false;
David Greenea20244d2011-03-02 17:23:43 +00003310 if (V2IsSplat) {
3311 if (!isUndefOrEqual(BitI1, NumElts))
3312 return false;
3313 } else {
3314 if (!isUndefOrEqual(BitI1, j + NumElts))
3315 return false;
3316 }
Evan Cheng39623da2006-04-20 08:58:49 +00003317 }
David Greenea20244d2011-03-02 17:23:43 +00003318 // Process the next 128 bits.
3319 Start += NumSectionElts;
3320 End += NumSectionElts;
Evan Cheng0038e592006-03-28 00:39:58 +00003321 }
David Greenea20244d2011-03-02 17:23:43 +00003322
Evan Cheng0038e592006-03-28 00:39:58 +00003323 return true;
3324}
3325
Nate Begeman9008ca62009-04-27 18:41:29 +00003326bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3327 SmallVector<int, 8> M;
3328 N->getMask(M);
3329 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003330}
3331
Evan Cheng4fcb9222006-03-28 02:43:26 +00003332/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3333/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Eric Christopherfd179292009-08-27 18:07:15 +00003334static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003335 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003336 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003337 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng4fcb9222006-03-28 02:43:26 +00003338 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003339
Nate Begeman9008ca62009-04-27 18:41:29 +00003340 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
3341 int BitI = Mask[i];
3342 int BitI1 = Mask[i+1];
Chris Lattner5a88b832007-02-25 07:10:00 +00003343 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengc5cdff22006-04-07 21:53:05 +00003344 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00003345 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00003346 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003347 return false;
3348 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00003349 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003350 return false;
3351 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003352 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003353 return true;
3354}
3355
Nate Begeman9008ca62009-04-27 18:41:29 +00003356bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3357 SmallVector<int, 8> M;
3358 N->getMask(M);
3359 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003360}
3361
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003362/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3363/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3364/// <0, 0, 1, 1>
Owen Andersone50ed302009-08-10 22:56:29 +00003365static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003366 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003367 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003368 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003369
David Greenea20244d2011-03-02 17:23:43 +00003370 // Handle vector lengths > 128 bits. Define a "section" as a set of
3371 // 128 bits. AVX defines UNPCK* to operate independently on 128-bit
3372 // sections.
3373 unsigned NumSections = VT.getSizeInBits() / 128;
3374 if (NumSections == 0 ) NumSections = 1; // Handle MMX
3375 unsigned NumSectionElts = NumElems / NumSections;
3376
3377 for (unsigned s = 0; s < NumSections; ++s) {
3378 for (unsigned i = s * NumSectionElts, j = s * NumSectionElts;
3379 i != NumSectionElts * (s + 1);
3380 i += 2, ++j) {
3381 int BitI = Mask[i];
3382 int BitI1 = Mask[i+1];
3383
3384 if (!isUndefOrEqual(BitI, j))
3385 return false;
3386 if (!isUndefOrEqual(BitI1, j))
3387 return false;
3388 }
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003389 }
David Greenea20244d2011-03-02 17:23:43 +00003390
Rafael Espindola15684b22009-04-24 12:40:33 +00003391 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003392}
3393
Nate Begeman9008ca62009-04-27 18:41:29 +00003394bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
3395 SmallVector<int, 8> M;
3396 N->getMask(M);
3397 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
3398}
3399
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003400/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3401/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3402/// <2, 2, 3, 3>
Owen Andersone50ed302009-08-10 22:56:29 +00003403static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003404 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003405 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
3406 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003407
Nate Begeman9008ca62009-04-27 18:41:29 +00003408 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
3409 int BitI = Mask[i];
3410 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003411 if (!isUndefOrEqual(BitI, j))
3412 return false;
3413 if (!isUndefOrEqual(BitI1, j))
3414 return false;
3415 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003416 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003417}
3418
Nate Begeman9008ca62009-04-27 18:41:29 +00003419bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
3420 SmallVector<int, 8> M;
3421 N->getMask(M);
3422 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
3423}
3424
Evan Cheng017dcc62006-04-21 01:05:10 +00003425/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3426/// specifies a shuffle of elements that is suitable for input to MOVSS,
3427/// MOVSD, and MOVD, i.e. setting the lowest element.
Owen Andersone50ed302009-08-10 22:56:29 +00003428static bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003429 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003430 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003431
3432 int NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003433
Nate Begeman9008ca62009-04-27 18:41:29 +00003434 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003435 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003436
Nate Begeman9008ca62009-04-27 18:41:29 +00003437 for (int i = 1; i < NumElts; ++i)
3438 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003439 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003440
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003441 return true;
3442}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003443
Nate Begeman9008ca62009-04-27 18:41:29 +00003444bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
3445 SmallVector<int, 8> M;
3446 N->getMask(M);
3447 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003448}
3449
Evan Cheng017dcc62006-04-21 01:05:10 +00003450/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
3451/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003452/// element of vector 2 and the other elements to come from vector 1 in order.
Owen Andersone50ed302009-08-10 22:56:29 +00003453static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003454 bool V2IsSplat = false, bool V2IsUndef = false) {
3455 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003456 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003457 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003458
Nate Begeman9008ca62009-04-27 18:41:29 +00003459 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003460 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003461
Nate Begeman9008ca62009-04-27 18:41:29 +00003462 for (int i = 1; i < NumOps; ++i)
3463 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3464 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3465 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003466 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003467
Evan Cheng39623da2006-04-20 08:58:49 +00003468 return true;
3469}
3470
Nate Begeman9008ca62009-04-27 18:41:29 +00003471static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00003472 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003473 SmallVector<int, 8> M;
3474 N->getMask(M);
3475 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00003476}
3477
Evan Chengd9539472006-04-14 21:59:03 +00003478/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3479/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003480bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N) {
3481 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003482 return false;
3483
3484 // Expect 1, 1, 3, 3
Rafael Espindola15684b22009-04-24 12:40:33 +00003485 for (unsigned i = 0; i < 2; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003486 int Elt = N->getMaskElt(i);
3487 if (Elt >= 0 && Elt != 1)
3488 return false;
Rafael Espindola15684b22009-04-24 12:40:33 +00003489 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003490
3491 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003492 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003493 int Elt = N->getMaskElt(i);
3494 if (Elt >= 0 && Elt != 3)
3495 return false;
3496 if (Elt == 3)
3497 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003498 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003499 // Don't use movshdup if it can be done with a shufps.
Nate Begeman9008ca62009-04-27 18:41:29 +00003500 // FIXME: verify that matching u, u, 3, 3 is what we want.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003501 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003502}
3503
3504/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3505/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003506bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N) {
3507 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003508 return false;
3509
3510 // Expect 0, 0, 2, 2
Nate Begeman9008ca62009-04-27 18:41:29 +00003511 for (unsigned i = 0; i < 2; ++i)
3512 if (N->getMaskElt(i) > 0)
3513 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003514
3515 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003516 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003517 int Elt = N->getMaskElt(i);
3518 if (Elt >= 0 && Elt != 2)
3519 return false;
3520 if (Elt == 2)
3521 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003522 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003523 // Don't use movsldup if it can be done with a shufps.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003524 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003525}
3526
Evan Cheng0b457f02008-09-25 20:50:48 +00003527/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3528/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003529bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
3530 int e = N->getValueType(0).getVectorNumElements() / 2;
Eric Christopherfd179292009-08-27 18:07:15 +00003531
Nate Begeman9008ca62009-04-27 18:41:29 +00003532 for (int i = 0; i < e; ++i)
3533 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003534 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003535 for (int i = 0; i < e; ++i)
3536 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003537 return false;
3538 return true;
3539}
3540
David Greenec38a03e2011-02-03 15:50:00 +00003541/// isVEXTRACTF128Index - Return true if the specified
3542/// EXTRACT_SUBVECTOR operand specifies a vector extract that is
3543/// suitable for input to VEXTRACTF128.
3544bool X86::isVEXTRACTF128Index(SDNode *N) {
3545 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3546 return false;
3547
3548 // The index should be aligned on a 128-bit boundary.
3549 uint64_t Index =
3550 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
3551
3552 unsigned VL = N->getValueType(0).getVectorNumElements();
3553 unsigned VBits = N->getValueType(0).getSizeInBits();
3554 unsigned ElSize = VBits / VL;
3555 bool Result = (Index * ElSize) % 128 == 0;
3556
3557 return Result;
3558}
3559
David Greeneccacdc12011-02-04 16:08:29 +00003560/// isVINSERTF128Index - Return true if the specified INSERT_SUBVECTOR
3561/// operand specifies a subvector insert that is suitable for input to
3562/// VINSERTF128.
3563bool X86::isVINSERTF128Index(SDNode *N) {
3564 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
3565 return false;
3566
3567 // The index should be aligned on a 128-bit boundary.
3568 uint64_t Index =
3569 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
3570
3571 unsigned VL = N->getValueType(0).getVectorNumElements();
3572 unsigned VBits = N->getValueType(0).getSizeInBits();
3573 unsigned ElSize = VBits / VL;
3574 bool Result = (Index * ElSize) % 128 == 0;
3575
3576 return Result;
3577}
3578
Evan Cheng63d33002006-03-22 08:01:21 +00003579/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003580/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Evan Cheng63d33002006-03-22 08:01:21 +00003581unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003582 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3583 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
3584
Evan Chengb9df0ca2006-03-22 02:53:00 +00003585 unsigned Shift = (NumOperands == 4) ? 2 : 1;
3586 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00003587 for (int i = 0; i < NumOperands; ++i) {
3588 int Val = SVOp->getMaskElt(NumOperands-i-1);
3589 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00003590 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00003591 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00003592 if (i != NumOperands - 1)
3593 Mask <<= Shift;
3594 }
Evan Cheng63d33002006-03-22 08:01:21 +00003595 return Mask;
3596}
3597
Evan Cheng506d3df2006-03-29 23:07:14 +00003598/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003599/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003600unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003601 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003602 unsigned Mask = 0;
3603 // 8 nodes, but we only care about the last 4.
3604 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003605 int Val = SVOp->getMaskElt(i);
3606 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00003607 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00003608 if (i != 4)
3609 Mask <<= 2;
3610 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003611 return Mask;
3612}
3613
3614/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003615/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003616unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003617 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003618 unsigned Mask = 0;
3619 // 8 nodes, but we only care about the first 4.
3620 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003621 int Val = SVOp->getMaskElt(i);
3622 if (Val >= 0)
3623 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00003624 if (i != 0)
3625 Mask <<= 2;
3626 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003627 return Mask;
3628}
3629
Nate Begemana09008b2009-10-19 02:17:23 +00003630/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
3631/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
3632unsigned X86::getShufflePALIGNRImmediate(SDNode *N) {
3633 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3634 EVT VVT = N->getValueType(0);
3635 unsigned EltSize = VVT.getVectorElementType().getSizeInBits() >> 3;
3636 int Val = 0;
3637
3638 unsigned i, e;
3639 for (i = 0, e = VVT.getVectorNumElements(); i != e; ++i) {
3640 Val = SVOp->getMaskElt(i);
3641 if (Val >= 0)
3642 break;
3643 }
3644 return (Val - i) * EltSize;
3645}
3646
David Greenec38a03e2011-02-03 15:50:00 +00003647/// getExtractVEXTRACTF128Immediate - Return the appropriate immediate
3648/// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF128
3649/// instructions.
3650unsigned X86::getExtractVEXTRACTF128Immediate(SDNode *N) {
3651 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3652 llvm_unreachable("Illegal extract subvector for VEXTRACTF128");
3653
3654 uint64_t Index =
3655 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
3656
3657 EVT VecVT = N->getOperand(0).getValueType();
3658 EVT ElVT = VecVT.getVectorElementType();
3659
3660 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
3661
3662 return Index / NumElemsPerChunk;
3663}
3664
David Greeneccacdc12011-02-04 16:08:29 +00003665/// getInsertVINSERTF128Immediate - Return the appropriate immediate
3666/// to insert at the specified INSERT_SUBVECTOR index with VINSERTF128
3667/// instructions.
3668unsigned X86::getInsertVINSERTF128Immediate(SDNode *N) {
3669 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
3670 llvm_unreachable("Illegal insert subvector for VINSERTF128");
3671
3672 uint64_t Index =
NAKAMURA Takumi27635382011-02-05 15:10:54 +00003673 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
David Greeneccacdc12011-02-04 16:08:29 +00003674
3675 EVT VecVT = N->getValueType(0);
3676 EVT ElVT = VecVT.getVectorElementType();
3677
3678 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
3679
3680 return Index / NumElemsPerChunk;
3681}
3682
Evan Cheng37b73872009-07-30 08:33:02 +00003683/// isZeroNode - Returns true if Elt is a constant zero or a floating point
3684/// constant +0.0.
3685bool X86::isZeroNode(SDValue Elt) {
3686 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00003687 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00003688 (isa<ConstantFPSDNode>(Elt) &&
3689 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
3690}
3691
Nate Begeman9008ca62009-04-27 18:41:29 +00003692/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
3693/// their permute mask.
3694static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
3695 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003696 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003697 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00003698 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00003699
Nate Begeman5a5ca152009-04-29 05:20:52 +00003700 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003701 int idx = SVOp->getMaskElt(i);
3702 if (idx < 0)
3703 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003704 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003705 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003706 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003707 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003708 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003709 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
3710 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003711}
3712
Evan Cheng779ccea2007-12-07 21:30:01 +00003713/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3714/// the two vector operands have swapped position.
Owen Andersone50ed302009-08-10 22:56:29 +00003715static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00003716 unsigned NumElems = VT.getVectorNumElements();
3717 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003718 int idx = Mask[i];
3719 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003720 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003721 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003722 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003723 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003724 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003725 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003726}
3727
Evan Cheng533a0aa2006-04-19 20:35:22 +00003728/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
3729/// match movhlps. The lower half elements should come from upper half of
3730/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003731/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00003732static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
3733 if (Op->getValueType(0).getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00003734 return false;
3735 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003736 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003737 return false;
3738 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003739 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003740 return false;
3741 return true;
3742}
3743
Evan Cheng5ced1d82006-04-06 23:23:56 +00003744/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00003745/// is promoted to a vector. It also returns the LoadSDNode by reference if
3746/// required.
3747static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00003748 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
3749 return false;
3750 N = N->getOperand(0).getNode();
3751 if (!ISD::isNON_EXTLoad(N))
3752 return false;
3753 if (LD)
3754 *LD = cast<LoadSDNode>(N);
3755 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003756}
3757
Evan Cheng533a0aa2006-04-19 20:35:22 +00003758/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
3759/// match movlp{s|d}. The lower half elements should come from lower half of
3760/// V1 (and in order), and the upper half elements should come from the upper
3761/// half of V2 (and in order). And since V1 will become the source of the
3762/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003763static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
3764 ShuffleVectorSDNode *Op) {
Evan Cheng466685d2006-10-09 20:57:25 +00003765 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003766 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00003767 // Is V2 is a vector load, don't do this transformation. We will try to use
3768 // load folding shufps op.
3769 if (ISD::isNON_EXTLoad(V2))
3770 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003771
Nate Begeman5a5ca152009-04-29 05:20:52 +00003772 unsigned NumElems = Op->getValueType(0).getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003773
Evan Cheng533a0aa2006-04-19 20:35:22 +00003774 if (NumElems != 2 && NumElems != 4)
3775 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003776 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003777 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003778 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003779 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003780 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003781 return false;
3782 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003783}
3784
Evan Cheng39623da2006-04-20 08:58:49 +00003785/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
3786/// all the same.
3787static bool isSplatVector(SDNode *N) {
3788 if (N->getOpcode() != ISD::BUILD_VECTOR)
3789 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003790
Dan Gohman475871a2008-07-27 21:46:04 +00003791 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00003792 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
3793 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003794 return false;
3795 return true;
3796}
3797
Evan Cheng213d2cf2007-05-17 18:45:50 +00003798/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00003799/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00003800/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00003801static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00003802 SDValue V1 = N->getOperand(0);
3803 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003804 unsigned NumElems = N->getValueType(0).getVectorNumElements();
3805 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003806 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003807 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003808 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00003809 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
3810 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003811 if (Opc != ISD::BUILD_VECTOR ||
3812 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00003813 return false;
3814 } else if (Idx >= 0) {
3815 unsigned Opc = V1.getOpcode();
3816 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
3817 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003818 if (Opc != ISD::BUILD_VECTOR ||
3819 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00003820 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00003821 }
3822 }
3823 return true;
3824}
3825
3826/// getZeroVector - Returns a vector of specified type with all zero elements.
3827///
Owen Andersone50ed302009-08-10 22:56:29 +00003828static SDValue getZeroVector(EVT VT, bool HasSSE2, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00003829 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003830 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003831
Dale Johannesen0488fb62010-09-30 23:57:10 +00003832 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003833 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00003834 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00003835 if (VT.getSizeInBits() == 128) { // SSE
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003836 if (HasSSE2) { // SSE2
3837 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3838 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
3839 } else { // SSE1
3840 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
3841 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
3842 }
3843 } else if (VT.getSizeInBits() == 256) { // AVX
3844 // 256-bit logic and arithmetic instructions in AVX are
3845 // all floating-point, no support for integer ops. Default
3846 // to emitting fp zeroed vectors then.
Owen Anderson825b72b2009-08-11 20:47:22 +00003847 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003848 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
3849 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
Evan Chengf0df0312008-05-15 08:39:06 +00003850 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003851 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00003852}
3853
Chris Lattner8a594482007-11-25 00:24:49 +00003854/// getOnesVector - Returns a vector of specified type with all bits set.
3855///
Owen Andersone50ed302009-08-10 22:56:29 +00003856static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003857 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003858
Chris Lattner8a594482007-11-25 00:24:49 +00003859 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
3860 // type. This ensures they get CSE'd.
Owen Anderson825b72b2009-08-11 20:47:22 +00003861 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00003862 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00003863 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003864 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00003865}
3866
3867
Evan Cheng39623da2006-04-20 08:58:49 +00003868/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
3869/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00003870static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003871 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003872 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003873
Evan Cheng39623da2006-04-20 08:58:49 +00003874 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003875 SmallVector<int, 8> MaskVec;
3876 SVOp->getMask(MaskVec);
Eric Christopherfd179292009-08-27 18:07:15 +00003877
Nate Begeman5a5ca152009-04-29 05:20:52 +00003878 for (unsigned i = 0; i != NumElems; ++i) {
3879 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003880 MaskVec[i] = NumElems;
3881 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00003882 }
Evan Cheng39623da2006-04-20 08:58:49 +00003883 }
Evan Cheng39623da2006-04-20 08:58:49 +00003884 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00003885 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
3886 SVOp->getOperand(1), &MaskVec[0]);
3887 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00003888}
3889
Evan Cheng017dcc62006-04-21 01:05:10 +00003890/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
3891/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00003892static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003893 SDValue V2) {
3894 unsigned NumElems = VT.getVectorNumElements();
3895 SmallVector<int, 8> Mask;
3896 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00003897 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003898 Mask.push_back(i);
3899 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00003900}
3901
Nate Begeman9008ca62009-04-27 18:41:29 +00003902/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003903static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003904 SDValue V2) {
3905 unsigned NumElems = VT.getVectorNumElements();
3906 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00003907 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003908 Mask.push_back(i);
3909 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00003910 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003911 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00003912}
3913
Nate Begeman9008ca62009-04-27 18:41:29 +00003914/// getUnpackhMask - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003915static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003916 SDValue V2) {
3917 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00003918 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00003919 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00003920 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003921 Mask.push_back(i + Half);
3922 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00003923 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003924 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003925}
3926
Bruno Cardoso Lopesbb0a9482010-08-13 17:50:47 +00003927/// PromoteSplat - Promote a splat of v4i32, v8i16 or v16i8 to v4f32.
3928static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003929 EVT PVT = MVT::v4f32;
Owen Andersone50ed302009-08-10 22:56:29 +00003930 EVT VT = SV->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00003931 DebugLoc dl = SV->getDebugLoc();
3932 SDValue V1 = SV->getOperand(0);
3933 int NumElems = VT.getVectorNumElements();
3934 int EltNo = SV->getSplatIndex();
Rafael Espindola15684b22009-04-24 12:40:33 +00003935
Nate Begeman9008ca62009-04-27 18:41:29 +00003936 // unpack elements to the correct location
3937 while (NumElems > 4) {
3938 if (EltNo < NumElems/2) {
3939 V1 = getUnpackl(DAG, dl, VT, V1, V1);
3940 } else {
3941 V1 = getUnpackh(DAG, dl, VT, V1, V1);
3942 EltNo -= NumElems/2;
3943 }
3944 NumElems >>= 1;
3945 }
Eric Christopherfd179292009-08-27 18:07:15 +00003946
Nate Begeman9008ca62009-04-27 18:41:29 +00003947 // Perform the splat.
3948 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003949 V1 = DAG.getNode(ISD::BITCAST, dl, PVT, V1);
Nate Begeman9008ca62009-04-27 18:41:29 +00003950 V1 = DAG.getVectorShuffle(PVT, dl, V1, DAG.getUNDEF(PVT), &SplatMask[0]);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003951 return DAG.getNode(ISD::BITCAST, dl, VT, V1);
Evan Chengc575ca22006-04-17 20:43:08 +00003952}
3953
Evan Chengba05f722006-04-21 23:03:30 +00003954/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00003955/// vector of zero or undef vector. This produces a shuffle where the low
3956/// element of V2 is swizzled into the zero/undef vector, landing at element
3957/// 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 +00003958static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Chengf0df0312008-05-15 08:39:06 +00003959 bool isZero, bool HasSSE2,
3960 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003961 EVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00003962 SDValue V1 = isZero
Nate Begeman9008ca62009-04-27 18:41:29 +00003963 ? getZeroVector(VT, HasSSE2, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
3964 unsigned NumElems = VT.getVectorNumElements();
3965 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00003966 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003967 // If this is the insertion idx, put the low elt of V2 here.
3968 MaskVec.push_back(i == Idx ? NumElems : i);
3969 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00003970}
3971
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003972/// getShuffleScalarElt - Returns the scalar element that will make up the ith
3973/// element of the result of the vector shuffle.
Benjamin Kramer050db522011-03-26 12:38:19 +00003974static SDValue getShuffleScalarElt(SDNode *N, int Index, SelectionDAG &DAG,
3975 unsigned Depth) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003976 if (Depth == 6)
3977 return SDValue(); // Limit search depth.
3978
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003979 SDValue V = SDValue(N, 0);
3980 EVT VT = V.getValueType();
3981 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003982
3983 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
3984 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
3985 Index = SV->getMaskElt(Index);
3986
3987 if (Index < 0)
3988 return DAG.getUNDEF(VT.getVectorElementType());
3989
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003990 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003991 SDValue NewV = (Index < NumElems) ? SV->getOperand(0) : SV->getOperand(1);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003992 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00003993 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003994
3995 // Recurse into target specific vector shuffles to find scalars.
3996 if (isTargetShuffle(Opcode)) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003997 int NumElems = VT.getVectorNumElements();
3998 SmallVector<unsigned, 16> ShuffleMask;
3999 SDValue ImmN;
4000
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004001 switch(Opcode) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004002 case X86ISD::SHUFPS:
4003 case X86ISD::SHUFPD:
4004 ImmN = N->getOperand(N->getNumOperands()-1);
4005 DecodeSHUFPSMask(NumElems,
4006 cast<ConstantSDNode>(ImmN)->getZExtValue(),
4007 ShuffleMask);
4008 break;
4009 case X86ISD::PUNPCKHBW:
4010 case X86ISD::PUNPCKHWD:
4011 case X86ISD::PUNPCKHDQ:
4012 case X86ISD::PUNPCKHQDQ:
4013 DecodePUNPCKHMask(NumElems, ShuffleMask);
4014 break;
4015 case X86ISD::UNPCKHPS:
4016 case X86ISD::UNPCKHPD:
4017 DecodeUNPCKHPMask(NumElems, ShuffleMask);
4018 break;
4019 case X86ISD::PUNPCKLBW:
4020 case X86ISD::PUNPCKLWD:
4021 case X86ISD::PUNPCKLDQ:
4022 case X86ISD::PUNPCKLQDQ:
David Greenec4db4e52011-02-28 19:06:56 +00004023 DecodePUNPCKLMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004024 break;
4025 case X86ISD::UNPCKLPS:
4026 case X86ISD::UNPCKLPD:
David Greenec4db4e52011-02-28 19:06:56 +00004027 case X86ISD::VUNPCKLPS:
4028 case X86ISD::VUNPCKLPD:
4029 case X86ISD::VUNPCKLPSY:
4030 case X86ISD::VUNPCKLPDY:
4031 DecodeUNPCKLPMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004032 break;
4033 case X86ISD::MOVHLPS:
4034 DecodeMOVHLPSMask(NumElems, ShuffleMask);
4035 break;
4036 case X86ISD::MOVLHPS:
4037 DecodeMOVLHPSMask(NumElems, ShuffleMask);
4038 break;
4039 case X86ISD::PSHUFD:
4040 ImmN = N->getOperand(N->getNumOperands()-1);
4041 DecodePSHUFMask(NumElems,
4042 cast<ConstantSDNode>(ImmN)->getZExtValue(),
4043 ShuffleMask);
4044 break;
4045 case X86ISD::PSHUFHW:
4046 ImmN = N->getOperand(N->getNumOperands()-1);
4047 DecodePSHUFHWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
4048 ShuffleMask);
4049 break;
4050 case X86ISD::PSHUFLW:
4051 ImmN = N->getOperand(N->getNumOperands()-1);
4052 DecodePSHUFLWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
4053 ShuffleMask);
4054 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004055 case X86ISD::MOVSS:
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00004056 case X86ISD::MOVSD: {
4057 // The index 0 always comes from the first element of the second source,
4058 // this is why MOVSS and MOVSD are used in the first place. The other
4059 // elements come from the other positions of the first source vector.
4060 unsigned OpNum = (Index == 0) ? 1 : 0;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004061 return getShuffleScalarElt(V.getOperand(OpNum).getNode(), Index, DAG,
4062 Depth+1);
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00004063 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004064 default:
4065 assert("not implemented for target shuffle node");
4066 return SDValue();
4067 }
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004068
4069 Index = ShuffleMask[Index];
4070 if (Index < 0)
4071 return DAG.getUNDEF(VT.getVectorElementType());
4072
4073 SDValue NewV = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
4074 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG,
4075 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004076 }
4077
4078 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004079 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004080 V = V.getOperand(0);
4081 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004082 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004083
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004084 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004085 return SDValue();
4086 }
4087
4088 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
4089 return (Index == 0) ? V.getOperand(0)
4090 : DAG.getUNDEF(VT.getVectorElementType());
4091
4092 if (V.getOpcode() == ISD::BUILD_VECTOR)
4093 return V.getOperand(Index);
4094
4095 return SDValue();
4096}
4097
4098/// getNumOfConsecutiveZeros - Return the number of elements of a vector
4099/// shuffle operation which come from a consecutively from a zero. The
Chris Lattner7a2bdde2011-04-15 05:18:47 +00004100/// search can start in two different directions, from left or right.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004101static
4102unsigned getNumOfConsecutiveZeros(SDNode *N, int NumElems,
4103 bool ZerosFromLeft, SelectionDAG &DAG) {
4104 int i = 0;
4105
4106 while (i < NumElems) {
4107 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004108 SDValue Elt = getShuffleScalarElt(N, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004109 if (!(Elt.getNode() &&
4110 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
4111 break;
4112 ++i;
4113 }
4114
4115 return i;
4116}
4117
4118/// isShuffleMaskConsecutive - Check if the shuffle mask indicies from MaskI to
4119/// MaskE correspond consecutively to elements from one of the vector operands,
4120/// starting from its index OpIdx. Also tell OpNum which source vector operand.
4121static
4122bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp, int MaskI, int MaskE,
4123 int OpIdx, int NumElems, unsigned &OpNum) {
4124 bool SeenV1 = false;
4125 bool SeenV2 = false;
4126
4127 for (int i = MaskI; i <= MaskE; ++i, ++OpIdx) {
4128 int Idx = SVOp->getMaskElt(i);
4129 // Ignore undef indicies
4130 if (Idx < 0)
4131 continue;
4132
4133 if (Idx < NumElems)
4134 SeenV1 = true;
4135 else
4136 SeenV2 = true;
4137
4138 // Only accept consecutive elements from the same vector
4139 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
4140 return false;
4141 }
4142
4143 OpNum = SeenV1 ? 0 : 1;
4144 return true;
4145}
4146
4147/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
4148/// logical left shift of a vector.
4149static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4150 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4151 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4152 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4153 false /* check zeros from right */, DAG);
4154 unsigned OpSrc;
4155
4156 if (!NumZeros)
4157 return false;
4158
4159 // Considering the elements in the mask that are not consecutive zeros,
4160 // check if they consecutively come from only one of the source vectors.
4161 //
4162 // V1 = {X, A, B, C} 0
4163 // \ \ \ /
4164 // vector_shuffle V1, V2 <1, 2, 3, X>
4165 //
4166 if (!isShuffleMaskConsecutive(SVOp,
4167 0, // Mask Start Index
4168 NumElems-NumZeros-1, // Mask End Index
4169 NumZeros, // Where to start looking in the src vector
4170 NumElems, // Number of elements in vector
4171 OpSrc)) // Which source operand ?
4172 return false;
4173
4174 isLeft = false;
4175 ShAmt = NumZeros;
4176 ShVal = SVOp->getOperand(OpSrc);
4177 return true;
4178}
4179
4180/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
4181/// logical left shift of a vector.
4182static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4183 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4184 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4185 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4186 true /* check zeros from left */, DAG);
4187 unsigned OpSrc;
4188
4189 if (!NumZeros)
4190 return false;
4191
4192 // Considering the elements in the mask that are not consecutive zeros,
4193 // check if they consecutively come from only one of the source vectors.
4194 //
4195 // 0 { A, B, X, X } = V2
4196 // / \ / /
4197 // vector_shuffle V1, V2 <X, X, 4, 5>
4198 //
4199 if (!isShuffleMaskConsecutive(SVOp,
4200 NumZeros, // Mask Start Index
4201 NumElems-1, // Mask End Index
4202 0, // Where to start looking in the src vector
4203 NumElems, // Number of elements in vector
4204 OpSrc)) // Which source operand ?
4205 return false;
4206
4207 isLeft = true;
4208 ShAmt = NumZeros;
4209 ShVal = SVOp->getOperand(OpSrc);
4210 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004211}
4212
4213/// isVectorShift - Returns true if the shuffle can be implemented as a
4214/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004215static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00004216 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004217 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
4218 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
4219 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004220
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004221 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00004222}
4223
Evan Chengc78d3b42006-04-24 18:01:45 +00004224/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
4225///
Dan Gohman475871a2008-07-27 21:46:04 +00004226static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00004227 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00004228 SelectionDAG &DAG,
4229 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004230 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00004231 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004232
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004233 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004234 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004235 bool First = true;
4236 for (unsigned i = 0; i < 16; ++i) {
4237 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
4238 if (ThisIsNonZero && First) {
4239 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004240 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004241 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004242 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004243 First = false;
4244 }
4245
4246 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00004247 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004248 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
4249 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004250 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004251 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00004252 }
4253 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004254 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
4255 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
4256 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00004257 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004258 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00004259 } else
4260 ThisElt = LastElt;
4261
Gabor Greifba36cb52008-08-28 21:40:38 +00004262 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00004263 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00004264 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00004265 }
4266 }
4267
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004268 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00004269}
4270
Bill Wendlinga348c562007-03-22 18:42:45 +00004271/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00004272///
Dan Gohman475871a2008-07-27 21:46:04 +00004273static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00004274 unsigned NumNonZero, unsigned NumZero,
4275 SelectionDAG &DAG,
4276 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004277 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00004278 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004279
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004280 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004281 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004282 bool First = true;
4283 for (unsigned i = 0; i < 8; ++i) {
4284 bool isNonZero = (NonZeros & (1 << i)) != 0;
4285 if (isNonZero) {
4286 if (First) {
4287 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004288 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004289 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004290 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004291 First = false;
4292 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004293 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004294 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00004295 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00004296 }
4297 }
4298
4299 return V;
4300}
4301
Evan Chengf26ffe92008-05-29 08:22:04 +00004302/// getVShift - Return a vector logical shift node.
4303///
Owen Andersone50ed302009-08-10 22:56:29 +00004304static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00004305 unsigned NumBits, SelectionDAG &DAG,
4306 const TargetLowering &TLI, DebugLoc dl) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004307 EVT ShVT = MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00004308 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004309 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
4310 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004311 DAG.getNode(Opc, dl, ShVT, SrcOp,
Owen Anderson95771af2011-02-25 21:41:48 +00004312 DAG.getConstant(NumBits,
4313 TLI.getShiftAmountTy(SrcOp.getValueType()))));
Evan Chengf26ffe92008-05-29 08:22:04 +00004314}
4315
Dan Gohman475871a2008-07-27 21:46:04 +00004316SDValue
Evan Chengc3630942009-12-09 21:00:30 +00004317X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00004318 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00004319
Evan Chengc3630942009-12-09 21:00:30 +00004320 // Check if the scalar load can be widened into a vector load. And if
4321 // the address is "base + cst" see if the cst can be "absorbed" into
4322 // the shuffle mask.
4323 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
4324 SDValue Ptr = LD->getBasePtr();
4325 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
4326 return SDValue();
4327 EVT PVT = LD->getValueType(0);
4328 if (PVT != MVT::i32 && PVT != MVT::f32)
4329 return SDValue();
4330
4331 int FI = -1;
4332 int64_t Offset = 0;
4333 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
4334 FI = FINode->getIndex();
4335 Offset = 0;
Chris Lattner0a9481f2011-02-13 22:25:43 +00004336 } else if (DAG.isBaseWithConstantOffset(Ptr) &&
Evan Chengc3630942009-12-09 21:00:30 +00004337 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4338 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4339 Offset = Ptr.getConstantOperandVal(1);
4340 Ptr = Ptr.getOperand(0);
4341 } else {
4342 return SDValue();
4343 }
4344
4345 SDValue Chain = LD->getChain();
4346 // Make sure the stack object alignment is at least 16.
4347 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
4348 if (DAG.InferPtrAlignment(Ptr) < 16) {
4349 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00004350 // Can't change the alignment. FIXME: It's possible to compute
4351 // the exact stack offset and reference FI + adjust offset instead.
4352 // If someone *really* cares about this. That's the way to implement it.
4353 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004354 } else {
4355 MFI->setObjectAlignment(FI, 16);
4356 }
4357 }
4358
4359 // (Offset % 16) must be multiple of 4. Then address is then
4360 // Ptr + (Offset & ~15).
4361 if (Offset < 0)
4362 return SDValue();
4363 if ((Offset % 16) & 3)
4364 return SDValue();
4365 int64_t StartOffset = Offset & ~15;
4366 if (StartOffset)
4367 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
4368 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
4369
4370 int EltNo = (Offset - StartOffset) >> 2;
4371 int Mask[4] = { EltNo, EltNo, EltNo, EltNo };
4372 EVT VT = (PVT == MVT::i32) ? MVT::v4i32 : MVT::v4f32;
Chris Lattner51abfe42010-09-21 06:02:19 +00004373 SDValue V1 = DAG.getLoad(VT, dl, Chain, Ptr,
4374 LD->getPointerInfo().getWithOffset(StartOffset),
David Greene67c9d422010-02-15 16:53:33 +00004375 false, false, 0);
Evan Chengc3630942009-12-09 21:00:30 +00004376 // Canonicalize it to a v4i32 shuffle.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004377 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, V1);
4378 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Chengc3630942009-12-09 21:00:30 +00004379 DAG.getVectorShuffle(MVT::v4i32, dl, V1,
Chris Lattner51abfe42010-09-21 06:02:19 +00004380 DAG.getUNDEF(MVT::v4i32),&Mask[0]));
Evan Chengc3630942009-12-09 21:00:30 +00004381 }
4382
4383 return SDValue();
4384}
4385
Michael J. Spencerec38de22010-10-10 22:04:20 +00004386/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
4387/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00004388/// load which has the same value as a build_vector whose operands are 'elts'.
4389///
4390/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00004391///
Nate Begeman1449f292010-03-24 22:19:06 +00004392/// FIXME: we'd also like to handle the case where the last elements are zero
4393/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
4394/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004395static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00004396 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004397 EVT EltVT = VT.getVectorElementType();
4398 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00004399
Nate Begemanfdea31a2010-03-24 20:49:50 +00004400 LoadSDNode *LDBase = NULL;
4401 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004402
Nate Begeman1449f292010-03-24 22:19:06 +00004403 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00004404 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00004405 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004406 for (unsigned i = 0; i < NumElems; ++i) {
4407 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00004408
Nate Begemanfdea31a2010-03-24 20:49:50 +00004409 if (!Elt.getNode() ||
4410 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
4411 return SDValue();
4412 if (!LDBase) {
4413 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
4414 return SDValue();
4415 LDBase = cast<LoadSDNode>(Elt.getNode());
4416 LastLoadedElt = i;
4417 continue;
4418 }
4419 if (Elt.getOpcode() == ISD::UNDEF)
4420 continue;
4421
4422 LoadSDNode *LD = cast<LoadSDNode>(Elt);
4423 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
4424 return SDValue();
4425 LastLoadedElt = i;
4426 }
Nate Begeman1449f292010-03-24 22:19:06 +00004427
4428 // If we have found an entire vector of loads and undefs, then return a large
4429 // load of the entire vector width starting at the base pointer. If we found
4430 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004431 if (LastLoadedElt == NumElems - 1) {
4432 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00004433 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004434 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004435 LDBase->isVolatile(), LDBase->isNonTemporal(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00004436 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004437 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004438 LDBase->isVolatile(), LDBase->isNonTemporal(),
4439 LDBase->getAlignment());
4440 } else if (NumElems == 4 && LastLoadedElt == 1) {
4441 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
4442 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Chris Lattner88641552010-09-22 00:34:38 +00004443 SDValue ResNode = DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys,
4444 Ops, 2, MVT::i32,
4445 LDBase->getMemOperand());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004446 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00004447 }
4448 return SDValue();
4449}
4450
Evan Chengc3630942009-12-09 21:00:30 +00004451SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004452X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004453 DebugLoc dl = Op.getDebugLoc();
David Greenea5f26012011-02-07 19:36:54 +00004454
David Greenef125a292011-02-08 19:04:41 +00004455 EVT VT = Op.getValueType();
4456 EVT ExtVT = VT.getVectorElementType();
4457
4458 unsigned NumElems = Op.getNumOperands();
4459
4460 // For AVX-length vectors, build the individual 128-bit pieces and
4461 // use shuffles to put them in place.
Owen Anderson95771af2011-02-25 21:41:48 +00004462 if (VT.getSizeInBits() > 256 &&
4463 Subtarget->hasAVX() &&
David Greenef125a292011-02-08 19:04:41 +00004464 !ISD::isBuildVectorAllZeros(Op.getNode())) {
4465 SmallVector<SDValue, 8> V;
4466 V.resize(NumElems);
4467 for (unsigned i = 0; i < NumElems; ++i) {
4468 V[i] = Op.getOperand(i);
4469 }
Owen Anderson95771af2011-02-25 21:41:48 +00004470
David Greenef125a292011-02-08 19:04:41 +00004471 EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElems/2);
4472
4473 // Build the lower subvector.
4474 SDValue Lower = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[0], NumElems/2);
4475 // Build the upper subvector.
4476 SDValue Upper = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[NumElems / 2],
4477 NumElems/2);
4478
4479 return ConcatVectors(Lower, Upper, DAG);
4480 }
4481
Chris Lattner6e80e442010-08-28 17:15:43 +00004482 // All zero's are handled with pxor in SSE2 and above, xorps in SSE1.
4483 // All one's are handled with pcmpeqd. In AVX, zero's are handled with
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004484 // vpxor in 128-bit and xor{pd,ps} in 256-bit, but no 256 version of pcmpeqd
4485 // is present, so AllOnes is ignored.
4486 if (ISD::isBuildVectorAllZeros(Op.getNode()) ||
4487 (Op.getValueType().getSizeInBits() != 256 &&
4488 ISD::isBuildVectorAllOnes(Op.getNode()))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004489 // Canonicalize this to <4 x i32> (SSE) to
Chris Lattner8a594482007-11-25 00:24:49 +00004490 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
4491 // eliminated on x86-32 hosts.
Dale Johannesen0488fb62010-09-30 23:57:10 +00004492 if (Op.getValueType() == MVT::v4i32)
Chris Lattner8a594482007-11-25 00:24:49 +00004493 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004494
Gabor Greifba36cb52008-08-28 21:40:38 +00004495 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00004496 return getOnesVector(Op.getValueType(), DAG, dl);
4497 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00004498 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004499
Owen Andersone50ed302009-08-10 22:56:29 +00004500 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004501
Evan Cheng0db9fe62006-04-25 20:13:52 +00004502 unsigned NumZero = 0;
4503 unsigned NumNonZero = 0;
4504 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004505 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00004506 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004507 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00004508 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00004509 if (Elt.getOpcode() == ISD::UNDEF)
4510 continue;
4511 Values.insert(Elt);
4512 if (Elt.getOpcode() != ISD::Constant &&
4513 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004514 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00004515 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00004516 NumZero++;
4517 else {
4518 NonZeros |= (1 << i);
4519 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004520 }
4521 }
4522
Chris Lattner97a2a562010-08-26 05:24:29 +00004523 // All undef vector. Return an UNDEF. All zero vectors were handled above.
4524 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00004525 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004526
Chris Lattner67f453a2008-03-09 05:42:06 +00004527 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00004528 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004529 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00004530 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00004531
Chris Lattner62098042008-03-09 01:05:04 +00004532 // If this is an insertion of an i64 value on x86-32, and if the top bits of
4533 // the value are obviously zero, truncate the value to i32 and do the
4534 // insertion that way. Only do this if the value is non-constant or if the
4535 // value is a constant being inserted into element 0. It is cheaper to do
4536 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00004537 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00004538 (!IsAllConstants || Idx == 0)) {
4539 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004540 // Handle SSE only.
4541 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
4542 EVT VecVT = MVT::v4i32;
4543 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00004544
Chris Lattner62098042008-03-09 01:05:04 +00004545 // Truncate the value (which may itself be a constant) to i32, and
4546 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00004547 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00004548 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00004549 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
4550 Subtarget->hasSSE2(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00004551
Chris Lattner62098042008-03-09 01:05:04 +00004552 // Now we have our 32-bit value zero extended in the low element of
4553 // a vector. If Idx != 0, swizzle it into place.
4554 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004555 SmallVector<int, 4> Mask;
4556 Mask.push_back(Idx);
4557 for (unsigned i = 1; i != VecElts; ++i)
4558 Mask.push_back(i);
4559 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopherfd179292009-08-27 18:07:15 +00004560 DAG.getUNDEF(Item.getValueType()),
Nate Begeman9008ca62009-04-27 18:41:29 +00004561 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004562 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004563 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00004564 }
4565 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004566
Chris Lattner19f79692008-03-08 22:59:52 +00004567 // If we have a constant or non-constant insertion into the low element of
4568 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
4569 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00004570 // depending on what the source datatype is.
4571 if (Idx == 0) {
4572 if (NumZero == 0) {
4573 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Owen Anderson825b72b2009-08-11 20:47:22 +00004574 } else if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
4575 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Eli Friedman10415532009-06-06 06:05:10 +00004576 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
4577 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
4578 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget->hasSSE2(),
4579 DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00004580 } else if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
4581 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Dale Johannesen0488fb62010-09-30 23:57:10 +00004582 assert(VT.getSizeInBits() == 128 && "Expected an SSE value type!");
4583 EVT MiddleVT = MVT::v4i32;
Eli Friedman10415532009-06-06 06:05:10 +00004584 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
4585 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
4586 Subtarget->hasSSE2(), DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004587 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00004588 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004589 }
Evan Chengf26ffe92008-05-29 08:22:04 +00004590
4591 // Is it a vector logical left shift?
4592 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00004593 X86::isZeroNode(Op.getOperand(0)) &&
4594 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004595 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00004596 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00004597 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004598 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00004599 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004600 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004601
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004602 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00004603 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004604
Chris Lattner19f79692008-03-08 22:59:52 +00004605 // Otherwise, if this is a vector with i32 or f32 elements, and the element
4606 // is a non-constant being inserted into an element other than the low one,
4607 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
4608 // movd/movss) to move this into the low element, then shuffle it into
4609 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004610 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00004611 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00004612
Evan Cheng0db9fe62006-04-25 20:13:52 +00004613 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00004614 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
4615 Subtarget->hasSSE2(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00004616 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004617 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00004618 MaskVec.push_back(i == Idx ? 0 : 1);
4619 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004620 }
4621 }
4622
Chris Lattner67f453a2008-03-09 05:42:06 +00004623 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00004624 if (Values.size() == 1) {
4625 if (EVTBits == 32) {
4626 // Instead of a shuffle like this:
4627 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
4628 // Check if it's possible to issue this instead.
4629 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
4630 unsigned Idx = CountTrailingZeros_32(NonZeros);
4631 SDValue Item = Op.getOperand(Idx);
4632 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
4633 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
4634 }
Dan Gohman475871a2008-07-27 21:46:04 +00004635 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004636 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004637
Dan Gohmana3941172007-07-24 22:55:08 +00004638 // A vector full of immediates; various special cases are already
4639 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004640 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00004641 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00004642
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00004643 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00004644 if (EVTBits == 64) {
4645 if (NumNonZero == 1) {
4646 // One half is zero or undef.
4647 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00004648 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00004649 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00004650 return getShuffleVectorZeroOrUndef(V2, Idx, true,
4651 Subtarget->hasSSE2(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00004652 }
Dan Gohman475871a2008-07-27 21:46:04 +00004653 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00004654 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004655
4656 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00004657 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00004658 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00004659 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004660 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004661 }
4662
Bill Wendling826f36f2007-03-28 00:57:11 +00004663 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00004664 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Chris Lattner97a2a562010-08-26 05:24:29 +00004665 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004666 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004667 }
4668
4669 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00004670 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00004671 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004672 if (NumElems == 4 && NumZero > 0) {
4673 for (unsigned i = 0; i < 4; ++i) {
4674 bool isZero = !(NonZeros & (1 << i));
4675 if (isZero)
Dale Johannesenace16102009-02-03 19:33:06 +00004676 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004677 else
Dale Johannesenace16102009-02-03 19:33:06 +00004678 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004679 }
4680
4681 for (unsigned i = 0; i < 2; ++i) {
4682 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
4683 default: break;
4684 case 0:
4685 V[i] = V[i*2]; // Must be a zero vector.
4686 break;
4687 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00004688 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004689 break;
4690 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00004691 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004692 break;
4693 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00004694 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004695 break;
4696 }
4697 }
4698
Nate Begeman9008ca62009-04-27 18:41:29 +00004699 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004700 bool Reverse = (NonZeros & 0x3) == 2;
4701 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004702 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004703 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
4704 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004705 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
4706 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004707 }
4708
Nate Begemanfdea31a2010-03-24 20:49:50 +00004709 if (Values.size() > 1 && VT.getSizeInBits() == 128) {
4710 // Check for a build vector of consecutive loads.
4711 for (unsigned i = 0; i < NumElems; ++i)
4712 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00004713
Nate Begemanfdea31a2010-03-24 20:49:50 +00004714 // Check for elements which are consecutive loads.
4715 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
4716 if (LD.getNode())
4717 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004718
4719 // For SSE 4.1, use insertps to put the high elements into the low element.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004720 if (getSubtarget()->hasSSE41()) {
Chris Lattner24faf612010-08-28 17:59:08 +00004721 SDValue Result;
4722 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
4723 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
4724 else
4725 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00004726
Chris Lattner24faf612010-08-28 17:59:08 +00004727 for (unsigned i = 1; i < NumElems; ++i) {
4728 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
4729 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00004730 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00004731 }
4732 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00004733 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00004734
Chris Lattner6e80e442010-08-28 17:15:43 +00004735 // Otherwise, expand into a number of unpckl*, start by extending each of
4736 // our (non-undef) elements to the full vector width with the element in the
4737 // bottom slot of the vector (which generates no code for SSE).
4738 for (unsigned i = 0; i < NumElems; ++i) {
4739 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
4740 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
4741 else
4742 V[i] = DAG.getUNDEF(VT);
4743 }
4744
4745 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004746 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
4747 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
4748 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00004749 unsigned EltStride = NumElems >> 1;
4750 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00004751 for (unsigned i = 0; i < EltStride; ++i) {
4752 // If V[i+EltStride] is undef and this is the first round of mixing,
4753 // then it is safe to just drop this shuffle: V[i] is already in the
4754 // right place, the one element (since it's the first round) being
4755 // inserted as undef can be dropped. This isn't safe for successive
4756 // rounds because they will permute elements within both vectors.
4757 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
4758 EltStride == NumElems/2)
4759 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004760
Chris Lattner6e80e442010-08-28 17:15:43 +00004761 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00004762 }
Chris Lattner6e80e442010-08-28 17:15:43 +00004763 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004764 }
4765 return V[0];
4766 }
Dan Gohman475871a2008-07-27 21:46:04 +00004767 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004768}
4769
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004770SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004771X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const {
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004772 // We support concatenate two MMX registers and place them in a MMX
4773 // register. This is better than doing a stack convert.
4774 DebugLoc dl = Op.getDebugLoc();
4775 EVT ResVT = Op.getValueType();
4776 assert(Op.getNumOperands() == 2);
4777 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
4778 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
4779 int Mask[2];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004780 SDValue InVec = DAG.getNode(ISD::BITCAST,dl, MVT::v1i64, Op.getOperand(0));
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004781 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4782 InVec = Op.getOperand(1);
4783 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4784 unsigned NumElts = ResVT.getVectorNumElements();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004785 VecOp = DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004786 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
4787 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
4788 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004789 InVec = DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, InVec);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004790 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4791 Mask[0] = 0; Mask[1] = 2;
4792 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
4793 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004794 return DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004795}
4796
Nate Begemanb9a47b82009-02-23 08:49:38 +00004797// v8i16 shuffles - Prefer shuffles in the following order:
4798// 1. [all] pshuflw, pshufhw, optional move
4799// 2. [ssse3] 1 x pshufb
4800// 3. [ssse3] 2 x pshufb + 1 x por
4801// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004802SDValue
4803X86TargetLowering::LowerVECTOR_SHUFFLEv8i16(SDValue Op,
4804 SelectionDAG &DAG) const {
4805 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00004806 SDValue V1 = SVOp->getOperand(0);
4807 SDValue V2 = SVOp->getOperand(1);
4808 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004809 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00004810
Nate Begemanb9a47b82009-02-23 08:49:38 +00004811 // Determine if more than 1 of the words in each of the low and high quadwords
4812 // of the result come from the same quadword of one of the two inputs. Undef
4813 // mask values count as coming from any quadword, for better codegen.
4814 SmallVector<unsigned, 4> LoQuad(4);
4815 SmallVector<unsigned, 4> HiQuad(4);
4816 BitVector InputQuads(4);
4817 for (unsigned i = 0; i < 8; ++i) {
4818 SmallVectorImpl<unsigned> &Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00004819 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004820 MaskVals.push_back(EltIdx);
4821 if (EltIdx < 0) {
4822 ++Quad[0];
4823 ++Quad[1];
4824 ++Quad[2];
4825 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00004826 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004827 }
4828 ++Quad[EltIdx / 4];
4829 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00004830 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004831
Nate Begemanb9a47b82009-02-23 08:49:38 +00004832 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004833 unsigned MaxQuad = 1;
4834 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004835 if (LoQuad[i] > MaxQuad) {
4836 BestLoQuad = i;
4837 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004838 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004839 }
4840
Nate Begemanb9a47b82009-02-23 08:49:38 +00004841 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004842 MaxQuad = 1;
4843 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004844 if (HiQuad[i] > MaxQuad) {
4845 BestHiQuad = i;
4846 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004847 }
4848 }
4849
Nate Begemanb9a47b82009-02-23 08:49:38 +00004850 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00004851 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00004852 // single pshufb instruction is necessary. If There are more than 2 input
4853 // quads, disable the next transformation since it does not help SSSE3.
4854 bool V1Used = InputQuads[0] || InputQuads[1];
4855 bool V2Used = InputQuads[2] || InputQuads[3];
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004856 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004857 if (InputQuads.count() == 2 && V1Used && V2Used) {
4858 BestLoQuad = InputQuads.find_first();
4859 BestHiQuad = InputQuads.find_next(BestLoQuad);
4860 }
4861 if (InputQuads.count() > 2) {
4862 BestLoQuad = -1;
4863 BestHiQuad = -1;
4864 }
4865 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004866
Nate Begemanb9a47b82009-02-23 08:49:38 +00004867 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
4868 // the shuffle mask. If a quad is scored as -1, that means that it contains
4869 // words from all 4 input quadwords.
4870 SDValue NewV;
4871 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004872 SmallVector<int, 8> MaskV;
4873 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
4874 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Eric Christopherfd179292009-08-27 18:07:15 +00004875 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004876 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
4877 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
4878 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00004879
Nate Begemanb9a47b82009-02-23 08:49:38 +00004880 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
4881 // source words for the shuffle, to aid later transformations.
4882 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00004883 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00004884 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004885 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00004886 if (idx != (int)i)
4887 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004888 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00004889 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004890 AllWordsInNewV = false;
4891 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00004892 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004893
Nate Begemanb9a47b82009-02-23 08:49:38 +00004894 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
4895 if (AllWordsInNewV) {
4896 for (int i = 0; i != 8; ++i) {
4897 int idx = MaskVals[i];
4898 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00004899 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004900 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004901 if ((idx != i) && idx < 4)
4902 pshufhw = false;
4903 if ((idx != i) && idx > 3)
4904 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00004905 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00004906 V1 = NewV;
4907 V2Used = false;
4908 BestLoQuad = 0;
4909 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004910 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004911
Nate Begemanb9a47b82009-02-23 08:49:38 +00004912 // If we've eliminated the use of V2, and the new mask is a pshuflw or
4913 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00004914 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00004915 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
4916 unsigned TargetMask = 0;
4917 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00004918 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00004919 TargetMask = pshufhw ? X86::getShufflePSHUFHWImmediate(NewV.getNode()):
4920 X86::getShufflePSHUFLWImmediate(NewV.getNode());
4921 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004922 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00004923 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004924 }
Eric Christopherfd179292009-08-27 18:07:15 +00004925
Nate Begemanb9a47b82009-02-23 08:49:38 +00004926 // If we have SSSE3, and all words of the result are from 1 input vector,
4927 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
4928 // is present, fall back to case 4.
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004929 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004930 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004931
Nate Begemanb9a47b82009-02-23 08:49:38 +00004932 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00004933 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00004934 // mask, and elements that come from V1 in the V2 mask, so that the two
4935 // results can be OR'd together.
4936 bool TwoInputs = V1Used && V2Used;
4937 for (unsigned i = 0; i != 8; ++i) {
4938 int EltIdx = MaskVals[i] * 2;
4939 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004940 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4941 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004942 continue;
4943 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004944 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
4945 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004946 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004947 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004948 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004949 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004950 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004951 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004952 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004953
Nate Begemanb9a47b82009-02-23 08:49:38 +00004954 // Calculate the shuffle mask for the second input, shuffle it, and
4955 // OR it with the first shuffled input.
4956 pshufbMask.clear();
4957 for (unsigned i = 0; i != 8; ++i) {
4958 int EltIdx = MaskVals[i] * 2;
4959 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004960 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4961 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004962 continue;
4963 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004964 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
4965 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004966 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004967 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00004968 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00004969 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004970 MVT::v16i8, &pshufbMask[0], 16));
4971 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004972 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004973 }
4974
4975 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
4976 // and update MaskVals with new element order.
4977 BitVector InOrder(8);
4978 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004979 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004980 for (int i = 0; i != 4; ++i) {
4981 int idx = MaskVals[i];
4982 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004983 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004984 InOrder.set(i);
4985 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004986 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004987 InOrder.set(i);
4988 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004989 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004990 }
4991 }
4992 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004993 MaskV.push_back(i);
Owen Anderson825b72b2009-08-11 20:47:22 +00004994 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004995 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004996
4997 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
4998 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
4999 NewV.getOperand(0),
5000 X86::getShufflePSHUFLWImmediate(NewV.getNode()),
5001 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005002 }
Eric Christopherfd179292009-08-27 18:07:15 +00005003
Nate Begemanb9a47b82009-02-23 08:49:38 +00005004 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
5005 // and update MaskVals with the new element order.
5006 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005007 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005008 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005009 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005010 for (unsigned i = 4; i != 8; ++i) {
5011 int idx = MaskVals[i];
5012 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005013 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005014 InOrder.set(i);
5015 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005016 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005017 InOrder.set(i);
5018 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005019 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005020 }
5021 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005022 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005023 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005024
5025 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
5026 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
5027 NewV.getOperand(0),
5028 X86::getShufflePSHUFHWImmediate(NewV.getNode()),
5029 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005030 }
Eric Christopherfd179292009-08-27 18:07:15 +00005031
Nate Begemanb9a47b82009-02-23 08:49:38 +00005032 // In case BestHi & BestLo were both -1, which means each quadword has a word
5033 // from each of the four input quadwords, calculate the InOrder bitvector now
5034 // before falling through to the insert/extract cleanup.
5035 if (BestLoQuad == -1 && BestHiQuad == -1) {
5036 NewV = V1;
5037 for (int i = 0; i != 8; ++i)
5038 if (MaskVals[i] < 0 || MaskVals[i] == i)
5039 InOrder.set(i);
5040 }
Eric Christopherfd179292009-08-27 18:07:15 +00005041
Nate Begemanb9a47b82009-02-23 08:49:38 +00005042 // The other elements are put in the right place using pextrw and pinsrw.
5043 for (unsigned i = 0; i != 8; ++i) {
5044 if (InOrder[i])
5045 continue;
5046 int EltIdx = MaskVals[i];
5047 if (EltIdx < 0)
5048 continue;
5049 SDValue ExtOp = (EltIdx < 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00005050 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005051 DAG.getIntPtrConstant(EltIdx))
Owen Anderson825b72b2009-08-11 20:47:22 +00005052 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005053 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00005054 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005055 DAG.getIntPtrConstant(i));
5056 }
5057 return NewV;
5058}
5059
5060// v16i8 shuffles - Prefer shuffles in the following order:
5061// 1. [ssse3] 1 x pshufb
5062// 2. [ssse3] 2 x pshufb + 1 x por
5063// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
5064static
Nate Begeman9008ca62009-04-27 18:41:29 +00005065SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00005066 SelectionDAG &DAG,
5067 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005068 SDValue V1 = SVOp->getOperand(0);
5069 SDValue V2 = SVOp->getOperand(1);
5070 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005071 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00005072 SVOp->getMask(MaskVals);
Eric Christopherfd179292009-08-27 18:07:15 +00005073
Nate Begemanb9a47b82009-02-23 08:49:38 +00005074 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00005075 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00005076 // present, fall back to case 3.
5077 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
5078 bool V1Only = true;
5079 bool V2Only = true;
5080 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005081 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00005082 if (EltIdx < 0)
5083 continue;
5084 if (EltIdx < 16)
5085 V2Only = false;
5086 else
5087 V1Only = false;
5088 }
Eric Christopherfd179292009-08-27 18:07:15 +00005089
Nate Begemanb9a47b82009-02-23 08:49:38 +00005090 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
5091 if (TLI.getSubtarget()->hasSSSE3()) {
5092 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005093
Nate Begemanb9a47b82009-02-23 08:49:38 +00005094 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00005095 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005096 //
5097 // Otherwise, we have elements from both input vectors, and must zero out
5098 // elements that come from V2 in the first mask, and V1 in the second mask
5099 // so that we can OR them together.
5100 bool TwoInputs = !(V1Only || V2Only);
5101 for (unsigned i = 0; i != 16; ++i) {
5102 int EltIdx = MaskVals[i];
5103 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005104 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005105 continue;
5106 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005107 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005108 }
5109 // If all the elements are from V2, assign it to V1 and return after
5110 // building the first pshufb.
5111 if (V2Only)
5112 V1 = V2;
Owen Anderson825b72b2009-08-11 20:47:22 +00005113 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005114 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005115 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005116 if (!TwoInputs)
5117 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00005118
Nate Begemanb9a47b82009-02-23 08:49:38 +00005119 // Calculate the shuffle mask for the second input, shuffle it, and
5120 // OR it with the first shuffled input.
5121 pshufbMask.clear();
5122 for (unsigned i = 0; i != 16; ++i) {
5123 int EltIdx = MaskVals[i];
5124 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005125 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005126 continue;
5127 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005128 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005129 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005130 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005131 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005132 MVT::v16i8, &pshufbMask[0], 16));
5133 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005134 }
Eric Christopherfd179292009-08-27 18:07:15 +00005135
Nate Begemanb9a47b82009-02-23 08:49:38 +00005136 // No SSSE3 - Calculate in place words and then fix all out of place words
5137 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
5138 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005139 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
5140 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005141 SDValue NewV = V2Only ? V2 : V1;
5142 for (int i = 0; i != 8; ++i) {
5143 int Elt0 = MaskVals[i*2];
5144 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00005145
Nate Begemanb9a47b82009-02-23 08:49:38 +00005146 // This word of the result is all undef, skip it.
5147 if (Elt0 < 0 && Elt1 < 0)
5148 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005149
Nate Begemanb9a47b82009-02-23 08:49:38 +00005150 // This word of the result is already in the correct place, skip it.
5151 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
5152 continue;
5153 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
5154 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005155
Nate Begemanb9a47b82009-02-23 08:49:38 +00005156 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
5157 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
5158 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00005159
5160 // If Elt0 and Elt1 are defined, are consecutive, and can be load
5161 // using a single extract together, load it and store it.
5162 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005163 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005164 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00005165 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005166 DAG.getIntPtrConstant(i));
5167 continue;
5168 }
5169
Nate Begemanb9a47b82009-02-23 08:49:38 +00005170 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00005171 // source byte is not also odd, shift the extracted word left 8 bits
5172 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005173 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005174 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005175 DAG.getIntPtrConstant(Elt1 / 2));
5176 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005177 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Owen Anderson95771af2011-02-25 21:41:48 +00005178 DAG.getConstant(8,
5179 TLI.getShiftAmountTy(InsElt.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005180 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005181 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
5182 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005183 }
5184 // If Elt0 is defined, extract it from the appropriate source. If the
5185 // source byte is not also even, shift the extracted word right 8 bits. If
5186 // Elt1 was also defined, OR the extracted values together before
5187 // inserting them in the result.
5188 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005189 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005190 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
5191 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005192 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Owen Anderson95771af2011-02-25 21:41:48 +00005193 DAG.getConstant(8,
5194 TLI.getShiftAmountTy(InsElt0.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005195 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005196 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
5197 DAG.getConstant(0x00FF, MVT::i16));
5198 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00005199 : InsElt0;
5200 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005201 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005202 DAG.getIntPtrConstant(i));
5203 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005204 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005205}
5206
Evan Cheng7a831ce2007-12-15 03:00:47 +00005207/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005208/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00005209/// done when every pair / quad of shuffle mask elements point to elements in
5210/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005211/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00005212static
Nate Begeman9008ca62009-04-27 18:41:29 +00005213SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005214 SelectionDAG &DAG, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00005215 EVT VT = SVOp->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00005216 SDValue V1 = SVOp->getOperand(0);
5217 SDValue V2 = SVOp->getOperand(1);
5218 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00005219 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005220 EVT NewVT;
Owen Anderson825b72b2009-08-11 20:47:22 +00005221 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005222 default: assert(false && "Unexpected!");
Owen Anderson825b72b2009-08-11 20:47:22 +00005223 case MVT::v4f32: NewVT = MVT::v2f64; break;
5224 case MVT::v4i32: NewVT = MVT::v2i64; break;
5225 case MVT::v8i16: NewVT = MVT::v4i32; break;
5226 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00005227 }
5228
Nate Begeman9008ca62009-04-27 18:41:29 +00005229 int Scale = NumElems / NewWidth;
5230 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00005231 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005232 int StartIdx = -1;
5233 for (int j = 0; j < Scale; ++j) {
5234 int EltIdx = SVOp->getMaskElt(i+j);
5235 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005236 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00005237 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00005238 StartIdx = EltIdx - (EltIdx % Scale);
5239 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00005240 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00005241 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005242 if (StartIdx == -1)
5243 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00005244 else
Nate Begeman9008ca62009-04-27 18:41:29 +00005245 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005246 }
5247
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005248 V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, V1);
5249 V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00005250 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005251}
5252
Evan Chengd880b972008-05-09 21:53:03 +00005253/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005254///
Owen Andersone50ed302009-08-10 22:56:29 +00005255static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00005256 SDValue SrcOp, SelectionDAG &DAG,
5257 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005258 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005259 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00005260 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00005261 LD = dyn_cast<LoadSDNode>(SrcOp);
5262 if (!LD) {
5263 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
5264 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00005265 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00005266 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00005267 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005268 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00005269 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005270 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00005271 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005272 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005273 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
5274 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
5275 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00005276 SrcOp.getOperand(0)
5277 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005278 }
5279 }
5280 }
5281
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005282 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005283 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005284 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00005285 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005286}
5287
Evan Chengace3c172008-07-22 21:13:36 +00005288/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
5289/// shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00005290static SDValue
Nate Begeman9008ca62009-04-27 18:41:29 +00005291LowerVECTOR_SHUFFLE_4wide(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
5292 SDValue V1 = SVOp->getOperand(0);
5293 SDValue V2 = SVOp->getOperand(1);
5294 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005295 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00005296
Evan Chengace3c172008-07-22 21:13:36 +00005297 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00005298 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00005299 SmallVector<int, 8> Mask1(4U, -1);
5300 SmallVector<int, 8> PermMask;
5301 SVOp->getMask(PermMask);
5302
Evan Chengace3c172008-07-22 21:13:36 +00005303 unsigned NumHi = 0;
5304 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00005305 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005306 int Idx = PermMask[i];
5307 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00005308 Locs[i] = std::make_pair(-1, -1);
5309 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005310 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
5311 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00005312 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00005313 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005314 NumLo++;
5315 } else {
5316 Locs[i] = std::make_pair(1, NumHi);
5317 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00005318 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005319 NumHi++;
5320 }
5321 }
5322 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005323
Evan Chengace3c172008-07-22 21:13:36 +00005324 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005325 // If no more than two elements come from either vector. This can be
5326 // implemented with two shuffles. First shuffle gather the elements.
5327 // The second shuffle, which takes the first shuffle as both of its
5328 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00005329 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005330
Nate Begeman9008ca62009-04-27 18:41:29 +00005331 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopherfd179292009-08-27 18:07:15 +00005332
Evan Chengace3c172008-07-22 21:13:36 +00005333 for (unsigned i = 0; i != 4; ++i) {
5334 if (Locs[i].first == -1)
5335 continue;
5336 else {
5337 unsigned Idx = (i < 2) ? 0 : 4;
5338 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00005339 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005340 }
5341 }
5342
Nate Begeman9008ca62009-04-27 18:41:29 +00005343 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005344 } else if (NumLo == 3 || NumHi == 3) {
5345 // Otherwise, we must have three elements from one vector, call it X, and
5346 // one element from the other, call it Y. First, use a shufps to build an
5347 // intermediate vector with the one element from Y and the element from X
5348 // that will be in the same half in the final destination (the indexes don't
5349 // matter). Then, use a shufps to build the final vector, taking the half
5350 // containing the element from Y from the intermediate, and the other half
5351 // from X.
5352 if (NumHi == 3) {
5353 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00005354 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005355 std::swap(V1, V2);
5356 }
5357
5358 // Find the element from V2.
5359 unsigned HiIndex;
5360 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005361 int Val = PermMask[HiIndex];
5362 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005363 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005364 if (Val >= 4)
5365 break;
5366 }
5367
Nate Begeman9008ca62009-04-27 18:41:29 +00005368 Mask1[0] = PermMask[HiIndex];
5369 Mask1[1] = -1;
5370 Mask1[2] = PermMask[HiIndex^1];
5371 Mask1[3] = -1;
5372 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005373
5374 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005375 Mask1[0] = PermMask[0];
5376 Mask1[1] = PermMask[1];
5377 Mask1[2] = HiIndex & 1 ? 6 : 4;
5378 Mask1[3] = HiIndex & 1 ? 4 : 6;
5379 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005380 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005381 Mask1[0] = HiIndex & 1 ? 2 : 0;
5382 Mask1[1] = HiIndex & 1 ? 0 : 2;
5383 Mask1[2] = PermMask[2];
5384 Mask1[3] = PermMask[3];
5385 if (Mask1[2] >= 0)
5386 Mask1[2] += 4;
5387 if (Mask1[3] >= 0)
5388 Mask1[3] += 4;
5389 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005390 }
Evan Chengace3c172008-07-22 21:13:36 +00005391 }
5392
5393 // Break it into (shuffle shuffle_hi, shuffle_lo).
5394 Locs.clear();
David Greenec4db4e52011-02-28 19:06:56 +00005395 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00005396 SmallVector<int,8> LoMask(4U, -1);
5397 SmallVector<int,8> HiMask(4U, -1);
5398
5399 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00005400 unsigned MaskIdx = 0;
5401 unsigned LoIdx = 0;
5402 unsigned HiIdx = 2;
5403 for (unsigned i = 0; i != 4; ++i) {
5404 if (i == 2) {
5405 MaskPtr = &HiMask;
5406 MaskIdx = 1;
5407 LoIdx = 0;
5408 HiIdx = 2;
5409 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005410 int Idx = PermMask[i];
5411 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00005412 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00005413 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00005414 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00005415 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005416 LoIdx++;
5417 } else {
5418 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00005419 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005420 HiIdx++;
5421 }
5422 }
5423
Nate Begeman9008ca62009-04-27 18:41:29 +00005424 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
5425 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
5426 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00005427 for (unsigned i = 0; i != 4; ++i) {
5428 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005429 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00005430 } else {
5431 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00005432 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00005433 }
5434 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005435 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00005436}
5437
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005438static bool MayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005439 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005440 V = V.getOperand(0);
5441 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
5442 V = V.getOperand(0);
5443 if (MayFoldLoad(V))
5444 return true;
5445 return false;
5446}
5447
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005448// FIXME: the version above should always be used. Since there's
5449// a bug where several vector shuffles can't be folded because the
5450// DAG is not updated during lowering and a node claims to have two
5451// uses while it only has one, use this version, and let isel match
5452// another instruction if the load really happens to have more than
5453// one use. Remove this version after this bug get fixed.
Evan Cheng835580f2010-10-07 20:50:20 +00005454// rdar://8434668, PR8156
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005455static bool RelaxedMayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005456 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005457 V = V.getOperand(0);
5458 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
5459 V = V.getOperand(0);
5460 if (ISD::isNormalLoad(V.getNode()))
5461 return true;
5462 return false;
5463}
5464
5465/// CanFoldShuffleIntoVExtract - Check if the current shuffle is used by
5466/// a vector extract, and if both can be later optimized into a single load.
5467/// This is done in visitEXTRACT_VECTOR_ELT and the conditions are checked
5468/// here because otherwise a target specific shuffle node is going to be
5469/// emitted for this shuffle, and the optimization not done.
5470/// FIXME: This is probably not the best approach, but fix the problem
5471/// until the right path is decided.
5472static
5473bool CanXFormVExtractWithShuffleIntoLoad(SDValue V, SelectionDAG &DAG,
5474 const TargetLowering &TLI) {
5475 EVT VT = V.getValueType();
5476 ShuffleVectorSDNode *SVOp = dyn_cast<ShuffleVectorSDNode>(V);
5477
5478 // Be sure that the vector shuffle is present in a pattern like this:
5479 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), c) -> (f32 load $addr)
5480 if (!V.hasOneUse())
5481 return false;
5482
5483 SDNode *N = *V.getNode()->use_begin();
5484 if (N->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
5485 return false;
5486
5487 SDValue EltNo = N->getOperand(1);
5488 if (!isa<ConstantSDNode>(EltNo))
5489 return false;
5490
5491 // If the bit convert changed the number of elements, it is unsafe
5492 // to examine the mask.
5493 bool HasShuffleIntoBitcast = false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005494 if (V.getOpcode() == ISD::BITCAST) {
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005495 EVT SrcVT = V.getOperand(0).getValueType();
5496 if (SrcVT.getVectorNumElements() != VT.getVectorNumElements())
5497 return false;
5498 V = V.getOperand(0);
5499 HasShuffleIntoBitcast = true;
5500 }
5501
5502 // Select the input vector, guarding against out of range extract vector.
5503 unsigned NumElems = VT.getVectorNumElements();
5504 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
5505 int Idx = (Elt > NumElems) ? -1 : SVOp->getMaskElt(Elt);
5506 V = (Idx < (int)NumElems) ? V.getOperand(0) : V.getOperand(1);
5507
5508 // Skip one more bit_convert if necessary
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005509 if (V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005510 V = V.getOperand(0);
5511
5512 if (ISD::isNormalLoad(V.getNode())) {
5513 // Is the original load suitable?
5514 LoadSDNode *LN0 = cast<LoadSDNode>(V);
5515
5516 // FIXME: avoid the multi-use bug that is preventing lots of
5517 // of foldings to be detected, this is still wrong of course, but
5518 // give the temporary desired behavior, and if it happens that
5519 // the load has real more uses, during isel it will not fold, and
5520 // will generate poor code.
5521 if (!LN0 || LN0->isVolatile()) // || !LN0->hasOneUse()
5522 return false;
5523
5524 if (!HasShuffleIntoBitcast)
5525 return true;
5526
5527 // If there's a bitcast before the shuffle, check if the load type and
5528 // alignment is valid.
5529 unsigned Align = LN0->getAlignment();
5530 unsigned NewAlign =
5531 TLI.getTargetData()->getABITypeAlignment(
5532 VT.getTypeForEVT(*DAG.getContext()));
5533
5534 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
5535 return false;
5536 }
5537
5538 return true;
5539}
5540
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005541static
Evan Cheng835580f2010-10-07 20:50:20 +00005542SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
5543 EVT VT = Op.getValueType();
5544
5545 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005546 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
5547 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00005548 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
5549 V1, DAG));
5550}
5551
5552static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005553SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
5554 bool HasSSE2) {
5555 SDValue V1 = Op.getOperand(0);
5556 SDValue V2 = Op.getOperand(1);
5557 EVT VT = Op.getValueType();
5558
5559 assert(VT != MVT::v2i64 && "unsupported shuffle type");
5560
5561 if (HasSSE2 && VT == MVT::v2f64)
5562 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
5563
5564 // v4f32 or v4i32
5565 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V2, DAG);
5566}
5567
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00005568static
5569SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
5570 SDValue V1 = Op.getOperand(0);
5571 SDValue V2 = Op.getOperand(1);
5572 EVT VT = Op.getValueType();
5573
5574 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
5575 "unsupported shuffle type");
5576
5577 if (V2.getOpcode() == ISD::UNDEF)
5578 V2 = V1;
5579
5580 // v4i32 or v4f32
5581 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
5582}
5583
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005584static
5585SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasSSE2) {
5586 SDValue V1 = Op.getOperand(0);
5587 SDValue V2 = Op.getOperand(1);
5588 EVT VT = Op.getValueType();
5589 unsigned NumElems = VT.getVectorNumElements();
5590
5591 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
5592 // operand of these instructions is only memory, so check if there's a
5593 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
5594 // same masks.
5595 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005596
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00005597 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005598 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005599 CanFoldLoad = true;
5600
5601 // When V1 is a load, it can be folded later into a store in isel, example:
5602 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
5603 // turns into:
5604 // (MOVLPSmr addr:$src1, VR128:$src2)
5605 // So, recognize this potential and also use MOVLPS or MOVLPD
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005606 if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005607 CanFoldLoad = true;
5608
Eric Christopher893a8822011-02-20 05:04:42 +00005609 // Both of them can't be memory operations though.
5610 if (MayFoldVectorLoad(V1) && MayFoldVectorLoad(V2))
5611 CanFoldLoad = false;
Owen Anderson95771af2011-02-25 21:41:48 +00005612
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005613 if (CanFoldLoad) {
5614 if (HasSSE2 && NumElems == 2)
5615 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
5616
5617 if (NumElems == 4)
5618 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
5619 }
5620
5621 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5622 // movl and movlp will both match v2i64, but v2i64 is never matched by
5623 // movl earlier because we make it strict to avoid messing with the movlp load
5624 // folding logic (see the code above getMOVLP call). Match it here then,
5625 // this is horrible, but will stay like this until we move all shuffle
5626 // matching to x86 specific nodes. Note that for the 1st condition all
5627 // types are matched with movsd.
5628 if ((HasSSE2 && NumElems == 2) || !X86::isMOVLMask(SVOp))
5629 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
5630 else if (HasSSE2)
5631 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
5632
5633
5634 assert(VT != MVT::v4i32 && "unsupported shuffle type");
5635
5636 // Invert the operand order and use SHUFPS to match it.
5637 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V2, V1,
5638 X86::getShuffleSHUFImmediate(SVOp), DAG);
5639}
5640
David Greenec4db4e52011-02-28 19:06:56 +00005641static inline unsigned getUNPCKLOpcode(EVT VT, const X86Subtarget *Subtarget) {
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005642 switch(VT.getSimpleVT().SimpleTy) {
5643 case MVT::v4i32: return X86ISD::PUNPCKLDQ;
5644 case MVT::v2i64: return X86ISD::PUNPCKLQDQ;
David Greenec4db4e52011-02-28 19:06:56 +00005645 case MVT::v4f32:
5646 return Subtarget->hasAVX() ? X86ISD::VUNPCKLPS : X86ISD::UNPCKLPS;
5647 case MVT::v2f64:
5648 return Subtarget->hasAVX() ? X86ISD::VUNPCKLPD : X86ISD::UNPCKLPD;
5649 case MVT::v8f32: return X86ISD::VUNPCKLPSY;
5650 case MVT::v4f64: return X86ISD::VUNPCKLPDY;
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005651 case MVT::v16i8: return X86ISD::PUNPCKLBW;
5652 case MVT::v8i16: return X86ISD::PUNPCKLWD;
5653 default:
Eric Christopherdd6e40a2011-02-19 03:19:09 +00005654 llvm_unreachable("Unknown type for unpckl");
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005655 }
5656 return 0;
5657}
5658
5659static inline unsigned getUNPCKHOpcode(EVT VT) {
5660 switch(VT.getSimpleVT().SimpleTy) {
5661 case MVT::v4i32: return X86ISD::PUNPCKHDQ;
5662 case MVT::v2i64: return X86ISD::PUNPCKHQDQ;
5663 case MVT::v4f32: return X86ISD::UNPCKHPS;
5664 case MVT::v2f64: return X86ISD::UNPCKHPD;
5665 case MVT::v16i8: return X86ISD::PUNPCKHBW;
5666 case MVT::v8i16: return X86ISD::PUNPCKHWD;
5667 default:
Eric Christopherdd6e40a2011-02-19 03:19:09 +00005668 llvm_unreachable("Unknown type for unpckh");
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005669 }
5670 return 0;
5671}
5672
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005673static
5674SDValue NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG,
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005675 const TargetLowering &TLI,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005676 const X86Subtarget *Subtarget) {
5677 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5678 EVT VT = Op.getValueType();
5679 DebugLoc dl = Op.getDebugLoc();
5680 SDValue V1 = Op.getOperand(0);
5681 SDValue V2 = Op.getOperand(1);
5682
5683 if (isZeroShuffle(SVOp))
5684 return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
5685
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005686 // Handle splat operations
5687 if (SVOp->isSplat()) {
5688 // Special case, this is the only place now where it's
5689 // allowed to return a vector_shuffle operation without
5690 // using a target specific node, because *hopefully* it
5691 // will be optimized away by the dag combiner.
5692 if (VT.getVectorNumElements() <= 4 &&
5693 CanXFormVExtractWithShuffleIntoLoad(Op, DAG, TLI))
5694 return Op;
5695
5696 // Handle splats by matching through known masks
5697 if (VT.getVectorNumElements() <= 4)
5698 return SDValue();
5699
Evan Cheng835580f2010-10-07 20:50:20 +00005700 // Canonicalize all of the remaining to v4f32.
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005701 return PromoteSplat(SVOp, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005702 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005703
5704 // If the shuffle can be profitably rewritten as a narrower shuffle, then
5705 // do it!
5706 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
5707 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5708 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005709 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005710 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
5711 // FIXME: Figure out a cleaner way to do this.
5712 // Try to make use of movq to zero out the top part.
5713 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
5714 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5715 if (NewOp.getNode()) {
5716 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
5717 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
5718 DAG, Subtarget, dl);
5719 }
5720 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
5721 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5722 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
5723 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
5724 DAG, Subtarget, dl);
5725 }
5726 }
5727 return SDValue();
5728}
5729
Dan Gohman475871a2008-07-27 21:46:04 +00005730SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005731X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00005732 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00005733 SDValue V1 = Op.getOperand(0);
5734 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00005735 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005736 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00005737 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005738 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005739 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
5740 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00005741 bool V1IsSplat = false;
5742 bool V2IsSplat = false;
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005743 bool HasSSE2 = Subtarget->hasSSE2() || Subtarget->hasAVX();
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00005744 bool HasSSE3 = Subtarget->hasSSE3() || Subtarget->hasAVX();
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00005745 bool HasSSSE3 = Subtarget->hasSSSE3() || Subtarget->hasAVX();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005746 MachineFunction &MF = DAG.getMachineFunction();
5747 bool OptForSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005748
Dale Johannesen0488fb62010-09-30 23:57:10 +00005749 // Shuffle operations on MMX not supported.
5750 if (isMMX)
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00005751 return Op;
5752
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005753 // Vector shuffle lowering takes 3 steps:
5754 //
5755 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
5756 // narrowing and commutation of operands should be handled.
5757 // 2) Matching of shuffles with known shuffle masks to x86 target specific
5758 // shuffle nodes.
5759 // 3) Rewriting of unmatched masks into new generic shuffle operations,
5760 // so the shuffle can be broken into other shuffles and the legalizer can
5761 // try the lowering again.
5762 //
5763 // The general ideia is that no vector_shuffle operation should be left to
5764 // be matched during isel, all of them must be converted to a target specific
5765 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00005766
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005767 // Normalize the input vectors. Here splats, zeroed vectors, profitable
5768 // narrowing and commutation of operands should be handled. The actual code
5769 // doesn't include all of those, work in progress...
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005770 SDValue NewOp = NormalizeVectorShuffle(Op, DAG, *this, Subtarget);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005771 if (NewOp.getNode())
5772 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00005773
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005774 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
5775 // unpckh_undef). Only use pshufd if speed is more important than size.
5776 if (OptForSize && X86::isUNPCKL_v_undef_Mask(SVOp))
5777 if (VT != MVT::v2i64 && VT != MVT::v2f64)
David Greenec4db4e52011-02-28 19:06:56 +00005778 return getTargetShuffleNode(getUNPCKLOpcode(VT, getSubtarget()), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005779 if (OptForSize && X86::isUNPCKH_v_undef_Mask(SVOp))
5780 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5781 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00005782
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005783 if (X86::isMOVDDUPMask(SVOp) && HasSSE3 && V2IsUndef &&
Dale Johannesen0488fb62010-09-30 23:57:10 +00005784 RelaxedMayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00005785 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005786
Dale Johannesen0488fb62010-09-30 23:57:10 +00005787 if (X86::isMOVHLPS_v_undef_Mask(SVOp))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005788 return getMOVHighToLow(Op, dl, DAG);
5789
5790 // Use to match splats
5791 if (HasSSE2 && X86::isUNPCKHMask(SVOp) && V2IsUndef &&
5792 (VT == MVT::v2f64 || VT == MVT::v2i64))
5793 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
5794
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005795 if (X86::isPSHUFDMask(SVOp)) {
5796 // The actual implementation will match the mask in the if above and then
5797 // during isel it can match several different instructions, not only pshufd
5798 // as its name says, sad but true, emulate the behavior for now...
5799 if (X86::isMOVDDUPMask(SVOp) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
5800 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
5801
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005802 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
5803
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005804 if (HasSSE2 && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005805 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
5806
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005807 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005808 return getTargetShuffleNode(X86ISD::SHUFPD, dl, VT, V1, V1,
5809 TargetMask, DAG);
5810
5811 if (VT == MVT::v4f32)
5812 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V1, V1,
5813 TargetMask, DAG);
5814 }
Eric Christopherfd179292009-08-27 18:07:15 +00005815
Evan Chengf26ffe92008-05-29 08:22:04 +00005816 // Check if this can be converted into a logical shift.
5817 bool isLeft = false;
5818 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00005819 SDValue ShVal;
Nate Begeman9008ca62009-04-27 18:41:29 +00005820 bool isShift = getSubtarget()->hasSSE2() &&
Evan Chengc3630942009-12-09 21:00:30 +00005821 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00005822 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00005823 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00005824 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00005825 EVT EltVT = VT.getVectorElementType();
5826 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00005827 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005828 }
Eric Christopherfd179292009-08-27 18:07:15 +00005829
Nate Begeman9008ca62009-04-27 18:41:29 +00005830 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005831 if (V1IsUndef)
5832 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00005833 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00005834 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Dale Johannesen0488fb62010-09-30 23:57:10 +00005835 if (!X86::isMOVLPMask(SVOp)) {
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005836 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00005837 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
5838
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005839 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00005840 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
5841 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00005842 }
Eric Christopherfd179292009-08-27 18:07:15 +00005843
Nate Begeman9008ca62009-04-27 18:41:29 +00005844 // FIXME: fold these into legal mask.
Dale Johannesen0488fb62010-09-30 23:57:10 +00005845 if (X86::isMOVLHPSMask(SVOp) && !X86::isUNPCKLMask(SVOp))
5846 return getMOVLowToHigh(Op, dl, DAG, HasSSE2);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005847
Dale Johannesen0488fb62010-09-30 23:57:10 +00005848 if (X86::isMOVHLPSMask(SVOp))
5849 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00005850
Dale Johannesen0488fb62010-09-30 23:57:10 +00005851 if (X86::isMOVSHDUPMask(SVOp) && HasSSE3 && V2IsUndef && NumElems == 4)
5852 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00005853
Dale Johannesen0488fb62010-09-30 23:57:10 +00005854 if (X86::isMOVSLDUPMask(SVOp) && HasSSE3 && V2IsUndef && NumElems == 4)
5855 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00005856
Dale Johannesen0488fb62010-09-30 23:57:10 +00005857 if (X86::isMOVLPMask(SVOp))
5858 return getMOVLP(Op, dl, DAG, HasSSE2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005859
Nate Begeman9008ca62009-04-27 18:41:29 +00005860 if (ShouldXformToMOVHLPS(SVOp) ||
5861 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
5862 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005863
Evan Chengf26ffe92008-05-29 08:22:04 +00005864 if (isShift) {
5865 // No better options. Use a vshl / vsrl.
Dan Gohman8a55ce42009-09-23 21:02:20 +00005866 EVT EltVT = VT.getVectorElementType();
5867 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00005868 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005869 }
Eric Christopherfd179292009-08-27 18:07:15 +00005870
Evan Cheng9eca5e82006-10-25 21:49:50 +00005871 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00005872 // FIXME: This should also accept a bitcast of a splat? Be careful, not
5873 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00005874 V1IsSplat = isSplatVector(V1.getNode());
5875 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00005876
Chris Lattner8a594482007-11-25 00:24:49 +00005877 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00005878 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005879 Op = CommuteVectorShuffle(SVOp, DAG);
5880 SVOp = cast<ShuffleVectorSDNode>(Op);
5881 V1 = SVOp->getOperand(0);
5882 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00005883 std::swap(V1IsSplat, V2IsSplat);
5884 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00005885 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00005886 }
5887
Nate Begeman9008ca62009-04-27 18:41:29 +00005888 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
5889 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00005890 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00005891 return V1;
5892 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
5893 // the instruction selector will not match, so get a canonical MOVL with
5894 // swapped operands to undo the commute.
5895 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00005896 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005897
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005898 if (X86::isUNPCKLMask(SVOp))
David Greenec4db4e52011-02-28 19:06:56 +00005899 return getTargetShuffleNode(getUNPCKLOpcode(VT, getSubtarget()),
5900 dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005901
5902 if (X86::isUNPCKHMask(SVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005903 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00005904
Evan Cheng9bbbb982006-10-25 20:48:19 +00005905 if (V2IsSplat) {
5906 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005907 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00005908 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00005909 SDValue NewMask = NormalizeMask(SVOp, DAG);
5910 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
5911 if (NSVOp != SVOp) {
5912 if (X86::isUNPCKLMask(NSVOp, true)) {
5913 return NewMask;
5914 } else if (X86::isUNPCKHMask(NSVOp, true)) {
5915 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005916 }
5917 }
5918 }
5919
Evan Cheng9eca5e82006-10-25 21:49:50 +00005920 if (Commuted) {
5921 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00005922 // FIXME: this seems wrong.
5923 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
5924 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005925
5926 if (X86::isUNPCKLMask(NewSVOp))
David Greenec4db4e52011-02-28 19:06:56 +00005927 return getTargetShuffleNode(getUNPCKLOpcode(VT, getSubtarget()),
5928 dl, VT, V2, V1, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005929
5930 if (X86::isUNPCKHMask(NewSVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005931 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V2, V1, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00005932 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005933
Nate Begeman9008ca62009-04-27 18:41:29 +00005934 // Normalize the node to match x86 shuffle ops if needed
Dale Johannesen0488fb62010-09-30 23:57:10 +00005935 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
Nate Begeman9008ca62009-04-27 18:41:29 +00005936 return CommuteVectorShuffle(SVOp, DAG);
5937
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00005938 // The checks below are all present in isShuffleMaskLegal, but they are
5939 // inlined here right now to enable us to directly emit target specific
5940 // nodes, and remove one by one until they don't return Op anymore.
5941 SmallVector<int, 16> M;
5942 SVOp->getMask(M);
5943
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00005944 if (isPALIGNRMask(M, VT, HasSSSE3))
5945 return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
5946 X86::getShufflePALIGNRImmediate(SVOp),
5947 DAG);
5948
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00005949 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
5950 SVOp->getSplatIndex() == 0 && V2IsUndef) {
David Greenec4db4e52011-02-28 19:06:56 +00005951 if (VT == MVT::v2f64) {
5952 X86ISD::NodeType Opcode =
5953 getSubtarget()->hasAVX() ? X86ISD::VUNPCKLPD : X86ISD::UNPCKLPD;
5954 return getTargetShuffleNode(Opcode, dl, VT, V1, V1, DAG);
5955 }
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00005956 if (VT == MVT::v2i64)
5957 return getTargetShuffleNode(X86ISD::PUNPCKLQDQ, dl, VT, V1, V1, DAG);
5958 }
5959
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00005960 if (isPSHUFHWMask(M, VT))
5961 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
5962 X86::getShufflePSHUFHWImmediate(SVOp),
5963 DAG);
5964
5965 if (isPSHUFLWMask(M, VT))
5966 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
5967 X86::getShufflePSHUFLWImmediate(SVOp),
5968 DAG);
5969
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00005970 if (isSHUFPMask(M, VT)) {
5971 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
5972 if (VT == MVT::v4f32 || VT == MVT::v4i32)
5973 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V1, V2,
5974 TargetMask, DAG);
5975 if (VT == MVT::v2f64 || VT == MVT::v2i64)
5976 return getTargetShuffleNode(X86ISD::SHUFPD, dl, VT, V1, V2,
5977 TargetMask, DAG);
5978 }
5979
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005980 if (X86::isUNPCKL_v_undef_Mask(SVOp))
5981 if (VT != MVT::v2i64 && VT != MVT::v2f64)
David Greenec4db4e52011-02-28 19:06:56 +00005982 return getTargetShuffleNode(getUNPCKLOpcode(VT, getSubtarget()),
5983 dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005984 if (X86::isUNPCKH_v_undef_Mask(SVOp))
5985 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5986 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
5987
Evan Cheng14b32e12007-12-11 01:46:18 +00005988 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
Owen Anderson825b72b2009-08-11 20:47:22 +00005989 if (VT == MVT::v8i16) {
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005990 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00005991 if (NewOp.getNode())
Evan Cheng14b32e12007-12-11 01:46:18 +00005992 return NewOp;
5993 }
5994
Owen Anderson825b72b2009-08-11 20:47:22 +00005995 if (VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005996 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005997 if (NewOp.getNode())
5998 return NewOp;
5999 }
Eric Christopherfd179292009-08-27 18:07:15 +00006000
Dale Johannesen0488fb62010-09-30 23:57:10 +00006001 // Handle all 4 wide cases with a number of shuffles.
6002 if (NumElems == 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00006003 return LowerVECTOR_SHUFFLE_4wide(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006004
Dan Gohman475871a2008-07-27 21:46:04 +00006005 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006006}
6007
Dan Gohman475871a2008-07-27 21:46:04 +00006008SDValue
6009X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00006010 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006011 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006012 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006013 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006014 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006015 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006016 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006017 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006018 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006019 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00006020 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
6021 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
6022 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006023 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6024 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006025 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006026 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00006027 Op.getOperand(0)),
6028 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006029 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006030 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006031 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006032 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006033 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Anderson825b72b2009-08-11 20:47:22 +00006034 } else if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00006035 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
6036 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006037 // result has a single use which is a store or a bitcast to i32. And in
6038 // the case of a store, it's not worth it if the index is a constant 0,
6039 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00006040 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00006041 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00006042 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006043 if ((User->getOpcode() != ISD::STORE ||
6044 (isa<ConstantSDNode>(Op.getOperand(1)) &&
6045 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006046 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00006047 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00006048 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00006049 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006050 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00006051 Op.getOperand(0)),
6052 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006053 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Owen Anderson825b72b2009-08-11 20:47:22 +00006054 } else if (VT == MVT::i32) {
Mon P Wangf0fcdd82009-01-15 21:10:20 +00006055 // ExtractPS works with constant index.
6056 if (isa<ConstantSDNode>(Op.getOperand(1)))
6057 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00006058 }
Dan Gohman475871a2008-07-27 21:46:04 +00006059 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006060}
6061
6062
Dan Gohman475871a2008-07-27 21:46:04 +00006063SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006064X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
6065 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006066 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00006067 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006068
David Greene74a579d2011-02-10 16:57:36 +00006069 SDValue Vec = Op.getOperand(0);
6070 EVT VecVT = Vec.getValueType();
6071
6072 // If this is a 256-bit vector result, first extract the 128-bit
6073 // vector and then extract from the 128-bit vector.
6074 if (VecVT.getSizeInBits() > 128) {
6075 DebugLoc dl = Op.getNode()->getDebugLoc();
6076 unsigned NumElems = VecVT.getVectorNumElements();
6077 SDValue Idx = Op.getOperand(1);
6078
6079 if (!isa<ConstantSDNode>(Idx))
6080 return SDValue();
6081
6082 unsigned ExtractNumElems = NumElems / (VecVT.getSizeInBits() / 128);
6083 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
6084
6085 // Get the 128-bit vector.
6086 bool Upper = IdxVal >= ExtractNumElems;
6087 Vec = Extract128BitVector(Vec, Idx, DAG, dl);
6088
6089 // Extract from it.
6090 SDValue ScaledIdx = Idx;
6091 if (Upper)
6092 ScaledIdx = DAG.getNode(ISD::SUB, dl, Idx.getValueType(), Idx,
6093 DAG.getConstant(ExtractNumElems,
6094 Idx.getValueType()));
6095 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, Op.getValueType(), Vec,
6096 ScaledIdx);
6097 }
6098
6099 assert(Vec.getValueSizeInBits() <= 128 && "Unexpected vector length");
6100
Evan Cheng62a3f152008-03-24 21:52:23 +00006101 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006102 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006103 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00006104 return Res;
6105 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00006106
Owen Andersone50ed302009-08-10 22:56:29 +00006107 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006108 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006109 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006110 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00006111 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006112 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00006113 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006114 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6115 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006116 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006117 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00006118 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006119 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00006120 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00006121 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00006122 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00006123 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00006124 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006125 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006126 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006127 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006128 if (Idx == 0)
6129 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00006130
Evan Cheng0db9fe62006-04-25 20:13:52 +00006131 // SHUFPS the element to the lowest double word, then movss.
Nate Begeman9008ca62009-04-27 18:41:29 +00006132 int Mask[4] = { Idx, -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006133 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006134 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00006135 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00006136 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00006137 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00006138 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00006139 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
6140 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
6141 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006142 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006143 if (Idx == 0)
6144 return Op;
6145
6146 // UNPCKHPD the element to the lowest double word, then movsd.
6147 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
6148 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00006149 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006150 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006151 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00006152 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00006153 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00006154 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006155 }
6156
Dan Gohman475871a2008-07-27 21:46:04 +00006157 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006158}
6159
Dan Gohman475871a2008-07-27 21:46:04 +00006160SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006161X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
6162 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006163 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006164 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006165 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006166
Dan Gohman475871a2008-07-27 21:46:04 +00006167 SDValue N0 = Op.getOperand(0);
6168 SDValue N1 = Op.getOperand(1);
6169 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00006170
Dan Gohman8a55ce42009-09-23 21:02:20 +00006171 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00006172 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00006173 unsigned Opc;
6174 if (VT == MVT::v8i16)
6175 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00006176 else if (VT == MVT::v16i8)
6177 Opc = X86ISD::PINSRB;
6178 else
6179 Opc = X86ISD::PINSRB;
6180
Nate Begeman14d12ca2008-02-11 04:19:36 +00006181 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
6182 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00006183 if (N1.getValueType() != MVT::i32)
6184 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
6185 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006186 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00006187 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00006188 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00006189 // Bits [7:6] of the constant are the source select. This will always be
6190 // zero here. The DAG Combiner may combine an extract_elt index into these
6191 // bits. For example (insert (extract, 3), 2) could be matched by putting
6192 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00006193 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00006194 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00006195 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00006196 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006197 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00006198 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00006199 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00006200 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00006201 } else if (EltVT == MVT::i32 && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00006202 // PINSR* works with constant index.
6203 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00006204 }
Dan Gohman475871a2008-07-27 21:46:04 +00006205 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006206}
6207
Dan Gohman475871a2008-07-27 21:46:04 +00006208SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006209X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006210 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006211 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006212
David Greene6b381262011-02-09 15:32:06 +00006213 DebugLoc dl = Op.getDebugLoc();
6214 SDValue N0 = Op.getOperand(0);
6215 SDValue N1 = Op.getOperand(1);
6216 SDValue N2 = Op.getOperand(2);
6217
6218 // If this is a 256-bit vector result, first insert into a 128-bit
6219 // vector and then insert into the 256-bit vector.
6220 if (VT.getSizeInBits() > 128) {
6221 if (!isa<ConstantSDNode>(N2))
6222 return SDValue();
6223
6224 // Get the 128-bit vector.
6225 unsigned NumElems = VT.getVectorNumElements();
6226 unsigned IdxVal = cast<ConstantSDNode>(N2)->getZExtValue();
6227 bool Upper = IdxVal >= NumElems / 2;
6228
6229 SDValue SubN0 = Extract128BitVector(N0, N2, DAG, dl);
6230
6231 // Insert into it.
6232 SDValue ScaledN2 = N2;
6233 if (Upper)
6234 ScaledN2 = DAG.getNode(ISD::SUB, dl, N2.getValueType(), N2,
Owen Anderson95771af2011-02-25 21:41:48 +00006235 DAG.getConstant(NumElems /
David Greene6b381262011-02-09 15:32:06 +00006236 (VT.getSizeInBits() / 128),
6237 N2.getValueType()));
6238 Op = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, SubN0.getValueType(), SubN0,
6239 N1, ScaledN2);
6240
6241 // Insert the 128-bit vector
6242 // FIXME: Why UNDEF?
6243 return Insert128BitVector(N0, Op, N2, DAG, dl);
6244 }
6245
Nate Begeman14d12ca2008-02-11 04:19:36 +00006246 if (Subtarget->hasSSE41())
6247 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
6248
Dan Gohman8a55ce42009-09-23 21:02:20 +00006249 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00006250 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00006251
Dan Gohman8a55ce42009-09-23 21:02:20 +00006252 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00006253 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
6254 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00006255 if (N1.getValueType() != MVT::i32)
6256 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
6257 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006258 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00006259 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006260 }
Dan Gohman475871a2008-07-27 21:46:04 +00006261 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006262}
6263
Dan Gohman475871a2008-07-27 21:46:04 +00006264SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006265X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) const {
David Greene2fcdfb42011-02-10 23:11:29 +00006266 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006267 DebugLoc dl = Op.getDebugLoc();
David Greene2fcdfb42011-02-10 23:11:29 +00006268 EVT OpVT = Op.getValueType();
6269
6270 // If this is a 256-bit vector result, first insert into a 128-bit
6271 // vector and then insert into the 256-bit vector.
6272 if (OpVT.getSizeInBits() > 128) {
6273 // Insert into a 128-bit vector.
6274 EVT VT128 = EVT::getVectorVT(*Context,
6275 OpVT.getVectorElementType(),
6276 OpVT.getVectorNumElements() / 2);
6277
6278 Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT128, Op.getOperand(0));
6279
6280 // Insert the 128-bit vector.
6281 return Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, OpVT), Op,
6282 DAG.getConstant(0, MVT::i32),
6283 DAG, dl);
6284 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00006285
Chris Lattnerf172ecd2010-07-04 23:07:25 +00006286 if (Op.getValueType() == MVT::v1i64 &&
6287 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00006288 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00006289
Owen Anderson825b72b2009-08-11 20:47:22 +00006290 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Dale Johannesen0488fb62010-09-30 23:57:10 +00006291 assert(Op.getValueType().getSimpleVT().getSizeInBits() == 128 &&
6292 "Expected an SSE type!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006293 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(),
Dale Johannesen0488fb62010-09-30 23:57:10 +00006294 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006295}
6296
David Greene91585092011-01-26 15:38:49 +00006297// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
6298// a simple subregister reference or explicit instructions to grab
6299// upper bits of a vector.
6300SDValue
6301X86TargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
6302 if (Subtarget->hasAVX()) {
David Greenea5f26012011-02-07 19:36:54 +00006303 DebugLoc dl = Op.getNode()->getDebugLoc();
6304 SDValue Vec = Op.getNode()->getOperand(0);
6305 SDValue Idx = Op.getNode()->getOperand(1);
6306
6307 if (Op.getNode()->getValueType(0).getSizeInBits() == 128
6308 && Vec.getNode()->getValueType(0).getSizeInBits() == 256) {
6309 return Extract128BitVector(Vec, Idx, DAG, dl);
6310 }
David Greene91585092011-01-26 15:38:49 +00006311 }
6312 return SDValue();
6313}
6314
David Greenecfe33c42011-01-26 19:13:22 +00006315// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
6316// simple superregister reference or explicit instructions to insert
6317// the upper bits of a vector.
6318SDValue
6319X86TargetLowering::LowerINSERT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
6320 if (Subtarget->hasAVX()) {
6321 DebugLoc dl = Op.getNode()->getDebugLoc();
6322 SDValue Vec = Op.getNode()->getOperand(0);
6323 SDValue SubVec = Op.getNode()->getOperand(1);
6324 SDValue Idx = Op.getNode()->getOperand(2);
6325
6326 if (Op.getNode()->getValueType(0).getSizeInBits() == 256
6327 && SubVec.getNode()->getValueType(0).getSizeInBits() == 128) {
David Greenea5f26012011-02-07 19:36:54 +00006328 return Insert128BitVector(Vec, SubVec, Idx, DAG, dl);
David Greenecfe33c42011-01-26 19:13:22 +00006329 }
6330 }
6331 return SDValue();
6332}
6333
Bill Wendling056292f2008-09-16 21:48:12 +00006334// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
6335// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
6336// one of the above mentioned nodes. It has to be wrapped because otherwise
6337// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
6338// be used to form addressing mode. These wrapped nodes will be selected
6339// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00006340SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006341X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006342 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00006343
Chris Lattner41621a22009-06-26 19:22:52 +00006344 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6345 // global base reg.
6346 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006347 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006348 CodeModel::Model M = getTargetMachine().getCodeModel();
6349
Chris Lattner4f066492009-07-11 20:29:19 +00006350 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006351 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00006352 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00006353 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006354 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00006355 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006356 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00006357
Evan Cheng1606e8e2009-03-13 07:51:59 +00006358 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00006359 CP->getAlignment(),
6360 CP->getOffset(), OpFlag);
6361 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00006362 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006363 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00006364 if (OpFlag) {
6365 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00006366 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006367 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006368 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006369 }
6370
6371 return Result;
6372}
6373
Dan Gohmand858e902010-04-17 15:26:15 +00006374SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00006375 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00006376
Chris Lattner18c59872009-06-27 04:16:01 +00006377 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6378 // global base reg.
6379 unsigned char OpFlag = 0;
6380 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006381 CodeModel::Model M = getTargetMachine().getCodeModel();
6382
Chris Lattner4f066492009-07-11 20:29:19 +00006383 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006384 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00006385 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00006386 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006387 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00006388 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006389 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00006390
Chris Lattner18c59872009-06-27 04:16:01 +00006391 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
6392 OpFlag);
6393 DebugLoc DL = JT->getDebugLoc();
6394 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00006395
Chris Lattner18c59872009-06-27 04:16:01 +00006396 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00006397 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00006398 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6399 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006400 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00006401 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00006402
Chris Lattner18c59872009-06-27 04:16:01 +00006403 return Result;
6404}
6405
6406SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006407X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00006408 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00006409
Chris Lattner18c59872009-06-27 04:16:01 +00006410 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6411 // global base reg.
6412 unsigned char OpFlag = 0;
6413 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006414 CodeModel::Model M = getTargetMachine().getCodeModel();
6415
Chris Lattner4f066492009-07-11 20:29:19 +00006416 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006417 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00006418 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00006419 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006420 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00006421 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006422 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00006423
Chris Lattner18c59872009-06-27 04:16:01 +00006424 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00006425
Chris Lattner18c59872009-06-27 04:16:01 +00006426 DebugLoc DL = Op.getDebugLoc();
6427 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00006428
6429
Chris Lattner18c59872009-06-27 04:16:01 +00006430 // With PIC, the address is actually $g + Offset.
6431 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00006432 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00006433 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6434 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006435 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00006436 Result);
6437 }
Eric Christopherfd179292009-08-27 18:07:15 +00006438
Chris Lattner18c59872009-06-27 04:16:01 +00006439 return Result;
6440}
6441
Dan Gohman475871a2008-07-27 21:46:04 +00006442SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006443X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00006444 // Create the TargetBlockAddressAddress node.
6445 unsigned char OpFlags =
6446 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00006447 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00006448 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +00006449 DebugLoc dl = Op.getDebugLoc();
6450 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
6451 /*isTarget=*/true, OpFlags);
6452
Dan Gohmanf705adb2009-10-30 01:28:02 +00006453 if (Subtarget->isPICStyleRIPRel() &&
6454 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00006455 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
6456 else
6457 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00006458
Dan Gohman29cbade2009-11-20 23:18:13 +00006459 // With PIC, the address is actually $g + Offset.
6460 if (isGlobalRelativeToPICBase(OpFlags)) {
6461 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
6462 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
6463 Result);
6464 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00006465
6466 return Result;
6467}
6468
6469SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00006470X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00006471 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00006472 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00006473 // Create the TargetGlobalAddress node, folding in the constant
6474 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00006475 unsigned char OpFlags =
6476 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006477 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00006478 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006479 if (OpFlags == X86II::MO_NO_FLAG &&
6480 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00006481 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00006482 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00006483 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006484 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00006485 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00006486 }
Eric Christopherfd179292009-08-27 18:07:15 +00006487
Chris Lattner4f066492009-07-11 20:29:19 +00006488 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006489 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00006490 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
6491 else
6492 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00006493
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006494 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00006495 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00006496 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
6497 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006498 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006499 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006500
Chris Lattner36c25012009-07-10 07:34:39 +00006501 // For globals that require a load from a stub to get the address, emit the
6502 // load.
6503 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00006504 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00006505 MachinePointerInfo::getGOT(), false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006506
Dan Gohman6520e202008-10-18 02:06:02 +00006507 // If there was a non-zero offset that we didn't fold, create an explicit
6508 // addition for it.
6509 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00006510 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00006511 DAG.getConstant(Offset, getPointerTy()));
6512
Evan Cheng0db9fe62006-04-25 20:13:52 +00006513 return Result;
6514}
6515
Evan Chengda43bcf2008-09-24 00:05:32 +00006516SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006517X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00006518 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00006519 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006520 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00006521}
6522
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006523static SDValue
6524GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00006525 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerb903bed2009-06-26 21:20:29 +00006526 unsigned char OperandFlags) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006527 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006528 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006529 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00006530 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006531 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00006532 GA->getOffset(),
6533 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006534 if (InFlag) {
6535 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00006536 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006537 } else {
6538 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00006539 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006540 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006541
6542 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00006543 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006544
Rafael Espindola15f1b662009-04-24 12:59:40 +00006545 SDValue Flag = Chain.getValue(1);
6546 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006547}
6548
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006549// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00006550static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006551LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006552 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00006553 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00006554 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
6555 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006556 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006557 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006558 InFlag = Chain.getValue(1);
6559
Chris Lattnerb903bed2009-06-26 21:20:29 +00006560 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006561}
6562
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006563// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00006564static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006565LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006566 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00006567 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
6568 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006569}
6570
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006571// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
6572// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00006573static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006574 const EVT PtrVT, TLSModel::Model model,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00006575 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00006576 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00006577
Chris Lattnerf93b90c2010-09-22 04:39:11 +00006578 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
6579 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
6580 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00006581
Michael J. Spencerec38de22010-10-10 22:04:20 +00006582 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00006583 DAG.getIntPtrConstant(0),
6584 MachinePointerInfo(Ptr), false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00006585
Chris Lattnerb903bed2009-06-26 21:20:29 +00006586 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006587 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
6588 // initialexec.
6589 unsigned WrapperKind = X86ISD::Wrapper;
6590 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00006591 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00006592 } else if (is64Bit) {
6593 assert(model == TLSModel::InitialExec);
6594 OperandFlags = X86II::MO_GOTTPOFF;
6595 WrapperKind = X86ISD::WrapperRIP;
6596 } else {
6597 assert(model == TLSModel::InitialExec);
6598 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00006599 }
Eric Christopherfd179292009-08-27 18:07:15 +00006600
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006601 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
6602 // exec)
Michael J. Spencerec38de22010-10-10 22:04:20 +00006603 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00006604 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00006605 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00006606 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00006607
Rafael Espindola9a580232009-02-27 13:37:18 +00006608 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00006609 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00006610 MachinePointerInfo::getGOT(), false, false, 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00006611
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006612 // The address of the thread local variable is the add of the thread
6613 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00006614 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006615}
6616
Dan Gohman475871a2008-07-27 21:46:04 +00006617SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006618X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00006619
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006620 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00006621 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00006622
Eric Christopher30ef0e52010-06-03 04:07:48 +00006623 if (Subtarget->isTargetELF()) {
6624 // TODO: implement the "local dynamic" model
6625 // TODO: implement the "initial exec"model for pic executables
Michael J. Spencerec38de22010-10-10 22:04:20 +00006626
Eric Christopher30ef0e52010-06-03 04:07:48 +00006627 // If GV is an alias then use the aliasee for determining
6628 // thread-localness.
6629 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
6630 GV = GA->resolveAliasedGlobal(false);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006631
6632 TLSModel::Model model
Eric Christopher30ef0e52010-06-03 04:07:48 +00006633 = getTLSModel(GV, getTargetMachine().getRelocationModel());
Michael J. Spencerec38de22010-10-10 22:04:20 +00006634
Eric Christopher30ef0e52010-06-03 04:07:48 +00006635 switch (model) {
6636 case TLSModel::GeneralDynamic:
6637 case TLSModel::LocalDynamic: // not implemented
6638 if (Subtarget->is64Bit())
6639 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
6640 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Michael J. Spencerec38de22010-10-10 22:04:20 +00006641
Eric Christopher30ef0e52010-06-03 04:07:48 +00006642 case TLSModel::InitialExec:
6643 case TLSModel::LocalExec:
6644 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
6645 Subtarget->is64Bit());
6646 }
6647 } else if (Subtarget->isTargetDarwin()) {
6648 // Darwin only has one model of TLS. Lower to that.
6649 unsigned char OpFlag = 0;
6650 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
6651 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006652
Eric Christopher30ef0e52010-06-03 04:07:48 +00006653 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6654 // global base reg.
6655 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
6656 !Subtarget->is64Bit();
6657 if (PIC32)
6658 OpFlag = X86II::MO_TLVP_PIC_BASE;
6659 else
6660 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006661 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00006662 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00006663 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00006664 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00006665 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006666
Eric Christopher30ef0e52010-06-03 04:07:48 +00006667 // With PIC32, the address is actually $g + Offset.
6668 if (PIC32)
6669 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6670 DAG.getNode(X86ISD::GlobalBaseReg,
6671 DebugLoc(), getPointerTy()),
6672 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006673
Eric Christopher30ef0e52010-06-03 04:07:48 +00006674 // Lowering the machine isd will make sure everything is in the right
6675 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00006676 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006677 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00006678 SDValue Args[] = { Chain, Offset };
6679 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006680
Eric Christopher30ef0e52010-06-03 04:07:48 +00006681 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
6682 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
6683 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00006684
Eric Christopher30ef0e52010-06-03 04:07:48 +00006685 // And our return value (tls address) is in the standard call return value
6686 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00006687 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
6688 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy());
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006689 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00006690
Eric Christopher30ef0e52010-06-03 04:07:48 +00006691 assert(false &&
6692 "TLS not implemented for this target.");
Eric Christopherfd179292009-08-27 18:07:15 +00006693
Torok Edwinc23197a2009-07-14 16:55:14 +00006694 llvm_unreachable("Unreachable");
Chris Lattner5867de12009-04-01 22:14:45 +00006695 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006696}
6697
Evan Cheng0db9fe62006-04-25 20:13:52 +00006698
Nadav Rotem43012222011-05-11 08:12:09 +00006699/// LowerShiftParts - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00006700/// take a 2 x i32 value to shift plus a shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +00006701SDValue X86TargetLowering::LowerShiftParts(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman4c1fa612008-03-03 22:22:09 +00006702 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00006703 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006704 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006705 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006706 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00006707 SDValue ShOpLo = Op.getOperand(0);
6708 SDValue ShOpHi = Op.getOperand(1);
6709 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00006710 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00006711 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00006712 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00006713
Dan Gohman475871a2008-07-27 21:46:04 +00006714 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006715 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00006716 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
6717 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006718 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006719 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
6720 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006721 }
Evan Chenge3413162006-01-09 18:33:28 +00006722
Owen Anderson825b72b2009-08-11 20:47:22 +00006723 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
6724 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00006725 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00006726 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00006727
Dan Gohman475871a2008-07-27 21:46:04 +00006728 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00006729 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00006730 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
6731 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00006732
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006733 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00006734 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
6735 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006736 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006737 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
6738 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006739 }
6740
Dan Gohman475871a2008-07-27 21:46:04 +00006741 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00006742 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006743}
Evan Chenga3195e82006-01-12 22:54:21 +00006744
Dan Gohmand858e902010-04-17 15:26:15 +00006745SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
6746 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006747 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00006748
Dale Johannesen0488fb62010-09-30 23:57:10 +00006749 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00006750 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00006751
Owen Anderson825b72b2009-08-11 20:47:22 +00006752 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00006753 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00006754
Eli Friedman36df4992009-05-27 00:47:34 +00006755 // These are really Legal; return the operand so the caller accepts it as
6756 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00006757 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00006758 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00006759 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00006760 Subtarget->is64Bit()) {
6761 return Op;
6762 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006763
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006764 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006765 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006766 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00006767 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006768 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00006769 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00006770 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006771 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006772 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00006773 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
6774}
Evan Cheng0db9fe62006-04-25 20:13:52 +00006775
Owen Andersone50ed302009-08-10 22:56:29 +00006776SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00006777 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00006778 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006779 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00006780 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00006781 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00006782 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006783 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006784 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00006785 else
Owen Anderson825b72b2009-08-11 20:47:22 +00006786 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006787
Chris Lattner492a43e2010-09-22 01:28:21 +00006788 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006789
Stuart Hastings84be9582011-06-02 15:57:11 +00006790 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(StackSlot);
6791 MachineMemOperand *MMO;
6792 if (FI) {
6793 int SSFI = FI->getIndex();
6794 MMO =
6795 DAG.getMachineFunction()
6796 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6797 MachineMemOperand::MOLoad, ByteSize, ByteSize);
6798 } else {
6799 MMO = cast<LoadSDNode>(StackSlot)->getMemOperand();
6800 StackSlot = StackSlot.getOperand(1);
6801 }
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006802 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00006803 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
6804 X86ISD::FILD, DL,
6805 Tys, Ops, array_lengthof(Ops),
6806 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006807
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006808 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006809 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00006810 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006811
6812 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
6813 // shouldn't be necessary except that RFP cannot be live across
6814 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006815 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00006816 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
6817 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006818 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00006819 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006820 SDValue Ops[] = {
6821 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
6822 };
Chris Lattner492a43e2010-09-22 01:28:21 +00006823 MachineMemOperand *MMO =
6824 DAG.getMachineFunction()
6825 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00006826 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006827
Chris Lattner492a43e2010-09-22 01:28:21 +00006828 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
6829 Ops, array_lengthof(Ops),
6830 Op.getValueType(), MMO);
6831 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006832 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006833 false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006834 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006835
Evan Cheng0db9fe62006-04-25 20:13:52 +00006836 return Result;
6837}
6838
Bill Wendling8b8a6362009-01-17 03:56:04 +00006839// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00006840SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
6841 SelectionDAG &DAG) const {
Bill Wendling8b8a6362009-01-17 03:56:04 +00006842 // This algorithm is not obvious. Here it is in C code, more or less:
6843 /*
6844 double uint64_to_double( uint32_t hi, uint32_t lo ) {
6845 static const __m128i exp = { 0x4330000045300000ULL, 0 };
6846 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00006847
Bill Wendling8b8a6362009-01-17 03:56:04 +00006848 // Copy ints to xmm registers.
6849 __m128i xh = _mm_cvtsi32_si128( hi );
6850 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00006851
Bill Wendling8b8a6362009-01-17 03:56:04 +00006852 // Combine into low half of a single xmm register.
6853 __m128i x = _mm_unpacklo_epi32( xh, xl );
6854 __m128d d;
6855 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00006856
Bill Wendling8b8a6362009-01-17 03:56:04 +00006857 // Merge in appropriate exponents to give the integer bits the right
6858 // magnitude.
6859 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00006860
Bill Wendling8b8a6362009-01-17 03:56:04 +00006861 // Subtract away the biases to deal with the IEEE-754 double precision
6862 // implicit 1.
6863 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00006864
Bill Wendling8b8a6362009-01-17 03:56:04 +00006865 // All conversions up to here are exact. The correctly rounded result is
6866 // calculated using the current rounding mode using the following
6867 // horizontal add.
6868 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
6869 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
6870 // store doesn't really need to be here (except
6871 // maybe to zero the other double)
6872 return sd;
6873 }
6874 */
Dale Johannesen040225f2008-10-21 23:07:49 +00006875
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006876 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00006877 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00006878
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006879 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00006880 std::vector<Constant*> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00006881 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
6882 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
6883 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
6884 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00006885 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006886 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006887
Bill Wendling8b8a6362009-01-17 03:56:04 +00006888 std::vector<Constant*> CV1;
Owen Andersona90b3dc2009-07-15 21:51:10 +00006889 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006890 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
Owen Andersona90b3dc2009-07-15 21:51:10 +00006891 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006892 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00006893 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006894 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006895
Owen Anderson825b72b2009-08-11 20:47:22 +00006896 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6897 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00006898 Op.getOperand(0),
6899 DAG.getIntPtrConstant(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006900 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6901 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00006902 Op.getOperand(0),
6903 DAG.getIntPtrConstant(0)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006904 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
6905 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00006906 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006907 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00006908 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006909 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck2);
Owen Anderson825b72b2009-08-11 20:47:22 +00006910 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00006911 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006912 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00006913 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006914
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006915 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00006916 int ShufMask[2] = { 1, -1 };
Owen Anderson825b72b2009-08-11 20:47:22 +00006917 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
6918 DAG.getUNDEF(MVT::v2f64), ShufMask);
6919 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
6920 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006921 DAG.getIntPtrConstant(0));
6922}
6923
Bill Wendling8b8a6362009-01-17 03:56:04 +00006924// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00006925SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
6926 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006927 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00006928 // FP constant to bias correct the final result.
6929 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00006930 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006931
6932 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00006933 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6934 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Bill Wendling8b8a6362009-01-17 03:56:04 +00006935 Op.getOperand(0),
6936 DAG.getIntPtrConstant(0)));
6937
Owen Anderson825b72b2009-08-11 20:47:22 +00006938 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006939 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00006940 DAG.getIntPtrConstant(0));
6941
6942 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00006943 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006944 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00006945 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006946 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006947 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00006948 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006949 MVT::v2f64, Bias)));
6950 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006951 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00006952 DAG.getIntPtrConstant(0));
6953
6954 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00006955 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006956
6957 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00006958 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00006959
Owen Anderson825b72b2009-08-11 20:47:22 +00006960 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006961 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00006962 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00006963 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006964 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00006965 }
6966
6967 // Handle final rounding.
6968 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00006969}
6970
Dan Gohmand858e902010-04-17 15:26:15 +00006971SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
6972 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00006973 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006974 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00006975
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006976 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00006977 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
6978 // the optimization here.
6979 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00006980 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00006981
Owen Andersone50ed302009-08-10 22:56:29 +00006982 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006983 EVT DstVT = Op.getValueType();
6984 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00006985 return LowerUINT_TO_FP_i64(Op, DAG);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006986 else if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00006987 return LowerUINT_TO_FP_i32(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00006988
6989 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00006990 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006991 if (SrcVT == MVT::i32) {
6992 SDValue WordOff = DAG.getConstant(4, getPointerTy());
6993 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
6994 getPointerTy(), StackSlot, WordOff);
6995 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006996 StackSlot, MachinePointerInfo(),
6997 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006998 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006999 OffsetSlot, MachinePointerInfo(),
7000 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007001 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
7002 return Fild;
7003 }
7004
7005 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
7006 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007007 StackSlot, MachinePointerInfo(),
7008 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007009 // For i64 source, we need to add the appropriate power of 2 if the input
7010 // was negative. This is the same as the optimization in
7011 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
7012 // we must be careful to do the computation in x87 extended precision, not
7013 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00007014 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
7015 MachineMemOperand *MMO =
7016 DAG.getMachineFunction()
7017 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7018 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007019
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007020 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
7021 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007022 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
7023 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007024
7025 APInt FF(32, 0x5F800000ULL);
7026
7027 // Check whether the sign bit is set.
7028 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
7029 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
7030 ISD::SETLT);
7031
7032 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
7033 SDValue FudgePtr = DAG.getConstantPool(
7034 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
7035 getPointerTy());
7036
7037 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
7038 SDValue Zero = DAG.getIntPtrConstant(0);
7039 SDValue Four = DAG.getIntPtrConstant(4);
7040 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
7041 Zero, Four);
7042 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
7043
7044 // Load the value out, extending it from f32 to f80.
7045 // FIXME: Avoid the extend by constructing the right constant pool?
Stuart Hastingsa9011292011-02-16 16:23:55 +00007046 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00007047 FudgePtr, MachinePointerInfo::getConstantPool(),
7048 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007049 // Extend everything to 80 bits to force it to be done on x87.
7050 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
7051 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00007052}
7053
Dan Gohman475871a2008-07-27 21:46:04 +00007054std::pair<SDValue,SDValue> X86TargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00007055FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) const {
Chris Lattner07290932010-09-22 01:05:16 +00007056 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00007057
Owen Andersone50ed302009-08-10 22:56:29 +00007058 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00007059
7060 if (!IsSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007061 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
7062 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00007063 }
7064
Owen Anderson825b72b2009-08-11 20:47:22 +00007065 assert(DstTy.getSimpleVT() <= MVT::i64 &&
7066 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00007067 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00007068
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007069 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007070 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00007071 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00007072 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00007073 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00007074 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00007075 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00007076 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007077
Evan Cheng87c89352007-10-15 20:11:21 +00007078 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
7079 // stack slot.
7080 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00007081 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00007082 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007083 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00007084
Michael J. Spencerec38de22010-10-10 22:04:20 +00007085
7086
Evan Cheng0db9fe62006-04-25 20:13:52 +00007087 unsigned Opc;
Owen Anderson825b72b2009-08-11 20:47:22 +00007088 switch (DstTy.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007089 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson825b72b2009-08-11 20:47:22 +00007090 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
7091 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
7092 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007093 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007094
Dan Gohman475871a2008-07-27 21:46:04 +00007095 SDValue Chain = DAG.getEntryNode();
7096 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00007097 EVT TheVT = Op.getOperand(0).getValueType();
7098 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007099 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00007100 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007101 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007102 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007103 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00007104 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00007105 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00007106 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00007107
Chris Lattner492a43e2010-09-22 01:28:21 +00007108 MachineMemOperand *MMO =
7109 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7110 MachineMemOperand::MOLoad, MemSize, MemSize);
7111 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
7112 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007113 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00007114 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007115 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
7116 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007117
Chris Lattner07290932010-09-22 01:05:16 +00007118 MachineMemOperand *MMO =
7119 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7120 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007121
Evan Cheng0db9fe62006-04-25 20:13:52 +00007122 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00007123 SDValue Ops[] = { Chain, Value, StackSlot };
Chris Lattner07290932010-09-22 01:05:16 +00007124 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
7125 Ops, 3, DstTy, MMO);
Evan Chengd9558e02006-01-06 00:43:03 +00007126
Chris Lattner27a6c732007-11-24 07:07:01 +00007127 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007128}
7129
Dan Gohmand858e902010-04-17 15:26:15 +00007130SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
7131 SelectionDAG &DAG) const {
Dale Johannesen0488fb62010-09-30 23:57:10 +00007132 if (Op.getValueType().isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007133 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007134
Eli Friedman948e95a2009-05-23 09:59:16 +00007135 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00007136 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00007137 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
7138 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00007139
Chris Lattner27a6c732007-11-24 07:07:01 +00007140 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007141 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00007142 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00007143}
7144
Dan Gohmand858e902010-04-17 15:26:15 +00007145SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
7146 SelectionDAG &DAG) const {
Eli Friedman948e95a2009-05-23 09:59:16 +00007147 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
7148 SDValue FIST = Vals.first, StackSlot = Vals.second;
7149 assert(FIST.getNode() && "Unexpected failure");
7150
7151 // Load the result.
7152 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00007153 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007154}
7155
Dan Gohmand858e902010-04-17 15:26:15 +00007156SDValue X86TargetLowering::LowerFABS(SDValue Op,
7157 SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007158 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007159 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007160 EVT VT = Op.getValueType();
7161 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007162 if (VT.isVector())
7163 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007164 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007165 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007166 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00007167 CV.push_back(C);
7168 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007169 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007170 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00007171 CV.push_back(C);
7172 CV.push_back(C);
7173 CV.push_back(C);
7174 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007175 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007176 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007177 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007178 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007179 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007180 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007181 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007182}
7183
Dan Gohmand858e902010-04-17 15:26:15 +00007184SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007185 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007186 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007187 EVT VT = Op.getValueType();
7188 EVT EltVT = VT;
Duncan Sandsda9ad382009-09-06 19:29:07 +00007189 if (VT.isVector())
Duncan Sands83ec4b62008-06-06 12:08:01 +00007190 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007191 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007192 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007193 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00007194 CV.push_back(C);
7195 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007196 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007197 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00007198 CV.push_back(C);
7199 CV.push_back(C);
7200 CV.push_back(C);
7201 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007202 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007203 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007204 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007205 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007206 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007207 false, false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00007208 if (VT.isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007209 return DAG.getNode(ISD::BITCAST, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007210 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007211 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007212 Op.getOperand(0)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007213 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00007214 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007215 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00007216 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00007217}
7218
Dan Gohmand858e902010-04-17 15:26:15 +00007219SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007220 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00007221 SDValue Op0 = Op.getOperand(0);
7222 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007223 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007224 EVT VT = Op.getValueType();
7225 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00007226
7227 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007228 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007229 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00007230 SrcVT = VT;
7231 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007232 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007233 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007234 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007235 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007236 }
7237
7238 // At this point the operands and the result should have the same
7239 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00007240
Evan Cheng68c47cb2007-01-05 07:55:56 +00007241 // First get the sign bit of second operand.
7242 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007243 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007244 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
7245 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007246 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007247 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
7248 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7249 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7250 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007251 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007252 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007253 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007254 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007255 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007256 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007257 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00007258
7259 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007260 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007261 // Op0 is MVT::f32, Op1 is MVT::f64.
7262 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
7263 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
7264 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007265 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00007266 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00007267 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007268 }
7269
Evan Cheng73d6cf12007-01-05 21:37:56 +00007270 // Clear first operand sign bit.
7271 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00007272 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007273 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
7274 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00007275 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007276 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
7277 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7278 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7279 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00007280 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007281 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007282 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007283 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007284 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007285 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007286 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00007287
7288 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00007289 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00007290}
7291
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00007292SDValue X86TargetLowering::LowerFGETSIGN(SDValue Op, SelectionDAG &DAG) const {
7293 SDValue N0 = Op.getOperand(0);
7294 DebugLoc dl = Op.getDebugLoc();
7295 EVT VT = Op.getValueType();
7296
7297 // Lower ISD::FGETSIGN to (AND (X86ISD::FGETSIGNx86 ...) 1).
7298 SDValue xFGETSIGN = DAG.getNode(X86ISD::FGETSIGNx86, dl, VT, N0,
7299 DAG.getConstant(1, VT));
7300 return DAG.getNode(ISD::AND, dl, VT, xFGETSIGN, DAG.getConstant(1, VT));
7301}
7302
Dan Gohman076aee32009-03-04 19:44:21 +00007303/// Emit nodes that will be selected as "test Op0,Op0", or something
7304/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00007305SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00007306 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00007307 DebugLoc dl = Op.getDebugLoc();
7308
Dan Gohman31125812009-03-07 01:58:32 +00007309 // CF and OF aren't always set the way we want. Determine which
7310 // of these we need.
7311 bool NeedCF = false;
7312 bool NeedOF = false;
7313 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007314 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00007315 case X86::COND_A: case X86::COND_AE:
7316 case X86::COND_B: case X86::COND_BE:
7317 NeedCF = true;
7318 break;
7319 case X86::COND_G: case X86::COND_GE:
7320 case X86::COND_L: case X86::COND_LE:
7321 case X86::COND_O: case X86::COND_NO:
7322 NeedOF = true;
7323 break;
Dan Gohman31125812009-03-07 01:58:32 +00007324 }
7325
Dan Gohman076aee32009-03-04 19:44:21 +00007326 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00007327 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
7328 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007329 if (Op.getResNo() != 0 || NeedOF || NeedCF)
7330 // Emit a CMP with 0, which is the TEST pattern.
7331 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
7332 DAG.getConstant(0, Op.getValueType()));
7333
7334 unsigned Opcode = 0;
7335 unsigned NumOperands = 0;
7336 switch (Op.getNode()->getOpcode()) {
7337 case ISD::ADD:
7338 // Due to an isel shortcoming, be conservative if this add is likely to be
7339 // selected as part of a load-modify-store instruction. When the root node
7340 // in a match is a store, isel doesn't know how to remap non-chain non-flag
7341 // uses of other nodes in the match, such as the ADD in this case. This
7342 // leads to the ADD being left around and reselected, with the result being
7343 // two adds in the output. Alas, even if none our users are stores, that
7344 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
7345 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
7346 // climbing the DAG back to the root, and it doesn't seem to be worth the
7347 // effort.
7348 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Dan Gohman076aee32009-03-04 19:44:21 +00007349 UE = Op.getNode()->use_end(); UI != UE; ++UI)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007350 if (UI->getOpcode() != ISD::CopyToReg && UI->getOpcode() != ISD::SETCC)
7351 goto default_case;
7352
7353 if (ConstantSDNode *C =
7354 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
7355 // An add of one will be selected as an INC.
7356 if (C->getAPIntValue() == 1) {
7357 Opcode = X86ISD::INC;
7358 NumOperands = 1;
7359 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00007360 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007361
7362 // An add of negative one (subtract of one) will be selected as a DEC.
7363 if (C->getAPIntValue().isAllOnesValue()) {
7364 Opcode = X86ISD::DEC;
7365 NumOperands = 1;
7366 break;
7367 }
Dan Gohman076aee32009-03-04 19:44:21 +00007368 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007369
7370 // Otherwise use a regular EFLAGS-setting add.
7371 Opcode = X86ISD::ADD;
7372 NumOperands = 2;
7373 break;
7374 case ISD::AND: {
7375 // If the primary and result isn't used, don't bother using X86ISD::AND,
7376 // because a TEST instruction will be better.
7377 bool NonFlagUse = false;
7378 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
7379 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
7380 SDNode *User = *UI;
7381 unsigned UOpNo = UI.getOperandNo();
7382 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
7383 // Look pass truncate.
7384 UOpNo = User->use_begin().getOperandNo();
7385 User = *User->use_begin();
7386 }
7387
7388 if (User->getOpcode() != ISD::BRCOND &&
7389 User->getOpcode() != ISD::SETCC &&
7390 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
7391 NonFlagUse = true;
7392 break;
7393 }
Dan Gohman076aee32009-03-04 19:44:21 +00007394 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007395
7396 if (!NonFlagUse)
7397 break;
7398 }
7399 // FALL THROUGH
7400 case ISD::SUB:
7401 case ISD::OR:
7402 case ISD::XOR:
7403 // Due to the ISEL shortcoming noted above, be conservative if this op is
7404 // likely to be selected as part of a load-modify-store instruction.
7405 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
7406 UE = Op.getNode()->use_end(); UI != UE; ++UI)
7407 if (UI->getOpcode() == ISD::STORE)
7408 goto default_case;
7409
7410 // Otherwise use a regular EFLAGS-setting instruction.
7411 switch (Op.getNode()->getOpcode()) {
7412 default: llvm_unreachable("unexpected operator!");
7413 case ISD::SUB: Opcode = X86ISD::SUB; break;
7414 case ISD::OR: Opcode = X86ISD::OR; break;
7415 case ISD::XOR: Opcode = X86ISD::XOR; break;
7416 case ISD::AND: Opcode = X86ISD::AND; break;
7417 }
7418
7419 NumOperands = 2;
7420 break;
7421 case X86ISD::ADD:
7422 case X86ISD::SUB:
7423 case X86ISD::INC:
7424 case X86ISD::DEC:
7425 case X86ISD::OR:
7426 case X86ISD::XOR:
7427 case X86ISD::AND:
7428 return SDValue(Op.getNode(), 1);
7429 default:
7430 default_case:
7431 break;
Dan Gohman076aee32009-03-04 19:44:21 +00007432 }
7433
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007434 if (Opcode == 0)
7435 // Emit a CMP with 0, which is the TEST pattern.
7436 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
7437 DAG.getConstant(0, Op.getValueType()));
7438
7439 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
7440 SmallVector<SDValue, 4> Ops;
7441 for (unsigned i = 0; i != NumOperands; ++i)
7442 Ops.push_back(Op.getOperand(i));
7443
7444 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
7445 DAG.ReplaceAllUsesWith(Op, New);
7446 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00007447}
7448
7449/// Emit nodes that will be selected as "cmp Op0,Op1", or something
7450/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00007451SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00007452 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00007453 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
7454 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00007455 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00007456
7457 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00007458 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00007459}
7460
Evan Chengd40d03e2010-01-06 19:38:29 +00007461/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
7462/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00007463SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
7464 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00007465 SDValue Op0 = And.getOperand(0);
7466 SDValue Op1 = And.getOperand(1);
7467 if (Op0.getOpcode() == ISD::TRUNCATE)
7468 Op0 = Op0.getOperand(0);
7469 if (Op1.getOpcode() == ISD::TRUNCATE)
7470 Op1 = Op1.getOperand(0);
7471
Evan Chengd40d03e2010-01-06 19:38:29 +00007472 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00007473 if (Op1.getOpcode() == ISD::SHL)
7474 std::swap(Op0, Op1);
7475 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00007476 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
7477 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00007478 // If we looked past a truncate, check that it's only truncating away
7479 // known zeros.
7480 unsigned BitWidth = Op0.getValueSizeInBits();
7481 unsigned AndBitWidth = And.getValueSizeInBits();
7482 if (BitWidth > AndBitWidth) {
7483 APInt Mask = APInt::getAllOnesValue(BitWidth), Zeros, Ones;
7484 DAG.ComputeMaskedBits(Op0, Mask, Zeros, Ones);
7485 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
7486 return SDValue();
7487 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007488 LHS = Op1;
7489 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00007490 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007491 } else if (Op1.getOpcode() == ISD::Constant) {
7492 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
7493 SDValue AndLHS = Op0;
Evan Chengd40d03e2010-01-06 19:38:29 +00007494 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
7495 LHS = AndLHS.getOperand(0);
7496 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00007497 }
Evan Chengd40d03e2010-01-06 19:38:29 +00007498 }
Evan Cheng0488db92007-09-25 01:57:46 +00007499
Evan Chengd40d03e2010-01-06 19:38:29 +00007500 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00007501 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00007502 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00007503 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00007504 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00007505 // Also promote i16 to i32 for performance / code size reason.
7506 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00007507 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00007508 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00007509
Evan Chengd40d03e2010-01-06 19:38:29 +00007510 // If the operand types disagree, extend the shift amount to match. Since
7511 // BT ignores high bits (like shifts) we can use anyextend.
7512 if (LHS.getValueType() != RHS.getValueType())
7513 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00007514
Evan Chengd40d03e2010-01-06 19:38:29 +00007515 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
7516 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
7517 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7518 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00007519 }
7520
Evan Cheng54de3ea2010-01-05 06:52:31 +00007521 return SDValue();
7522}
7523
Dan Gohmand858e902010-04-17 15:26:15 +00007524SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng54de3ea2010-01-05 06:52:31 +00007525 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
7526 SDValue Op0 = Op.getOperand(0);
7527 SDValue Op1 = Op.getOperand(1);
7528 DebugLoc dl = Op.getDebugLoc();
7529 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
7530
7531 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00007532 // Lower (X & (1 << N)) == 0 to BT(X, N).
7533 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
7534 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Andrew Trickf6c39412011-03-23 23:11:02 +00007535 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00007536 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00007537 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00007538 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
7539 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
7540 if (NewSetCC.getNode())
7541 return NewSetCC;
7542 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00007543
Chris Lattner481eebc2010-12-19 21:23:48 +00007544 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
7545 // these.
7546 if (Op1.getOpcode() == ISD::Constant &&
Andrew Trickf6c39412011-03-23 23:11:02 +00007547 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
Evan Cheng2c755ba2010-02-27 07:36:59 +00007548 cast<ConstantSDNode>(Op1)->isNullValue()) &&
7549 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007550
Chris Lattner481eebc2010-12-19 21:23:48 +00007551 // If the input is a setcc, then reuse the input setcc or use a new one with
7552 // the inverted condition.
7553 if (Op0.getOpcode() == X86ISD::SETCC) {
7554 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
7555 bool Invert = (CC == ISD::SETNE) ^
7556 cast<ConstantSDNode>(Op1)->isNullValue();
7557 if (!Invert) return Op0;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007558
Evan Cheng2c755ba2010-02-27 07:36:59 +00007559 CCode = X86::GetOppositeBranchCondition(CCode);
Chris Lattner481eebc2010-12-19 21:23:48 +00007560 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7561 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
7562 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007563 }
7564
Evan Chenge5b51ac2010-04-17 06:13:15 +00007565 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00007566 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00007567 if (X86CC == X86::COND_INVALID)
7568 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00007569
Chris Lattnerc19d1c32010-12-19 22:08:31 +00007570 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00007571 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnerc19d1c32010-12-19 22:08:31 +00007572 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
Evan Cheng0488db92007-09-25 01:57:46 +00007573}
7574
Dan Gohmand858e902010-04-17 15:26:15 +00007575SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00007576 SDValue Cond;
7577 SDValue Op0 = Op.getOperand(0);
7578 SDValue Op1 = Op.getOperand(1);
7579 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00007580 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00007581 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
7582 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007583 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00007584
7585 if (isFP) {
7586 unsigned SSECC = 8;
Owen Andersone50ed302009-08-10 22:56:29 +00007587 EVT VT0 = Op0.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00007588 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
7589 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00007590 bool Swap = false;
7591
7592 switch (SetCCOpcode) {
7593 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00007594 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00007595 case ISD::SETEQ: SSECC = 0; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00007596 case ISD::SETOGT:
Nate Begeman30a0de92008-07-17 16:51:19 +00007597 case ISD::SETGT: Swap = true; // Fallthrough
7598 case ISD::SETLT:
7599 case ISD::SETOLT: SSECC = 1; break;
7600 case ISD::SETOGE:
7601 case ISD::SETGE: Swap = true; // Fallthrough
7602 case ISD::SETLE:
7603 case ISD::SETOLE: SSECC = 2; break;
7604 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00007605 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00007606 case ISD::SETNE: SSECC = 4; break;
7607 case ISD::SETULE: Swap = true;
7608 case ISD::SETUGE: SSECC = 5; break;
7609 case ISD::SETULT: Swap = true;
7610 case ISD::SETUGT: SSECC = 6; break;
7611 case ISD::SETO: SSECC = 7; break;
7612 }
7613 if (Swap)
7614 std::swap(Op0, Op1);
7615
Nate Begemanfb8ead02008-07-25 19:05:58 +00007616 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00007617 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00007618 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00007619 SDValue UNORD, EQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00007620 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
7621 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00007622 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00007623 }
7624 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00007625 SDValue ORD, NEQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00007626 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
7627 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00007628 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00007629 }
Torok Edwinc23197a2009-07-14 16:55:14 +00007630 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00007631 }
7632 // Handle all other FP comparisons here.
Owen Anderson825b72b2009-08-11 20:47:22 +00007633 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00007634 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007635
Nate Begeman30a0de92008-07-17 16:51:19 +00007636 // We are handling one of the integer comparisons here. Since SSE only has
7637 // GT and EQ comparisons for integer, swapping operands and multiple
7638 // operations may be required for some comparisons.
7639 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
7640 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007641
Owen Anderson825b72b2009-08-11 20:47:22 +00007642 switch (VT.getSimpleVT().SimpleTy) {
Nate Begeman30a0de92008-07-17 16:51:19 +00007643 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007644 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007645 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007646 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
7647 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00007648 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007649
Nate Begeman30a0de92008-07-17 16:51:19 +00007650 switch (SetCCOpcode) {
7651 default: break;
7652 case ISD::SETNE: Invert = true;
7653 case ISD::SETEQ: Opc = EQOpc; break;
7654 case ISD::SETLT: Swap = true;
7655 case ISD::SETGT: Opc = GTOpc; break;
7656 case ISD::SETGE: Swap = true;
7657 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
7658 case ISD::SETULT: Swap = true;
7659 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
7660 case ISD::SETUGE: Swap = true;
7661 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
7662 }
7663 if (Swap)
7664 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007665
Nate Begeman30a0de92008-07-17 16:51:19 +00007666 // Since SSE has no unsigned integer comparisons, we need to flip the sign
7667 // bits of the inputs before performing those operations.
7668 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00007669 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00007670 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
7671 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00007672 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00007673 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
7674 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00007675 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
7676 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00007677 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007678
Dale Johannesenace16102009-02-03 19:33:06 +00007679 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00007680
7681 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00007682 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00007683 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00007684
Nate Begeman30a0de92008-07-17 16:51:19 +00007685 return Result;
7686}
Evan Cheng0488db92007-09-25 01:57:46 +00007687
Evan Cheng370e5342008-12-03 08:38:43 +00007688// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00007689static bool isX86LogicalCmp(SDValue Op) {
7690 unsigned Opc = Op.getNode()->getOpcode();
7691 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
7692 return true;
7693 if (Op.getResNo() == 1 &&
7694 (Opc == X86ISD::ADD ||
7695 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00007696 Opc == X86ISD::ADC ||
7697 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00007698 Opc == X86ISD::SMUL ||
7699 Opc == X86ISD::UMUL ||
7700 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00007701 Opc == X86ISD::DEC ||
7702 Opc == X86ISD::OR ||
7703 Opc == X86ISD::XOR ||
7704 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00007705 return true;
7706
Chris Lattner9637d5b2010-12-05 07:49:54 +00007707 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
7708 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007709
Dan Gohman076aee32009-03-04 19:44:21 +00007710 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00007711}
7712
Chris Lattnera2b56002010-12-05 01:23:24 +00007713static bool isZero(SDValue V) {
7714 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
7715 return C && C->isNullValue();
7716}
7717
Chris Lattner96908b12010-12-05 02:00:51 +00007718static bool isAllOnes(SDValue V) {
7719 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
7720 return C && C->isAllOnesValue();
7721}
7722
Dan Gohmand858e902010-04-17 15:26:15 +00007723SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00007724 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007725 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00007726 SDValue Op1 = Op.getOperand(1);
7727 SDValue Op2 = Op.getOperand(2);
7728 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00007729 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00007730
Dan Gohman1a492952009-10-20 16:22:37 +00007731 if (Cond.getOpcode() == ISD::SETCC) {
7732 SDValue NewCond = LowerSETCC(Cond, DAG);
7733 if (NewCond.getNode())
7734 Cond = NewCond;
7735 }
Evan Cheng734503b2006-09-11 02:19:56 +00007736
Chris Lattnera2b56002010-12-05 01:23:24 +00007737 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00007738 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00007739 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00007740 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007741 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00007742 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
7743 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007744 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007745
Chris Lattnera2b56002010-12-05 01:23:24 +00007746 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007747
7748 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00007749 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
7750 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00007751
7752 SDValue CmpOp0 = Cmp.getOperand(0);
7753 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
7754 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007755
Chris Lattner96908b12010-12-05 02:00:51 +00007756 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00007757 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
7758 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007759
Chris Lattner96908b12010-12-05 02:00:51 +00007760 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
7761 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007762
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007763 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00007764 if (N2C == 0 || !N2C->isNullValue())
7765 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
7766 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007767 }
7768 }
7769
Chris Lattnera2b56002010-12-05 01:23:24 +00007770 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00007771 if (Cond.getOpcode() == ISD::AND &&
7772 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
7773 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00007774 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00007775 Cond = Cond.getOperand(0);
7776 }
7777
Evan Cheng3f41d662007-10-08 22:16:29 +00007778 // If condition flag is set by a X86ISD::CMP, then use it as the condition
7779 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00007780 if (Cond.getOpcode() == X86ISD::SETCC ||
7781 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00007782 CC = Cond.getOperand(0);
7783
Dan Gohman475871a2008-07-27 21:46:04 +00007784 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00007785 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00007786 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00007787
Evan Cheng3f41d662007-10-08 22:16:29 +00007788 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007789 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00007790 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00007791 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00007792
Chris Lattnerd1980a52009-03-12 06:52:53 +00007793 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
7794 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00007795 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00007796 addTest = false;
7797 }
7798 }
7799
7800 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007801 // Look pass the truncate.
7802 if (Cond.getOpcode() == ISD::TRUNCATE)
7803 Cond = Cond.getOperand(0);
7804
7805 // We know the result of AND is compared against zero. Try to match
7806 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00007807 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00007808 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00007809 if (NewSetCC.getNode()) {
7810 CC = NewSetCC.getOperand(0);
7811 Cond = NewSetCC.getOperand(1);
7812 addTest = false;
7813 }
7814 }
7815 }
7816
7817 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007818 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00007819 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00007820 }
7821
Benjamin Kramere915ff32010-12-22 23:09:28 +00007822 // a < b ? -1 : 0 -> RES = ~setcc_carry
7823 // a < b ? 0 : -1 -> RES = setcc_carry
7824 // a >= b ? -1 : 0 -> RES = setcc_carry
7825 // a >= b ? 0 : -1 -> RES = ~setcc_carry
7826 if (Cond.getOpcode() == X86ISD::CMP) {
7827 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
7828
7829 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
7830 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
7831 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
7832 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
7833 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
7834 return DAG.getNOT(DL, Res, Res.getValueType());
7835 return Res;
7836 }
7837 }
7838
Evan Cheng0488db92007-09-25 01:57:46 +00007839 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
7840 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007841 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007842 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00007843 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00007844}
7845
Evan Cheng370e5342008-12-03 08:38:43 +00007846// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
7847// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
7848// from the AND / OR.
7849static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
7850 Opc = Op.getOpcode();
7851 if (Opc != ISD::OR && Opc != ISD::AND)
7852 return false;
7853 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
7854 Op.getOperand(0).hasOneUse() &&
7855 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
7856 Op.getOperand(1).hasOneUse());
7857}
7858
Evan Cheng961d6d42009-02-02 08:19:07 +00007859// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
7860// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00007861static bool isXor1OfSetCC(SDValue Op) {
7862 if (Op.getOpcode() != ISD::XOR)
7863 return false;
7864 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
7865 if (N1C && N1C->getAPIntValue() == 1) {
7866 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
7867 Op.getOperand(0).hasOneUse();
7868 }
7869 return false;
7870}
7871
Dan Gohmand858e902010-04-17 15:26:15 +00007872SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00007873 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007874 SDValue Chain = Op.getOperand(0);
7875 SDValue Cond = Op.getOperand(1);
7876 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007877 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00007878 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00007879
Dan Gohman1a492952009-10-20 16:22:37 +00007880 if (Cond.getOpcode() == ISD::SETCC) {
7881 SDValue NewCond = LowerSETCC(Cond, DAG);
7882 if (NewCond.getNode())
7883 Cond = NewCond;
7884 }
Chris Lattnere55484e2008-12-25 05:34:37 +00007885#if 0
7886 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00007887 else if (Cond.getOpcode() == X86ISD::ADD ||
7888 Cond.getOpcode() == X86ISD::SUB ||
7889 Cond.getOpcode() == X86ISD::SMUL ||
7890 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00007891 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00007892#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00007893
Evan Chengad9c0a32009-12-15 00:53:42 +00007894 // Look pass (and (setcc_carry (cmp ...)), 1).
7895 if (Cond.getOpcode() == ISD::AND &&
7896 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
7897 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00007898 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00007899 Cond = Cond.getOperand(0);
7900 }
7901
Evan Cheng3f41d662007-10-08 22:16:29 +00007902 // If condition flag is set by a X86ISD::CMP, then use it as the condition
7903 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00007904 if (Cond.getOpcode() == X86ISD::SETCC ||
7905 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00007906 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007907
Dan Gohman475871a2008-07-27 21:46:04 +00007908 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00007909 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00007910 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00007911 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00007912 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00007913 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00007914 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00007915 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00007916 default: break;
7917 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00007918 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00007919 // These can only come from an arithmetic instruction with overflow,
7920 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00007921 Cond = Cond.getNode()->getOperand(1);
7922 addTest = false;
7923 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00007924 }
Evan Cheng0488db92007-09-25 01:57:46 +00007925 }
Evan Cheng370e5342008-12-03 08:38:43 +00007926 } else {
7927 unsigned CondOpc;
7928 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
7929 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00007930 if (CondOpc == ISD::OR) {
7931 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
7932 // two branches instead of an explicit OR instruction with a
7933 // separate test.
7934 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00007935 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00007936 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007937 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00007938 Chain, Dest, CC, Cmp);
7939 CC = Cond.getOperand(1).getOperand(0);
7940 Cond = Cmp;
7941 addTest = false;
7942 }
7943 } else { // ISD::AND
7944 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
7945 // two branches instead of an explicit AND instruction with a
7946 // separate test. However, we only do this if this block doesn't
7947 // have a fall-through edge, because this requires an explicit
7948 // jmp when the condition is false.
7949 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00007950 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00007951 Op.getNode()->hasOneUse()) {
7952 X86::CondCode CCode =
7953 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
7954 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007955 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00007956 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00007957 // Look for an unconditional branch following this conditional branch.
7958 // We need this because we need to reverse the successors in order
7959 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00007960 if (User->getOpcode() == ISD::BR) {
7961 SDValue FalseBB = User->getOperand(1);
7962 SDNode *NewBR =
7963 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00007964 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00007965 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00007966 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00007967
Dale Johannesene4d209d2009-02-03 20:21:25 +00007968 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00007969 Chain, Dest, CC, Cmp);
7970 X86::CondCode CCode =
7971 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
7972 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007973 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00007974 Cond = Cmp;
7975 addTest = false;
7976 }
7977 }
Dan Gohman279c22e2008-10-21 03:29:32 +00007978 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00007979 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
7980 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
7981 // It should be transformed during dag combiner except when the condition
7982 // is set by a arithmetics with overflow node.
7983 X86::CondCode CCode =
7984 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
7985 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007986 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00007987 Cond = Cond.getOperand(0).getOperand(1);
7988 addTest = false;
Dan Gohman279c22e2008-10-21 03:29:32 +00007989 }
Evan Cheng0488db92007-09-25 01:57:46 +00007990 }
7991
7992 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007993 // Look pass the truncate.
7994 if (Cond.getOpcode() == ISD::TRUNCATE)
7995 Cond = Cond.getOperand(0);
7996
7997 // We know the result of AND is compared against zero. Try to match
7998 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00007999 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008000 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
8001 if (NewSetCC.getNode()) {
8002 CC = NewSetCC.getOperand(0);
8003 Cond = NewSetCC.getOperand(1);
8004 addTest = false;
8005 }
8006 }
8007 }
8008
8009 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008010 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00008011 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00008012 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00008013 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00008014 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00008015}
8016
Anton Korobeynikove060b532007-04-17 19:34:00 +00008017
8018// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
8019// Calls to _alloca is needed to probe the stack when allocating more than 4k
8020// bytes in one go. Touching the stack at 4K increments is necessary to ensure
8021// that the guard pages used by the OS virtual memory manager are allocated in
8022// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00008023SDValue
8024X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008025 SelectionDAG &DAG) const {
Duncan Sands1e1ca0b2010-10-21 16:02:12 +00008026 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows()) &&
Michael J. Spencere9c253e2010-10-21 01:41:01 +00008027 "This should be used only on Windows targets");
NAKAMURA Takumia2e07622011-03-24 07:07:00 +00008028 assert(!Subtarget->isTargetEnvMacho());
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008029 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008030
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008031 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00008032 SDValue Chain = Op.getOperand(0);
8033 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008034 // FIXME: Ensure alignment here
8035
Dan Gohman475871a2008-07-27 21:46:04 +00008036 SDValue Flag;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008037
Owen Anderson825b72b2009-08-11 20:47:22 +00008038 EVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
NAKAMURA Takumia2e07622011-03-24 07:07:00 +00008039 unsigned Reg = (Subtarget->is64Bit() ? X86::RAX : X86::EAX);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008040
NAKAMURA Takumia2e07622011-03-24 07:07:00 +00008041 Chain = DAG.getCopyToReg(Chain, dl, Reg, Size, Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00008042 Flag = Chain.getValue(1);
8043
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008044 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00008045
Michael J. Spencere9c253e2010-10-21 01:41:01 +00008046 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00008047 Flag = Chain.getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008048
Dale Johannesendd64c412009-02-04 00:33:20 +00008049 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008050
Dan Gohman475871a2008-07-27 21:46:04 +00008051 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008052 return DAG.getMergeValues(Ops1, 2, dl);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008053}
8054
Dan Gohmand858e902010-04-17 15:26:15 +00008055SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00008056 MachineFunction &MF = DAG.getMachineFunction();
8057 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
8058
Dan Gohman69de1932008-02-06 22:27:42 +00008059 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00008060 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00008061
Anton Korobeynikove7beda12010-10-03 22:52:07 +00008062 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00008063 // vastart just stores the address of the VarArgsFrameIndex slot into the
8064 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00008065 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
8066 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00008067 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
8068 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008069 }
8070
8071 // __va_list_tag:
8072 // gp_offset (0 - 6 * 8)
8073 // fp_offset (48 - 48 + 8 * 16)
8074 // overflow_arg_area (point to parameters coming in memory).
8075 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00008076 SmallVector<SDValue, 8> MemOps;
8077 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00008078 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00008079 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00008080 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
8081 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008082 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008083 MemOps.push_back(Store);
8084
8085 // Store fp_offset
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));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008088 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00008089 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
8090 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008091 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008092 MemOps.push_back(Store);
8093
8094 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +00008095 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008096 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00008097 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
8098 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00008099 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
8100 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +00008101 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008102 MemOps.push_back(Store);
8103
8104 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +00008105 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008106 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00008107 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
8108 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00008109 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
8110 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008111 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +00008112 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00008113 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00008114}
8115
Dan Gohmand858e902010-04-17 15:26:15 +00008116SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +00008117 assert(Subtarget->is64Bit() &&
8118 "LowerVAARG only handles 64-bit va_arg!");
8119 assert((Subtarget->isTargetLinux() ||
8120 Subtarget->isTargetDarwin()) &&
8121 "Unhandled target in LowerVAARG");
8122 assert(Op.getNode()->getNumOperands() == 4);
8123 SDValue Chain = Op.getOperand(0);
8124 SDValue SrcPtr = Op.getOperand(1);
8125 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
8126 unsigned Align = Op.getConstantOperandVal(3);
8127 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +00008128
Dan Gohman320afb82010-10-12 18:00:49 +00008129 EVT ArgVT = Op.getNode()->getValueType(0);
8130 const Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
8131 uint32_t ArgSize = getTargetData()->getTypeAllocSize(ArgTy);
8132 uint8_t ArgMode;
8133
8134 // Decide which area this value should be read from.
8135 // TODO: Implement the AMD64 ABI in its entirety. This simple
8136 // selection mechanism works only for the basic types.
8137 if (ArgVT == MVT::f80) {
8138 llvm_unreachable("va_arg for f80 not yet implemented");
8139 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
8140 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
8141 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
8142 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
8143 } else {
8144 llvm_unreachable("Unhandled argument type in LowerVAARG");
8145 }
8146
8147 if (ArgMode == 2) {
8148 // Sanity Check: Make sure using fp_offset makes sense.
Michael J. Spencer87b86652010-10-19 07:32:42 +00008149 assert(!UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +00008150 !(DAG.getMachineFunction()
8151 .getFunction()->hasFnAttr(Attribute::NoImplicitFloat)) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00008152 Subtarget->hasXMM());
Dan Gohman320afb82010-10-12 18:00:49 +00008153 }
8154
8155 // Insert VAARG_64 node into the DAG
8156 // VAARG_64 returns two values: Variable Argument Address, Chain
8157 SmallVector<SDValue, 11> InstOps;
8158 InstOps.push_back(Chain);
8159 InstOps.push_back(SrcPtr);
8160 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
8161 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
8162 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
8163 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
8164 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
8165 VTs, &InstOps[0], InstOps.size(),
8166 MVT::i64,
8167 MachinePointerInfo(SV),
8168 /*Align=*/0,
8169 /*Volatile=*/false,
8170 /*ReadMem=*/true,
8171 /*WriteMem=*/true);
8172 Chain = VAARG.getValue(1);
8173
8174 // Load the next argument and return it
8175 return DAG.getLoad(ArgVT, dl,
8176 Chain,
8177 VAARG,
8178 MachinePointerInfo(),
8179 false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +00008180}
8181
Dan Gohmand858e902010-04-17 15:26:15 +00008182SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) const {
Evan Chengae642192007-03-02 23:16:35 +00008183 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00008184 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00008185 SDValue Chain = Op.getOperand(0);
8186 SDValue DstPtr = Op.getOperand(1);
8187 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00008188 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
8189 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +00008190 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00008191
Chris Lattnere72f2022010-09-21 05:40:29 +00008192 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00008193 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +00008194 false,
Chris Lattnere72f2022010-09-21 05:40:29 +00008195 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +00008196}
8197
Dan Gohman475871a2008-07-27 21:46:04 +00008198SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00008199X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008200 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00008201 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00008202 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00008203 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00008204 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00008205 case Intrinsic::x86_sse_comieq_ss:
8206 case Intrinsic::x86_sse_comilt_ss:
8207 case Intrinsic::x86_sse_comile_ss:
8208 case Intrinsic::x86_sse_comigt_ss:
8209 case Intrinsic::x86_sse_comige_ss:
8210 case Intrinsic::x86_sse_comineq_ss:
8211 case Intrinsic::x86_sse_ucomieq_ss:
8212 case Intrinsic::x86_sse_ucomilt_ss:
8213 case Intrinsic::x86_sse_ucomile_ss:
8214 case Intrinsic::x86_sse_ucomigt_ss:
8215 case Intrinsic::x86_sse_ucomige_ss:
8216 case Intrinsic::x86_sse_ucomineq_ss:
8217 case Intrinsic::x86_sse2_comieq_sd:
8218 case Intrinsic::x86_sse2_comilt_sd:
8219 case Intrinsic::x86_sse2_comile_sd:
8220 case Intrinsic::x86_sse2_comigt_sd:
8221 case Intrinsic::x86_sse2_comige_sd:
8222 case Intrinsic::x86_sse2_comineq_sd:
8223 case Intrinsic::x86_sse2_ucomieq_sd:
8224 case Intrinsic::x86_sse2_ucomilt_sd:
8225 case Intrinsic::x86_sse2_ucomile_sd:
8226 case Intrinsic::x86_sse2_ucomigt_sd:
8227 case Intrinsic::x86_sse2_ucomige_sd:
8228 case Intrinsic::x86_sse2_ucomineq_sd: {
8229 unsigned Opc = 0;
8230 ISD::CondCode CC = ISD::SETCC_INVALID;
8231 switch (IntNo) {
8232 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008233 case Intrinsic::x86_sse_comieq_ss:
8234 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008235 Opc = X86ISD::COMI;
8236 CC = ISD::SETEQ;
8237 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00008238 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008239 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008240 Opc = X86ISD::COMI;
8241 CC = ISD::SETLT;
8242 break;
8243 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008244 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008245 Opc = X86ISD::COMI;
8246 CC = ISD::SETLE;
8247 break;
8248 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008249 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008250 Opc = X86ISD::COMI;
8251 CC = ISD::SETGT;
8252 break;
8253 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008254 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008255 Opc = X86ISD::COMI;
8256 CC = ISD::SETGE;
8257 break;
8258 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008259 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008260 Opc = X86ISD::COMI;
8261 CC = ISD::SETNE;
8262 break;
8263 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008264 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008265 Opc = X86ISD::UCOMI;
8266 CC = ISD::SETEQ;
8267 break;
8268 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008269 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008270 Opc = X86ISD::UCOMI;
8271 CC = ISD::SETLT;
8272 break;
8273 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008274 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008275 Opc = X86ISD::UCOMI;
8276 CC = ISD::SETLE;
8277 break;
8278 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008279 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008280 Opc = X86ISD::UCOMI;
8281 CC = ISD::SETGT;
8282 break;
8283 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008284 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008285 Opc = X86ISD::UCOMI;
8286 CC = ISD::SETGE;
8287 break;
8288 case Intrinsic::x86_sse_ucomineq_ss:
8289 case Intrinsic::x86_sse2_ucomineq_sd:
8290 Opc = X86ISD::UCOMI;
8291 CC = ISD::SETNE;
8292 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00008293 }
Evan Cheng734503b2006-09-11 02:19:56 +00008294
Dan Gohman475871a2008-07-27 21:46:04 +00008295 SDValue LHS = Op.getOperand(1);
8296 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00008297 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00008298 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00008299 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
8300 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8301 DAG.getConstant(X86CC, MVT::i8), Cond);
8302 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00008303 }
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008304 // ptest and testp intrinsics. The intrinsic these come from are designed to
8305 // return an integer value, not just an instruction so lower it to the ptest
8306 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00008307 case Intrinsic::x86_sse41_ptestz:
8308 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008309 case Intrinsic::x86_sse41_ptestnzc:
8310 case Intrinsic::x86_avx_ptestz_256:
8311 case Intrinsic::x86_avx_ptestc_256:
8312 case Intrinsic::x86_avx_ptestnzc_256:
8313 case Intrinsic::x86_avx_vtestz_ps:
8314 case Intrinsic::x86_avx_vtestc_ps:
8315 case Intrinsic::x86_avx_vtestnzc_ps:
8316 case Intrinsic::x86_avx_vtestz_pd:
8317 case Intrinsic::x86_avx_vtestc_pd:
8318 case Intrinsic::x86_avx_vtestnzc_pd:
8319 case Intrinsic::x86_avx_vtestz_ps_256:
8320 case Intrinsic::x86_avx_vtestc_ps_256:
8321 case Intrinsic::x86_avx_vtestnzc_ps_256:
8322 case Intrinsic::x86_avx_vtestz_pd_256:
8323 case Intrinsic::x86_avx_vtestc_pd_256:
8324 case Intrinsic::x86_avx_vtestnzc_pd_256: {
8325 bool IsTestPacked = false;
Eric Christopher71c67532009-07-29 00:28:05 +00008326 unsigned X86CC = 0;
8327 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00008328 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008329 case Intrinsic::x86_avx_vtestz_ps:
8330 case Intrinsic::x86_avx_vtestz_pd:
8331 case Intrinsic::x86_avx_vtestz_ps_256:
8332 case Intrinsic::x86_avx_vtestz_pd_256:
8333 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00008334 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008335 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +00008336 // ZF = 1
8337 X86CC = X86::COND_E;
8338 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008339 case Intrinsic::x86_avx_vtestc_ps:
8340 case Intrinsic::x86_avx_vtestc_pd:
8341 case Intrinsic::x86_avx_vtestc_ps_256:
8342 case Intrinsic::x86_avx_vtestc_pd_256:
8343 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00008344 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008345 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00008346 // CF = 1
8347 X86CC = X86::COND_B;
8348 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008349 case Intrinsic::x86_avx_vtestnzc_ps:
8350 case Intrinsic::x86_avx_vtestnzc_pd:
8351 case Intrinsic::x86_avx_vtestnzc_ps_256:
8352 case Intrinsic::x86_avx_vtestnzc_pd_256:
8353 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +00008354 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008355 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00008356 // ZF and CF = 0
8357 X86CC = X86::COND_A;
8358 break;
8359 }
Eric Christopherfd179292009-08-27 18:07:15 +00008360
Eric Christopher71c67532009-07-29 00:28:05 +00008361 SDValue LHS = Op.getOperand(1);
8362 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008363 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
8364 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00008365 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
8366 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
8367 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00008368 }
Evan Cheng5759f972008-05-04 09:15:50 +00008369
8370 // Fix vector shift instructions where the last operand is a non-immediate
8371 // i32 value.
8372 case Intrinsic::x86_sse2_pslli_w:
8373 case Intrinsic::x86_sse2_pslli_d:
8374 case Intrinsic::x86_sse2_pslli_q:
8375 case Intrinsic::x86_sse2_psrli_w:
8376 case Intrinsic::x86_sse2_psrli_d:
8377 case Intrinsic::x86_sse2_psrli_q:
8378 case Intrinsic::x86_sse2_psrai_w:
8379 case Intrinsic::x86_sse2_psrai_d:
8380 case Intrinsic::x86_mmx_pslli_w:
8381 case Intrinsic::x86_mmx_pslli_d:
8382 case Intrinsic::x86_mmx_pslli_q:
8383 case Intrinsic::x86_mmx_psrli_w:
8384 case Intrinsic::x86_mmx_psrli_d:
8385 case Intrinsic::x86_mmx_psrli_q:
8386 case Intrinsic::x86_mmx_psrai_w:
8387 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00008388 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00008389 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00008390 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00008391
8392 unsigned NewIntNo = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00008393 EVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00008394 switch (IntNo) {
8395 case Intrinsic::x86_sse2_pslli_w:
8396 NewIntNo = Intrinsic::x86_sse2_psll_w;
8397 break;
8398 case Intrinsic::x86_sse2_pslli_d:
8399 NewIntNo = Intrinsic::x86_sse2_psll_d;
8400 break;
8401 case Intrinsic::x86_sse2_pslli_q:
8402 NewIntNo = Intrinsic::x86_sse2_psll_q;
8403 break;
8404 case Intrinsic::x86_sse2_psrli_w:
8405 NewIntNo = Intrinsic::x86_sse2_psrl_w;
8406 break;
8407 case Intrinsic::x86_sse2_psrli_d:
8408 NewIntNo = Intrinsic::x86_sse2_psrl_d;
8409 break;
8410 case Intrinsic::x86_sse2_psrli_q:
8411 NewIntNo = Intrinsic::x86_sse2_psrl_q;
8412 break;
8413 case Intrinsic::x86_sse2_psrai_w:
8414 NewIntNo = Intrinsic::x86_sse2_psra_w;
8415 break;
8416 case Intrinsic::x86_sse2_psrai_d:
8417 NewIntNo = Intrinsic::x86_sse2_psra_d;
8418 break;
8419 default: {
Owen Anderson825b72b2009-08-11 20:47:22 +00008420 ShAmtVT = MVT::v2i32;
Evan Cheng5759f972008-05-04 09:15:50 +00008421 switch (IntNo) {
8422 case Intrinsic::x86_mmx_pslli_w:
8423 NewIntNo = Intrinsic::x86_mmx_psll_w;
8424 break;
8425 case Intrinsic::x86_mmx_pslli_d:
8426 NewIntNo = Intrinsic::x86_mmx_psll_d;
8427 break;
8428 case Intrinsic::x86_mmx_pslli_q:
8429 NewIntNo = Intrinsic::x86_mmx_psll_q;
8430 break;
8431 case Intrinsic::x86_mmx_psrli_w:
8432 NewIntNo = Intrinsic::x86_mmx_psrl_w;
8433 break;
8434 case Intrinsic::x86_mmx_psrli_d:
8435 NewIntNo = Intrinsic::x86_mmx_psrl_d;
8436 break;
8437 case Intrinsic::x86_mmx_psrli_q:
8438 NewIntNo = Intrinsic::x86_mmx_psrl_q;
8439 break;
8440 case Intrinsic::x86_mmx_psrai_w:
8441 NewIntNo = Intrinsic::x86_mmx_psra_w;
8442 break;
8443 case Intrinsic::x86_mmx_psrai_d:
8444 NewIntNo = Intrinsic::x86_mmx_psra_d;
8445 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00008446 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00008447 }
8448 break;
8449 }
8450 }
Mon P Wangefa42202009-09-03 19:56:25 +00008451
8452 // The vector shift intrinsics with scalars uses 32b shift amounts but
8453 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
8454 // to be zero.
8455 SDValue ShOps[4];
8456 ShOps[0] = ShAmt;
8457 ShOps[1] = DAG.getConstant(0, MVT::i32);
8458 if (ShAmtVT == MVT::v4i32) {
8459 ShOps[2] = DAG.getUNDEF(MVT::i32);
8460 ShOps[3] = DAG.getUNDEF(MVT::i32);
8461 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
8462 } else {
8463 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
Dale Johannesen0488fb62010-09-30 23:57:10 +00008464// FIXME this must be lowered to get rid of the invalid type.
Mon P Wangefa42202009-09-03 19:56:25 +00008465 }
8466
Owen Andersone50ed302009-08-10 22:56:29 +00008467 EVT VT = Op.getValueType();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008468 ShAmt = DAG.getNode(ISD::BITCAST, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008469 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008470 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00008471 Op.getOperand(1), ShAmt);
8472 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00008473 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008474}
Evan Cheng72261582005-12-20 06:22:03 +00008475
Dan Gohmand858e902010-04-17 15:26:15 +00008476SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
8477 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +00008478 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
8479 MFI->setReturnAddressIsTaken(true);
8480
Bill Wendling64e87322009-01-16 19:25:27 +00008481 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008482 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00008483
8484 if (Depth > 0) {
8485 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
8486 SDValue Offset =
8487 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00008488 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008489 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00008490 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008491 FrameAddr, Offset),
Chris Lattner51abfe42010-09-21 06:02:19 +00008492 MachinePointerInfo(), false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +00008493 }
8494
8495 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00008496 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00008497 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Chris Lattner51abfe42010-09-21 06:02:19 +00008498 RetAddrFI, MachinePointerInfo(), false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00008499}
8500
Dan Gohmand858e902010-04-17 15:26:15 +00008501SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +00008502 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
8503 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00008504
Owen Andersone50ed302009-08-10 22:56:29 +00008505 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008506 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00008507 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
8508 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00008509 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00008510 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +00008511 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
8512 MachinePointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +00008513 false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00008514 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00008515}
8516
Dan Gohman475871a2008-07-27 21:46:04 +00008517SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008518 SelectionDAG &DAG) const {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00008519 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008520}
8521
Dan Gohmand858e902010-04-17 15:26:15 +00008522SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008523 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00008524 SDValue Chain = Op.getOperand(0);
8525 SDValue Offset = Op.getOperand(1);
8526 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008527 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008528
Dan Gohmand8816272010-08-11 18:14:00 +00008529 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
8530 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
8531 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008532 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008533
Dan Gohmand8816272010-08-11 18:14:00 +00008534 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
8535 DAG.getIntPtrConstant(TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008536 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +00008537 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
8538 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00008539 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008540 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008541
Dale Johannesene4d209d2009-02-03 20:21:25 +00008542 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008543 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008544 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008545}
8546
Dan Gohman475871a2008-07-27 21:46:04 +00008547SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008548 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008549 SDValue Root = Op.getOperand(0);
8550 SDValue Trmp = Op.getOperand(1); // trampoline
8551 SDValue FPtr = Op.getOperand(2); // nested function
8552 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008553 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008554
Dan Gohman69de1932008-02-06 22:27:42 +00008555 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008556
8557 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00008558 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00008559
8560 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +00008561 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
8562 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +00008563
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00008564 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
8565 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00008566
8567 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
8568
8569 // Load the pointer to the nested function into R11.
8570 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00008571 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00008572 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008573 Addr, MachinePointerInfo(TrmpAddr),
8574 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008575
Owen Anderson825b72b2009-08-11 20:47:22 +00008576 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8577 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008578 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
8579 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +00008580 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00008581
8582 // Load the 'nest' parameter value into R10.
8583 // R10 is specified in X86CallingConv.td
8584 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +00008585 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8586 DAG.getConstant(10, MVT::i64));
8587 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008588 Addr, MachinePointerInfo(TrmpAddr, 10),
8589 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008590
Owen Anderson825b72b2009-08-11 20:47:22 +00008591 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8592 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008593 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
8594 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +00008595 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00008596
8597 // Jump to the nested function.
8598 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +00008599 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8600 DAG.getConstant(20, MVT::i64));
8601 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008602 Addr, MachinePointerInfo(TrmpAddr, 20),
8603 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008604
8605 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +00008606 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8607 DAG.getConstant(22, MVT::i64));
8608 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00008609 MachinePointerInfo(TrmpAddr, 22),
8610 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008611
Dan Gohman475871a2008-07-27 21:46:04 +00008612 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00008613 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008614 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008615 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00008616 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00008617 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00008618 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00008619 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008620
8621 switch (CC) {
8622 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00008623 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00008624 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00008625 case CallingConv::X86_StdCall: {
8626 // Pass 'nest' parameter in ECX.
8627 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00008628 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008629
8630 // Check that ECX wasn't needed by an 'inreg' parameter.
8631 const FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00008632 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008633
Chris Lattner58d74912008-03-12 17:45:29 +00008634 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00008635 unsigned InRegCount = 0;
8636 unsigned Idx = 1;
8637
8638 for (FunctionType::param_iterator I = FTy->param_begin(),
8639 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00008640 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00008641 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00008642 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008643
8644 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +00008645 report_fatal_error("Nest register in use - reduce number of inreg"
8646 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00008647 }
8648 }
8649 break;
8650 }
8651 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +00008652 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00008653 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00008654 // Pass 'nest' parameter in EAX.
8655 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00008656 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008657 break;
8658 }
8659
Dan Gohman475871a2008-07-27 21:46:04 +00008660 SDValue OutChains[4];
8661 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008662
Owen Anderson825b72b2009-08-11 20:47:22 +00008663 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8664 DAG.getConstant(10, MVT::i32));
8665 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008666
Chris Lattnera62fe662010-02-05 19:20:30 +00008667 // This is storing the opcode for MOV32ri.
8668 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00008669 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00008670 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008671 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008672 Trmp, MachinePointerInfo(TrmpAddr),
8673 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008674
Owen Anderson825b72b2009-08-11 20:47:22 +00008675 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8676 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008677 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
8678 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +00008679 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008680
Chris Lattnera62fe662010-02-05 19:20:30 +00008681 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +00008682 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8683 DAG.getConstant(5, MVT::i32));
8684 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00008685 MachinePointerInfo(TrmpAddr, 5),
8686 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008687
Owen Anderson825b72b2009-08-11 20:47:22 +00008688 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8689 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008690 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
8691 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +00008692 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008693
Dan Gohman475871a2008-07-27 21:46:04 +00008694 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00008695 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008696 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008697 }
8698}
8699
Dan Gohmand858e902010-04-17 15:26:15 +00008700SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
8701 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008702 /*
8703 The rounding mode is in bits 11:10 of FPSR, and has the following
8704 settings:
8705 00 Round to nearest
8706 01 Round to -inf
8707 10 Round to +inf
8708 11 Round to 0
8709
8710 FLT_ROUNDS, on the other hand, expects the following:
8711 -1 Undefined
8712 0 Round to 0
8713 1 Round to nearest
8714 2 Round to +inf
8715 3 Round to -inf
8716
8717 To perform the conversion, we do:
8718 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
8719 */
8720
8721 MachineFunction &MF = DAG.getMachineFunction();
8722 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00008723 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008724 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00008725 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +00008726 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008727
8728 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00008729 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +00008730 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008731
Michael J. Spencerec38de22010-10-10 22:04:20 +00008732
Chris Lattner2156b792010-09-22 01:11:26 +00008733 MachineMemOperand *MMO =
8734 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8735 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008736
Chris Lattner2156b792010-09-22 01:11:26 +00008737 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
8738 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
8739 DAG.getVTList(MVT::Other),
8740 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008741
8742 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +00008743 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Chris Lattner51abfe42010-09-21 06:02:19 +00008744 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008745
8746 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00008747 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +00008748 DAG.getNode(ISD::SRL, DL, MVT::i16,
8749 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00008750 CWD, DAG.getConstant(0x800, MVT::i16)),
8751 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00008752 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +00008753 DAG.getNode(ISD::SRL, DL, MVT::i16,
8754 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00008755 CWD, DAG.getConstant(0x400, MVT::i16)),
8756 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008757
Dan Gohman475871a2008-07-27 21:46:04 +00008758 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +00008759 DAG.getNode(ISD::AND, DL, MVT::i16,
8760 DAG.getNode(ISD::ADD, DL, MVT::i16,
8761 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +00008762 DAG.getConstant(1, MVT::i16)),
8763 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008764
8765
Duncan Sands83ec4b62008-06-06 12:08:01 +00008766 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +00008767 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008768}
8769
Dan Gohmand858e902010-04-17 15:26:15 +00008770SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008771 EVT VT = Op.getValueType();
8772 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008773 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008774 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00008775
8776 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008777 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00008778 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +00008779 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00008780 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008781 }
Evan Cheng18efe262007-12-14 02:13:44 +00008782
Evan Cheng152804e2007-12-14 08:30:15 +00008783 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008784 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008785 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00008786
8787 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00008788 SDValue Ops[] = {
8789 Op,
8790 DAG.getConstant(NumBits+NumBits-1, OpVT),
8791 DAG.getConstant(X86::COND_E, MVT::i8),
8792 Op.getValue(1)
8793 };
8794 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00008795
8796 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00008797 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00008798
Owen Anderson825b72b2009-08-11 20:47:22 +00008799 if (VT == MVT::i8)
8800 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008801 return Op;
8802}
8803
Dan Gohmand858e902010-04-17 15:26:15 +00008804SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008805 EVT VT = Op.getValueType();
8806 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008807 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008808 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00008809
8810 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008811 if (VT == MVT::i8) {
8812 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00008813 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008814 }
Evan Cheng152804e2007-12-14 08:30:15 +00008815
8816 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008817 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008818 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00008819
8820 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00008821 SDValue Ops[] = {
8822 Op,
8823 DAG.getConstant(NumBits, OpVT),
8824 DAG.getConstant(X86::COND_E, MVT::i8),
8825 Op.getValue(1)
8826 };
8827 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00008828
Owen Anderson825b72b2009-08-11 20:47:22 +00008829 if (VT == MVT::i8)
8830 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008831 return Op;
8832}
8833
Dan Gohmand858e902010-04-17 15:26:15 +00008834SDValue X86TargetLowering::LowerMUL_V2I64(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008835 EVT VT = Op.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00008836 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008837 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00008838
Mon P Wangaf9b9522008-12-18 21:42:19 +00008839 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
8840 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
8841 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
8842 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
8843 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
8844 //
8845 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
8846 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
8847 // return AloBlo + AloBhi + AhiBlo;
8848
8849 SDValue A = Op.getOperand(0);
8850 SDValue B = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008851
Dale Johannesene4d209d2009-02-03 20:21:25 +00008852 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008853 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8854 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008855 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008856 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8857 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008858 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008859 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008860 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008861 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008862 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008863 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008864 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008865 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008866 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008867 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008868 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8869 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008870 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008871 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8872 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008873 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
8874 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +00008875 return Res;
8876}
8877
Nadav Rotem43012222011-05-11 08:12:09 +00008878SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
8879
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008880 EVT VT = Op.getValueType();
8881 DebugLoc dl = Op.getDebugLoc();
8882 SDValue R = Op.getOperand(0);
Nadav Rotem43012222011-05-11 08:12:09 +00008883 SDValue Amt = Op.getOperand(1);
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008884
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008885 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008886
Nadav Rotem43012222011-05-11 08:12:09 +00008887 // Must have SSE2.
8888 if (!Subtarget->hasSSE2()) return SDValue();
Nate Begeman51409212010-07-28 00:21:48 +00008889
Nadav Rotem43012222011-05-11 08:12:09 +00008890 // Optimize shl/srl/sra with constant shift amount.
8891 if (isSplatVector(Amt.getNode())) {
8892 SDValue SclrAmt = Amt->getOperand(0);
8893 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SclrAmt)) {
8894 uint64_t ShiftAmt = C->getZExtValue();
8895
8896 if (VT == MVT::v2i64 && Op.getOpcode() == ISD::SHL)
8897 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8898 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8899 R, DAG.getConstant(ShiftAmt, MVT::i32));
8900
8901 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SHL)
8902 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8903 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
8904 R, DAG.getConstant(ShiftAmt, MVT::i32));
8905
8906 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SHL)
8907 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8908 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
8909 R, DAG.getConstant(ShiftAmt, MVT::i32));
8910
8911 if (VT == MVT::v2i64 && Op.getOpcode() == ISD::SRL)
8912 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8913 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8914 R, DAG.getConstant(ShiftAmt, MVT::i32));
8915
8916 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SRL)
8917 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8918 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
8919 R, DAG.getConstant(ShiftAmt, MVT::i32));
8920
8921 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SRL)
8922 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8923 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
8924 R, DAG.getConstant(ShiftAmt, MVT::i32));
8925
8926 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SRA)
8927 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8928 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
8929 R, DAG.getConstant(ShiftAmt, MVT::i32));
8930
8931 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SRA)
8932 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8933 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
8934 R, DAG.getConstant(ShiftAmt, MVT::i32));
8935 }
8936 }
8937
8938 // Lower SHL with variable shift amount.
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00008939 // Cannot lower SHL without SSE2 or later.
8940 if (!Subtarget->hasSSE2()) return SDValue();
Nadav Rotem43012222011-05-11 08:12:09 +00008941
8942 if (VT == MVT::v4i32 && Op->getOpcode() == ISD::SHL) {
Nate Begeman51409212010-07-28 00:21:48 +00008943 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8944 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
8945 Op.getOperand(1), DAG.getConstant(23, MVT::i32));
8946
8947 ConstantInt *CI = ConstantInt::get(*Context, APInt(32, 0x3f800000U));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008948
Nate Begeman51409212010-07-28 00:21:48 +00008949 std::vector<Constant*> CV(4, CI);
8950 Constant *C = ConstantVector::get(CV);
8951 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8952 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008953 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00008954 false, false, 16);
8955
8956 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008957 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +00008958 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
8959 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
8960 }
Nadav Rotem43012222011-05-11 08:12:09 +00008961 if (VT == MVT::v16i8 && Op->getOpcode() == ISD::SHL) {
Nate Begeman51409212010-07-28 00:21:48 +00008962 // a = a << 5;
8963 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8964 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
8965 Op.getOperand(1), DAG.getConstant(5, MVT::i32));
8966
8967 ConstantInt *CM1 = ConstantInt::get(*Context, APInt(8, 15));
8968 ConstantInt *CM2 = ConstantInt::get(*Context, APInt(8, 63));
8969
8970 std::vector<Constant*> CVM1(16, CM1);
8971 std::vector<Constant*> CVM2(16, CM2);
8972 Constant *C = ConstantVector::get(CVM1);
8973 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8974 SDValue M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008975 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00008976 false, false, 16);
8977
8978 // r = pblendv(r, psllw(r & (char16)15, 4), a);
8979 M = DAG.getNode(ISD::AND, dl, VT, R, M);
8980 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8981 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
8982 DAG.getConstant(4, MVT::i32));
Nate Begeman672fb622010-12-20 22:04:24 +00008983 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT, R, M, Op);
Nate Begeman51409212010-07-28 00:21:48 +00008984 // a += a
8985 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008986
Nate Begeman51409212010-07-28 00:21:48 +00008987 C = ConstantVector::get(CVM2);
8988 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8989 M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008990 MachinePointerInfo::getConstantPool(),
Chris Lattner51abfe42010-09-21 06:02:19 +00008991 false, false, 16);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008992
Nate Begeman51409212010-07-28 00:21:48 +00008993 // r = pblendv(r, psllw(r & (char16)63, 2), a);
8994 M = DAG.getNode(ISD::AND, dl, VT, R, M);
8995 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8996 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
8997 DAG.getConstant(2, MVT::i32));
Nate Begeman672fb622010-12-20 22:04:24 +00008998 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT, R, M, Op);
Nate Begeman51409212010-07-28 00:21:48 +00008999 // a += a
9000 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +00009001
Nate Begeman51409212010-07-28 00:21:48 +00009002 // return pblendv(r, r+r, a);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009003 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT,
Nate Begeman51409212010-07-28 00:21:48 +00009004 R, DAG.getNode(ISD::ADD, dl, VT, R, R), Op);
9005 return R;
9006 }
9007 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00009008}
Mon P Wangaf9b9522008-12-18 21:42:19 +00009009
Dan Gohmand858e902010-04-17 15:26:15 +00009010SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
Bill Wendling74c37652008-12-09 22:08:41 +00009011 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
9012 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +00009013 // looks for this combo and may remove the "setcc" instruction if the "setcc"
9014 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +00009015 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +00009016 SDValue LHS = N->getOperand(0);
9017 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +00009018 unsigned BaseOp = 0;
9019 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +00009020 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +00009021 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00009022 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +00009023 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +00009024 // A subtract of one will be selected as a INC. Note that INC doesn't
9025 // set CF, so we can't do this for UADDO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +00009026 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
9027 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +00009028 BaseOp = X86ISD::INC;
9029 Cond = X86::COND_O;
9030 break;
9031 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00009032 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +00009033 Cond = X86::COND_O;
9034 break;
9035 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00009036 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +00009037 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00009038 break;
9039 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +00009040 // A subtract of one will be selected as a DEC. Note that DEC doesn't
9041 // set CF, so we can't do this for USUBO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +00009042 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
9043 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +00009044 BaseOp = X86ISD::DEC;
9045 Cond = X86::COND_O;
9046 break;
9047 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00009048 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +00009049 Cond = X86::COND_O;
9050 break;
9051 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00009052 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +00009053 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00009054 break;
9055 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00009056 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +00009057 Cond = X86::COND_O;
9058 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +00009059 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
9060 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
9061 MVT::i32);
9062 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009063
Chris Lattnerb20e0b12010-12-05 07:30:36 +00009064 SDValue SetCC =
9065 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
9066 DAG.getConstant(X86::COND_O, MVT::i32),
9067 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009068
Chris Lattnerb20e0b12010-12-05 07:30:36 +00009069 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
9070 return Sum;
9071 }
Bill Wendling74c37652008-12-09 22:08:41 +00009072 }
Bill Wendling3fafd932008-11-26 22:37:40 +00009073
Bill Wendling61edeb52008-12-02 01:06:39 +00009074 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00009075 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +00009076 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +00009077
Bill Wendling61edeb52008-12-02 01:06:39 +00009078 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +00009079 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
9080 DAG.getConstant(Cond, MVT::i32),
9081 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +00009082
Bill Wendling61edeb52008-12-02 01:06:39 +00009083 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
9084 return Sum;
Bill Wendling41ea7e72008-11-24 19:21:46 +00009085}
9086
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00009087SDValue X86TargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op, SelectionDAG &DAG) const{
9088 DebugLoc dl = Op.getDebugLoc();
9089 SDNode* Node = Op.getNode();
9090 EVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
9091 EVT VT = Node->getValueType(0);
9092
9093 if (Subtarget->hasSSE2() && VT.isVector()) {
9094 unsigned BitsDiff = VT.getScalarType().getSizeInBits() -
9095 ExtraVT.getScalarType().getSizeInBits();
9096 SDValue ShAmt = DAG.getConstant(BitsDiff, MVT::i32);
9097
9098 unsigned SHLIntrinsicsID = 0;
9099 unsigned SRAIntrinsicsID = 0;
9100 switch (VT.getSimpleVT().SimpleTy) {
9101 default:
9102 return SDValue();
9103 case MVT::v2i64: {
9104 SHLIntrinsicsID = Intrinsic::x86_sse2_pslli_q;
9105 SRAIntrinsicsID = 0;
9106 break;
9107 }
9108 case MVT::v4i32: {
9109 SHLIntrinsicsID = Intrinsic::x86_sse2_pslli_d;
9110 SRAIntrinsicsID = Intrinsic::x86_sse2_psrai_d;
9111 break;
9112 }
9113 case MVT::v8i16: {
9114 SHLIntrinsicsID = Intrinsic::x86_sse2_pslli_w;
9115 SRAIntrinsicsID = Intrinsic::x86_sse2_psrai_w;
9116 break;
9117 }
9118 }
9119
9120 SDValue Tmp1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9121 DAG.getConstant(SHLIntrinsicsID, MVT::i32),
9122 Node->getOperand(0), ShAmt);
9123
9124 // In case of 1 bit sext, no need to shr
9125 if (ExtraVT.getScalarType().getSizeInBits() == 1) return Tmp1;
9126
9127 if (SRAIntrinsicsID) {
9128 Tmp1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9129 DAG.getConstant(SRAIntrinsicsID, MVT::i32),
9130 Tmp1, ShAmt);
9131 }
9132 return Tmp1;
9133 }
9134
9135 return SDValue();
9136}
9137
9138
Eric Christopher9a9d2752010-07-22 02:48:34 +00009139SDValue X86TargetLowering::LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG) const{
9140 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00009141
Eric Christopher77ed1352011-07-08 00:04:56 +00009142 // Go ahead and emit the fence on x86-64 even if we asked for no-sse2.
9143 // There isn't any reason to disable it if the target processor supports it.
9144 if (!Subtarget->hasSSE2() && !Subtarget->is64Bit()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +00009145 SDValue Chain = Op.getOperand(0);
Eric Christopher77ed1352011-07-08 00:04:56 +00009146 SDValue Zero = DAG.getConstant(0, MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +00009147 SDValue Ops[] = {
9148 DAG.getRegister(X86::ESP, MVT::i32), // Base
9149 DAG.getTargetConstant(1, MVT::i8), // Scale
9150 DAG.getRegister(0, MVT::i32), // Index
9151 DAG.getTargetConstant(0, MVT::i32), // Disp
9152 DAG.getRegister(0, MVT::i32), // Segment.
9153 Zero,
9154 Chain
9155 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00009156 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +00009157 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
9158 array_lengthof(Ops));
9159 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +00009160 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00009161
Eric Christopher9a9d2752010-07-22 02:48:34 +00009162 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +00009163 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +00009164 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009165
Chris Lattner132929a2010-08-14 17:26:09 +00009166 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
9167 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
9168 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
9169 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +00009170
Chris Lattner132929a2010-08-14 17:26:09 +00009171 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
9172 if (!Op1 && !Op2 && !Op3 && Op4)
9173 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009174
Chris Lattner132929a2010-08-14 17:26:09 +00009175 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
9176 if (Op1 && !Op2 && !Op3 && !Op4)
9177 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009178
9179 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +00009180 // (MFENCE)>;
9181 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +00009182}
9183
Dan Gohmand858e902010-04-17 15:26:15 +00009184SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009185 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +00009186 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +00009187 unsigned Reg = 0;
9188 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00009189 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00009190 default:
9191 assert(false && "Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +00009192 case MVT::i8: Reg = X86::AL; size = 1; break;
9193 case MVT::i16: Reg = X86::AX; size = 2; break;
9194 case MVT::i32: Reg = X86::EAX; size = 4; break;
9195 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +00009196 assert(Subtarget->is64Bit() && "Node not type legal!");
9197 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00009198 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00009199 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +00009200 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +00009201 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +00009202 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +00009203 Op.getOperand(1),
9204 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +00009205 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +00009206 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009207 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +00009208 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
9209 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
9210 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +00009211 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +00009212 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +00009213 return cpOut;
9214}
9215
Duncan Sands1607f052008-12-01 11:39:25 +00009216SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00009217 SelectionDAG &DAG) const {
Duncan Sands1607f052008-12-01 11:39:25 +00009218 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009219 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +00009220 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009221 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +00009222 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00009223 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
9224 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +00009225 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +00009226 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
9227 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +00009228 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +00009229 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +00009230 rdx.getValue(1)
9231 };
Dale Johannesene4d209d2009-02-03 20:21:25 +00009232 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009233}
9234
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009235SDValue X86TargetLowering::LowerBITCAST(SDValue Op,
Dale Johannesen7d07b482010-05-21 00:52:33 +00009236 SelectionDAG &DAG) const {
9237 EVT SrcVT = Op.getOperand(0).getValueType();
9238 EVT DstVT = Op.getValueType();
Chris Lattner2a786eb2010-12-19 20:19:20 +00009239 assert(Subtarget->is64Bit() && !Subtarget->hasSSE2() &&
9240 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +00009241 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +00009242 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009243 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +00009244 // i64 <=> MMX conversions are Legal.
9245 if (SrcVT==MVT::i64 && DstVT.isVector())
9246 return Op;
9247 if (DstVT==MVT::i64 && SrcVT.isVector())
9248 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +00009249 // MMX <=> MMX conversions are Legal.
9250 if (SrcVT.isVector() && DstVT.isVector())
9251 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +00009252 // All other conversions need to be expanded.
9253 return SDValue();
9254}
Chris Lattner5b856542010-12-20 00:59:46 +00009255
Dan Gohmand858e902010-04-17 15:26:15 +00009256SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen71d1bf52008-09-29 22:25:26 +00009257 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +00009258 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00009259 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009260 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +00009261 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009262 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009263 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +00009264 Node->getOperand(0),
9265 Node->getOperand(1), negOp,
9266 cast<AtomicSDNode>(Node)->getSrcValue(),
9267 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang63307c32008-05-05 19:05:59 +00009268}
9269
Chris Lattner5b856542010-12-20 00:59:46 +00009270static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
9271 EVT VT = Op.getNode()->getValueType(0);
9272
9273 // Let legalize expand this if it isn't a legal type yet.
9274 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
9275 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009276
Chris Lattner5b856542010-12-20 00:59:46 +00009277 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009278
Chris Lattner5b856542010-12-20 00:59:46 +00009279 unsigned Opc;
9280 bool ExtraOp = false;
9281 switch (Op.getOpcode()) {
9282 default: assert(0 && "Invalid code");
9283 case ISD::ADDC: Opc = X86ISD::ADD; break;
9284 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
9285 case ISD::SUBC: Opc = X86ISD::SUB; break;
9286 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
9287 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009288
Chris Lattner5b856542010-12-20 00:59:46 +00009289 if (!ExtraOp)
9290 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
9291 Op.getOperand(1));
9292 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
9293 Op.getOperand(1), Op.getOperand(2));
9294}
9295
Evan Cheng0db9fe62006-04-25 20:13:52 +00009296/// LowerOperation - Provide custom lowering hooks for some operations.
9297///
Dan Gohmand858e902010-04-17 15:26:15 +00009298SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00009299 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00009300 default: llvm_unreachable("Should not custom lower this!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00009301 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op,DAG);
Eric Christopher9a9d2752010-07-22 02:48:34 +00009302 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op,DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009303 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
9304 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009305 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00009306 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009307 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
9308 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
9309 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
David Greene91585092011-01-26 15:38:49 +00009310 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
David Greenecfe33c42011-01-26 19:13:22 +00009311 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009312 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
9313 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
9314 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00009315 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +00009316 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +00009317 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009318 case ISD::SHL_PARTS:
9319 case ISD::SRA_PARTS:
Nadav Rotem43012222011-05-11 08:12:09 +00009320 case ISD::SRL_PARTS: return LowerShiftParts(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009321 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00009322 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009323 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00009324 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009325 case ISD::FABS: return LowerFABS(Op, DAG);
9326 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00009327 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00009328 case ISD::FGETSIGN: return LowerFGETSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00009329 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman30a0de92008-07-17 16:51:19 +00009330 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00009331 case ISD::SELECT: return LowerSELECT(Op, DAG);
9332 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009333 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009334 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +00009335 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +00009336 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009337 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00009338 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
9339 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009340 case ISD::FRAME_TO_ARGS_OFFSET:
9341 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009342 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009343 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009344 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00009345 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +00009346 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
9347 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Mon P Wangaf9b9522008-12-18 21:42:19 +00009348 case ISD::MUL: return LowerMUL_V2I64(Op, DAG);
Nadav Rotem43012222011-05-11 08:12:09 +00009349 case ISD::SRA:
9350 case ISD::SRL:
9351 case ISD::SHL: return LowerShift(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +00009352 case ISD::SADDO:
9353 case ISD::UADDO:
9354 case ISD::SSUBO:
9355 case ISD::USUBO:
9356 case ISD::SMULO:
9357 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00009358 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009359 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +00009360 case ISD::ADDC:
9361 case ISD::ADDE:
9362 case ISD::SUBC:
9363 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009364 }
Chris Lattner27a6c732007-11-24 07:07:01 +00009365}
9366
Duncan Sands1607f052008-12-01 11:39:25 +00009367void X86TargetLowering::
9368ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00009369 SelectionDAG &DAG, unsigned NewOp) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009370 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009371 DebugLoc dl = Node->getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00009372 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +00009373
9374 SDValue Chain = Node->getOperand(0);
9375 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00009376 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00009377 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00009378 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00009379 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +00009380 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +00009381 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +00009382 SDValue Result =
9383 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
9384 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +00009385 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +00009386 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00009387 Results.push_back(Result.getValue(2));
9388}
9389
Duncan Sands126d9072008-07-04 11:47:58 +00009390/// ReplaceNodeResults - Replace a node with an illegal result type
9391/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00009392void X86TargetLowering::ReplaceNodeResults(SDNode *N,
9393 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00009394 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +00009395 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +00009396 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +00009397 default:
Duncan Sands1607f052008-12-01 11:39:25 +00009398 assert(false && "Do not know how to custom type legalize this operation!");
9399 return;
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00009400 case ISD::SIGN_EXTEND_INREG:
Chris Lattner5b856542010-12-20 00:59:46 +00009401 case ISD::ADDC:
9402 case ISD::ADDE:
9403 case ISD::SUBC:
9404 case ISD::SUBE:
9405 // We don't want to expand or promote these.
9406 return;
Duncan Sands1607f052008-12-01 11:39:25 +00009407 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +00009408 std::pair<SDValue,SDValue> Vals =
9409 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +00009410 SDValue FIST = Vals.first, StackSlot = Vals.second;
9411 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +00009412 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +00009413 // Return a load from the stack slot.
Chris Lattner51abfe42010-09-21 06:02:19 +00009414 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
9415 MachinePointerInfo(), false, false, 0));
Duncan Sands1607f052008-12-01 11:39:25 +00009416 }
9417 return;
9418 }
9419 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009420 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +00009421 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009422 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00009423 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +00009424 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00009425 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00009426 eax.getValue(2));
9427 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
9428 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +00009429 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00009430 Results.push_back(edx.getValue(1));
9431 return;
9432 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009433 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +00009434 EVT T = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009435 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Duncan Sands1607f052008-12-01 11:39:25 +00009436 SDValue cpInL, cpInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00009437 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
9438 DAG.getConstant(0, MVT::i32));
9439 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
9440 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00009441 cpInL = DAG.getCopyToReg(N->getOperand(0), dl, X86::EAX, cpInL, SDValue());
9442 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl, X86::EDX, cpInH,
Duncan Sands1607f052008-12-01 11:39:25 +00009443 cpInL.getValue(1));
9444 SDValue swapInL, swapInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00009445 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
9446 DAG.getConstant(0, MVT::i32));
9447 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
9448 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00009449 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl, X86::EBX, swapInL,
Duncan Sands1607f052008-12-01 11:39:25 +00009450 cpInH.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00009451 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl, X86::ECX, swapInH,
Duncan Sands1607f052008-12-01 11:39:25 +00009452 swapInL.getValue(1));
9453 SDValue Ops[] = { swapInH.getValue(0),
9454 N->getOperand(1),
9455 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009456 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +00009457 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
9458 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG8_DAG, dl, Tys,
9459 Ops, 3, T, MMO);
Dale Johannesendd64c412009-02-04 00:33:20 +00009460 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl, X86::EAX,
Owen Anderson825b72b2009-08-11 20:47:22 +00009461 MVT::i32, Result.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00009462 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl, X86::EDX,
Owen Anderson825b72b2009-08-11 20:47:22 +00009463 MVT::i32, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +00009464 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Owen Anderson825b72b2009-08-11 20:47:22 +00009465 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00009466 Results.push_back(cpOutH.getValue(1));
9467 return;
9468 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009469 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +00009470 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
9471 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009472 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +00009473 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
9474 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009475 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +00009476 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
9477 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009478 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +00009479 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
9480 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009481 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +00009482 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
9483 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009484 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +00009485 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
9486 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009487 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +00009488 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
9489 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00009490 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00009491}
9492
Evan Cheng72261582005-12-20 06:22:03 +00009493const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
9494 switch (Opcode) {
9495 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +00009496 case X86ISD::BSF: return "X86ISD::BSF";
9497 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +00009498 case X86ISD::SHLD: return "X86ISD::SHLD";
9499 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00009500 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00009501 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00009502 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00009503 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00009504 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00009505 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00009506 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
9507 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
9508 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00009509 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00009510 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +00009511 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +00009512 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +00009513 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +00009514 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00009515 case X86ISD::COMI: return "X86ISD::COMI";
9516 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00009517 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +00009518 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Stuart Hastings865f0932011-06-03 23:53:54 +00009519 case X86ISD::FSETCCsd: return "X86ISD::FSETCCsd";
9520 case X86ISD::FSETCCss: return "X86ISD::FSETCCss";
Evan Cheng72261582005-12-20 06:22:03 +00009521 case X86ISD::CMOV: return "X86ISD::CMOV";
9522 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00009523 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00009524 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
9525 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +00009526 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00009527 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +00009528 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +00009529 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +00009530 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +00009531 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
9532 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +00009533 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +00009534 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +00009535 case X86ISD::ANDNP: return "X86ISD::ANDNP";
Nate Begemanb65c1752010-12-17 22:55:37 +00009536 case X86ISD::PSIGNB: return "X86ISD::PSIGNB";
9537 case X86ISD::PSIGNW: return "X86ISD::PSIGNW";
9538 case X86ISD::PSIGND: return "X86ISD::PSIGND";
Nate Begeman672fb622010-12-20 22:04:24 +00009539 case X86ISD::PBLENDVB: return "X86ISD::PBLENDVB";
Evan Cheng8ca29322006-11-10 21:43:37 +00009540 case X86ISD::FMAX: return "X86ISD::FMAX";
9541 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +00009542 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
9543 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00009544 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +00009545 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009546 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00009547 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009548 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +00009549 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
9550 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009551 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
9552 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
9553 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
9554 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
9555 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
9556 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +00009557 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
9558 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +00009559 case X86ISD::VSHL: return "X86ISD::VSHL";
9560 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +00009561 case X86ISD::CMPPD: return "X86ISD::CMPPD";
9562 case X86ISD::CMPPS: return "X86ISD::CMPPS";
9563 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
9564 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
9565 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
9566 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
9567 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
9568 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
9569 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
9570 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +00009571 case X86ISD::ADD: return "X86ISD::ADD";
9572 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +00009573 case X86ISD::ADC: return "X86ISD::ADC";
9574 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +00009575 case X86ISD::SMUL: return "X86ISD::SMUL";
9576 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +00009577 case X86ISD::INC: return "X86ISD::INC";
9578 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +00009579 case X86ISD::OR: return "X86ISD::OR";
9580 case X86ISD::XOR: return "X86ISD::XOR";
9581 case X86ISD::AND: return "X86ISD::AND";
Evan Cheng73f24c92009-03-30 21:36:47 +00009582 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +00009583 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009584 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009585 case X86ISD::PALIGN: return "X86ISD::PALIGN";
9586 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
9587 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
9588 case X86ISD::PSHUFHW_LD: return "X86ISD::PSHUFHW_LD";
9589 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
9590 case X86ISD::PSHUFLW_LD: return "X86ISD::PSHUFLW_LD";
9591 case X86ISD::SHUFPS: return "X86ISD::SHUFPS";
9592 case X86ISD::SHUFPD: return "X86ISD::SHUFPD";
9593 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009594 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00009595 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009596 case X86ISD::MOVHLPD: return "X86ISD::MOVHLPD";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00009597 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
9598 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009599 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
9600 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
9601 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
9602 case X86ISD::MOVSHDUP_LD: return "X86ISD::MOVSHDUP_LD";
9603 case X86ISD::MOVSLDUP_LD: return "X86ISD::MOVSLDUP_LD";
9604 case X86ISD::MOVSD: return "X86ISD::MOVSD";
9605 case X86ISD::MOVSS: return "X86ISD::MOVSS";
9606 case X86ISD::UNPCKLPS: return "X86ISD::UNPCKLPS";
9607 case X86ISD::UNPCKLPD: return "X86ISD::UNPCKLPD";
David Greenefbf05d32011-02-22 23:31:46 +00009608 case X86ISD::VUNPCKLPS: return "X86ISD::VUNPCKLPS";
9609 case X86ISD::VUNPCKLPD: return "X86ISD::VUNPCKLPD";
9610 case X86ISD::VUNPCKLPSY: return "X86ISD::VUNPCKLPSY";
9611 case X86ISD::VUNPCKLPDY: return "X86ISD::VUNPCKLPDY";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009612 case X86ISD::UNPCKHPS: return "X86ISD::UNPCKHPS";
9613 case X86ISD::UNPCKHPD: return "X86ISD::UNPCKHPD";
9614 case X86ISD::PUNPCKLBW: return "X86ISD::PUNPCKLBW";
9615 case X86ISD::PUNPCKLWD: return "X86ISD::PUNPCKLWD";
9616 case X86ISD::PUNPCKLDQ: return "X86ISD::PUNPCKLDQ";
9617 case X86ISD::PUNPCKLQDQ: return "X86ISD::PUNPCKLQDQ";
9618 case X86ISD::PUNPCKHBW: return "X86ISD::PUNPCKHBW";
9619 case X86ISD::PUNPCKHWD: return "X86ISD::PUNPCKHWD";
9620 case X86ISD::PUNPCKHDQ: return "X86ISD::PUNPCKHDQ";
9621 case X86ISD::PUNPCKHQDQ: return "X86ISD::PUNPCKHQDQ";
Dan Gohmand6708ea2009-08-15 01:38:56 +00009622 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +00009623 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +00009624 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Evan Cheng72261582005-12-20 06:22:03 +00009625 }
9626}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00009627
Chris Lattnerc9addb72007-03-30 23:15:24 +00009628// isLegalAddressingMode - Return true if the addressing mode represented
9629// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +00009630bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerc9addb72007-03-30 23:15:24 +00009631 const Type *Ty) const {
9632 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009633 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +00009634 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +00009635
Chris Lattnerc9addb72007-03-30 23:15:24 +00009636 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009637 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +00009638 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00009639
Chris Lattnerc9addb72007-03-30 23:15:24 +00009640 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +00009641 unsigned GVFlags =
9642 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009643
Chris Lattnerdfed4132009-07-10 07:38:24 +00009644 // If a reference to this global requires an extra load, we can't fold it.
9645 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +00009646 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009647
Chris Lattnerdfed4132009-07-10 07:38:24 +00009648 // If BaseGV requires a register for the PIC base, we cannot also have a
9649 // BaseReg specified.
9650 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +00009651 return false;
Evan Cheng52787842007-08-01 23:46:47 +00009652
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009653 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +00009654 if ((M != CodeModel::Small || R != Reloc::Static) &&
9655 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009656 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +00009657 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009658
Chris Lattnerc9addb72007-03-30 23:15:24 +00009659 switch (AM.Scale) {
9660 case 0:
9661 case 1:
9662 case 2:
9663 case 4:
9664 case 8:
9665 // These scales always work.
9666 break;
9667 case 3:
9668 case 5:
9669 case 9:
9670 // These scales are formed with basereg+scalereg. Only accept if there is
9671 // no basereg yet.
9672 if (AM.HasBaseReg)
9673 return false;
9674 break;
9675 default: // Other stuff never works.
9676 return false;
9677 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009678
Chris Lattnerc9addb72007-03-30 23:15:24 +00009679 return true;
9680}
9681
9682
Evan Cheng2bd122c2007-10-26 01:56:11 +00009683bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009684 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +00009685 return false;
Evan Chenge127a732007-10-29 07:57:50 +00009686 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
9687 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00009688 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +00009689 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00009690 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +00009691}
9692
Owen Andersone50ed302009-08-10 22:56:29 +00009693bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +00009694 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009695 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009696 unsigned NumBits1 = VT1.getSizeInBits();
9697 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00009698 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009699 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00009700 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009701}
Evan Cheng2bd122c2007-10-26 01:56:11 +00009702
Dan Gohman97121ba2009-04-08 00:15:30 +00009703bool X86TargetLowering::isZExtFree(const Type *Ty1, const Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00009704 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009705 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00009706}
9707
Owen Andersone50ed302009-08-10 22:56:29 +00009708bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00009709 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00009710 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00009711}
9712
Owen Andersone50ed302009-08-10 22:56:29 +00009713bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +00009714 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +00009715 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +00009716}
9717
Evan Cheng60c07e12006-07-05 22:17:51 +00009718/// isShuffleMaskLegal - Targets can use this to indicate that they only
9719/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
9720/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
9721/// are assumed to be legal.
9722bool
Eric Christopherfd179292009-08-27 18:07:15 +00009723X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +00009724 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +00009725 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +00009726 if (VT.getSizeInBits() == 64)
Eric Christophercff6f852010-04-15 01:40:20 +00009727 return isPALIGNRMask(M, VT, Subtarget->hasSSSE3());
Nate Begeman9008ca62009-04-27 18:41:29 +00009728
Nate Begemana09008b2009-10-19 02:17:23 +00009729 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +00009730 return (VT.getVectorNumElements() == 2 ||
9731 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
9732 isMOVLMask(M, VT) ||
9733 isSHUFPMask(M, VT) ||
9734 isPSHUFDMask(M, VT) ||
9735 isPSHUFHWMask(M, VT) ||
9736 isPSHUFLWMask(M, VT) ||
Nate Begemana09008b2009-10-19 02:17:23 +00009737 isPALIGNRMask(M, VT, Subtarget->hasSSSE3()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +00009738 isUNPCKLMask(M, VT) ||
9739 isUNPCKHMask(M, VT) ||
9740 isUNPCKL_v_undef_Mask(M, VT) ||
9741 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00009742}
9743
Dan Gohman7d8143f2008-04-09 20:09:42 +00009744bool
Nate Begeman5a5ca152009-04-29 05:20:52 +00009745X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +00009746 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00009747 unsigned NumElts = VT.getVectorNumElements();
9748 // FIXME: This collection of masks seems suspect.
9749 if (NumElts == 2)
9750 return true;
9751 if (NumElts == 4 && VT.getSizeInBits() == 128) {
9752 return (isMOVLMask(Mask, VT) ||
9753 isCommutedMOVLMask(Mask, VT, true) ||
9754 isSHUFPMask(Mask, VT) ||
9755 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00009756 }
9757 return false;
9758}
9759
9760//===----------------------------------------------------------------------===//
9761// X86 Scheduler Hooks
9762//===----------------------------------------------------------------------===//
9763
Mon P Wang63307c32008-05-05 19:05:59 +00009764// private utility function
9765MachineBasicBlock *
9766X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
9767 MachineBasicBlock *MBB,
9768 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009769 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009770 unsigned LoadOpc,
9771 unsigned CXchgOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009772 unsigned notOpc,
9773 unsigned EAXreg,
9774 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009775 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00009776 // For the atomic bitwise operator, we generate
9777 // thisMBB:
9778 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00009779 // ld t1 = [bitinstr.addr]
9780 // op t2 = t1, [bitinstr.val]
9781 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00009782 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
9783 // bz newMBB
9784 // fallthrough -->nextMBB
9785 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9786 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009787 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00009788 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009789
Mon P Wang63307c32008-05-05 19:05:59 +00009790 /// First build the CFG
9791 MachineFunction *F = MBB->getParent();
9792 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009793 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9794 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9795 F->insert(MBBIter, newMBB);
9796 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009797
Dan Gohman14152b42010-07-06 20:24:04 +00009798 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9799 nextMBB->splice(nextMBB->begin(), thisMBB,
9800 llvm::next(MachineBasicBlock::iterator(bInstr)),
9801 thisMBB->end());
9802 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009803
Mon P Wang63307c32008-05-05 19:05:59 +00009804 // Update thisMBB to fall through to newMBB
9805 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009806
Mon P Wang63307c32008-05-05 19:05:59 +00009807 // newMBB jumps to itself and fall through to nextMBB
9808 newMBB->addSuccessor(nextMBB);
9809 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009810
Mon P Wang63307c32008-05-05 19:05:59 +00009811 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009812 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009813 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +00009814 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00009815 MachineOperand& destOper = bInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009816 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00009817 int numArgs = bInstr->getNumOperands() - 1;
9818 for (int i=0; i < numArgs; ++i)
9819 argOpers[i] = &bInstr->getOperand(i+1);
9820
9821 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009822 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009823 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00009824
Dale Johannesen140be2d2008-08-19 18:47:28 +00009825 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009826 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00009827 for (int i=0; i <= lastAddrIndx; ++i)
9828 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009829
Dale Johannesen140be2d2008-08-19 18:47:28 +00009830 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009831 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00009832 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009833 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009834 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009835 tt = t1;
9836
Dale Johannesen140be2d2008-08-19 18:47:28 +00009837 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +00009838 assert((argOpers[valArgIndx]->isReg() ||
9839 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00009840 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +00009841 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009842 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00009843 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009844 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009845 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +00009846 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009847
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009848 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +00009849 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009850
Dale Johannesene4d209d2009-02-03 20:21:25 +00009851 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +00009852 for (int i=0; i <= lastAddrIndx; ++i)
9853 (*MIB).addOperand(*argOpers[i]);
9854 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +00009855 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009856 (*MIB).setMemRefs(bInstr->memoperands_begin(),
9857 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +00009858
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009859 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +00009860 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +00009861
Mon P Wang63307c32008-05-05 19:05:59 +00009862 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009863 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00009864
Dan Gohman14152b42010-07-06 20:24:04 +00009865 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00009866 return nextMBB;
9867}
9868
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00009869// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +00009870MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009871X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
9872 MachineBasicBlock *MBB,
9873 unsigned regOpcL,
9874 unsigned regOpcH,
9875 unsigned immOpcL,
9876 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009877 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009878 // For the atomic bitwise operator, we generate
9879 // thisMBB (instructions are in pairs, except cmpxchg8b)
9880 // ld t1,t2 = [bitinstr.addr]
9881 // newMBB:
9882 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
9883 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +00009884 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009885 // mov ECX, EBX <- t5, t6
9886 // mov EAX, EDX <- t1, t2
9887 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
9888 // mov t3, t4 <- EAX, EDX
9889 // bz newMBB
9890 // result in out1, out2
9891 // fallthrough -->nextMBB
9892
9893 const TargetRegisterClass *RC = X86::GR32RegisterClass;
9894 const unsigned LoadOpc = X86::MOV32rm;
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009895 const unsigned NotOpc = X86::NOT32r;
9896 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9897 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
9898 MachineFunction::iterator MBBIter = MBB;
9899 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009900
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009901 /// First build the CFG
9902 MachineFunction *F = MBB->getParent();
9903 MachineBasicBlock *thisMBB = MBB;
9904 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9905 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9906 F->insert(MBBIter, newMBB);
9907 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009908
Dan Gohman14152b42010-07-06 20:24:04 +00009909 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9910 nextMBB->splice(nextMBB->begin(), thisMBB,
9911 llvm::next(MachineBasicBlock::iterator(bInstr)),
9912 thisMBB->end());
9913 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009914
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009915 // Update thisMBB to fall through to newMBB
9916 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009917
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009918 // newMBB jumps to itself and fall through to nextMBB
9919 newMBB->addSuccessor(nextMBB);
9920 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009921
Dale Johannesene4d209d2009-02-03 20:21:25 +00009922 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009923 // Insert instructions into newMBB based on incoming instruction
9924 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009925 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009926 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009927 MachineOperand& dest1Oper = bInstr->getOperand(0);
9928 MachineOperand& dest2Oper = bInstr->getOperand(1);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009929 MachineOperand* argOpers[2 + X86::AddrNumOperands];
9930 for (int i=0; i < 2 + X86::AddrNumOperands; ++i) {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009931 argOpers[i] = &bInstr->getOperand(i+2);
9932
Dan Gohman71ea4e52010-05-14 21:01:44 +00009933 // We use some of the operands multiple times, so conservatively just
9934 // clear any kill flags that might be present.
9935 if (argOpers[i]->isReg() && argOpers[i]->isUse())
9936 argOpers[i]->setIsKill(false);
9937 }
9938
Evan Chengad5b52f2010-01-08 19:14:57 +00009939 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009940 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +00009941
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009942 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009943 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009944 for (int i=0; i <= lastAddrIndx; ++i)
9945 (*MIB).addOperand(*argOpers[i]);
9946 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009947 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +00009948 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +00009949 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009950 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +00009951 MachineOperand newOp3 = *(argOpers[3]);
9952 if (newOp3.isImm())
9953 newOp3.setImm(newOp3.getImm()+4);
9954 else
9955 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009956 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +00009957 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009958
9959 // t3/4 are defined later, at the bottom of the loop
9960 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
9961 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009962 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009963 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009964 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009965 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
9966
Evan Cheng306b4ca2010-01-08 23:41:50 +00009967 // The subsequent operations should be using the destination registers of
9968 //the PHI instructions.
Scott Michelfdc40a02009-02-17 22:15:04 +00009969 if (invSrc) {
Evan Cheng306b4ca2010-01-08 23:41:50 +00009970 t1 = F->getRegInfo().createVirtualRegister(RC);
9971 t2 = F->getRegInfo().createVirtualRegister(RC);
9972 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
9973 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009974 } else {
Evan Cheng306b4ca2010-01-08 23:41:50 +00009975 t1 = dest1Oper.getReg();
9976 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009977 }
9978
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009979 int valArgIndx = lastAddrIndx + 1;
9980 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +00009981 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009982 "invalid operand");
9983 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
9984 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009985 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009986 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009987 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009988 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +00009989 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00009990 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009991 (*MIB).addOperand(*argOpers[valArgIndx]);
9992 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00009993 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009994 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00009995 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009996 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009997 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009998 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009999 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +000010000 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +000010001 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000010002 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010003
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010004 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010005 MIB.addReg(t1);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010006 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010007 MIB.addReg(t2);
10008
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010009 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010010 MIB.addReg(t5);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010011 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010012 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +000010013
Dale Johannesene4d209d2009-02-03 20:21:25 +000010014 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010015 for (int i=0; i <= lastAddrIndx; ++i)
10016 (*MIB).addOperand(*argOpers[i]);
10017
10018 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000010019 (*MIB).setMemRefs(bInstr->memoperands_begin(),
10020 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010021
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010022 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010023 MIB.addReg(X86::EAX);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010024 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010025 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +000010026
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010027 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000010028 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010029
Dan Gohman14152b42010-07-06 20:24:04 +000010030 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010031 return nextMBB;
10032}
10033
10034// private utility function
10035MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +000010036X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
10037 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000010038 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +000010039 // For the atomic min/max operator, we generate
10040 // thisMBB:
10041 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +000010042 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +000010043 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +000010044 // cmp t1, t2
10045 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +000010046 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +000010047 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
10048 // bz newMBB
10049 // fallthrough -->nextMBB
10050 //
10051 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10052 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +000010053 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +000010054 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000010055
Mon P Wang63307c32008-05-05 19:05:59 +000010056 /// First build the CFG
10057 MachineFunction *F = MBB->getParent();
10058 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +000010059 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
10060 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
10061 F->insert(MBBIter, newMBB);
10062 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010063
Dan Gohman14152b42010-07-06 20:24:04 +000010064 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
10065 nextMBB->splice(nextMBB->begin(), thisMBB,
10066 llvm::next(MachineBasicBlock::iterator(mInstr)),
10067 thisMBB->end());
10068 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010069
Mon P Wang63307c32008-05-05 19:05:59 +000010070 // Update thisMBB to fall through to newMBB
10071 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010072
Mon P Wang63307c32008-05-05 19:05:59 +000010073 // newMBB jumps to newMBB and fall through to nextMBB
10074 newMBB->addSuccessor(nextMBB);
10075 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010076
Dale Johannesene4d209d2009-02-03 20:21:25 +000010077 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +000010078 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010079 assert(mInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000010080 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +000010081 MachineOperand& destOper = mInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010082 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +000010083 int numArgs = mInstr->getNumOperands() - 1;
10084 for (int i=0; i < numArgs; ++i)
10085 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +000010086
Mon P Wang63307c32008-05-05 19:05:59 +000010087 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010088 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000010089 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +000010090
Mon P Wangab3e7472008-05-05 22:56:23 +000010091 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010092 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +000010093 for (int i=0; i <= lastAddrIndx; ++i)
10094 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +000010095
Mon P Wang63307c32008-05-05 19:05:59 +000010096 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +000010097 assert((argOpers[valArgIndx]->isReg() ||
10098 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +000010099 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +000010100
10101 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +000010102 if (argOpers[valArgIndx]->isReg())
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010103 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +000010104 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000010105 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +000010106 (*MIB).addOperand(*argOpers[valArgIndx]);
10107
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010108 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +000010109 MIB.addReg(t1);
10110
Dale Johannesene4d209d2009-02-03 20:21:25 +000010111 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +000010112 MIB.addReg(t1);
10113 MIB.addReg(t2);
10114
10115 // Generate movc
10116 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010117 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +000010118 MIB.addReg(t2);
10119 MIB.addReg(t1);
10120
10121 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +000010122 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +000010123 for (int i=0; i <= lastAddrIndx; ++i)
10124 (*MIB).addOperand(*argOpers[i]);
10125 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +000010126 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000010127 (*MIB).setMemRefs(mInstr->memoperands_begin(),
10128 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +000010129
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010130 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +000010131 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +000010132
Mon P Wang63307c32008-05-05 19:05:59 +000010133 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000010134 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000010135
Dan Gohman14152b42010-07-06 20:24:04 +000010136 mInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +000010137 return nextMBB;
10138}
10139
Eric Christopherf83a5de2009-08-27 18:08:16 +000010140// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010141// or XMM0_V32I8 in AVX all of this code can be replaced with that
10142// in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +000010143MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +000010144X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +000010145 unsigned numArgs, bool memArg) const {
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010146 assert((Subtarget->hasSSE42() || Subtarget->hasAVX()) &&
10147 "Target must have SSE4.2 or AVX features enabled");
10148
Eric Christopherb120ab42009-08-18 22:50:32 +000010149 DebugLoc dl = MI->getDebugLoc();
10150 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Eric Christopherb120ab42009-08-18 22:50:32 +000010151 unsigned Opc;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010152 if (!Subtarget->hasAVX()) {
10153 if (memArg)
10154 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
10155 else
10156 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
10157 } else {
10158 if (memArg)
10159 Opc = numArgs == 3 ? X86::VPCMPISTRM128rm : X86::VPCMPESTRM128rm;
10160 else
10161 Opc = numArgs == 3 ? X86::VPCMPISTRM128rr : X86::VPCMPESTRM128rr;
10162 }
Eric Christopherb120ab42009-08-18 22:50:32 +000010163
Eric Christopher41c902f2010-11-30 08:20:21 +000010164 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Eric Christopherb120ab42009-08-18 22:50:32 +000010165 for (unsigned i = 0; i < numArgs; ++i) {
10166 MachineOperand &Op = MI->getOperand(i+1);
Eric Christopherb120ab42009-08-18 22:50:32 +000010167 if (!(Op.isReg() && Op.isImplicit()))
10168 MIB.addOperand(Op);
10169 }
Eric Christopher41c902f2010-11-30 08:20:21 +000010170 BuildMI(*BB, MI, dl, TII->get(X86::MOVAPSrr), MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +000010171 .addReg(X86::XMM0);
10172
Dan Gohman14152b42010-07-06 20:24:04 +000010173 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +000010174 return BB;
10175}
10176
10177MachineBasicBlock *
Eric Christopher228232b2010-11-30 07:20:12 +000010178X86TargetLowering::EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000010179 DebugLoc dl = MI->getDebugLoc();
10180 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010181
Eric Christopher228232b2010-11-30 07:20:12 +000010182 // Address into RAX/EAX, other two args into ECX, EDX.
10183 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
10184 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
10185 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
10186 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +000010187 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010188
Eric Christopher228232b2010-11-30 07:20:12 +000010189 unsigned ValOps = X86::AddrNumOperands;
10190 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
10191 .addReg(MI->getOperand(ValOps).getReg());
10192 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
10193 .addReg(MI->getOperand(ValOps+1).getReg());
10194
10195 // The instruction doesn't actually take any operands though.
10196 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010197
Eric Christopher228232b2010-11-30 07:20:12 +000010198 MI->eraseFromParent(); // The pseudo is gone now.
10199 return BB;
10200}
10201
10202MachineBasicBlock *
10203X86TargetLowering::EmitMwait(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000010204 DebugLoc dl = MI->getDebugLoc();
10205 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010206
Eric Christopher228232b2010-11-30 07:20:12 +000010207 // First arg in ECX, the second in EAX.
10208 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
10209 .addReg(MI->getOperand(0).getReg());
10210 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EAX)
10211 .addReg(MI->getOperand(1).getReg());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010212
Eric Christopher228232b2010-11-30 07:20:12 +000010213 // The instruction doesn't actually take any operands though.
10214 BuildMI(*BB, MI, dl, TII->get(X86::MWAITrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010215
Eric Christopher228232b2010-11-30 07:20:12 +000010216 MI->eraseFromParent(); // The pseudo is gone now.
10217 return BB;
10218}
10219
10220MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +000010221X86TargetLowering::EmitVAARG64WithCustomInserter(
10222 MachineInstr *MI,
10223 MachineBasicBlock *MBB) const {
10224 // Emit va_arg instruction on X86-64.
10225
10226 // Operands to this pseudo-instruction:
10227 // 0 ) Output : destination address (reg)
10228 // 1-5) Input : va_list address (addr, i64mem)
10229 // 6 ) ArgSize : Size (in bytes) of vararg type
10230 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
10231 // 8 ) Align : Alignment of type
10232 // 9 ) EFLAGS (implicit-def)
10233
10234 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
10235 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
10236
10237 unsigned DestReg = MI->getOperand(0).getReg();
10238 MachineOperand &Base = MI->getOperand(1);
10239 MachineOperand &Scale = MI->getOperand(2);
10240 MachineOperand &Index = MI->getOperand(3);
10241 MachineOperand &Disp = MI->getOperand(4);
10242 MachineOperand &Segment = MI->getOperand(5);
10243 unsigned ArgSize = MI->getOperand(6).getImm();
10244 unsigned ArgMode = MI->getOperand(7).getImm();
10245 unsigned Align = MI->getOperand(8).getImm();
10246
10247 // Memory Reference
10248 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
10249 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
10250 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
10251
10252 // Machine Information
10253 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10254 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
10255 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
10256 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
10257 DebugLoc DL = MI->getDebugLoc();
10258
10259 // struct va_list {
10260 // i32 gp_offset
10261 // i32 fp_offset
10262 // i64 overflow_area (address)
10263 // i64 reg_save_area (address)
10264 // }
10265 // sizeof(va_list) = 24
10266 // alignment(va_list) = 8
10267
10268 unsigned TotalNumIntRegs = 6;
10269 unsigned TotalNumXMMRegs = 8;
10270 bool UseGPOffset = (ArgMode == 1);
10271 bool UseFPOffset = (ArgMode == 2);
10272 unsigned MaxOffset = TotalNumIntRegs * 8 +
10273 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
10274
10275 /* Align ArgSize to a multiple of 8 */
10276 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
10277 bool NeedsAlign = (Align > 8);
10278
10279 MachineBasicBlock *thisMBB = MBB;
10280 MachineBasicBlock *overflowMBB;
10281 MachineBasicBlock *offsetMBB;
10282 MachineBasicBlock *endMBB;
10283
10284 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
10285 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
10286 unsigned OffsetReg = 0;
10287
10288 if (!UseGPOffset && !UseFPOffset) {
10289 // If we only pull from the overflow region, we don't create a branch.
10290 // We don't need to alter control flow.
10291 OffsetDestReg = 0; // unused
10292 OverflowDestReg = DestReg;
10293
10294 offsetMBB = NULL;
10295 overflowMBB = thisMBB;
10296 endMBB = thisMBB;
10297 } else {
10298 // First emit code to check if gp_offset (or fp_offset) is below the bound.
10299 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
10300 // If not, pull from overflow_area. (branch to overflowMBB)
10301 //
10302 // thisMBB
10303 // | .
10304 // | .
10305 // offsetMBB overflowMBB
10306 // | .
10307 // | .
10308 // endMBB
10309
10310 // Registers for the PHI in endMBB
10311 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
10312 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
10313
10314 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
10315 MachineFunction *MF = MBB->getParent();
10316 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
10317 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
10318 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
10319
10320 MachineFunction::iterator MBBIter = MBB;
10321 ++MBBIter;
10322
10323 // Insert the new basic blocks
10324 MF->insert(MBBIter, offsetMBB);
10325 MF->insert(MBBIter, overflowMBB);
10326 MF->insert(MBBIter, endMBB);
10327
10328 // Transfer the remainder of MBB and its successor edges to endMBB.
10329 endMBB->splice(endMBB->begin(), thisMBB,
10330 llvm::next(MachineBasicBlock::iterator(MI)),
10331 thisMBB->end());
10332 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
10333
10334 // Make offsetMBB and overflowMBB successors of thisMBB
10335 thisMBB->addSuccessor(offsetMBB);
10336 thisMBB->addSuccessor(overflowMBB);
10337
10338 // endMBB is a successor of both offsetMBB and overflowMBB
10339 offsetMBB->addSuccessor(endMBB);
10340 overflowMBB->addSuccessor(endMBB);
10341
10342 // Load the offset value into a register
10343 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
10344 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
10345 .addOperand(Base)
10346 .addOperand(Scale)
10347 .addOperand(Index)
10348 .addDisp(Disp, UseFPOffset ? 4 : 0)
10349 .addOperand(Segment)
10350 .setMemRefs(MMOBegin, MMOEnd);
10351
10352 // Check if there is enough room left to pull this argument.
10353 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
10354 .addReg(OffsetReg)
10355 .addImm(MaxOffset + 8 - ArgSizeA8);
10356
10357 // Branch to "overflowMBB" if offset >= max
10358 // Fall through to "offsetMBB" otherwise
10359 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
10360 .addMBB(overflowMBB);
10361 }
10362
10363 // In offsetMBB, emit code to use the reg_save_area.
10364 if (offsetMBB) {
10365 assert(OffsetReg != 0);
10366
10367 // Read the reg_save_area address.
10368 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
10369 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
10370 .addOperand(Base)
10371 .addOperand(Scale)
10372 .addOperand(Index)
10373 .addDisp(Disp, 16)
10374 .addOperand(Segment)
10375 .setMemRefs(MMOBegin, MMOEnd);
10376
10377 // Zero-extend the offset
10378 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
10379 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
10380 .addImm(0)
10381 .addReg(OffsetReg)
10382 .addImm(X86::sub_32bit);
10383
10384 // Add the offset to the reg_save_area to get the final address.
10385 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
10386 .addReg(OffsetReg64)
10387 .addReg(RegSaveReg);
10388
10389 // Compute the offset for the next argument
10390 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
10391 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
10392 .addReg(OffsetReg)
10393 .addImm(UseFPOffset ? 16 : 8);
10394
10395 // Store it back into the va_list.
10396 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
10397 .addOperand(Base)
10398 .addOperand(Scale)
10399 .addOperand(Index)
10400 .addDisp(Disp, UseFPOffset ? 4 : 0)
10401 .addOperand(Segment)
10402 .addReg(NextOffsetReg)
10403 .setMemRefs(MMOBegin, MMOEnd);
10404
10405 // Jump to endMBB
10406 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
10407 .addMBB(endMBB);
10408 }
10409
10410 //
10411 // Emit code to use overflow area
10412 //
10413
10414 // Load the overflow_area address into a register.
10415 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
10416 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
10417 .addOperand(Base)
10418 .addOperand(Scale)
10419 .addOperand(Index)
10420 .addDisp(Disp, 8)
10421 .addOperand(Segment)
10422 .setMemRefs(MMOBegin, MMOEnd);
10423
10424 // If we need to align it, do so. Otherwise, just copy the address
10425 // to OverflowDestReg.
10426 if (NeedsAlign) {
10427 // Align the overflow address
10428 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
10429 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
10430
10431 // aligned_addr = (addr + (align-1)) & ~(align-1)
10432 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
10433 .addReg(OverflowAddrReg)
10434 .addImm(Align-1);
10435
10436 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
10437 .addReg(TmpReg)
10438 .addImm(~(uint64_t)(Align-1));
10439 } else {
10440 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
10441 .addReg(OverflowAddrReg);
10442 }
10443
10444 // Compute the next overflow address after this argument.
10445 // (the overflow address should be kept 8-byte aligned)
10446 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
10447 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
10448 .addReg(OverflowDestReg)
10449 .addImm(ArgSizeA8);
10450
10451 // Store the new overflow address.
10452 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
10453 .addOperand(Base)
10454 .addOperand(Scale)
10455 .addOperand(Index)
10456 .addDisp(Disp, 8)
10457 .addOperand(Segment)
10458 .addReg(NextAddrReg)
10459 .setMemRefs(MMOBegin, MMOEnd);
10460
10461 // If we branched, emit the PHI to the front of endMBB.
10462 if (offsetMBB) {
10463 BuildMI(*endMBB, endMBB->begin(), DL,
10464 TII->get(X86::PHI), DestReg)
10465 .addReg(OffsetDestReg).addMBB(offsetMBB)
10466 .addReg(OverflowDestReg).addMBB(overflowMBB);
10467 }
10468
10469 // Erase the pseudo instruction
10470 MI->eraseFromParent();
10471
10472 return endMBB;
10473}
10474
10475MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +000010476X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
10477 MachineInstr *MI,
10478 MachineBasicBlock *MBB) const {
10479 // Emit code to save XMM registers to the stack. The ABI says that the
10480 // number of registers to save is given in %al, so it's theoretically
10481 // possible to do an indirect jump trick to avoid saving all of them,
10482 // however this code takes a simpler approach and just executes all
10483 // of the stores if %al is non-zero. It's less code, and it's probably
10484 // easier on the hardware branch predictor, and stores aren't all that
10485 // expensive anyway.
10486
10487 // Create the new basic blocks. One block contains all the XMM stores,
10488 // and one block is the final destination regardless of whether any
10489 // stores were performed.
10490 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
10491 MachineFunction *F = MBB->getParent();
10492 MachineFunction::iterator MBBIter = MBB;
10493 ++MBBIter;
10494 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
10495 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
10496 F->insert(MBBIter, XMMSaveMBB);
10497 F->insert(MBBIter, EndMBB);
10498
Dan Gohman14152b42010-07-06 20:24:04 +000010499 // Transfer the remainder of MBB and its successor edges to EndMBB.
10500 EndMBB->splice(EndMBB->begin(), MBB,
10501 llvm::next(MachineBasicBlock::iterator(MI)),
10502 MBB->end());
10503 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
10504
Dan Gohmand6708ea2009-08-15 01:38:56 +000010505 // The original block will now fall through to the XMM save block.
10506 MBB->addSuccessor(XMMSaveMBB);
10507 // The XMMSaveMBB will fall through to the end block.
10508 XMMSaveMBB->addSuccessor(EndMBB);
10509
10510 // Now add the instructions.
10511 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10512 DebugLoc DL = MI->getDebugLoc();
10513
10514 unsigned CountReg = MI->getOperand(0).getReg();
10515 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
10516 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
10517
10518 if (!Subtarget->isTargetWin64()) {
10519 // If %al is 0, branch around the XMM save block.
10520 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +000010521 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010522 MBB->addSuccessor(EndMBB);
10523 }
10524
10525 // In the XMM save block, save all the XMM argument registers.
10526 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
10527 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +000010528 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +000010529 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +000010530 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +000010531 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +000010532 /*Size=*/16, /*Align=*/16);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010533 BuildMI(XMMSaveMBB, DL, TII->get(X86::MOVAPSmr))
10534 .addFrameIndex(RegSaveFrameIndex)
10535 .addImm(/*Scale=*/1)
10536 .addReg(/*IndexReg=*/0)
10537 .addImm(/*Disp=*/Offset)
10538 .addReg(/*Segment=*/0)
10539 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +000010540 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010541 }
10542
Dan Gohman14152b42010-07-06 20:24:04 +000010543 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +000010544
10545 return EndMBB;
10546}
Mon P Wang63307c32008-05-05 19:05:59 +000010547
Evan Cheng60c07e12006-07-05 22:17:51 +000010548MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +000010549X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010550 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +000010551 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10552 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +000010553
Chris Lattner52600972009-09-02 05:57:00 +000010554 // To "insert" a SELECT_CC instruction, we actually have to insert the
10555 // diamond control-flow pattern. The incoming instruction knows the
10556 // destination vreg to set, the condition code register to branch on, the
10557 // true/false values to select between, and a branch opcode to use.
10558 const BasicBlock *LLVM_BB = BB->getBasicBlock();
10559 MachineFunction::iterator It = BB;
10560 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +000010561
Chris Lattner52600972009-09-02 05:57:00 +000010562 // thisMBB:
10563 // ...
10564 // TrueVal = ...
10565 // cmpTY ccX, r1, r2
10566 // bCC copy1MBB
10567 // fallthrough --> copy0MBB
10568 MachineBasicBlock *thisMBB = BB;
10569 MachineFunction *F = BB->getParent();
10570 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
10571 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +000010572 F->insert(It, copy0MBB);
10573 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +000010574
Bill Wendling730c07e2010-06-25 20:48:10 +000010575 // If the EFLAGS register isn't dead in the terminator, then claim that it's
10576 // live into the sink and copy blocks.
10577 const MachineFunction *MF = BB->getParent();
10578 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
10579 BitVector ReservedRegs = TRI->getReservedRegs(*MF);
Bill Wendling730c07e2010-06-25 20:48:10 +000010580
Dan Gohman14152b42010-07-06 20:24:04 +000010581 for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) {
10582 const MachineOperand &MO = MI->getOperand(I);
10583 if (!MO.isReg() || !MO.isUse() || MO.isKill()) continue;
Bill Wendling730c07e2010-06-25 20:48:10 +000010584 unsigned Reg = MO.getReg();
10585 if (Reg != X86::EFLAGS) continue;
10586 copy0MBB->addLiveIn(Reg);
10587 sinkMBB->addLiveIn(Reg);
10588 }
10589
Dan Gohman14152b42010-07-06 20:24:04 +000010590 // Transfer the remainder of BB and its successor edges to sinkMBB.
10591 sinkMBB->splice(sinkMBB->begin(), BB,
10592 llvm::next(MachineBasicBlock::iterator(MI)),
10593 BB->end());
10594 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
10595
10596 // Add the true and fallthrough blocks as its successors.
10597 BB->addSuccessor(copy0MBB);
10598 BB->addSuccessor(sinkMBB);
10599
10600 // Create the conditional branch instruction.
10601 unsigned Opc =
10602 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
10603 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
10604
Chris Lattner52600972009-09-02 05:57:00 +000010605 // copy0MBB:
10606 // %FalseValue = ...
10607 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000010608 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000010609
Chris Lattner52600972009-09-02 05:57:00 +000010610 // sinkMBB:
10611 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
10612 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000010613 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
10614 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000010615 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
10616 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
10617
Dan Gohman14152b42010-07-06 20:24:04 +000010618 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000010619 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000010620}
10621
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010622MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010623X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010624 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010625 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10626 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010627
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000010628 assert(!Subtarget->isTargetEnvMacho());
10629
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010630 // The lowering is pretty easy: we're just emitting the call to _alloca. The
10631 // non-trivial part is impdef of ESP.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010632
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000010633 if (Subtarget->isTargetWin64()) {
10634 if (Subtarget->isTargetCygMing()) {
10635 // ___chkstk(Mingw64):
10636 // Clobbers R10, R11, RAX and EFLAGS.
10637 // Updates RSP.
10638 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
10639 .addExternalSymbol("___chkstk")
10640 .addReg(X86::RAX, RegState::Implicit)
10641 .addReg(X86::RSP, RegState::Implicit)
10642 .addReg(X86::RAX, RegState::Define | RegState::Implicit)
10643 .addReg(X86::RSP, RegState::Define | RegState::Implicit)
10644 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
10645 } else {
10646 // __chkstk(MSVCRT): does not update stack pointer.
10647 // Clobbers R10, R11 and EFLAGS.
10648 // FIXME: RAX(allocated size) might be reused and not killed.
10649 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
10650 .addExternalSymbol("__chkstk")
10651 .addReg(X86::RAX, RegState::Implicit)
10652 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
10653 // RAX has the offset to subtracted from RSP.
10654 BuildMI(*BB, MI, DL, TII->get(X86::SUB64rr), X86::RSP)
10655 .addReg(X86::RSP)
10656 .addReg(X86::RAX);
10657 }
10658 } else {
10659 const char *StackProbeSymbol =
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010660 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
10661
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000010662 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
10663 .addExternalSymbol(StackProbeSymbol)
10664 .addReg(X86::EAX, RegState::Implicit)
10665 .addReg(X86::ESP, RegState::Implicit)
10666 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
10667 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
10668 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
10669 }
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010670
Dan Gohman14152b42010-07-06 20:24:04 +000010671 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010672 return BB;
10673}
Chris Lattner52600972009-09-02 05:57:00 +000010674
10675MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000010676X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
10677 MachineBasicBlock *BB) const {
10678 // This is pretty easy. We're taking the value that we received from
10679 // our load from the relocation, sticking it in either RDI (x86-64)
10680 // or EAX and doing an indirect call. The return value will then
10681 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000010682 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000010683 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000010684 DebugLoc DL = MI->getDebugLoc();
10685 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000010686
10687 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000010688 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000010689
Eric Christopher30ef0e52010-06-03 04:07:48 +000010690 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000010691 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10692 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000010693 .addReg(X86::RIP)
10694 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010695 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000010696 MI->getOperand(3).getTargetFlags())
10697 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000010698 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000010699 addDirectMem(MIB, X86::RDI);
Eric Christopher61025492010-06-15 23:08:42 +000010700 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000010701 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10702 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000010703 .addReg(0)
10704 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010705 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000010706 MI->getOperand(3).getTargetFlags())
10707 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000010708 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000010709 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010710 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000010711 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10712 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000010713 .addReg(TII->getGlobalBaseReg(F))
10714 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010715 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000010716 MI->getOperand(3).getTargetFlags())
10717 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000010718 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000010719 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010720 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000010721
Dan Gohman14152b42010-07-06 20:24:04 +000010722 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000010723 return BB;
10724}
10725
10726MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000010727X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010728 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000010729 switch (MI->getOpcode()) {
10730 default: assert(false && "Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000010731 case X86::TAILJMPd64:
10732 case X86::TAILJMPr64:
10733 case X86::TAILJMPm64:
10734 assert(!"TAILJMP64 would not be touched here.");
10735 case X86::TCRETURNdi64:
10736 case X86::TCRETURNri64:
10737 case X86::TCRETURNmi64:
10738 // Defs of TCRETURNxx64 has Win64's callee-saved registers, as subset.
10739 // On AMD64, additional defs should be added before register allocation.
10740 if (!Subtarget->isTargetWin64()) {
10741 MI->addRegisterDefined(X86::RSI);
10742 MI->addRegisterDefined(X86::RDI);
10743 MI->addRegisterDefined(X86::XMM6);
10744 MI->addRegisterDefined(X86::XMM7);
10745 MI->addRegisterDefined(X86::XMM8);
10746 MI->addRegisterDefined(X86::XMM9);
10747 MI->addRegisterDefined(X86::XMM10);
10748 MI->addRegisterDefined(X86::XMM11);
10749 MI->addRegisterDefined(X86::XMM12);
10750 MI->addRegisterDefined(X86::XMM13);
10751 MI->addRegisterDefined(X86::XMM14);
10752 MI->addRegisterDefined(X86::XMM15);
10753 }
10754 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010755 case X86::WIN_ALLOCA:
10756 return EmitLoweredWinAlloca(MI, BB);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010757 case X86::TLSCall_32:
10758 case X86::TLSCall_64:
10759 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000010760 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000010761 case X86::CMOV_FR32:
10762 case X86::CMOV_FR64:
10763 case X86::CMOV_V4F32:
10764 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000010765 case X86::CMOV_V2I64:
Chris Lattner314a1132010-03-14 18:31:44 +000010766 case X86::CMOV_GR16:
10767 case X86::CMOV_GR32:
10768 case X86::CMOV_RFP32:
10769 case X86::CMOV_RFP64:
10770 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010771 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000010772
Dale Johannesen849f2142007-07-03 00:53:03 +000010773 case X86::FP32_TO_INT16_IN_MEM:
10774 case X86::FP32_TO_INT32_IN_MEM:
10775 case X86::FP32_TO_INT64_IN_MEM:
10776 case X86::FP64_TO_INT16_IN_MEM:
10777 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000010778 case X86::FP64_TO_INT64_IN_MEM:
10779 case X86::FP80_TO_INT16_IN_MEM:
10780 case X86::FP80_TO_INT32_IN_MEM:
10781 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000010782 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10783 DebugLoc DL = MI->getDebugLoc();
10784
Evan Cheng60c07e12006-07-05 22:17:51 +000010785 // Change the floating point control register to use "round towards zero"
10786 // mode when truncating to an integer value.
10787 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000010788 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000010789 addFrameReference(BuildMI(*BB, MI, DL,
10790 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010791
10792 // Load the old value of the high byte of the control word...
10793 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +000010794 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohman14152b42010-07-06 20:24:04 +000010795 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000010796 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010797
10798 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000010799 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000010800 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000010801
10802 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000010803 addFrameReference(BuildMI(*BB, MI, DL,
10804 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010805
10806 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000010807 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000010808 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000010809
10810 // Get the X86 opcode to use.
10811 unsigned Opc;
10812 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010813 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000010814 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
10815 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
10816 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
10817 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
10818 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
10819 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000010820 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
10821 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
10822 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000010823 }
10824
10825 X86AddressMode AM;
10826 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000010827 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000010828 AM.BaseType = X86AddressMode::RegBase;
10829 AM.Base.Reg = Op.getReg();
10830 } else {
10831 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000010832 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000010833 }
10834 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000010835 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000010836 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010837 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000010838 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000010839 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010840 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000010841 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000010842 AM.GV = Op.getGlobal();
10843 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000010844 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010845 }
Dan Gohman14152b42010-07-06 20:24:04 +000010846 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010847 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000010848
10849 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000010850 addFrameReference(BuildMI(*BB, MI, DL,
10851 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010852
Dan Gohman14152b42010-07-06 20:24:04 +000010853 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000010854 return BB;
10855 }
Eric Christopherb120ab42009-08-18 22:50:32 +000010856 // String/text processing lowering.
10857 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010858 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000010859 return EmitPCMP(MI, BB, 3, false /* in-mem */);
10860 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010861 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000010862 return EmitPCMP(MI, BB, 3, true /* in-mem */);
10863 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010864 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000010865 return EmitPCMP(MI, BB, 5, false /* in mem */);
10866 case X86::PCMPESTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010867 case X86::VPCMPESTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000010868 return EmitPCMP(MI, BB, 5, true /* in mem */);
10869
Eric Christopher228232b2010-11-30 07:20:12 +000010870 // Thread synchronization.
10871 case X86::MONITOR:
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010872 return EmitMonitor(MI, BB);
Eric Christopher228232b2010-11-30 07:20:12 +000010873 case X86::MWAIT:
10874 return EmitMwait(MI, BB);
10875
Eric Christopherb120ab42009-08-18 22:50:32 +000010876 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +000010877 case X86::ATOMAND32:
10878 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010879 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010880 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010881 X86::NOT32r, X86::EAX,
10882 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000010883 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +000010884 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
10885 X86::OR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010886 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010887 X86::NOT32r, X86::EAX,
10888 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000010889 case X86::ATOMXOR32:
10890 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010891 X86::XOR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010892 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010893 X86::NOT32r, X86::EAX,
10894 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010895 case X86::ATOMNAND32:
10896 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010897 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010898 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010899 X86::NOT32r, X86::EAX,
10900 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +000010901 case X86::ATOMMIN32:
10902 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
10903 case X86::ATOMMAX32:
10904 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
10905 case X86::ATOMUMIN32:
10906 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
10907 case X86::ATOMUMAX32:
10908 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +000010909
10910 case X86::ATOMAND16:
10911 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
10912 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010913 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010914 X86::NOT16r, X86::AX,
10915 X86::GR16RegisterClass);
10916 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +000010917 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010918 X86::OR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010919 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010920 X86::NOT16r, X86::AX,
10921 X86::GR16RegisterClass);
10922 case X86::ATOMXOR16:
10923 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
10924 X86::XOR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010925 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010926 X86::NOT16r, X86::AX,
10927 X86::GR16RegisterClass);
10928 case X86::ATOMNAND16:
10929 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
10930 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010931 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010932 X86::NOT16r, X86::AX,
10933 X86::GR16RegisterClass, true);
10934 case X86::ATOMMIN16:
10935 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
10936 case X86::ATOMMAX16:
10937 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
10938 case X86::ATOMUMIN16:
10939 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
10940 case X86::ATOMUMAX16:
10941 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
10942
10943 case X86::ATOMAND8:
10944 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
10945 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010946 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010947 X86::NOT8r, X86::AL,
10948 X86::GR8RegisterClass);
10949 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +000010950 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010951 X86::OR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010952 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010953 X86::NOT8r, X86::AL,
10954 X86::GR8RegisterClass);
10955 case X86::ATOMXOR8:
10956 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
10957 X86::XOR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010958 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010959 X86::NOT8r, X86::AL,
10960 X86::GR8RegisterClass);
10961 case X86::ATOMNAND8:
10962 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
10963 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010964 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010965 X86::NOT8r, X86::AL,
10966 X86::GR8RegisterClass, true);
10967 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010968 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +000010969 case X86::ATOMAND64:
10970 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010971 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010972 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010973 X86::NOT64r, X86::RAX,
10974 X86::GR64RegisterClass);
10975 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +000010976 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
10977 X86::OR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010978 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010979 X86::NOT64r, X86::RAX,
10980 X86::GR64RegisterClass);
10981 case X86::ATOMXOR64:
10982 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010983 X86::XOR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010984 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010985 X86::NOT64r, X86::RAX,
10986 X86::GR64RegisterClass);
10987 case X86::ATOMNAND64:
10988 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
10989 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010990 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010991 X86::NOT64r, X86::RAX,
10992 X86::GR64RegisterClass, true);
10993 case X86::ATOMMIN64:
10994 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
10995 case X86::ATOMMAX64:
10996 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
10997 case X86::ATOMUMIN64:
10998 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
10999 case X86::ATOMUMAX64:
11000 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011001
11002 // This group does 64-bit operations on a 32-bit host.
11003 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000011004 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011005 X86::AND32rr, X86::AND32rr,
11006 X86::AND32ri, X86::AND32ri,
11007 false);
11008 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000011009 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011010 X86::OR32rr, X86::OR32rr,
11011 X86::OR32ri, X86::OR32ri,
11012 false);
11013 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000011014 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011015 X86::XOR32rr, X86::XOR32rr,
11016 X86::XOR32ri, X86::XOR32ri,
11017 false);
11018 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000011019 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011020 X86::AND32rr, X86::AND32rr,
11021 X86::AND32ri, X86::AND32ri,
11022 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011023 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000011024 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011025 X86::ADD32rr, X86::ADC32rr,
11026 X86::ADD32ri, X86::ADC32ri,
11027 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011028 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000011029 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011030 X86::SUB32rr, X86::SBB32rr,
11031 X86::SUB32ri, X86::SBB32ri,
11032 false);
Dale Johannesen880ae362008-10-03 22:25:52 +000011033 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000011034 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +000011035 X86::MOV32rr, X86::MOV32rr,
11036 X86::MOV32ri, X86::MOV32ri,
11037 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +000011038 case X86::VASTART_SAVE_XMM_REGS:
11039 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000011040
11041 case X86::VAARG_64:
11042 return EmitVAARG64WithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000011043 }
11044}
11045
11046//===----------------------------------------------------------------------===//
11047// X86 Optimization Hooks
11048//===----------------------------------------------------------------------===//
11049
Dan Gohman475871a2008-07-27 21:46:04 +000011050void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +000011051 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000011052 APInt &KnownZero,
11053 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000011054 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000011055 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +000011056 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000011057 assert((Opc >= ISD::BUILTIN_OP_END ||
11058 Opc == ISD::INTRINSIC_WO_CHAIN ||
11059 Opc == ISD::INTRINSIC_W_CHAIN ||
11060 Opc == ISD::INTRINSIC_VOID) &&
11061 "Should use MaskedValueIsZero if you don't know whether Op"
11062 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000011063
Dan Gohmanf4f92f52008-02-13 23:07:24 +000011064 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000011065 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000011066 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000011067 case X86ISD::ADD:
11068 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000011069 case X86ISD::ADC:
11070 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000011071 case X86ISD::SMUL:
11072 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000011073 case X86ISD::INC:
11074 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000011075 case X86ISD::OR:
11076 case X86ISD::XOR:
11077 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000011078 // These nodes' second result is a boolean.
11079 if (Op.getResNo() == 0)
11080 break;
11081 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011082 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000011083 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
11084 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000011085 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +000011086 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000011087}
Chris Lattner259e97c2006-01-31 19:43:35 +000011088
Owen Andersonbc146b02010-09-21 20:42:50 +000011089unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
11090 unsigned Depth) const {
11091 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
11092 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
11093 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000011094
Owen Andersonbc146b02010-09-21 20:42:50 +000011095 // Fallback case.
11096 return 1;
11097}
11098
Evan Cheng206ee9d2006-07-07 08:33:52 +000011099/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000011100/// node is a GlobalAddress + offset.
11101bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000011102 const GlobalValue* &GA,
11103 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000011104 if (N->getOpcode() == X86ISD::Wrapper) {
11105 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000011106 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000011107 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000011108 return true;
11109 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000011110 }
Evan Chengad4196b2008-05-12 19:56:52 +000011111 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000011112}
11113
Evan Cheng206ee9d2006-07-07 08:33:52 +000011114/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
11115/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
11116/// if the load addresses are consecutive, non-overlapping, and in the right
Nate Begemanfdea31a2010-03-24 20:49:50 +000011117/// order.
Dan Gohman475871a2008-07-27 21:46:04 +000011118static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Mon P Wanga0fd0d52010-12-19 23:55:53 +000011119 TargetLowering::DAGCombinerInfo &DCI) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000011120 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000011121 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000011122
Eli Friedman7a5e5552009-06-07 06:52:44 +000011123 if (VT.getSizeInBits() != 128)
11124 return SDValue();
11125
Mon P Wanga0fd0d52010-12-19 23:55:53 +000011126 // Don't create instructions with illegal types after legalize types has run.
11127 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
11128 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
11129 return SDValue();
11130
Nate Begemanfdea31a2010-03-24 20:49:50 +000011131 SmallVector<SDValue, 16> Elts;
11132 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000011133 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000011134
Nate Begemanfdea31a2010-03-24 20:49:50 +000011135 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000011136}
Evan Chengd880b972008-05-09 21:53:03 +000011137
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000011138/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
11139/// generation and convert it from being a bunch of shuffles and extracts
11140/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000011141static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
11142 const TargetLowering &TLI) {
11143 SDValue InputVector = N->getOperand(0);
11144
11145 // Only operate on vectors of 4 elements, where the alternative shuffling
11146 // gets to be more expensive.
11147 if (InputVector.getValueType() != MVT::v4i32)
11148 return SDValue();
11149
11150 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
11151 // single use which is a sign-extend or zero-extend, and all elements are
11152 // used.
11153 SmallVector<SDNode *, 4> Uses;
11154 unsigned ExtractedElements = 0;
11155 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
11156 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
11157 if (UI.getUse().getResNo() != InputVector.getResNo())
11158 return SDValue();
11159
11160 SDNode *Extract = *UI;
11161 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
11162 return SDValue();
11163
11164 if (Extract->getValueType(0) != MVT::i32)
11165 return SDValue();
11166 if (!Extract->hasOneUse())
11167 return SDValue();
11168 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
11169 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
11170 return SDValue();
11171 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
11172 return SDValue();
11173
11174 // Record which element was extracted.
11175 ExtractedElements |=
11176 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
11177
11178 Uses.push_back(Extract);
11179 }
11180
11181 // If not all the elements were used, this may not be worthwhile.
11182 if (ExtractedElements != 15)
11183 return SDValue();
11184
11185 // Ok, we've now decided to do the transformation.
11186 DebugLoc dl = InputVector.getDebugLoc();
11187
11188 // Store the value to a temporary stack slot.
11189 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000011190 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
11191 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000011192
11193 // Replace each use (extract) with a load of the appropriate element.
11194 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
11195 UE = Uses.end(); UI != UE; ++UI) {
11196 SDNode *Extract = *UI;
11197
Nadav Rotem86694292011-05-17 08:31:57 +000011198 // cOMpute the element's address.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000011199 SDValue Idx = Extract->getOperand(1);
11200 unsigned EltSize =
11201 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
11202 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
11203 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
11204
Nadav Rotem86694292011-05-17 08:31:57 +000011205 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
Chris Lattner51abfe42010-09-21 06:02:19 +000011206 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000011207
11208 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000011209 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000011210 ScalarAddr, MachinePointerInfo(),
11211 false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000011212
11213 // Replace the exact with the load.
11214 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
11215 }
11216
11217 // The replacement was made in place; don't return anything.
11218 return SDValue();
11219}
11220
Chris Lattner83e6c992006-10-04 06:57:07 +000011221/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000011222static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +000011223 const X86Subtarget *Subtarget) {
11224 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000011225 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000011226 // Get the LHS/RHS of the select.
11227 SDValue LHS = N->getOperand(1);
11228 SDValue RHS = N->getOperand(2);
Eric Christopherfd179292009-08-27 18:07:15 +000011229
Dan Gohman670e5392009-09-21 18:03:22 +000011230 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000011231 // instructions match the semantics of the common C idiom x<y?x:y but not
11232 // x<=y?x:y, because of how they handle negative zero (which can be
11233 // ignored in unsafe-math mode).
Chris Lattner83e6c992006-10-04 06:57:07 +000011234 if (Subtarget->hasSSE2() &&
Owen Anderson825b72b2009-08-11 20:47:22 +000011235 (LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64) &&
Chris Lattner47b4ce82009-03-11 05:48:52 +000011236 Cond.getOpcode() == ISD::SETCC) {
11237 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011238
Chris Lattner47b4ce82009-03-11 05:48:52 +000011239 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000011240 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000011241 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
11242 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000011243 switch (CC) {
11244 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000011245 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000011246 // Converting this to a min would handle NaNs incorrectly, and swapping
11247 // the operands would cause it to handle comparisons between positive
11248 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000011249 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000011250 if (!UnsafeFPMath &&
11251 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
11252 break;
11253 std::swap(LHS, RHS);
11254 }
Dan Gohman670e5392009-09-21 18:03:22 +000011255 Opcode = X86ISD::FMIN;
11256 break;
11257 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000011258 // Converting this to a min would handle comparisons between positive
11259 // and negative zero incorrectly.
11260 if (!UnsafeFPMath &&
11261 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
11262 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011263 Opcode = X86ISD::FMIN;
11264 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000011265 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000011266 // Converting this to a min would handle both negative zeros and NaNs
11267 // incorrectly, but we can swap the operands to fix both.
11268 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000011269 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011270 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000011271 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011272 Opcode = X86ISD::FMIN;
11273 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011274
Dan Gohman670e5392009-09-21 18:03:22 +000011275 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000011276 // Converting this to a max would handle comparisons between positive
11277 // and negative zero incorrectly.
11278 if (!UnsafeFPMath &&
11279 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(LHS))
11280 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011281 Opcode = X86ISD::FMAX;
11282 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000011283 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000011284 // Converting this to a max would handle NaNs incorrectly, and swapping
11285 // the operands would cause it to handle comparisons between positive
11286 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000011287 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000011288 if (!UnsafeFPMath &&
11289 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
11290 break;
11291 std::swap(LHS, RHS);
11292 }
Dan Gohman670e5392009-09-21 18:03:22 +000011293 Opcode = X86ISD::FMAX;
11294 break;
11295 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000011296 // Converting this to a max would handle both negative zeros and NaNs
11297 // incorrectly, but we can swap the operands to fix both.
11298 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000011299 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011300 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011301 case ISD::SETGE:
11302 Opcode = X86ISD::FMAX;
11303 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000011304 }
Dan Gohman670e5392009-09-21 18:03:22 +000011305 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000011306 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
11307 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000011308 switch (CC) {
11309 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000011310 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000011311 // Converting this to a min would handle comparisons between positive
11312 // and negative zero incorrectly, and swapping the operands would
11313 // cause it to handle NaNs incorrectly.
11314 if (!UnsafeFPMath &&
11315 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000011316 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000011317 break;
11318 std::swap(LHS, RHS);
11319 }
Dan Gohman670e5392009-09-21 18:03:22 +000011320 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000011321 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011322 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000011323 // Converting this to a min would handle NaNs incorrectly.
11324 if (!UnsafeFPMath &&
11325 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
11326 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011327 Opcode = X86ISD::FMIN;
11328 break;
11329 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000011330 // Converting this to a min would handle both negative zeros and NaNs
11331 // incorrectly, but we can swap the operands to fix both.
11332 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000011333 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011334 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011335 case ISD::SETGE:
11336 Opcode = X86ISD::FMIN;
11337 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011338
Dan Gohman670e5392009-09-21 18:03:22 +000011339 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000011340 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000011341 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000011342 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011343 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000011344 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011345 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000011346 // Converting this to a max would handle comparisons between positive
11347 // and negative zero incorrectly, and swapping the operands would
11348 // cause it to handle NaNs incorrectly.
11349 if (!UnsafeFPMath &&
11350 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000011351 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000011352 break;
11353 std::swap(LHS, RHS);
11354 }
Dan Gohman670e5392009-09-21 18:03:22 +000011355 Opcode = X86ISD::FMAX;
11356 break;
11357 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000011358 // Converting this to a max would handle both negative zeros and NaNs
11359 // incorrectly, but we can swap the operands to fix both.
11360 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000011361 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011362 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000011363 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011364 Opcode = X86ISD::FMAX;
11365 break;
11366 }
Chris Lattner83e6c992006-10-04 06:57:07 +000011367 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011368
Chris Lattner47b4ce82009-03-11 05:48:52 +000011369 if (Opcode)
11370 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000011371 }
Eric Christopherfd179292009-08-27 18:07:15 +000011372
Chris Lattnerd1980a52009-03-12 06:52:53 +000011373 // If this is a select between two integer constants, try to do some
11374 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000011375 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
11376 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000011377 // Don't do this for crazy integer types.
11378 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
11379 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000011380 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000011381 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000011382
Chris Lattnercee56e72009-03-13 05:53:31 +000011383 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000011384 // Efficiently invertible.
11385 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
11386 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
11387 isa<ConstantSDNode>(Cond.getOperand(1))))) {
11388 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000011389 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000011390 }
Eric Christopherfd179292009-08-27 18:07:15 +000011391
Chris Lattnerd1980a52009-03-12 06:52:53 +000011392 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000011393 if (FalseC->getAPIntValue() == 0 &&
11394 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000011395 if (NeedsCondInvert) // Invert the condition if needed.
11396 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
11397 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011398
Chris Lattnerd1980a52009-03-12 06:52:53 +000011399 // Zero extend the condition if needed.
11400 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011401
Chris Lattnercee56e72009-03-13 05:53:31 +000011402 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000011403 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000011404 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000011405 }
Eric Christopherfd179292009-08-27 18:07:15 +000011406
Chris Lattner97a29a52009-03-13 05:22:11 +000011407 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000011408 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000011409 if (NeedsCondInvert) // Invert the condition if needed.
11410 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
11411 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011412
Chris Lattner97a29a52009-03-13 05:22:11 +000011413 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000011414 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
11415 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000011416 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000011417 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000011418 }
Eric Christopherfd179292009-08-27 18:07:15 +000011419
Chris Lattnercee56e72009-03-13 05:53:31 +000011420 // Optimize cases that will turn into an LEA instruction. This requires
11421 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000011422 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000011423 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000011424 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000011425
Chris Lattnercee56e72009-03-13 05:53:31 +000011426 bool isFastMultiplier = false;
11427 if (Diff < 10) {
11428 switch ((unsigned char)Diff) {
11429 default: break;
11430 case 1: // result = add base, cond
11431 case 2: // result = lea base( , cond*2)
11432 case 3: // result = lea base(cond, cond*2)
11433 case 4: // result = lea base( , cond*4)
11434 case 5: // result = lea base(cond, cond*4)
11435 case 8: // result = lea base( , cond*8)
11436 case 9: // result = lea base(cond, cond*8)
11437 isFastMultiplier = true;
11438 break;
11439 }
11440 }
Eric Christopherfd179292009-08-27 18:07:15 +000011441
Chris Lattnercee56e72009-03-13 05:53:31 +000011442 if (isFastMultiplier) {
11443 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
11444 if (NeedsCondInvert) // Invert the condition if needed.
11445 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
11446 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011447
Chris Lattnercee56e72009-03-13 05:53:31 +000011448 // Zero extend the condition if needed.
11449 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
11450 Cond);
11451 // Scale the condition by the difference.
11452 if (Diff != 1)
11453 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
11454 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011455
Chris Lattnercee56e72009-03-13 05:53:31 +000011456 // Add the base if non-zero.
11457 if (FalseC->getAPIntValue() != 0)
11458 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
11459 SDValue(FalseC, 0));
11460 return Cond;
11461 }
Eric Christopherfd179292009-08-27 18:07:15 +000011462 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000011463 }
11464 }
Eric Christopherfd179292009-08-27 18:07:15 +000011465
Dan Gohman475871a2008-07-27 21:46:04 +000011466 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000011467}
11468
Chris Lattnerd1980a52009-03-12 06:52:53 +000011469/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
11470static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
11471 TargetLowering::DAGCombinerInfo &DCI) {
11472 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000011473
Chris Lattnerd1980a52009-03-12 06:52:53 +000011474 // If the flag operand isn't dead, don't touch this CMOV.
11475 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
11476 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000011477
Evan Chengb5a55d92011-05-24 01:48:22 +000011478 SDValue FalseOp = N->getOperand(0);
11479 SDValue TrueOp = N->getOperand(1);
11480 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
11481 SDValue Cond = N->getOperand(3);
11482 if (CC == X86::COND_E || CC == X86::COND_NE) {
11483 switch (Cond.getOpcode()) {
11484 default: break;
11485 case X86ISD::BSR:
11486 case X86ISD::BSF:
11487 // If operand of BSR / BSF are proven never zero, then ZF cannot be set.
11488 if (DAG.isKnownNeverZero(Cond.getOperand(0)))
11489 return (CC == X86::COND_E) ? FalseOp : TrueOp;
11490 }
11491 }
11492
Chris Lattnerd1980a52009-03-12 06:52:53 +000011493 // If this is a select between two integer constants, try to do some
11494 // optimizations. Note that the operands are ordered the opposite of SELECT
11495 // operands.
Evan Chengb5a55d92011-05-24 01:48:22 +000011496 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(TrueOp)) {
11497 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(FalseOp)) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000011498 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
11499 // larger than FalseC (the false value).
Chris Lattnerd1980a52009-03-12 06:52:53 +000011500 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
11501 CC = X86::GetOppositeBranchCondition(CC);
11502 std::swap(TrueC, FalseC);
11503 }
Eric Christopherfd179292009-08-27 18:07:15 +000011504
Chris Lattnerd1980a52009-03-12 06:52:53 +000011505 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000011506 // This is efficient for any integer data type (including i8/i16) and
11507 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000011508 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000011509 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11510 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011511
Chris Lattnerd1980a52009-03-12 06:52:53 +000011512 // Zero extend the condition if needed.
11513 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011514
Chris Lattnerd1980a52009-03-12 06:52:53 +000011515 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
11516 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000011517 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000011518 if (N->getNumValues() == 2) // Dead flag value?
11519 return DCI.CombineTo(N, Cond, SDValue());
11520 return Cond;
11521 }
Eric Christopherfd179292009-08-27 18:07:15 +000011522
Chris Lattnercee56e72009-03-13 05:53:31 +000011523 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
11524 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000011525 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000011526 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11527 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011528
Chris Lattner97a29a52009-03-13 05:22:11 +000011529 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000011530 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
11531 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000011532 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
11533 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000011534
Chris Lattner97a29a52009-03-13 05:22:11 +000011535 if (N->getNumValues() == 2) // Dead flag value?
11536 return DCI.CombineTo(N, Cond, SDValue());
11537 return Cond;
11538 }
Eric Christopherfd179292009-08-27 18:07:15 +000011539
Chris Lattnercee56e72009-03-13 05:53:31 +000011540 // Optimize cases that will turn into an LEA instruction. This requires
11541 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000011542 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000011543 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000011544 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000011545
Chris Lattnercee56e72009-03-13 05:53:31 +000011546 bool isFastMultiplier = false;
11547 if (Diff < 10) {
11548 switch ((unsigned char)Diff) {
11549 default: break;
11550 case 1: // result = add base, cond
11551 case 2: // result = lea base( , cond*2)
11552 case 3: // result = lea base(cond, cond*2)
11553 case 4: // result = lea base( , cond*4)
11554 case 5: // result = lea base(cond, cond*4)
11555 case 8: // result = lea base( , cond*8)
11556 case 9: // result = lea base(cond, cond*8)
11557 isFastMultiplier = true;
11558 break;
11559 }
11560 }
Eric Christopherfd179292009-08-27 18:07:15 +000011561
Chris Lattnercee56e72009-03-13 05:53:31 +000011562 if (isFastMultiplier) {
11563 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000011564 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11565 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000011566 // Zero extend the condition if needed.
11567 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
11568 Cond);
11569 // Scale the condition by the difference.
11570 if (Diff != 1)
11571 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
11572 DAG.getConstant(Diff, Cond.getValueType()));
11573
11574 // Add the base if non-zero.
11575 if (FalseC->getAPIntValue() != 0)
11576 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
11577 SDValue(FalseC, 0));
11578 if (N->getNumValues() == 2) // Dead flag value?
11579 return DCI.CombineTo(N, Cond, SDValue());
11580 return Cond;
11581 }
Eric Christopherfd179292009-08-27 18:07:15 +000011582 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000011583 }
11584 }
11585 return SDValue();
11586}
11587
11588
Evan Cheng0b0cd912009-03-28 05:57:29 +000011589/// PerformMulCombine - Optimize a single multiply with constant into two
11590/// in order to implement it with two cheaper instructions, e.g.
11591/// LEA + SHL, LEA + LEA.
11592static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
11593 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000011594 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
11595 return SDValue();
11596
Owen Andersone50ed302009-08-10 22:56:29 +000011597 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000011598 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000011599 return SDValue();
11600
11601 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
11602 if (!C)
11603 return SDValue();
11604 uint64_t MulAmt = C->getZExtValue();
11605 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
11606 return SDValue();
11607
11608 uint64_t MulAmt1 = 0;
11609 uint64_t MulAmt2 = 0;
11610 if ((MulAmt % 9) == 0) {
11611 MulAmt1 = 9;
11612 MulAmt2 = MulAmt / 9;
11613 } else if ((MulAmt % 5) == 0) {
11614 MulAmt1 = 5;
11615 MulAmt2 = MulAmt / 5;
11616 } else if ((MulAmt % 3) == 0) {
11617 MulAmt1 = 3;
11618 MulAmt2 = MulAmt / 3;
11619 }
11620 if (MulAmt2 &&
11621 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
11622 DebugLoc DL = N->getDebugLoc();
11623
11624 if (isPowerOf2_64(MulAmt2) &&
11625 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
11626 // If second multiplifer is pow2, issue it first. We want the multiply by
11627 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
11628 // is an add.
11629 std::swap(MulAmt1, MulAmt2);
11630
11631 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000011632 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000011633 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000011634 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000011635 else
Evan Cheng73f24c92009-03-30 21:36:47 +000011636 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000011637 DAG.getConstant(MulAmt1, VT));
11638
Eric Christopherfd179292009-08-27 18:07:15 +000011639 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000011640 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000011641 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000011642 else
Evan Cheng73f24c92009-03-30 21:36:47 +000011643 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000011644 DAG.getConstant(MulAmt2, VT));
11645
11646 // Do not add new nodes to DAG combiner worklist.
11647 DCI.CombineTo(N, NewMul, false);
11648 }
11649 return SDValue();
11650}
11651
Evan Chengad9c0a32009-12-15 00:53:42 +000011652static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
11653 SDValue N0 = N->getOperand(0);
11654 SDValue N1 = N->getOperand(1);
11655 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
11656 EVT VT = N0.getValueType();
11657
11658 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
11659 // since the result of setcc_c is all zero's or all ones.
11660 if (N1C && N0.getOpcode() == ISD::AND &&
11661 N0.getOperand(1).getOpcode() == ISD::Constant) {
11662 SDValue N00 = N0.getOperand(0);
11663 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
11664 ((N00.getOpcode() == ISD::ANY_EXTEND ||
11665 N00.getOpcode() == ISD::ZERO_EXTEND) &&
11666 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
11667 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
11668 APInt ShAmt = N1C->getAPIntValue();
11669 Mask = Mask.shl(ShAmt);
11670 if (Mask != 0)
11671 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
11672 N00, DAG.getConstant(Mask, VT));
11673 }
11674 }
11675
11676 return SDValue();
11677}
Evan Cheng0b0cd912009-03-28 05:57:29 +000011678
Nate Begeman740ab032009-01-26 00:52:55 +000011679/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
11680/// when possible.
11681static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
11682 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000011683 EVT VT = N->getValueType(0);
11684 if (!VT.isVector() && VT.isInteger() &&
11685 N->getOpcode() == ISD::SHL)
11686 return PerformSHLCombine(N, DAG);
11687
Nate Begeman740ab032009-01-26 00:52:55 +000011688 // On X86 with SSE2 support, we can transform this to a vector shift if
11689 // all elements are shifted by the same amount. We can't do this in legalize
11690 // because the a constant vector is typically transformed to a constant pool
11691 // so we have no knowledge of the shift amount.
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011692 if (!Subtarget->hasSSE2())
11693 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000011694
Owen Anderson825b72b2009-08-11 20:47:22 +000011695 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011696 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000011697
Mon P Wang3becd092009-01-28 08:12:05 +000011698 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000011699 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000011700 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000011701 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000011702 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
11703 unsigned NumElts = VT.getVectorNumElements();
11704 unsigned i = 0;
11705 for (; i != NumElts; ++i) {
11706 SDValue Arg = ShAmtOp.getOperand(i);
11707 if (Arg.getOpcode() == ISD::UNDEF) continue;
11708 BaseShAmt = Arg;
11709 break;
11710 }
11711 for (; i != NumElts; ++i) {
11712 SDValue Arg = ShAmtOp.getOperand(i);
11713 if (Arg.getOpcode() == ISD::UNDEF) continue;
11714 if (Arg != BaseShAmt) {
11715 return SDValue();
11716 }
11717 }
11718 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000011719 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000011720 SDValue InVec = ShAmtOp.getOperand(0);
11721 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
11722 unsigned NumElts = InVec.getValueType().getVectorNumElements();
11723 unsigned i = 0;
11724 for (; i != NumElts; ++i) {
11725 SDValue Arg = InVec.getOperand(i);
11726 if (Arg.getOpcode() == ISD::UNDEF) continue;
11727 BaseShAmt = Arg;
11728 break;
11729 }
11730 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
11731 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000011732 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000011733 if (C->getZExtValue() == SplatIdx)
11734 BaseShAmt = InVec.getOperand(1);
11735 }
11736 }
11737 if (BaseShAmt.getNode() == 0)
11738 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
11739 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +000011740 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011741 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000011742
Mon P Wangefa42202009-09-03 19:56:25 +000011743 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000011744 if (EltVT.bitsGT(MVT::i32))
11745 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
11746 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000011747 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000011748
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011749 // The shift amount is identical so we can do a vector shift.
11750 SDValue ValOp = N->getOperand(0);
11751 switch (N->getOpcode()) {
11752 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000011753 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011754 break;
11755 case ISD::SHL:
Owen Anderson825b72b2009-08-11 20:47:22 +000011756 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011757 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011758 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011759 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011760 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011761 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011762 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011763 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011764 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011765 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011766 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011767 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011768 break;
11769 case ISD::SRA:
Owen Anderson825b72b2009-08-11 20:47:22 +000011770 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011771 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011772 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011773 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011774 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011775 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011776 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011777 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011778 break;
11779 case ISD::SRL:
Owen Anderson825b72b2009-08-11 20:47:22 +000011780 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011781 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011782 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011783 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011784 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011785 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011786 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011787 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011788 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011789 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011790 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011791 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011792 break;
Nate Begeman740ab032009-01-26 00:52:55 +000011793 }
11794 return SDValue();
11795}
11796
Nate Begemanb65c1752010-12-17 22:55:37 +000011797
Stuart Hastings865f0932011-06-03 23:53:54 +000011798// CMPEQCombine - Recognize the distinctive (AND (setcc ...) (setcc ..))
11799// where both setccs reference the same FP CMP, and rewrite for CMPEQSS
11800// and friends. Likewise for OR -> CMPNEQSS.
11801static SDValue CMPEQCombine(SDNode *N, SelectionDAG &DAG,
11802 TargetLowering::DAGCombinerInfo &DCI,
11803 const X86Subtarget *Subtarget) {
11804 unsigned opcode;
11805
11806 // SSE1 supports CMP{eq|ne}SS, and SSE2 added CMP{eq|ne}SD, but
11807 // we're requiring SSE2 for both.
11808 if (Subtarget->hasSSE2() && isAndOrOfSetCCs(SDValue(N, 0U), opcode)) {
11809 SDValue N0 = N->getOperand(0);
11810 SDValue N1 = N->getOperand(1);
11811 SDValue CMP0 = N0->getOperand(1);
11812 SDValue CMP1 = N1->getOperand(1);
11813 DebugLoc DL = N->getDebugLoc();
11814
11815 // The SETCCs should both refer to the same CMP.
11816 if (CMP0.getOpcode() != X86ISD::CMP || CMP0 != CMP1)
11817 return SDValue();
11818
11819 SDValue CMP00 = CMP0->getOperand(0);
11820 SDValue CMP01 = CMP0->getOperand(1);
11821 EVT VT = CMP00.getValueType();
11822
11823 if (VT == MVT::f32 || VT == MVT::f64) {
11824 bool ExpectingFlags = false;
11825 // Check for any users that want flags:
11826 for (SDNode::use_iterator UI = N->use_begin(),
11827 UE = N->use_end();
11828 !ExpectingFlags && UI != UE; ++UI)
11829 switch (UI->getOpcode()) {
11830 default:
11831 case ISD::BR_CC:
11832 case ISD::BRCOND:
11833 case ISD::SELECT:
11834 ExpectingFlags = true;
11835 break;
11836 case ISD::CopyToReg:
11837 case ISD::SIGN_EXTEND:
11838 case ISD::ZERO_EXTEND:
11839 case ISD::ANY_EXTEND:
11840 break;
11841 }
11842
11843 if (!ExpectingFlags) {
11844 enum X86::CondCode cc0 = (enum X86::CondCode)N0.getConstantOperandVal(0);
11845 enum X86::CondCode cc1 = (enum X86::CondCode)N1.getConstantOperandVal(0);
11846
11847 if (cc1 == X86::COND_E || cc1 == X86::COND_NE) {
11848 X86::CondCode tmp = cc0;
11849 cc0 = cc1;
11850 cc1 = tmp;
11851 }
11852
11853 if ((cc0 == X86::COND_E && cc1 == X86::COND_NP) ||
11854 (cc0 == X86::COND_NE && cc1 == X86::COND_P)) {
11855 bool is64BitFP = (CMP00.getValueType() == MVT::f64);
11856 X86ISD::NodeType NTOperator = is64BitFP ?
11857 X86ISD::FSETCCsd : X86ISD::FSETCCss;
11858 // FIXME: need symbolic constants for these magic numbers.
11859 // See X86ATTInstPrinter.cpp:printSSECC().
11860 unsigned x86cc = (cc0 == X86::COND_E) ? 0 : 4;
11861 SDValue OnesOrZeroesF = DAG.getNode(NTOperator, DL, MVT::f32, CMP00, CMP01,
11862 DAG.getConstant(x86cc, MVT::i8));
11863 SDValue OnesOrZeroesI = DAG.getNode(ISD::BITCAST, DL, MVT::i32,
11864 OnesOrZeroesF);
11865 SDValue ANDed = DAG.getNode(ISD::AND, DL, MVT::i32, OnesOrZeroesI,
11866 DAG.getConstant(1, MVT::i32));
11867 SDValue OneBitOfTruth = DAG.getNode(ISD::TRUNCATE, DL, MVT::i8, ANDed);
11868 return OneBitOfTruth;
11869 }
11870 }
11871 }
11872 }
11873 return SDValue();
11874}
11875
Nate Begemanb65c1752010-12-17 22:55:37 +000011876static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
11877 TargetLowering::DAGCombinerInfo &DCI,
11878 const X86Subtarget *Subtarget) {
11879 if (DCI.isBeforeLegalizeOps())
11880 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011881
Stuart Hastings865f0932011-06-03 23:53:54 +000011882 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
11883 if (R.getNode())
11884 return R;
11885
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000011886 // Want to form ANDNP nodes:
11887 // 1) In the hopes of then easily combining them with OR and AND nodes
11888 // to form PBLEND/PSIGN.
11889 // 2) To match ANDN packed intrinsics
Nate Begemanb65c1752010-12-17 22:55:37 +000011890 EVT VT = N->getValueType(0);
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000011891 if (VT != MVT::v2i64 && VT != MVT::v4i64)
Nate Begemanb65c1752010-12-17 22:55:37 +000011892 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011893
Nate Begemanb65c1752010-12-17 22:55:37 +000011894 SDValue N0 = N->getOperand(0);
11895 SDValue N1 = N->getOperand(1);
11896 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011897
Nate Begemanb65c1752010-12-17 22:55:37 +000011898 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011899 if (N0.getOpcode() == ISD::XOR &&
Nate Begemanb65c1752010-12-17 22:55:37 +000011900 ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000011901 return DAG.getNode(X86ISD::ANDNP, DL, VT, N0.getOperand(0), N1);
Nate Begemanb65c1752010-12-17 22:55:37 +000011902
11903 // Check RHS for vnot
11904 if (N1.getOpcode() == ISD::XOR &&
11905 ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000011906 return DAG.getNode(X86ISD::ANDNP, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011907
Nate Begemanb65c1752010-12-17 22:55:37 +000011908 return SDValue();
11909}
11910
Evan Cheng760d1942010-01-04 21:22:48 +000011911static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000011912 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000011913 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +000011914 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000011915 return SDValue();
11916
Stuart Hastings865f0932011-06-03 23:53:54 +000011917 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
11918 if (R.getNode())
11919 return R;
11920
Evan Cheng760d1942010-01-04 21:22:48 +000011921 EVT VT = N->getValueType(0);
Nate Begemanb65c1752010-12-17 22:55:37 +000011922 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64 && VT != MVT::v2i64)
Evan Cheng760d1942010-01-04 21:22:48 +000011923 return SDValue();
11924
Evan Cheng760d1942010-01-04 21:22:48 +000011925 SDValue N0 = N->getOperand(0);
11926 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011927
Nate Begemanb65c1752010-12-17 22:55:37 +000011928 // look for psign/blend
11929 if (Subtarget->hasSSSE3()) {
11930 if (VT == MVT::v2i64) {
11931 // Canonicalize pandn to RHS
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000011932 if (N0.getOpcode() == X86ISD::ANDNP)
Nate Begemanb65c1752010-12-17 22:55:37 +000011933 std::swap(N0, N1);
11934 // or (and (m, x), (pandn m, y))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000011935 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::ANDNP) {
Nate Begemanb65c1752010-12-17 22:55:37 +000011936 SDValue Mask = N1.getOperand(0);
11937 SDValue X = N1.getOperand(1);
11938 SDValue Y;
11939 if (N0.getOperand(0) == Mask)
11940 Y = N0.getOperand(1);
11941 if (N0.getOperand(1) == Mask)
11942 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011943
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000011944 // Check to see if the mask appeared in both the AND and ANDNP and
Nate Begemanb65c1752010-12-17 22:55:37 +000011945 if (!Y.getNode())
11946 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011947
Nate Begemanb65c1752010-12-17 22:55:37 +000011948 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
11949 if (Mask.getOpcode() != ISD::BITCAST ||
11950 X.getOpcode() != ISD::BITCAST ||
11951 Y.getOpcode() != ISD::BITCAST)
11952 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011953
Nate Begemanb65c1752010-12-17 22:55:37 +000011954 // Look through mask bitcast.
11955 Mask = Mask.getOperand(0);
11956 EVT MaskVT = Mask.getValueType();
11957
11958 // Validate that the Mask operand is a vector sra node. The sra node
11959 // will be an intrinsic.
11960 if (Mask.getOpcode() != ISD::INTRINSIC_WO_CHAIN)
11961 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011962
Nate Begemanb65c1752010-12-17 22:55:37 +000011963 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
11964 // there is no psrai.b
11965 switch (cast<ConstantSDNode>(Mask.getOperand(0))->getZExtValue()) {
11966 case Intrinsic::x86_sse2_psrai_w:
11967 case Intrinsic::x86_sse2_psrai_d:
11968 break;
11969 default: return SDValue();
11970 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011971
Nate Begemanb65c1752010-12-17 22:55:37 +000011972 // Check that the SRA is all signbits.
11973 SDValue SraC = Mask.getOperand(2);
11974 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
11975 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
11976 if ((SraAmt + 1) != EltBits)
11977 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011978
Nate Begemanb65c1752010-12-17 22:55:37 +000011979 DebugLoc DL = N->getDebugLoc();
11980
11981 // Now we know we at least have a plendvb with the mask val. See if
11982 // we can form a psignb/w/d.
11983 // psign = x.type == y.type == mask.type && y = sub(0, x);
11984 X = X.getOperand(0);
11985 Y = Y.getOperand(0);
11986 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
11987 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
11988 X.getValueType() == MaskVT && X.getValueType() == Y.getValueType()){
11989 unsigned Opc = 0;
11990 switch (EltBits) {
11991 case 8: Opc = X86ISD::PSIGNB; break;
11992 case 16: Opc = X86ISD::PSIGNW; break;
11993 case 32: Opc = X86ISD::PSIGND; break;
11994 default: break;
11995 }
11996 if (Opc) {
11997 SDValue Sign = DAG.getNode(Opc, DL, MaskVT, X, Mask.getOperand(1));
11998 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Sign);
11999 }
12000 }
12001 // PBLENDVB only available on SSE 4.1
12002 if (!Subtarget->hasSSE41())
12003 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012004
Nate Begemanb65c1752010-12-17 22:55:37 +000012005 X = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, X);
12006 Y = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Y);
12007 Mask = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Mask);
Nate Begeman672fb622010-12-20 22:04:24 +000012008 Mask = DAG.getNode(X86ISD::PBLENDVB, DL, MVT::v16i8, X, Y, Mask);
Nate Begemanb65c1752010-12-17 22:55:37 +000012009 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Mask);
12010 }
12011 }
12012 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012013
Nate Begemanb65c1752010-12-17 22:55:37 +000012014 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000012015 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
12016 std::swap(N0, N1);
12017 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
12018 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000012019 if (!N0.hasOneUse() || !N1.hasOneUse())
12020 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000012021
12022 SDValue ShAmt0 = N0.getOperand(1);
12023 if (ShAmt0.getValueType() != MVT::i8)
12024 return SDValue();
12025 SDValue ShAmt1 = N1.getOperand(1);
12026 if (ShAmt1.getValueType() != MVT::i8)
12027 return SDValue();
12028 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
12029 ShAmt0 = ShAmt0.getOperand(0);
12030 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
12031 ShAmt1 = ShAmt1.getOperand(0);
12032
12033 DebugLoc DL = N->getDebugLoc();
12034 unsigned Opc = X86ISD::SHLD;
12035 SDValue Op0 = N0.getOperand(0);
12036 SDValue Op1 = N1.getOperand(0);
12037 if (ShAmt0.getOpcode() == ISD::SUB) {
12038 Opc = X86ISD::SHRD;
12039 std::swap(Op0, Op1);
12040 std::swap(ShAmt0, ShAmt1);
12041 }
12042
Evan Cheng8b1190a2010-04-28 01:18:01 +000012043 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000012044 if (ShAmt1.getOpcode() == ISD::SUB) {
12045 SDValue Sum = ShAmt1.getOperand(0);
12046 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000012047 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
12048 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
12049 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
12050 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000012051 return DAG.getNode(Opc, DL, VT,
12052 Op0, Op1,
12053 DAG.getNode(ISD::TRUNCATE, DL,
12054 MVT::i8, ShAmt0));
12055 }
12056 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
12057 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
12058 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000012059 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000012060 return DAG.getNode(Opc, DL, VT,
12061 N0.getOperand(0), N1.getOperand(0),
12062 DAG.getNode(ISD::TRUNCATE, DL,
12063 MVT::i8, ShAmt0));
12064 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012065
Evan Cheng760d1942010-01-04 21:22:48 +000012066 return SDValue();
12067}
12068
Chris Lattner149a4e52008-02-22 02:09:43 +000012069/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000012070static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000012071 const X86Subtarget *Subtarget) {
Chris Lattner149a4e52008-02-22 02:09:43 +000012072 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
12073 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000012074 // A preferable solution to the general problem is to figure out the right
12075 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000012076
12077 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng7e2ff772008-05-08 00:57:18 +000012078 StoreSDNode *St = cast<StoreSDNode>(N);
Owen Andersone50ed302009-08-10 22:56:29 +000012079 EVT VT = St->getValue().getValueType();
Evan Cheng536e6672009-03-12 05:59:15 +000012080 if (VT.getSizeInBits() != 64)
12081 return SDValue();
12082
Devang Patel578efa92009-06-05 21:57:13 +000012083 const Function *F = DAG.getMachineFunction().getFunction();
12084 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Eric Christopherfd179292009-08-27 18:07:15 +000012085 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
Devang Patel578efa92009-06-05 21:57:13 +000012086 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +000012087 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000012088 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000012089 isa<LoadSDNode>(St->getValue()) &&
12090 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
12091 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000012092 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000012093 LoadSDNode *Ld = 0;
12094 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000012095 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000012096 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000012097 // Must be a store of a load. We currently handle two cases: the load
12098 // is a direct child, and it's under an intervening TokenFactor. It is
12099 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000012100 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000012101 Ld = cast<LoadSDNode>(St->getChain());
12102 else if (St->getValue().hasOneUse() &&
12103 ChainVal->getOpcode() == ISD::TokenFactor) {
12104 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000012105 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000012106 TokenFactorIndex = i;
12107 Ld = cast<LoadSDNode>(St->getValue());
12108 } else
12109 Ops.push_back(ChainVal->getOperand(i));
12110 }
12111 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000012112
Evan Cheng536e6672009-03-12 05:59:15 +000012113 if (!Ld || !ISD::isNormalLoad(Ld))
12114 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000012115
Evan Cheng536e6672009-03-12 05:59:15 +000012116 // If this is not the MMX case, i.e. we are just turning i64 load/store
12117 // into f64 load/store, avoid the transformation if there are multiple
12118 // uses of the loaded value.
12119 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
12120 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000012121
Evan Cheng536e6672009-03-12 05:59:15 +000012122 DebugLoc LdDL = Ld->getDebugLoc();
12123 DebugLoc StDL = N->getDebugLoc();
12124 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
12125 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
12126 // pair instead.
12127 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000012128 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000012129 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
12130 Ld->getPointerInfo(), Ld->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000012131 Ld->isNonTemporal(), Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000012132 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000012133 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000012134 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000012135 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000012136 Ops.size());
12137 }
Evan Cheng536e6672009-03-12 05:59:15 +000012138 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000012139 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000012140 St->isVolatile(), St->isNonTemporal(),
12141 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000012142 }
Evan Cheng536e6672009-03-12 05:59:15 +000012143
12144 // Otherwise, lower to two pairs of 32-bit loads / stores.
12145 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000012146 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
12147 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000012148
Owen Anderson825b72b2009-08-11 20:47:22 +000012149 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000012150 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000012151 Ld->isVolatile(), Ld->isNonTemporal(),
12152 Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000012153 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000012154 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000012155 Ld->isVolatile(), Ld->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000012156 MinAlign(Ld->getAlignment(), 4));
12157
12158 SDValue NewChain = LoLd.getValue(1);
12159 if (TokenFactorIndex != -1) {
12160 Ops.push_back(LoLd);
12161 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000012162 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000012163 Ops.size());
12164 }
12165
12166 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000012167 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
12168 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000012169
12170 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000012171 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000012172 St->isVolatile(), St->isNonTemporal(),
12173 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000012174 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000012175 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000012176 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000012177 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000012178 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000012179 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000012180 }
Dan Gohman475871a2008-07-27 21:46:04 +000012181 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000012182}
12183
Chris Lattner6cf73262008-01-25 06:14:17 +000012184/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
12185/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000012186static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000012187 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
12188 // F[X]OR(0.0, x) -> x
12189 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000012190 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
12191 if (C->getValueAPF().isPosZero())
12192 return N->getOperand(1);
12193 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
12194 if (C->getValueAPF().isPosZero())
12195 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000012196 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000012197}
12198
12199/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000012200static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000012201 // FAND(0.0, x) -> 0.0
12202 // FAND(x, 0.0) -> 0.0
12203 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
12204 if (C->getValueAPF().isPosZero())
12205 return N->getOperand(0);
12206 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
12207 if (C->getValueAPF().isPosZero())
12208 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000012209 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000012210}
12211
Dan Gohmane5af2d32009-01-29 01:59:02 +000012212static SDValue PerformBTCombine(SDNode *N,
12213 SelectionDAG &DAG,
12214 TargetLowering::DAGCombinerInfo &DCI) {
12215 // BT ignores high bits in the bit index operand.
12216 SDValue Op1 = N->getOperand(1);
12217 if (Op1.hasOneUse()) {
12218 unsigned BitWidth = Op1.getValueSizeInBits();
12219 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
12220 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000012221 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
12222 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000012223 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000012224 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
12225 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
12226 DCI.CommitTargetLoweringOpt(TLO);
12227 }
12228 return SDValue();
12229}
Chris Lattner83e6c992006-10-04 06:57:07 +000012230
Eli Friedman7a5e5552009-06-07 06:52:44 +000012231static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
12232 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000012233 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000012234 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000012235 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000012236 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000012237 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000012238 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000012239 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000012240 }
12241 return SDValue();
12242}
12243
Evan Cheng2e489c42009-12-16 00:53:11 +000012244static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
12245 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
12246 // (and (i32 x86isd::setcc_carry), 1)
12247 // This eliminates the zext. This transformation is necessary because
12248 // ISD::SETCC is always legalized to i8.
12249 DebugLoc dl = N->getDebugLoc();
12250 SDValue N0 = N->getOperand(0);
12251 EVT VT = N->getValueType(0);
12252 if (N0.getOpcode() == ISD::AND &&
12253 N0.hasOneUse() &&
12254 N0.getOperand(0).hasOneUse()) {
12255 SDValue N00 = N0.getOperand(0);
12256 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
12257 return SDValue();
12258 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
12259 if (!C || C->getZExtValue() != 1)
12260 return SDValue();
12261 return DAG.getNode(ISD::AND, dl, VT,
12262 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
12263 N00.getOperand(0), N00.getOperand(1)),
12264 DAG.getConstant(1, VT));
12265 }
12266
12267 return SDValue();
12268}
12269
Chris Lattnerc19d1c32010-12-19 22:08:31 +000012270// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
12271static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG) {
12272 unsigned X86CC = N->getConstantOperandVal(0);
12273 SDValue EFLAG = N->getOperand(1);
12274 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012275
Chris Lattnerc19d1c32010-12-19 22:08:31 +000012276 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
12277 // a zext and produces an all-ones bit which is more useful than 0/1 in some
12278 // cases.
12279 if (X86CC == X86::COND_B)
12280 return DAG.getNode(ISD::AND, DL, MVT::i8,
12281 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
12282 DAG.getConstant(X86CC, MVT::i8), EFLAG),
12283 DAG.getConstant(1, MVT::i8));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012284
Chris Lattnerc19d1c32010-12-19 22:08:31 +000012285 return SDValue();
12286}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012287
Benjamin Kramer1396c402011-06-18 11:09:41 +000012288static SDValue PerformSINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG,
12289 const X86TargetLowering *XTLI) {
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000012290 SDValue Op0 = N->getOperand(0);
12291 // Transform (SINT_TO_FP (i64 ...)) into an x87 operation if we have
12292 // a 32-bit target where SSE doesn't support i64->FP operations.
12293 if (Op0.getOpcode() == ISD::LOAD) {
12294 LoadSDNode *Ld = cast<LoadSDNode>(Op0.getNode());
12295 EVT VT = Ld->getValueType(0);
12296 if (!Ld->isVolatile() && !N->getValueType(0).isVector() &&
12297 ISD::isNON_EXTLoad(Op0.getNode()) && Op0.hasOneUse() &&
12298 !XTLI->getSubtarget()->is64Bit() &&
12299 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Benjamin Kramer1396c402011-06-18 11:09:41 +000012300 SDValue FILDChain = XTLI->BuildFILD(SDValue(N, 0), Ld->getValueType(0),
12301 Ld->getChain(), Op0, DAG);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000012302 DAG.ReplaceAllUsesOfValueWith(Op0.getValue(1), FILDChain.getValue(1));
12303 return FILDChain;
12304 }
12305 }
12306 return SDValue();
12307}
12308
Chris Lattner23a01992010-12-20 01:37:09 +000012309// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
12310static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
12311 X86TargetLowering::DAGCombinerInfo &DCI) {
12312 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
12313 // the result is either zero or one (depending on the input carry bit).
12314 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
12315 if (X86::isZeroNode(N->getOperand(0)) &&
12316 X86::isZeroNode(N->getOperand(1)) &&
12317 // We don't have a good way to replace an EFLAGS use, so only do this when
12318 // dead right now.
12319 SDValue(N, 1).use_empty()) {
12320 DebugLoc DL = N->getDebugLoc();
12321 EVT VT = N->getValueType(0);
12322 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
12323 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
12324 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
12325 DAG.getConstant(X86::COND_B,MVT::i8),
12326 N->getOperand(2)),
12327 DAG.getConstant(1, VT));
12328 return DCI.CombineTo(N, Res1, CarryOut);
12329 }
12330
12331 return SDValue();
12332}
12333
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000012334// fold (add Y, (sete X, 0)) -> adc 0, Y
12335// (add Y, (setne X, 0)) -> sbb -1, Y
12336// (sub (sete X, 0), Y) -> sbb 0, Y
12337// (sub (setne X, 0), Y) -> adc -1, Y
12338static SDValue OptimizeConditonalInDecrement(SDNode *N, SelectionDAG &DAG) {
12339 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012340
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000012341 // Look through ZExts.
12342 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
12343 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
12344 return SDValue();
12345
12346 SDValue SetCC = Ext.getOperand(0);
12347 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
12348 return SDValue();
12349
12350 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
12351 if (CC != X86::COND_E && CC != X86::COND_NE)
12352 return SDValue();
12353
12354 SDValue Cmp = SetCC.getOperand(1);
12355 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000012356 !X86::isZeroNode(Cmp.getOperand(1)) ||
12357 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000012358 return SDValue();
12359
12360 SDValue CmpOp0 = Cmp.getOperand(0);
12361 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
12362 DAG.getConstant(1, CmpOp0.getValueType()));
12363
12364 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
12365 if (CC == X86::COND_NE)
12366 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
12367 DL, OtherVal.getValueType(), OtherVal,
12368 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
12369 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
12370 DL, OtherVal.getValueType(), OtherVal,
12371 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
12372}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000012373
Dan Gohman475871a2008-07-27 21:46:04 +000012374SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000012375 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000012376 SelectionDAG &DAG = DCI.DAG;
12377 switch (N->getOpcode()) {
12378 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012379 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerc19d1c32010-12-19 22:08:31 +000012380 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, *this);
Chris Lattneraf723b92008-01-25 05:46:26 +000012381 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +000012382 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000012383 case ISD::ADD:
12384 case ISD::SUB: return OptimizeConditonalInDecrement(N, DAG);
Chris Lattner23a01992010-12-20 01:37:09 +000012385 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000012386 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000012387 case ISD::SHL:
12388 case ISD::SRA:
12389 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000012390 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000012391 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000012392 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000012393 case ISD::SINT_TO_FP: return PerformSINT_TO_FPCombine(N, DAG, this);
Chris Lattner6cf73262008-01-25 06:14:17 +000012394 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000012395 case X86ISD::FOR: return PerformFORCombine(N, DAG);
12396 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000012397 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000012398 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Evan Cheng2e489c42009-12-16 00:53:11 +000012399 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG);
Chris Lattnerc19d1c32010-12-19 22:08:31 +000012400 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000012401 case X86ISD::SHUFPS: // Handle all target specific shuffles
12402 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000012403 case X86ISD::PALIGN:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000012404 case X86ISD::PUNPCKHBW:
12405 case X86ISD::PUNPCKHWD:
12406 case X86ISD::PUNPCKHDQ:
12407 case X86ISD::PUNPCKHQDQ:
12408 case X86ISD::UNPCKHPS:
12409 case X86ISD::UNPCKHPD:
12410 case X86ISD::PUNPCKLBW:
12411 case X86ISD::PUNPCKLWD:
12412 case X86ISD::PUNPCKLDQ:
12413 case X86ISD::PUNPCKLQDQ:
12414 case X86ISD::UNPCKLPS:
12415 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +000012416 case X86ISD::VUNPCKLPS:
12417 case X86ISD::VUNPCKLPD:
12418 case X86ISD::VUNPCKLPSY:
12419 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000012420 case X86ISD::MOVHLPS:
12421 case X86ISD::MOVLHPS:
12422 case X86ISD::PSHUFD:
12423 case X86ISD::PSHUFHW:
12424 case X86ISD::PSHUFLW:
12425 case X86ISD::MOVSS:
12426 case X86ISD::MOVSD:
Mon P Wanga0fd0d52010-12-19 23:55:53 +000012427 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI);
Evan Cheng206ee9d2006-07-07 08:33:52 +000012428 }
12429
Dan Gohman475871a2008-07-27 21:46:04 +000012430 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000012431}
12432
Evan Chenge5b51ac2010-04-17 06:13:15 +000012433/// isTypeDesirableForOp - Return true if the target has native support for
12434/// the specified value type and it is 'desirable' to use the type for the
12435/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
12436/// instruction encodings are longer and some i16 instructions are slow.
12437bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
12438 if (!isTypeLegal(VT))
12439 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000012440 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000012441 return true;
12442
12443 switch (Opc) {
12444 default:
12445 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000012446 case ISD::LOAD:
12447 case ISD::SIGN_EXTEND:
12448 case ISD::ZERO_EXTEND:
12449 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000012450 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000012451 case ISD::SRL:
12452 case ISD::SUB:
12453 case ISD::ADD:
12454 case ISD::MUL:
12455 case ISD::AND:
12456 case ISD::OR:
12457 case ISD::XOR:
12458 return false;
12459 }
12460}
12461
12462/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000012463/// beneficial for dag combiner to promote the specified node. If true, it
12464/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000012465bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000012466 EVT VT = Op.getValueType();
12467 if (VT != MVT::i16)
12468 return false;
12469
Evan Cheng4c26e932010-04-19 19:29:22 +000012470 bool Promote = false;
12471 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000012472 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000012473 default: break;
12474 case ISD::LOAD: {
12475 LoadSDNode *LD = cast<LoadSDNode>(Op);
12476 // If the non-extending load has a single use and it's not live out, then it
12477 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000012478 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
12479 Op.hasOneUse()*/) {
12480 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
12481 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
12482 // The only case where we'd want to promote LOAD (rather then it being
12483 // promoted as an operand is when it's only use is liveout.
12484 if (UI->getOpcode() != ISD::CopyToReg)
12485 return false;
12486 }
12487 }
Evan Cheng4c26e932010-04-19 19:29:22 +000012488 Promote = true;
12489 break;
12490 }
12491 case ISD::SIGN_EXTEND:
12492 case ISD::ZERO_EXTEND:
12493 case ISD::ANY_EXTEND:
12494 Promote = true;
12495 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000012496 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000012497 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000012498 SDValue N0 = Op.getOperand(0);
12499 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000012500 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000012501 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000012502 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000012503 break;
12504 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000012505 case ISD::ADD:
12506 case ISD::MUL:
12507 case ISD::AND:
12508 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000012509 case ISD::XOR:
12510 Commute = true;
12511 // fallthrough
12512 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000012513 SDValue N0 = Op.getOperand(0);
12514 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000012515 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000012516 return false;
12517 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000012518 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000012519 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000012520 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000012521 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000012522 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000012523 }
12524 }
12525
12526 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000012527 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000012528}
12529
Evan Cheng60c07e12006-07-05 22:17:51 +000012530//===----------------------------------------------------------------------===//
12531// X86 Inline Assembly Support
12532//===----------------------------------------------------------------------===//
12533
Chris Lattnerb8105652009-07-20 17:51:36 +000012534bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
12535 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000012536
12537 std::string AsmStr = IA->getAsmString();
12538
12539 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000012540 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000012541 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000012542
12543 switch (AsmPieces.size()) {
12544 default: return false;
12545 case 1:
12546 AsmStr = AsmPieces[0];
12547 AsmPieces.clear();
12548 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
12549
Chris Lattner7a2bdde2011-04-15 05:18:47 +000012550 // FIXME: this should verify that we are targeting a 486 or better. If not,
Evan Cheng55d42002011-01-08 01:24:27 +000012551 // we will turn this bswap into something that will be lowered to logical ops
12552 // instead of emitting the bswap asm. For now, we don't support 486 or lower
12553 // so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000012554 // bswap $0
12555 if (AsmPieces.size() == 2 &&
12556 (AsmPieces[0] == "bswap" ||
12557 AsmPieces[0] == "bswapq" ||
12558 AsmPieces[0] == "bswapl") &&
12559 (AsmPieces[1] == "$0" ||
12560 AsmPieces[1] == "${0:q}")) {
12561 // No need to check constraints, nothing other than the equivalent of
12562 // "=r,0" would be valid here.
Evan Cheng55d42002011-01-08 01:24:27 +000012563 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
12564 if (!Ty || Ty->getBitWidth() % 16 != 0)
12565 return false;
12566 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000012567 }
12568 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000012569 if (CI->getType()->isIntegerTy(16) &&
Chris Lattnerb8105652009-07-20 17:51:36 +000012570 AsmPieces.size() == 3 &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000012571 (AsmPieces[0] == "rorw" || AsmPieces[0] == "rolw") &&
Chris Lattnerb8105652009-07-20 17:51:36 +000012572 AsmPieces[1] == "$$8," &&
12573 AsmPieces[2] == "${0:w}" &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000012574 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
12575 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000012576 const std::string &ConstraintsStr = IA->getConstraintString();
12577 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000012578 std::sort(AsmPieces.begin(), AsmPieces.end());
12579 if (AsmPieces.size() == 4 &&
12580 AsmPieces[0] == "~{cc}" &&
12581 AsmPieces[1] == "~{dirflag}" &&
12582 AsmPieces[2] == "~{flags}" &&
12583 AsmPieces[3] == "~{fpsr}") {
Evan Cheng55d42002011-01-08 01:24:27 +000012584 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
12585 if (!Ty || Ty->getBitWidth() % 16 != 0)
12586 return false;
12587 return IntrinsicLowering::LowerToByteSwap(CI);
Dan Gohman0ef701e2010-03-04 19:58:08 +000012588 }
Chris Lattnerb8105652009-07-20 17:51:36 +000012589 }
12590 break;
12591 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000012592 if (CI->getType()->isIntegerTy(32) &&
12593 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
12594 SmallVector<StringRef, 4> Words;
12595 SplitString(AsmPieces[0], Words, " \t,");
12596 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
12597 Words[2] == "${0:w}") {
12598 Words.clear();
12599 SplitString(AsmPieces[1], Words, " \t,");
12600 if (Words.size() == 3 && Words[0] == "rorl" && Words[1] == "$$16" &&
12601 Words[2] == "$0") {
12602 Words.clear();
12603 SplitString(AsmPieces[2], Words, " \t,");
12604 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
12605 Words[2] == "${0:w}") {
12606 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000012607 const std::string &ConstraintsStr = IA->getConstraintString();
12608 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Peter Collingbourne948cf022010-11-13 19:54:30 +000012609 std::sort(AsmPieces.begin(), AsmPieces.end());
12610 if (AsmPieces.size() == 4 &&
12611 AsmPieces[0] == "~{cc}" &&
12612 AsmPieces[1] == "~{dirflag}" &&
12613 AsmPieces[2] == "~{flags}" &&
12614 AsmPieces[3] == "~{fpsr}") {
Evan Cheng55d42002011-01-08 01:24:27 +000012615 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
12616 if (!Ty || Ty->getBitWidth() % 16 != 0)
12617 return false;
12618 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000012619 }
12620 }
12621 }
12622 }
12623 }
Evan Cheng55d42002011-01-08 01:24:27 +000012624
12625 if (CI->getType()->isIntegerTy(64)) {
12626 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
12627 if (Constraints.size() >= 2 &&
12628 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
12629 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
12630 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
12631 SmallVector<StringRef, 4> Words;
12632 SplitString(AsmPieces[0], Words, " \t");
12633 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
Chris Lattnerb8105652009-07-20 17:51:36 +000012634 Words.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000012635 SplitString(AsmPieces[1], Words, " \t");
12636 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
12637 Words.clear();
12638 SplitString(AsmPieces[2], Words, " \t,");
12639 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
12640 Words[2] == "%edx") {
12641 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
12642 if (!Ty || Ty->getBitWidth() % 16 != 0)
12643 return false;
12644 return IntrinsicLowering::LowerToByteSwap(CI);
12645 }
Chris Lattnerb8105652009-07-20 17:51:36 +000012646 }
12647 }
12648 }
12649 }
12650 break;
12651 }
12652 return false;
12653}
12654
12655
12656
Chris Lattnerf4dff842006-07-11 02:54:03 +000012657/// getConstraintType - Given a constraint letter, return the type of
12658/// constraint it is for this target.
12659X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000012660X86TargetLowering::getConstraintType(const std::string &Constraint) const {
12661 if (Constraint.size() == 1) {
12662 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000012663 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000012664 case 'q':
12665 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000012666 case 'f':
12667 case 't':
12668 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000012669 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000012670 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000012671 case 'Y':
Eric Christopher31b5f002011-07-07 22:29:07 +000012672 case 'l':
Chris Lattner4234f572007-03-25 02:14:49 +000012673 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000012674 case 'a':
12675 case 'b':
12676 case 'c':
12677 case 'd':
12678 case 'S':
12679 case 'D':
12680 case 'A':
12681 return C_Register;
12682 case 'I':
12683 case 'J':
12684 case 'K':
12685 case 'L':
12686 case 'M':
12687 case 'N':
12688 case 'G':
12689 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000012690 case 'e':
12691 case 'Z':
12692 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000012693 default:
12694 break;
12695 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000012696 }
Chris Lattner4234f572007-03-25 02:14:49 +000012697 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000012698}
12699
John Thompson44ab89e2010-10-29 17:29:13 +000012700/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000012701/// This object must already have been set up with the operand type
12702/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000012703TargetLowering::ConstraintWeight
12704 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000012705 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000012706 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012707 Value *CallOperandVal = info.CallOperandVal;
12708 // If we don't have a value, we can't do a match,
12709 // but allow it at the lowest weight.
12710 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000012711 return CW_Default;
12712 const Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000012713 // Look at the constraint type.
12714 switch (*constraint) {
12715 default:
John Thompson44ab89e2010-10-29 17:29:13 +000012716 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
12717 case 'R':
12718 case 'q':
12719 case 'Q':
12720 case 'a':
12721 case 'b':
12722 case 'c':
12723 case 'd':
12724 case 'S':
12725 case 'D':
12726 case 'A':
12727 if (CallOperandVal->getType()->isIntegerTy())
12728 weight = CW_SpecificReg;
12729 break;
12730 case 'f':
12731 case 't':
12732 case 'u':
12733 if (type->isFloatingPointTy())
12734 weight = CW_SpecificReg;
12735 break;
12736 case 'y':
Chris Lattner2a786eb2010-12-19 20:19:20 +000012737 if (type->isX86_MMXTy() && Subtarget->hasMMX())
John Thompson44ab89e2010-10-29 17:29:13 +000012738 weight = CW_SpecificReg;
12739 break;
12740 case 'x':
12741 case 'Y':
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012742 if ((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasXMM())
John Thompson44ab89e2010-10-29 17:29:13 +000012743 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012744 break;
12745 case 'I':
12746 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
12747 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000012748 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012749 }
12750 break;
John Thompson44ab89e2010-10-29 17:29:13 +000012751 case 'J':
12752 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12753 if (C->getZExtValue() <= 63)
12754 weight = CW_Constant;
12755 }
12756 break;
12757 case 'K':
12758 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12759 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
12760 weight = CW_Constant;
12761 }
12762 break;
12763 case 'L':
12764 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12765 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
12766 weight = CW_Constant;
12767 }
12768 break;
12769 case 'M':
12770 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12771 if (C->getZExtValue() <= 3)
12772 weight = CW_Constant;
12773 }
12774 break;
12775 case 'N':
12776 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12777 if (C->getZExtValue() <= 0xff)
12778 weight = CW_Constant;
12779 }
12780 break;
12781 case 'G':
12782 case 'C':
12783 if (dyn_cast<ConstantFP>(CallOperandVal)) {
12784 weight = CW_Constant;
12785 }
12786 break;
12787 case 'e':
12788 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12789 if ((C->getSExtValue() >= -0x80000000LL) &&
12790 (C->getSExtValue() <= 0x7fffffffLL))
12791 weight = CW_Constant;
12792 }
12793 break;
12794 case 'Z':
12795 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12796 if (C->getZExtValue() <= 0xffffffff)
12797 weight = CW_Constant;
12798 }
12799 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012800 }
12801 return weight;
12802}
12803
Dale Johannesenba2a0b92008-01-29 02:21:21 +000012804/// LowerXConstraint - try to replace an X constraint, which matches anything,
12805/// with another that has more specific requirements based on the type of the
12806/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000012807const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000012808LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000012809 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
12810 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000012811 if (ConstraintVT.isFloatingPoint()) {
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012812 if (Subtarget->hasXMMInt())
Chris Lattner5e764232008-04-26 23:02:14 +000012813 return "Y";
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012814 if (Subtarget->hasXMM())
Chris Lattner5e764232008-04-26 23:02:14 +000012815 return "x";
12816 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012817
Chris Lattner5e764232008-04-26 23:02:14 +000012818 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000012819}
12820
Chris Lattner48884cd2007-08-25 00:47:38 +000012821/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
12822/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000012823void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +000012824 std::string &Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000012825 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000012826 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000012827 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000012828
Eric Christopher100c8332011-06-02 23:16:42 +000012829 // Only support length 1 constraints for now.
12830 if (Constraint.length() > 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +000012831
Eric Christopher100c8332011-06-02 23:16:42 +000012832 char ConstraintLetter = Constraint[0];
12833 switch (ConstraintLetter) {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012834 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000012835 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000012836 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000012837 if (C->getZExtValue() <= 31) {
12838 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000012839 break;
12840 }
Devang Patel84f7fd22007-03-17 00:13:28 +000012841 }
Chris Lattner48884cd2007-08-25 00:47:38 +000012842 return;
Evan Cheng364091e2008-09-22 23:57:37 +000012843 case 'J':
12844 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000012845 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000012846 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
12847 break;
12848 }
12849 }
12850 return;
12851 case 'K':
12852 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000012853 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000012854 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
12855 break;
12856 }
12857 }
12858 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000012859 case 'N':
12860 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000012861 if (C->getZExtValue() <= 255) {
12862 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000012863 break;
12864 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000012865 }
Chris Lattner48884cd2007-08-25 00:47:38 +000012866 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000012867 case 'e': {
12868 // 32-bit signed value
12869 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000012870 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
12871 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000012872 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000012873 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000012874 break;
12875 }
12876 // FIXME gcc accepts some relocatable values here too, but only in certain
12877 // memory models; it's complicated.
12878 }
12879 return;
12880 }
12881 case 'Z': {
12882 // 32-bit unsigned value
12883 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000012884 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
12885 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000012886 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
12887 break;
12888 }
12889 }
12890 // FIXME gcc accepts some relocatable values here too, but only in certain
12891 // memory models; it's complicated.
12892 return;
12893 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012894 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012895 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000012896 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000012897 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000012898 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000012899 break;
12900 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012901
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000012902 // In any sort of PIC mode addresses need to be computed at runtime by
12903 // adding in a register or some sort of table lookup. These can't
12904 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000012905 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000012906 return;
12907
Chris Lattnerdc43a882007-05-03 16:52:29 +000012908 // If we are in non-pic codegen mode, we allow the address of a global (with
12909 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000012910 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000012911 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000012912
Chris Lattner49921962009-05-08 18:23:14 +000012913 // Match either (GA), (GA+C), (GA+C1+C2), etc.
12914 while (1) {
12915 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
12916 Offset += GA->getOffset();
12917 break;
12918 } else if (Op.getOpcode() == ISD::ADD) {
12919 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
12920 Offset += C->getZExtValue();
12921 Op = Op.getOperand(0);
12922 continue;
12923 }
12924 } else if (Op.getOpcode() == ISD::SUB) {
12925 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
12926 Offset += -C->getZExtValue();
12927 Op = Op.getOperand(0);
12928 continue;
12929 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012930 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012931
Chris Lattner49921962009-05-08 18:23:14 +000012932 // Otherwise, this isn't something we can handle, reject it.
12933 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000012934 }
Eric Christopherfd179292009-08-27 18:07:15 +000012935
Dan Gohman46510a72010-04-15 01:51:59 +000012936 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012937 // If we require an extra load to get this address, as in PIC mode, we
12938 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000012939 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
12940 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012941 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000012942
Devang Patel0d881da2010-07-06 22:08:15 +000012943 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
12944 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000012945 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012946 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012947 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012948
Gabor Greifba36cb52008-08-28 21:40:38 +000012949 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000012950 Ops.push_back(Result);
12951 return;
12952 }
Dale Johannesen1784d162010-06-25 21:55:36 +000012953 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012954}
12955
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012956std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000012957X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000012958 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000012959 // First, see if this is a constraint that directly corresponds to an LLVM
12960 // register class.
12961 if (Constraint.size() == 1) {
12962 // GCC Constraint Letters
12963 switch (Constraint[0]) {
12964 default: break;
Eric Christopherd176af82011-06-29 17:23:50 +000012965 // TODO: Slight differences here in allocation order and leaving
12966 // RIP in the class. Do they matter any more here than they do
12967 // in the normal allocation?
12968 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
12969 if (Subtarget->is64Bit()) {
Nick Lewycky9bf45d02011-07-08 00:19:27 +000012970 if (VT == MVT::i32 || VT == MVT::f32)
Eric Christopherd176af82011-06-29 17:23:50 +000012971 return std::make_pair(0U, X86::GR32RegisterClass);
12972 else if (VT == MVT::i16)
12973 return std::make_pair(0U, X86::GR16RegisterClass);
Eric Christopher5427ede2011-07-14 20:13:52 +000012974 else if (VT == MVT::i8 || VT == MVT::i1)
Eric Christopherd176af82011-06-29 17:23:50 +000012975 return std::make_pair(0U, X86::GR8RegisterClass);
Nick Lewycky9bf45d02011-07-08 00:19:27 +000012976 else if (VT == MVT::i64 || VT == MVT::f64)
Eric Christopherd176af82011-06-29 17:23:50 +000012977 return std::make_pair(0U, X86::GR64RegisterClass);
12978 break;
12979 }
12980 // 32-bit fallthrough
12981 case 'Q': // Q_REGS
Nick Lewycky9bf45d02011-07-08 00:19:27 +000012982 if (VT == MVT::i32 || VT == MVT::f32)
Eric Christopherd176af82011-06-29 17:23:50 +000012983 return std::make_pair(0U, X86::GR32_ABCDRegisterClass);
12984 else if (VT == MVT::i16)
12985 return std::make_pair(0U, X86::GR16_ABCDRegisterClass);
Eric Christopher5427ede2011-07-14 20:13:52 +000012986 else if (VT == MVT::i8 || VT == MVT::i1)
Eric Christopherd176af82011-06-29 17:23:50 +000012987 return std::make_pair(0U, X86::GR8_ABCD_LRegisterClass);
12988 else if (VT == MVT::i64)
12989 return std::make_pair(0U, X86::GR64_ABCDRegisterClass);
12990 break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000012991 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000012992 case 'l': // INDEX_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000012993 if (VT == MVT::i8 || VT == MVT::i1)
Chris Lattner0f65cad2007-04-09 05:49:22 +000012994 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012995 if (VT == MVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +000012996 return std::make_pair(0U, X86::GR16RegisterClass);
Eric Christopher2bbecd82011-05-19 21:33:47 +000012997 if (VT == MVT::i32 || VT == MVT::f32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +000012998 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +000012999 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000013000 case 'R': // LEGACY_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000013001 if (VT == MVT::i8 || VT == MVT::i1)
Dale Johannesen5f3663e2009-10-07 22:47:20 +000013002 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
13003 if (VT == MVT::i16)
13004 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
13005 if (VT == MVT::i32 || !Subtarget->is64Bit())
13006 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
13007 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000013008 case 'f': // FP Stack registers.
13009 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
13010 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000013011 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000013012 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000013013 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000013014 return std::make_pair(0U, X86::RFP64RegisterClass);
13015 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000013016 case 'y': // MMX_REGS if MMX allowed.
13017 if (!Subtarget->hasMMX()) break;
13018 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000013019 case 'Y': // SSE_REGS if SSE2 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000013020 if (!Subtarget->hasXMMInt()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000013021 // FALL THROUGH.
13022 case 'x': // SSE_REGS if SSE1 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000013023 if (!Subtarget->hasXMM()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000013024
Owen Anderson825b72b2009-08-11 20:47:22 +000013025 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000013026 default: break;
13027 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000013028 case MVT::f32:
13029 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +000013030 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000013031 case MVT::f64:
13032 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +000013033 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000013034 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000013035 case MVT::v16i8:
13036 case MVT::v8i16:
13037 case MVT::v4i32:
13038 case MVT::v2i64:
13039 case MVT::v4f32:
13040 case MVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +000013041 return std::make_pair(0U, X86::VR128RegisterClass);
13042 }
Chris Lattnerad043e82007-04-09 05:11:28 +000013043 break;
13044 }
13045 }
Scott Michelfdc40a02009-02-17 22:15:04 +000013046
Chris Lattnerf76d1802006-07-31 23:26:50 +000013047 // Use the default implementation in TargetLowering to convert the register
13048 // constraint into a member of a register class.
13049 std::pair<unsigned, const TargetRegisterClass*> Res;
13050 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000013051
13052 // Not found as a standard register?
13053 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000013054 // Map st(0) -> st(7) -> ST0
13055 if (Constraint.size() == 7 && Constraint[0] == '{' &&
13056 tolower(Constraint[1]) == 's' &&
13057 tolower(Constraint[2]) == 't' &&
13058 Constraint[3] == '(' &&
13059 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
13060 Constraint[5] == ')' &&
13061 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000013062
Chris Lattner56d77c72009-09-13 22:41:48 +000013063 Res.first = X86::ST0+Constraint[4]-'0';
13064 Res.second = X86::RFP80RegisterClass;
13065 return Res;
13066 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000013067
Chris Lattner56d77c72009-09-13 22:41:48 +000013068 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000013069 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000013070 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +000013071 Res.second = X86::RFP80RegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000013072 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000013073 }
Chris Lattner56d77c72009-09-13 22:41:48 +000013074
13075 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000013076 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000013077 Res.first = X86::EFLAGS;
13078 Res.second = X86::CCRRegisterClass;
13079 return Res;
13080 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000013081
Dale Johannesen330169f2008-11-13 21:52:36 +000013082 // 'A' means EAX + EDX.
13083 if (Constraint == "A") {
13084 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +000013085 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000013086 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000013087 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000013088 return Res;
13089 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013090
Chris Lattnerf76d1802006-07-31 23:26:50 +000013091 // Otherwise, check to see if this is a register class of the wrong value
13092 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
13093 // turn into {ax},{dx}.
13094 if (Res.second->hasType(VT))
13095 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013096
Chris Lattnerf76d1802006-07-31 23:26:50 +000013097 // All of the single-register GCC register classes map their values onto
13098 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
13099 // really want an 8-bit or 32-bit register, map to the appropriate register
13100 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +000013101 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000013102 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000013103 unsigned DestReg = 0;
13104 switch (Res.first) {
13105 default: break;
13106 case X86::AX: DestReg = X86::AL; break;
13107 case X86::DX: DestReg = X86::DL; break;
13108 case X86::CX: DestReg = X86::CL; break;
13109 case X86::BX: DestReg = X86::BL; break;
13110 }
13111 if (DestReg) {
13112 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000013113 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000013114 }
Owen Anderson825b72b2009-08-11 20:47:22 +000013115 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000013116 unsigned DestReg = 0;
13117 switch (Res.first) {
13118 default: break;
13119 case X86::AX: DestReg = X86::EAX; break;
13120 case X86::DX: DestReg = X86::EDX; break;
13121 case X86::CX: DestReg = X86::ECX; break;
13122 case X86::BX: DestReg = X86::EBX; break;
13123 case X86::SI: DestReg = X86::ESI; break;
13124 case X86::DI: DestReg = X86::EDI; break;
13125 case X86::BP: DestReg = X86::EBP; break;
13126 case X86::SP: DestReg = X86::ESP; break;
13127 }
13128 if (DestReg) {
13129 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000013130 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000013131 }
Owen Anderson825b72b2009-08-11 20:47:22 +000013132 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000013133 unsigned DestReg = 0;
13134 switch (Res.first) {
13135 default: break;
13136 case X86::AX: DestReg = X86::RAX; break;
13137 case X86::DX: DestReg = X86::RDX; break;
13138 case X86::CX: DestReg = X86::RCX; break;
13139 case X86::BX: DestReg = X86::RBX; break;
13140 case X86::SI: DestReg = X86::RSI; break;
13141 case X86::DI: DestReg = X86::RDI; break;
13142 case X86::BP: DestReg = X86::RBP; break;
13143 case X86::SP: DestReg = X86::RSP; break;
13144 }
13145 if (DestReg) {
13146 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000013147 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000013148 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000013149 }
Chris Lattner6ba50a92008-08-26 06:19:02 +000013150 } else if (Res.second == X86::FR32RegisterClass ||
13151 Res.second == X86::FR64RegisterClass ||
13152 Res.second == X86::VR128RegisterClass) {
13153 // Handle references to XMM physical registers that got mapped into the
13154 // wrong class. This can happen with constraints like {xmm0} where the
13155 // target independent register mapper will just pick the first match it can
13156 // find, ignoring the required type.
Owen Anderson825b72b2009-08-11 20:47:22 +000013157 if (VT == MVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +000013158 Res.second = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +000013159 else if (VT == MVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +000013160 Res.second = X86::FR64RegisterClass;
13161 else if (X86::VR128RegisterClass->hasType(VT))
13162 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000013163 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013164
Chris Lattnerf76d1802006-07-31 23:26:50 +000013165 return Res;
13166}