blob: e06d84c54a0da505c6ec6c02a0f662aae2995c4f [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
Evan Cheng6be2c582006-04-05 23:38:46 +00001066 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +00001067 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +00001068
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001069
Eli Friedman962f5492010-06-02 19:35:46 +00001070 // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
1071 // handle type legalization for these operations here.
Dan Gohman71c62a22010-06-02 19:13:40 +00001072 //
Eli Friedman962f5492010-06-02 19:35:46 +00001073 // FIXME: We really should do custom legalization for addition and
1074 // subtraction on x86-32 once PR3203 is fixed. We really can't do much better
1075 // than generic legalization for 64-bit multiplication-with-overflow, though.
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001076 for (unsigned i = 0, e = 3+Subtarget->is64Bit(); i != e; ++i) {
1077 // Add/Sub/Mul with overflow operations are custom lowered.
1078 MVT VT = IntVTs[i];
1079 setOperationAction(ISD::SADDO, VT, Custom);
1080 setOperationAction(ISD::UADDO, VT, Custom);
1081 setOperationAction(ISD::SSUBO, VT, Custom);
1082 setOperationAction(ISD::USUBO, VT, Custom);
1083 setOperationAction(ISD::SMULO, VT, Custom);
1084 setOperationAction(ISD::UMULO, VT, Custom);
Eli Friedmana993f0a2010-06-02 00:27:18 +00001085 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001086
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001087 // There are no 8-bit 3-address imul/mul instructions
1088 setOperationAction(ISD::SMULO, MVT::i8, Expand);
1089 setOperationAction(ISD::UMULO, MVT::i8, Expand);
Bill Wendling41ea7e72008-11-24 19:21:46 +00001090
Evan Chengd54f2d52009-03-31 19:38:51 +00001091 if (!Subtarget->is64Bit()) {
1092 // These libcalls are not available in 32-bit.
1093 setLibcallName(RTLIB::SHL_I128, 0);
1094 setLibcallName(RTLIB::SRL_I128, 0);
1095 setLibcallName(RTLIB::SRA_I128, 0);
1096 }
1097
Evan Cheng206ee9d2006-07-07 08:33:52 +00001098 // We have target-specific dag combine patterns for the following nodes:
1099 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00001100 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Evan Chengd880b972008-05-09 21:53:03 +00001101 setTargetDAGCombine(ISD::BUILD_VECTOR);
Chris Lattner83e6c992006-10-04 06:57:07 +00001102 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +00001103 setTargetDAGCombine(ISD::SHL);
1104 setTargetDAGCombine(ISD::SRA);
1105 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +00001106 setTargetDAGCombine(ISD::OR);
Nate Begemanb65c1752010-12-17 22:55:37 +00001107 setTargetDAGCombine(ISD::AND);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001108 setTargetDAGCombine(ISD::ADD);
1109 setTargetDAGCombine(ISD::SUB);
Chris Lattner149a4e52008-02-22 02:09:43 +00001110 setTargetDAGCombine(ISD::STORE);
Evan Cheng2e489c42009-12-16 00:53:11 +00001111 setTargetDAGCombine(ISD::ZERO_EXTEND);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +00001112 setTargetDAGCombine(ISD::SINT_TO_FP);
Evan Cheng0b0cd912009-03-28 05:57:29 +00001113 if (Subtarget->is64Bit())
1114 setTargetDAGCombine(ISD::MUL);
Evan Cheng206ee9d2006-07-07 08:33:52 +00001115
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001116 computeRegisterProperties();
1117
Evan Cheng05219282011-01-06 06:52:41 +00001118 // On Darwin, -Os means optimize for size without hurting performance,
1119 // do not reduce the limit.
Dan Gohman87060f52008-06-30 21:00:56 +00001120 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001121 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 16 : 8;
Evan Cheng255f20f2010-04-01 06:04:33 +00001122 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001123 maxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
1124 maxStoresPerMemmove = 8; // For @llvm.memmove -> sequence of stores
1125 maxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Evan Chengfb8075d2008-02-28 00:43:03 +00001126 setPrefLoopAlignment(16);
Evan Cheng6ebf7bc2009-05-13 21:42:09 +00001127 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +00001128
1129 setPrefFunctionAlignment(4);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001130}
1131
Scott Michel5b8f82e2008-03-10 15:42:14 +00001132
Owen Anderson825b72b2009-08-11 20:47:22 +00001133MVT::SimpleValueType X86TargetLowering::getSetCCResultType(EVT VT) const {
1134 return MVT::i8;
Scott Michel5b8f82e2008-03-10 15:42:14 +00001135}
1136
1137
Evan Cheng29286502008-01-23 23:17:41 +00001138/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1139/// the desired ByVal argument alignment.
1140static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
1141 if (MaxAlign == 16)
1142 return;
1143 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
1144 if (VTy->getBitWidth() == 128)
1145 MaxAlign = 16;
Evan Cheng29286502008-01-23 23:17:41 +00001146 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
1147 unsigned EltAlign = 0;
1148 getMaxByValAlign(ATy->getElementType(), EltAlign);
1149 if (EltAlign > MaxAlign)
1150 MaxAlign = EltAlign;
1151 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
1152 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1153 unsigned EltAlign = 0;
1154 getMaxByValAlign(STy->getElementType(i), EltAlign);
1155 if (EltAlign > MaxAlign)
1156 MaxAlign = EltAlign;
1157 if (MaxAlign == 16)
1158 break;
1159 }
1160 }
1161 return;
1162}
1163
1164/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1165/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001166/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1167/// are at 4-byte boundaries.
Evan Cheng29286502008-01-23 23:17:41 +00001168unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001169 if (Subtarget->is64Bit()) {
1170 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001171 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001172 if (TyAlign > 8)
1173 return TyAlign;
1174 return 8;
1175 }
1176
Evan Cheng29286502008-01-23 23:17:41 +00001177 unsigned Align = 4;
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001178 if (Subtarget->hasXMM())
Dale Johannesen0c191872008-02-08 19:48:20 +00001179 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001180 return Align;
1181}
Chris Lattner2b02a442007-02-25 08:29:00 +00001182
Evan Chengf0df0312008-05-15 08:39:06 +00001183/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001184/// and store operations as a result of memset, memcpy, and memmove
1185/// lowering. If DstAlign is zero that means it's safe to destination
1186/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1187/// means there isn't a need to check it against alignment requirement,
1188/// probably because the source does not need to be loaded. If
1189/// 'NonScalarIntSafe' is true, that means it's safe to return a
1190/// non-scalar-integer type, e.g. empty string source, constant, or loaded
1191/// from memory. 'MemcpyStrSrc' indicates whether the memcpy source is
1192/// constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001193/// It returns EVT::Other if the type should be determined using generic
1194/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001195EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001196X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1197 unsigned DstAlign, unsigned SrcAlign,
Evan Chengf28f8bc2010-04-02 19:36:14 +00001198 bool NonScalarIntSafe,
Evan Chengc3b0c342010-04-08 07:37:57 +00001199 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001200 MachineFunction &MF) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001201 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1202 // linux. This is because the stack realignment code can't handle certain
1203 // cases like PR2962. This should be removed when PR2962 is fixed.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001204 const Function *F = MF.getFunction();
Evan Chenga5e13622011-01-07 19:35:30 +00001205 if (NonScalarIntSafe &&
1206 !F->hasFnAttr(Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001207 if (Size >= 16 &&
Evan Chenga5e13622011-01-07 19:35:30 +00001208 (Subtarget->isUnalignedMemAccessFast() ||
1209 ((DstAlign == 0 || DstAlign >= 16) &&
1210 (SrcAlign == 0 || SrcAlign >= 16))) &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001211 Subtarget->getStackAlignment() >= 16) {
1212 if (Subtarget->hasSSE2())
1213 return MVT::v4i32;
Evan Chengf28f8bc2010-04-02 19:36:14 +00001214 if (Subtarget->hasSSE1())
Evan Cheng255f20f2010-04-01 06:04:33 +00001215 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001216 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001217 !Subtarget->is64Bit() &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001218 Subtarget->getStackAlignment() >= 8 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001219 Subtarget->hasXMMInt()) {
Evan Chengc3b0c342010-04-08 07:37:57 +00001220 // Do not use f64 to lower memcpy if source is string constant. It's
1221 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001222 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001223 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001224 }
Evan Chengf0df0312008-05-15 08:39:06 +00001225 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001226 return MVT::i64;
1227 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001228}
1229
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001230/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1231/// current function. The returned value is a member of the
1232/// MachineJumpTableInfo::JTEntryKind enum.
1233unsigned X86TargetLowering::getJumpTableEncoding() const {
1234 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1235 // symbol.
1236 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1237 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001238 return MachineJumpTableInfo::EK_Custom32;
Michael J. Spencerec38de22010-10-10 22:04:20 +00001239
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001240 // Otherwise, use the normal jump table encoding heuristics.
1241 return TargetLowering::getJumpTableEncoding();
1242}
1243
Chris Lattnerc64daab2010-01-26 05:02:42 +00001244const MCExpr *
1245X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1246 const MachineBasicBlock *MBB,
1247 unsigned uid,MCContext &Ctx) const{
1248 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1249 Subtarget->isPICStyleGOT());
1250 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1251 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001252 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1253 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001254}
1255
Evan Chengcc415862007-11-09 01:32:10 +00001256/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1257/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001258SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001259 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001260 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001261 // This doesn't have DebugLoc associated with it, but is not really the
1262 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001263 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001264 return Table;
1265}
1266
Chris Lattner589c6f62010-01-26 06:28:43 +00001267/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1268/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1269/// MCExpr.
1270const MCExpr *X86TargetLowering::
1271getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1272 MCContext &Ctx) const {
1273 // X86-64 uses RIP relative addressing based on the jump table label.
1274 if (Subtarget->isPICStyleRIPRel())
1275 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1276
1277 // Otherwise, the reference is relative to the PIC base.
Chris Lattner142b5312010-11-14 22:48:15 +00001278 return MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
Chris Lattner589c6f62010-01-26 06:28:43 +00001279}
1280
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001281// FIXME: Why this routine is here? Move to RegInfo!
Evan Chengdee81012010-07-26 21:50:05 +00001282std::pair<const TargetRegisterClass*, uint8_t>
1283X86TargetLowering::findRepresentativeClass(EVT VT) const{
1284 const TargetRegisterClass *RRC = 0;
1285 uint8_t Cost = 1;
1286 switch (VT.getSimpleVT().SimpleTy) {
1287 default:
1288 return TargetLowering::findRepresentativeClass(VT);
1289 case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
1290 RRC = (Subtarget->is64Bit()
1291 ? X86::GR64RegisterClass : X86::GR32RegisterClass);
1292 break;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001293 case MVT::x86mmx:
Evan Chengdee81012010-07-26 21:50:05 +00001294 RRC = X86::VR64RegisterClass;
1295 break;
1296 case MVT::f32: case MVT::f64:
1297 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1298 case MVT::v4f32: case MVT::v2f64:
1299 case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1300 case MVT::v4f64:
1301 RRC = X86::VR128RegisterClass;
1302 break;
1303 }
1304 return std::make_pair(RRC, Cost);
1305}
1306
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001307bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1308 unsigned &Offset) const {
1309 if (!Subtarget->isTargetLinux())
1310 return false;
1311
1312 if (Subtarget->is64Bit()) {
1313 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1314 Offset = 0x28;
1315 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1316 AddressSpace = 256;
1317 else
1318 AddressSpace = 257;
1319 } else {
1320 // %gs:0x14 on i386
1321 Offset = 0x14;
1322 AddressSpace = 256;
1323 }
1324 return true;
1325}
1326
1327
Chris Lattner2b02a442007-02-25 08:29:00 +00001328//===----------------------------------------------------------------------===//
1329// Return Value Calling Convention Implementation
1330//===----------------------------------------------------------------------===//
1331
Chris Lattner59ed56b2007-02-28 04:55:35 +00001332#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001333
Michael J. Spencerec38de22010-10-10 22:04:20 +00001334bool
Eric Christopher471e4222011-06-08 23:55:35 +00001335X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv,
1336 MachineFunction &MF, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001337 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001338 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001339 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001340 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001341 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001342 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001343}
1344
Dan Gohman98ca4f22009-08-05 01:29:28 +00001345SDValue
1346X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001347 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001348 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001349 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001350 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001351 MachineFunction &MF = DAG.getMachineFunction();
1352 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001353
Chris Lattner9774c912007-02-27 05:28:59 +00001354 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001355 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001356 RVLocs, *DAG.getContext());
1357 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001358
Evan Chengdcea1632010-02-04 02:40:39 +00001359 // Add the regs to the liveout set for the function.
1360 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1361 for (unsigned i = 0; i != RVLocs.size(); ++i)
1362 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1363 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001364
Dan Gohman475871a2008-07-27 21:46:04 +00001365 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001366
Dan Gohman475871a2008-07-27 21:46:04 +00001367 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001368 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1369 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001370 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1371 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001372
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001373 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001374 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1375 CCValAssign &VA = RVLocs[i];
1376 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001377 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001378 EVT ValVT = ValToCopy.getValueType();
1379
Dale Johannesenc4510512010-09-24 19:05:48 +00001380 // If this is x86-64, and we disabled SSE, we can't return FP values,
1381 // or SSE or MMX vectors.
1382 if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
1383 VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001384 (Subtarget->is64Bit() && !Subtarget->hasXMM())) {
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001385 report_fatal_error("SSE register return with SSE disabled");
1386 }
1387 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1388 // llvm-gcc has never done it right and no one has noticed, so this
1389 // should be OK for now.
1390 if (ValVT == MVT::f64 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001391 (Subtarget->is64Bit() && !Subtarget->hasXMMInt()))
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001392 report_fatal_error("SSE2 register return with SSE2 disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001393
Chris Lattner447ff682008-03-11 03:23:40 +00001394 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1395 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001396 if (VA.getLocReg() == X86::ST0 ||
1397 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001398 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1399 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001400 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001401 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001402 RetOps.push_back(ValToCopy);
1403 // Don't emit a copytoreg.
1404 continue;
1405 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001406
Evan Cheng242b38b2009-02-23 09:03:22 +00001407 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1408 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001409 if (Subtarget->is64Bit()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001410 if (ValVT == MVT::x86mmx) {
Chris Lattner97a2a562010-08-26 05:24:29 +00001411 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001412 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ValToCopy);
Eric Christopher90eb4022010-07-22 00:26:08 +00001413 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1414 ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001415 // If we don't have SSE2 available, convert to v4f32 so the generated
1416 // register is legal.
1417 if (!Subtarget->hasSSE2())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001418 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32,ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001419 }
Evan Cheng242b38b2009-02-23 09:03:22 +00001420 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001421 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00001422
Dale Johannesendd64c412009-02-04 00:33:20 +00001423 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001424 Flag = Chain.getValue(1);
1425 }
Dan Gohman61a92132008-04-21 23:59:07 +00001426
1427 // The x86-64 ABI for returning structs by value requires that we copy
1428 // the sret argument into %rax for the return. We saved the argument into
1429 // a virtual register in the entry block, so now we copy the value out
1430 // and into %rax.
1431 if (Subtarget->is64Bit() &&
1432 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1433 MachineFunction &MF = DAG.getMachineFunction();
1434 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1435 unsigned Reg = FuncInfo->getSRetReturnReg();
Michael J. Spencerec38de22010-10-10 22:04:20 +00001436 assert(Reg &&
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001437 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001438 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001439
Dale Johannesendd64c412009-02-04 00:33:20 +00001440 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001441 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001442
1443 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001444 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001445 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001446
Chris Lattner447ff682008-03-11 03:23:40 +00001447 RetOps[0] = Chain; // Update chain.
1448
1449 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001450 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001451 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001452
1453 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001454 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001455}
1456
Evan Cheng3d2125c2010-11-30 23:55:39 +00001457bool X86TargetLowering::isUsedByReturnOnly(SDNode *N) const {
1458 if (N->getNumValues() != 1)
1459 return false;
1460 if (!N->hasNUsesOfValue(1, 0))
1461 return false;
1462
1463 SDNode *Copy = *N->use_begin();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001464 if (Copy->getOpcode() != ISD::CopyToReg &&
1465 Copy->getOpcode() != ISD::FP_EXTEND)
Evan Cheng3d2125c2010-11-30 23:55:39 +00001466 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001467
1468 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001469 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001470 UI != UE; ++UI) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001471 if (UI->getOpcode() != X86ISD::RET_FLAG)
1472 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001473 HasRet = true;
1474 }
Evan Cheng3d2125c2010-11-30 23:55:39 +00001475
Evan Cheng1bf891a2010-12-01 22:59:46 +00001476 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001477}
1478
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001479EVT
1480X86TargetLowering::getTypeForExtArgOrReturn(LLVMContext &Context, EVT VT,
Cameron Zwarich44579682011-03-17 14:21:56 +00001481 ISD::NodeType ExtendKind) const {
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001482 MVT ReturnMVT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001483 // TODO: Is this also valid on 32-bit?
1484 if (Subtarget->is64Bit() && VT == MVT::i1 && ExtendKind == ISD::ZERO_EXTEND)
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001485 ReturnMVT = MVT::i8;
1486 else
1487 ReturnMVT = MVT::i32;
1488
1489 EVT MinVT = getRegisterType(Context, ReturnMVT);
1490 return VT.bitsLT(MinVT) ? MinVT : VT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001491}
1492
Dan Gohman98ca4f22009-08-05 01:29:28 +00001493/// LowerCallResult - Lower the result values of a call into the
1494/// appropriate copies out of appropriate physical registers.
1495///
1496SDValue
1497X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001498 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001499 const SmallVectorImpl<ISD::InputArg> &Ins,
1500 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001501 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001502
Chris Lattnere32bbf62007-02-28 07:09:55 +00001503 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001504 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001505 bool Is64Bit = Subtarget->is64Bit();
Eric Christopher471e4222011-06-08 23:55:35 +00001506 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1507 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001508 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001509
Chris Lattner3085e152007-02-25 08:59:22 +00001510 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001511 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001512 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001513 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001514
Torok Edwin3f142c32009-02-01 18:15:56 +00001515 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001516 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001517 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasXMM())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001518 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001519 }
1520
Evan Cheng79fb3b42009-02-20 20:43:02 +00001521 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001522
1523 // If this is a call to a function that returns an fp value on the floating
1524 // point stack, we must guarantee the the value is popped from the stack, so
1525 // a CopyFromReg is not good enough - the copy instruction may be eliminated
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001526 // if the return value is not used. We use the FpPOP_RETVAL instruction
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001527 // instead.
1528 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1529 // If we prefer to use the value in xmm registers, copy it out as f80 and
1530 // use a truncate to move it from fp stack reg to xmm reg.
1531 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001532 SDValue Ops[] = { Chain, InFlag };
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001533 Chain = SDValue(DAG.getMachineNode(X86::FpPOP_RETVAL, dl, CopyVT,
1534 MVT::Other, MVT::Glue, Ops, 2), 1);
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001535 Val = Chain.getValue(0);
1536
1537 // Round the f80 to the right size, which also moves it to the appropriate
1538 // xmm register.
1539 if (CopyVT != VA.getValVT())
1540 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1541 // This truncation won't change the value.
1542 DAG.getIntPtrConstant(1));
Evan Cheng79fb3b42009-02-20 20:43:02 +00001543 } else {
1544 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1545 CopyVT, InFlag).getValue(1);
1546 Val = Chain.getValue(0);
1547 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001548 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001549 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001550 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001551
Dan Gohman98ca4f22009-08-05 01:29:28 +00001552 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001553}
1554
1555
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001556//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001557// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001558//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001559// StdCall calling convention seems to be standard for many Windows' API
1560// routines and around. It differs from C calling convention just a little:
1561// callee should clean up the stack, not caller. Symbols should be also
1562// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001563// For info on fast calling convention see Fast Calling Convention (tail call)
1564// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001565
Dan Gohman98ca4f22009-08-05 01:29:28 +00001566/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001567/// semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001568static bool CallIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
1569 if (Outs.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001570 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001571
Dan Gohman98ca4f22009-08-05 01:29:28 +00001572 return Outs[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001573}
1574
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001575/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001576/// return semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001577static bool
1578ArgsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
1579 if (Ins.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001580 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001581
Dan Gohman98ca4f22009-08-05 01:29:28 +00001582 return Ins[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001583}
1584
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001585/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1586/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001587/// the specific parameter attribute. The copy will be passed as a byval
1588/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001589static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001590CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001591 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1592 DebugLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00001593 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Michael J. Spencerec38de22010-10-10 22:04:20 +00001594
Dale Johannesendd64c412009-02-04 00:33:20 +00001595 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Stuart Hastings03d58262011-03-10 00:25:53 +00001596 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001597 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001598}
1599
Chris Lattner29689432010-03-11 00:22:57 +00001600/// IsTailCallConvention - Return true if the calling convention is one that
1601/// supports tail call optimization.
1602static bool IsTailCallConvention(CallingConv::ID CC) {
1603 return (CC == CallingConv::Fast || CC == CallingConv::GHC);
1604}
1605
Evan Cheng485fafc2011-03-21 01:19:09 +00001606bool X86TargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
1607 if (!CI->isTailCall())
1608 return false;
1609
1610 CallSite CS(CI);
1611 CallingConv::ID CalleeCC = CS.getCallingConv();
1612 if (!IsTailCallConvention(CalleeCC) && CalleeCC != CallingConv::C)
1613 return false;
1614
1615 return true;
1616}
1617
Evan Cheng0c439eb2010-01-27 00:07:07 +00001618/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1619/// a tailcall target by changing its ABI.
1620static bool FuncIsMadeTailCallSafe(CallingConv::ID CC) {
Chris Lattner29689432010-03-11 00:22:57 +00001621 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001622}
1623
Dan Gohman98ca4f22009-08-05 01:29:28 +00001624SDValue
1625X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001626 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001627 const SmallVectorImpl<ISD::InputArg> &Ins,
1628 DebugLoc dl, SelectionDAG &DAG,
1629 const CCValAssign &VA,
1630 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001631 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001632 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001633 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Evan Cheng0c439eb2010-01-27 00:07:07 +00001634 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001635 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001636 EVT ValVT;
1637
1638 // If value is passed by pointer we have address passed instead of the value
1639 // itself.
1640 if (VA.getLocInfo() == CCValAssign::Indirect)
1641 ValVT = VA.getLocVT();
1642 else
1643 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001644
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001645 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001646 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001647 // In case of tail call optimization mark all arguments mutable. Since they
1648 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001649 if (Flags.isByVal()) {
Evan Chengee2e0e32011-03-30 23:44:13 +00001650 unsigned Bytes = Flags.getByValSize();
1651 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
1652 int FI = MFI->CreateFixedObject(Bytes, VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001653 return DAG.getFrameIndex(FI, getPointerTy());
1654 } else {
1655 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001656 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001657 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1658 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001659 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00001660 false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001661 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001662}
1663
Dan Gohman475871a2008-07-27 21:46:04 +00001664SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001665X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001666 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001667 bool isVarArg,
1668 const SmallVectorImpl<ISD::InputArg> &Ins,
1669 DebugLoc dl,
1670 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001671 SmallVectorImpl<SDValue> &InVals)
1672 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001673 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001674 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001675
Gordon Henriksen86737662008-01-05 16:56:59 +00001676 const Function* Fn = MF.getFunction();
1677 if (Fn->hasExternalLinkage() &&
1678 Subtarget->isTargetCygMing() &&
1679 Fn->getName() == "main")
1680 FuncInfo->setForceFramePointer(true);
1681
Evan Cheng1bc78042006-04-26 01:20:17 +00001682 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001683 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001684 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001685
Chris Lattner29689432010-03-11 00:22:57 +00001686 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1687 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001688
Chris Lattner638402b2007-02-28 07:00:42 +00001689 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001690 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001691 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001692 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001693
1694 // Allocate shadow area for Win64
1695 if (IsWin64) {
1696 CCInfo.AllocateStack(32, 8);
1697 }
1698
Duncan Sands45907662010-10-31 13:21:44 +00001699 CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001700
Chris Lattnerf39f7712007-02-28 05:46:49 +00001701 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001702 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001703 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1704 CCValAssign &VA = ArgLocs[i];
1705 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1706 // places.
1707 assert(VA.getValNo() != LastVal &&
1708 "Don't support value assigned to multiple locs yet");
1709 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001710
Chris Lattnerf39f7712007-02-28 05:46:49 +00001711 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001712 EVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001713 TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001714 if (RegVT == MVT::i32)
Chris Lattnerf39f7712007-02-28 05:46:49 +00001715 RC = X86::GR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001716 else if (Is64Bit && RegVT == MVT::i64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001717 RC = X86::GR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001718 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001719 RC = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001720 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001721 RC = X86::FR64RegisterClass;
Bruno Cardoso Lopesac098352010-08-05 23:35:51 +00001722 else if (RegVT.isVector() && RegVT.getSizeInBits() == 256)
1723 RC = X86::VR256RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001724 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001725 RC = X86::VR128RegisterClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001726 else if (RegVT == MVT::x86mmx)
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001727 RC = X86::VR64RegisterClass;
1728 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001729 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001730
Devang Patel68e6bee2011-02-21 23:21:26 +00001731 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001732 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001733
Chris Lattnerf39f7712007-02-28 05:46:49 +00001734 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1735 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1736 // right size.
1737 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001738 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001739 DAG.getValueType(VA.getValVT()));
1740 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001741 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001742 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001743 else if (VA.getLocInfo() == CCValAssign::BCvt)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001744 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001745
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001746 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001747 // Handle MMX values passed in XMM regs.
1748 if (RegVT.isVector()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001749 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(),
1750 ArgValue);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001751 } else
1752 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001753 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001754 } else {
1755 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001756 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001757 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001758
1759 // If value is passed via pointer - do a load.
1760 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00001761 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
1762 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001763
Dan Gohman98ca4f22009-08-05 01:29:28 +00001764 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001765 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001766
Dan Gohman61a92132008-04-21 23:59:07 +00001767 // The x86-64 ABI for returning structs by value requires that we copy
1768 // the sret argument into %rax for the return. Save the argument into
1769 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001770 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001771 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1772 unsigned Reg = FuncInfo->getSRetReturnReg();
1773 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001774 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001775 FuncInfo->setSRetReturnReg(Reg);
1776 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001777 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001778 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001779 }
1780
Chris Lattnerf39f7712007-02-28 05:46:49 +00001781 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001782 // Align stack specially for tail calls.
1783 if (FuncIsMadeTailCallSafe(CallConv))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001784 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001785
Evan Cheng1bc78042006-04-26 01:20:17 +00001786 // If the function takes variable number of arguments, make a frame index for
1787 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001788 if (isVarArg) {
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001789 if (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
1790 CallConv != CallingConv::X86_ThisCall)) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00001791 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00001792 }
1793 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001794 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1795
1796 // FIXME: We should really autogenerate these arrays
1797 static const unsigned GPR64ArgRegsWin64[] = {
1798 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001799 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001800 static const unsigned GPR64ArgRegs64Bit[] = {
1801 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1802 };
1803 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001804 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1805 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1806 };
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001807 const unsigned *GPR64ArgRegs;
1808 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001809
1810 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001811 // The XMM registers which might contain var arg parameters are shadowed
1812 // in their paired GPR. So we only need to save the GPR to their home
1813 // slots.
1814 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001815 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001816 } else {
1817 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1818 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001819
1820 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit, TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001821 }
1822 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1823 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001824
Devang Patel578efa92009-06-05 21:57:13 +00001825 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001826 assert(!(NumXMMRegs && !Subtarget->hasXMM()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001827 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001828 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001829 "SSE register cannot be used when SSE is disabled!");
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001830 if (UseSoftFloat || NoImplicitFloatOps || !Subtarget->hasXMM())
Torok Edwin3f142c32009-02-01 18:15:56 +00001831 // Kernel mode asks for SSE to be disabled, so don't push them
1832 // on the stack.
1833 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001834
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001835 if (IsWin64) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001836 const TargetFrameLowering &TFI = *getTargetMachine().getFrameLowering();
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001837 // Get to the caller-allocated home save location. Add 8 to account
1838 // for the return address.
1839 int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001840 FuncInfo->setRegSaveFrameIndex(
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001841 MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001842 // Fixup to set vararg frame on shadow area (4 x i64).
1843 if (NumIntRegs < 4)
1844 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001845 } else {
1846 // For X86-64, if there are vararg parameters that are passed via
1847 // registers, then we must store them to their spots on the stack so they
1848 // may be loaded by deferencing the result of va_next.
1849 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
1850 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
1851 FuncInfo->setRegSaveFrameIndex(
1852 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00001853 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001854 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001855
Gordon Henriksen86737662008-01-05 16:56:59 +00001856 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001857 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00001858 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
1859 getPointerTy());
1860 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001861 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00001862 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
1863 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00001864 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00001865 X86::GR64RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00001866 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001867 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001868 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001869 MachinePointerInfo::getFixedStack(
1870 FuncInfo->getRegSaveFrameIndex(), Offset),
1871 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001872 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001873 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001874 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001875
Dan Gohmanface41a2009-08-16 21:24:25 +00001876 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
1877 // Now store the XMM (fp + vector) parameter registers.
1878 SmallVector<SDValue, 11> SaveXMMOps;
1879 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001880
Devang Patel68e6bee2011-02-21 23:21:26 +00001881 unsigned AL = MF.addLiveIn(X86::AL, X86::GR8RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00001882 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
1883 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001884
Dan Gohman1e93df62010-04-17 14:41:14 +00001885 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1886 FuncInfo->getRegSaveFrameIndex()));
1887 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1888 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00001889
Dan Gohmanface41a2009-08-16 21:24:25 +00001890 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001891 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00001892 X86::VR128RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00001893 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
1894 SaveXMMOps.push_back(Val);
1895 }
1896 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
1897 MVT::Other,
1898 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00001899 }
Dan Gohmanface41a2009-08-16 21:24:25 +00001900
1901 if (!MemOps.empty())
1902 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1903 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001904 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001905 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001906
Gordon Henriksen86737662008-01-05 16:56:59 +00001907 // Some CCs need callee pop.
Evan Chengef41ff62011-06-23 17:54:54 +00001908 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg, GuaranteedTailCallOpt)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001909 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001910 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00001911 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001912 // If this is an sret function, the return should pop the hidden pointer.
Chris Lattner29689432010-03-11 00:22:57 +00001913 if (!Is64Bit && !IsTailCallConvention(CallConv) && ArgsAreStructReturn(Ins))
Dan Gohman1e93df62010-04-17 14:41:14 +00001914 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001915 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001916
Gordon Henriksen86737662008-01-05 16:56:59 +00001917 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001918 // RegSaveFrameIndex is X86-64 only.
1919 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001920 if (CallConv == CallingConv::X86_FastCall ||
1921 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00001922 // fastcc functions can't have varargs.
1923 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00001924 }
Evan Cheng25caf632006-05-23 21:06:34 +00001925
Dan Gohman98ca4f22009-08-05 01:29:28 +00001926 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001927}
1928
Dan Gohman475871a2008-07-27 21:46:04 +00001929SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001930X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
1931 SDValue StackPtr, SDValue Arg,
1932 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00001933 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001934 ISD::ArgFlagsTy Flags) const {
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001935 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00001936 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00001937 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001938 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00001939 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001940
1941 return DAG.getStore(Chain, dl, Arg, PtrOff,
1942 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00001943 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00001944}
1945
Bill Wendling64e87322009-01-16 19:25:27 +00001946/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001947/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00001948SDValue
1949X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00001950 SDValue &OutRetAddr, SDValue Chain,
1951 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00001952 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001953 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00001954 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001955 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00001956
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001957 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00001958 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
1959 false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001960 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001961}
1962
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001963/// EmitTailCallStoreRetAddr - Emit a store of the return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001964/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00001965static SDValue
1966EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00001967 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00001968 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001969 // Store the return address to the appropriate stack slot.
1970 if (!FPDiff) return Chain;
1971 // Calculate the new stack slot for the return address.
1972 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00001973 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00001974 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00001975 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00001976 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001977 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00001978 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00001979 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001980 return Chain;
1981}
1982
Dan Gohman98ca4f22009-08-05 01:29:28 +00001983SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001984X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001985 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001986 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001987 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001988 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001989 const SmallVectorImpl<ISD::InputArg> &Ins,
1990 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001991 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001992 MachineFunction &MF = DAG.getMachineFunction();
1993 bool Is64Bit = Subtarget->is64Bit();
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00001994 bool IsWin64 = Subtarget->isTargetWin64();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001995 bool IsStructRet = CallIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00001996 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001997
Evan Cheng5f941932010-02-05 02:21:12 +00001998 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00001999 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00002000 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
2001 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00002002 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00002003
2004 // Sibcalls are automatically detected tailcalls which do not require
2005 // ABI changes.
Dan Gohman1797ed52010-02-08 20:27:50 +00002006 if (!GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00002007 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00002008
2009 if (isTailCall)
2010 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00002011 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00002012
Chris Lattner29689432010-03-11 00:22:57 +00002013 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
2014 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00002015
Chris Lattner638402b2007-02-28 07:00:42 +00002016 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00002017 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002018 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00002019 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002020
2021 // Allocate shadow area for Win64
2022 if (IsWin64) {
2023 CCInfo.AllocateStack(32, 8);
2024 }
2025
Duncan Sands45907662010-10-31 13:21:44 +00002026 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00002027
Chris Lattner423c5f42007-02-28 05:31:48 +00002028 // Get a count of how many bytes are to be pushed on the stack.
2029 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00002030 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00002031 // This is a sibcall. The memory operands are available in caller's
2032 // own caller's stack.
2033 NumBytes = 0;
Chris Lattner29689432010-03-11 00:22:57 +00002034 else if (GuaranteedTailCallOpt && IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00002035 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002036
Gordon Henriksen86737662008-01-05 16:56:59 +00002037 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00002038 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002039 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00002040 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00002041 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
2042 FPDiff = NumBytesCallerPushed - NumBytes;
2043
2044 // Set the delta of movement of the returnaddr stackslot.
2045 // But only set if delta is greater than previous delta.
2046 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
2047 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
2048 }
2049
Evan Chengf22f9b32010-02-06 03:28:46 +00002050 if (!IsSibcall)
2051 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002052
Dan Gohman475871a2008-07-27 21:46:04 +00002053 SDValue RetAddrFrIdx;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002054 // Load return address for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00002055 if (isTailCall && FPDiff)
2056 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
2057 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002058
Dan Gohman475871a2008-07-27 21:46:04 +00002059 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
2060 SmallVector<SDValue, 8> MemOpChains;
2061 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00002062
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002063 // Walk the register/memloc assignments, inserting copies/loads. In the case
2064 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00002065 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2066 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00002067 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00002068 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002069 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00002070 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00002071
Chris Lattner423c5f42007-02-28 05:31:48 +00002072 // Promote the value if needed.
2073 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002074 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00002075 case CCValAssign::Full: break;
2076 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002077 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002078 break;
2079 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002080 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002081 break;
2082 case CCValAssign::AExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002083 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
2084 // Special case: passing MMX values in XMM registers.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002085 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00002086 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
2087 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002088 } else
2089 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
2090 break;
2091 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002092 Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002093 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002094 case CCValAssign::Indirect: {
2095 // Store the argument.
2096 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00002097 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002098 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00002099 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002100 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002101 Arg = SpillSlot;
2102 break;
2103 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00002104 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002105
Chris Lattner423c5f42007-02-28 05:31:48 +00002106 if (VA.isRegLoc()) {
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002107 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
2108 if (isVarArg && IsWin64) {
2109 // Win64 ABI requires argument XMM reg to be copied to the corresponding
2110 // shadow reg if callee is a varargs function.
2111 unsigned ShadowReg = 0;
2112 switch (VA.getLocReg()) {
2113 case X86::XMM0: ShadowReg = X86::RCX; break;
2114 case X86::XMM1: ShadowReg = X86::RDX; break;
2115 case X86::XMM2: ShadowReg = X86::R8; break;
2116 case X86::XMM3: ShadowReg = X86::R9; break;
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002117 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002118 if (ShadowReg)
2119 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002120 }
Evan Chengf22f9b32010-02-06 03:28:46 +00002121 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00002122 assert(VA.isMemLoc());
2123 if (StackPtr.getNode() == 0)
2124 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
2125 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
2126 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002127 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002128 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002129
Evan Cheng32fe1032006-05-25 00:59:30 +00002130 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002131 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002132 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002133
Evan Cheng347d5f72006-04-28 21:29:37 +00002134 // Build a sequence of copy-to-reg nodes chained together with token chain
2135 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002136 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002137 // Tail call byval lowering might overwrite argument registers so in case of
2138 // tail call optimization the copies to registers are lowered later.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002139 if (!isTailCall)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002140 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002141 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002142 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002143 InFlag = Chain.getValue(1);
2144 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002145
Chris Lattner88e1fd52009-07-09 04:24:46 +00002146 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002147 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2148 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002149 if (!isTailCall) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002150 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
2151 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00002152 DebugLoc(), getPointerTy()),
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002153 InFlag);
2154 InFlag = Chain.getValue(1);
2155 } else {
2156 // If we are tail calling and generating PIC/GOT style code load the
2157 // address of the callee into ECX. The value in ecx is used as target of
2158 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2159 // for tail calls on PIC/GOT architectures. Normally we would just put the
2160 // address of GOT into ebx and then call target@PLT. But for tail calls
2161 // ebx would be restored (since ebx is callee saved) before jumping to the
2162 // target@PLT.
2163
2164 // Note: The actual moving to ECX is done further down.
2165 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2166 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2167 !G->getGlobal()->hasProtectedVisibility())
2168 Callee = LowerGlobalAddress(Callee, DAG);
2169 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002170 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002171 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002172 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002173
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002174 if (Is64Bit && isVarArg && !IsWin64) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002175 // From AMD64 ABI document:
2176 // For calls that may call functions that use varargs or stdargs
2177 // (prototype-less calls or calls to functions containing ellipsis (...) in
2178 // the declaration) %al is used as hidden argument to specify the number
2179 // of SSE registers used. The contents of %al do not need to match exactly
2180 // the number of registers, but must be an ubound on the number of SSE
2181 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002182
Gordon Henriksen86737662008-01-05 16:56:59 +00002183 // Count the number of XMM registers allocated.
2184 static const unsigned XMMArgRegs[] = {
2185 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2186 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2187 };
2188 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00002189 assert((Subtarget->hasXMM() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002190 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002191
Dale Johannesendd64c412009-02-04 00:33:20 +00002192 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Owen Anderson825b72b2009-08-11 20:47:22 +00002193 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002194 InFlag = Chain.getValue(1);
2195 }
2196
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002197
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002198 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002199 if (isTailCall) {
2200 // Force all the incoming stack arguments to be loaded from the stack
2201 // before any new outgoing arguments are stored to the stack, because the
2202 // outgoing stack slots may alias the incoming argument stack slots, and
2203 // the alias isn't otherwise explicit. This is slightly more conservative
2204 // than necessary, because it means that each store effectively depends
2205 // on every argument instead of just those arguments it would clobber.
2206 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2207
Dan Gohman475871a2008-07-27 21:46:04 +00002208 SmallVector<SDValue, 8> MemOpChains2;
2209 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002210 int FI = 0;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002211 // Do not flag preceding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00002212 InFlag = SDValue();
Dan Gohman1797ed52010-02-08 20:27:50 +00002213 if (GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002214 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2215 CCValAssign &VA = ArgLocs[i];
2216 if (VA.isRegLoc())
2217 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002218 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002219 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002220 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002221 // Create frame index.
2222 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002223 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002224 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002225 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002226
Duncan Sands276dcbd2008-03-21 09:14:45 +00002227 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002228 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002229 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002230 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002231 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00002232 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002233 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002234
Dan Gohman98ca4f22009-08-05 01:29:28 +00002235 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2236 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002237 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002238 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002239 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002240 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002241 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002242 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002243 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002244 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002245 }
2246 }
2247
2248 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002249 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002250 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002251
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002252 // Copy arguments to their registers.
2253 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002254 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002255 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002256 InFlag = Chain.getValue(1);
2257 }
Dan Gohman475871a2008-07-27 21:46:04 +00002258 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002259
Gordon Henriksen86737662008-01-05 16:56:59 +00002260 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002261 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002262 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002263 }
2264
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002265 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2266 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2267 // In the 64-bit large code model, we have to make all calls
2268 // through a register, since the call instruction's 32-bit
2269 // pc-relative offset may not be large enough to hold the whole
2270 // address.
2271 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002272 // If the callee is a GlobalAddress node (quite common, every direct call
2273 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2274 // it.
2275
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002276 // We should use extra load for direct calls to dllimported functions in
2277 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002278 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002279 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002280 unsigned char OpFlags = 0;
John McCall3a3465b2011-06-15 20:36:13 +00002281 bool ExtraLoad = false;
2282 unsigned WrapperKind = ISD::DELETED_NODE;
Eric Christopherfd179292009-08-27 18:07:15 +00002283
Chris Lattner48a7d022009-07-09 05:02:21 +00002284 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2285 // external symbols most go through the PLT in PIC mode. If the symbol
2286 // has hidden or protected visibility, or if it is static or local, then
2287 // we don't need to use the PLT - we can directly call it.
2288 if (Subtarget->isTargetELF() &&
2289 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002290 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002291 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002292 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002293 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002294 (!Subtarget->getTargetTriple().isMacOSX() ||
2295 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002296 // PC-relative references to external symbols should go through $stub,
2297 // unless we're building with the leopard linker or later, which
2298 // automatically synthesizes these stubs.
2299 OpFlags = X86II::MO_DARWIN_STUB;
John McCall3a3465b2011-06-15 20:36:13 +00002300 } else if (Subtarget->isPICStyleRIPRel() &&
2301 isa<Function>(GV) &&
2302 cast<Function>(GV)->hasFnAttr(Attribute::NonLazyBind)) {
2303 // If the function is marked as non-lazy, generate an indirect call
2304 // which loads from the GOT directly. This avoids runtime overhead
2305 // at the cost of eager binding (and one extra byte of encoding).
2306 OpFlags = X86II::MO_GOTPCREL;
2307 WrapperKind = X86ISD::WrapperRIP;
2308 ExtraLoad = true;
Chris Lattner74e726e2009-07-09 05:27:35 +00002309 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002310
Devang Patel0d881da2010-07-06 22:08:15 +00002311 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002312 G->getOffset(), OpFlags);
John McCall3a3465b2011-06-15 20:36:13 +00002313
2314 // Add a wrapper if needed.
2315 if (WrapperKind != ISD::DELETED_NODE)
2316 Callee = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Callee);
2317 // Add extra indirection if needed.
2318 if (ExtraLoad)
2319 Callee = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Callee,
2320 MachinePointerInfo::getGOT(),
2321 false, false, 0);
Chris Lattner48a7d022009-07-09 05:02:21 +00002322 }
Bill Wendling056292f2008-09-16 21:48:12 +00002323 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002324 unsigned char OpFlags = 0;
2325
Evan Cheng1bf891a2010-12-01 22:59:46 +00002326 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2327 // external symbols should go through the PLT.
2328 if (Subtarget->isTargetELF() &&
2329 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2330 OpFlags = X86II::MO_PLT;
2331 } else if (Subtarget->isPICStyleStubAny() &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002332 (!Subtarget->getTargetTriple().isMacOSX() ||
2333 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Evan Cheng1bf891a2010-12-01 22:59:46 +00002334 // PC-relative references to external symbols should go through $stub,
2335 // unless we're building with the leopard linker or later, which
2336 // automatically synthesizes these stubs.
2337 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002338 }
Eric Christopherfd179292009-08-27 18:07:15 +00002339
Chris Lattner48a7d022009-07-09 05:02:21 +00002340 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2341 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002342 }
2343
Chris Lattnerd96d0722007-02-25 06:40:16 +00002344 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002345 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002346 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002347
Evan Chengf22f9b32010-02-06 03:28:46 +00002348 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002349 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2350 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002351 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002352 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002353
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002354 Ops.push_back(Chain);
2355 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002356
Dan Gohman98ca4f22009-08-05 01:29:28 +00002357 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002358 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002359
Gordon Henriksen86737662008-01-05 16:56:59 +00002360 // Add argument registers to the end of the list so that they are known live
2361 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002362 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2363 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2364 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002365
Evan Cheng586ccac2008-03-18 23:36:35 +00002366 // Add an implicit use GOT pointer in EBX.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002367 if (!isTailCall && Subtarget->isPICStyleGOT())
Evan Cheng586ccac2008-03-18 23:36:35 +00002368 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
2369
Anton Korobeynikov3a1e54a2010-08-17 21:06:07 +00002370 // Add an implicit use of AL for non-Windows x86 64-bit vararg functions.
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002371 if (Is64Bit && isVarArg && !IsWin64)
Owen Anderson825b72b2009-08-11 20:47:22 +00002372 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
Evan Cheng586ccac2008-03-18 23:36:35 +00002373
Gabor Greifba36cb52008-08-28 21:40:38 +00002374 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002375 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002376
Dan Gohman98ca4f22009-08-05 01:29:28 +00002377 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002378 // We used to do:
2379 //// If this is the first return lowered for this function, add the regs
2380 //// to the liveout set for the function.
2381 // This isn't right, although it's probably harmless on x86; liveouts
2382 // should be computed from returns not tail calls. Consider a void
2383 // function making a tail call to a function returning int.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002384 return DAG.getNode(X86ISD::TC_RETURN, dl,
2385 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002386 }
2387
Dale Johannesenace16102009-02-03 19:33:06 +00002388 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002389 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002390
Chris Lattner2d297092006-05-23 18:50:38 +00002391 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002392 unsigned NumBytesForCalleeToPush;
Evan Chengef41ff62011-06-23 17:54:54 +00002393 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg, GuaranteedTailCallOpt))
Gordon Henriksen86737662008-01-05 16:56:59 +00002394 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Chris Lattner29689432010-03-11 00:22:57 +00002395 else if (!Is64Bit && !IsTailCallConvention(CallConv) && IsStructRet)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002396 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002397 // pops the hidden struct pointer, so we have to push it back.
2398 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002399 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002400 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002401 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002402
Gordon Henriksenae636f82008-01-03 16:47:34 +00002403 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002404 if (!IsSibcall) {
2405 Chain = DAG.getCALLSEQ_END(Chain,
2406 DAG.getIntPtrConstant(NumBytes, true),
2407 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2408 true),
2409 InFlag);
2410 InFlag = Chain.getValue(1);
2411 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002412
Chris Lattner3085e152007-02-25 08:59:22 +00002413 // Handle result values, copying them out of physregs into vregs that we
2414 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002415 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2416 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002417}
2418
Evan Cheng25ab6902006-09-08 06:48:29 +00002419
2420//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002421// Fast Calling Convention (tail call) implementation
2422//===----------------------------------------------------------------------===//
2423
2424// Like std call, callee cleans arguments, convention except that ECX is
2425// reserved for storing the tail called function address. Only 2 registers are
2426// free for argument passing (inreg). Tail call optimization is performed
2427// provided:
2428// * tailcallopt is enabled
2429// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002430// On X86_64 architecture with GOT-style position independent code only local
2431// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002432// To keep the stack aligned according to platform abi the function
2433// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2434// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002435// If a tail called function callee has more arguments than the caller the
2436// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002437// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002438// original REtADDR, but before the saved framepointer or the spilled registers
2439// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2440// stack layout:
2441// arg1
2442// arg2
2443// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002444// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002445// move area ]
2446// (possible EBP)
2447// ESI
2448// EDI
2449// local1 ..
2450
2451/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2452/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002453unsigned
2454X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2455 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002456 MachineFunction &MF = DAG.getMachineFunction();
2457 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002458 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002459 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002460 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002461 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002462 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002463 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2464 // Number smaller than 12 so just add the difference.
2465 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2466 } else {
2467 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002468 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002469 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002470 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002471 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002472}
2473
Evan Cheng5f941932010-02-05 02:21:12 +00002474/// MatchingStackOffset - Return true if the given stack call argument is
2475/// already available in the same position (relatively) of the caller's
2476/// incoming argument stack.
2477static
2478bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2479 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2480 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002481 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2482 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002483 if (Arg.getOpcode() == ISD::CopyFromReg) {
2484 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00002485 if (!TargetRegisterInfo::isVirtualRegister(VR))
Evan Cheng5f941932010-02-05 02:21:12 +00002486 return false;
2487 MachineInstr *Def = MRI->getVRegDef(VR);
2488 if (!Def)
2489 return false;
2490 if (!Flags.isByVal()) {
2491 if (!TII->isLoadFromStackSlot(Def, FI))
2492 return false;
2493 } else {
2494 unsigned Opcode = Def->getOpcode();
2495 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2496 Def->getOperand(1).isFI()) {
2497 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002498 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002499 } else
2500 return false;
2501 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002502 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2503 if (Flags.isByVal())
2504 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002505 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002506 // define @foo(%struct.X* %A) {
2507 // tail call @bar(%struct.X* byval %A)
2508 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002509 return false;
2510 SDValue Ptr = Ld->getBasePtr();
2511 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2512 if (!FINode)
2513 return false;
2514 FI = FINode->getIndex();
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002515 } else if (Arg.getOpcode() == ISD::FrameIndex && Flags.isByVal()) {
Chad Rosier14d71aa2011-06-25 18:51:28 +00002516 FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Arg);
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002517 FI = FINode->getIndex();
2518 Bytes = Flags.getByValSize();
Evan Cheng4cae1332010-03-05 08:38:04 +00002519 } else
2520 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002521
Evan Cheng4cae1332010-03-05 08:38:04 +00002522 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002523 if (!MFI->isFixedObjectIndex(FI))
2524 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002525 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002526}
2527
Dan Gohman98ca4f22009-08-05 01:29:28 +00002528/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2529/// for tail call optimization. Targets which want to do tail call
2530/// optimization should implement this function.
2531bool
2532X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002533 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002534 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002535 bool isCalleeStructRet,
2536 bool isCallerStructRet,
Evan Chengb1712452010-01-27 06:25:16 +00002537 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002538 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002539 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002540 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002541 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002542 CalleeCC != CallingConv::C)
2543 return false;
2544
Evan Cheng7096ae42010-01-29 06:45:59 +00002545 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002546 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002547 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Cheng13617962010-04-30 01:12:32 +00002548 CallingConv::ID CallerCC = CallerF->getCallingConv();
2549 bool CCMatch = CallerCC == CalleeCC;
2550
Dan Gohman1797ed52010-02-08 20:27:50 +00002551 if (GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002552 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002553 return true;
2554 return false;
2555 }
2556
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002557 // Look for obvious safe cases to perform tail call optimization that do not
2558 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002559
Evan Cheng2c12cb42010-03-26 16:26:03 +00002560 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2561 // emit a special epilogue.
2562 if (RegInfo->needsStackRealignment(MF))
2563 return false;
2564
Evan Chenga375d472010-03-15 18:54:48 +00002565 // Also avoid sibcall optimization if either caller or callee uses struct
2566 // return semantics.
2567 if (isCalleeStructRet || isCallerStructRet)
2568 return false;
2569
Chad Rosier2416da32011-06-24 21:15:36 +00002570 // An stdcall caller is expected to clean up its arguments; the callee
2571 // isn't going to do that.
2572 if (!CCMatch && CallerCC==CallingConv::X86_StdCall)
2573 return false;
2574
Chad Rosier871f6642011-05-18 19:59:50 +00002575 // Do not sibcall optimize vararg calls unless all arguments are passed via
Chad Rosiera1660892011-05-20 00:59:28 +00002576 // registers.
Chad Rosier871f6642011-05-18 19:59:50 +00002577 if (isVarArg && !Outs.empty()) {
Chad Rosiera1660892011-05-20 00:59:28 +00002578
2579 // Optimizing for varargs on Win64 is unlikely to be safe without
2580 // additional testing.
2581 if (Subtarget->isTargetWin64())
2582 return false;
2583
Chad Rosier871f6642011-05-18 19:59:50 +00002584 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002585 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
2586 getTargetMachine(), ArgLocs, *DAG.getContext());
Chad Rosier871f6642011-05-18 19:59:50 +00002587
Chad Rosier871f6642011-05-18 19:59:50 +00002588 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
2589 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i)
2590 if (!ArgLocs[i].isRegLoc())
2591 return false;
2592 }
2593
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002594 // If the call result is in ST0 / ST1, it needs to be popped off the x87 stack.
2595 // Therefore if it's not used by the call it is not safe to optimize this into
2596 // a sibcall.
2597 bool Unused = false;
2598 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2599 if (!Ins[i].Used) {
2600 Unused = true;
2601 break;
2602 }
2603 }
2604 if (Unused) {
2605 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002606 CCState CCInfo(CalleeCC, false, DAG.getMachineFunction(),
2607 getTargetMachine(), RVLocs, *DAG.getContext());
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002608 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002609 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002610 CCValAssign &VA = RVLocs[i];
2611 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2612 return false;
2613 }
2614 }
2615
Evan Cheng13617962010-04-30 01:12:32 +00002616 // If the calling conventions do not match, then we'd better make sure the
2617 // results are returned in the same way as what the caller expects.
2618 if (!CCMatch) {
2619 SmallVector<CCValAssign, 16> RVLocs1;
Eric Christopher471e4222011-06-08 23:55:35 +00002620 CCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
2621 getTargetMachine(), RVLocs1, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002622 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2623
2624 SmallVector<CCValAssign, 16> RVLocs2;
Eric Christopher471e4222011-06-08 23:55:35 +00002625 CCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
2626 getTargetMachine(), RVLocs2, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002627 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2628
2629 if (RVLocs1.size() != RVLocs2.size())
2630 return false;
2631 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2632 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2633 return false;
2634 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2635 return false;
2636 if (RVLocs1[i].isRegLoc()) {
2637 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2638 return false;
2639 } else {
2640 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2641 return false;
2642 }
2643 }
2644 }
2645
Evan Chenga6bff982010-01-30 01:22:00 +00002646 // If the callee takes no arguments then go on to check the results of the
2647 // call.
2648 if (!Outs.empty()) {
2649 // Check if stack adjustment is needed. For now, do not do this if any
2650 // argument is passed on the stack.
2651 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002652 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
2653 getTargetMachine(), ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002654
2655 // Allocate shadow area for Win64
2656 if (Subtarget->isTargetWin64()) {
2657 CCInfo.AllocateStack(32, 8);
2658 }
2659
Duncan Sands45907662010-10-31 13:21:44 +00002660 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Stuart Hastings6db2c2f2011-05-17 16:59:46 +00002661 if (CCInfo.getNextStackOffset()) {
Evan Chengb2c92902010-02-02 02:22:50 +00002662 MachineFunction &MF = DAG.getMachineFunction();
2663 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2664 return false;
Evan Chengb2c92902010-02-02 02:22:50 +00002665
2666 // Check if the arguments are already laid out in the right way as
2667 // the caller's fixed stack objects.
2668 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002669 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2670 const X86InstrInfo *TII =
2671 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002672 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2673 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002674 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002675 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002676 if (VA.getLocInfo() == CCValAssign::Indirect)
2677 return false;
2678 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002679 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2680 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002681 return false;
2682 }
2683 }
2684 }
Evan Cheng9c044672010-05-29 01:35:22 +00002685
2686 // If the tailcall address may be in a register, then make sure it's
2687 // possible to register allocate for it. In 32-bit, the call address can
2688 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002689 // callee-saved registers are restored. These happen to be the same
2690 // registers used to pass 'inreg' arguments so watch out for those.
2691 if (!Subtarget->is64Bit() &&
2692 !isa<GlobalAddressSDNode>(Callee) &&
Evan Cheng9c044672010-05-29 01:35:22 +00002693 !isa<ExternalSymbolSDNode>(Callee)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002694 unsigned NumInRegs = 0;
2695 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2696 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002697 if (!VA.isRegLoc())
2698 continue;
2699 unsigned Reg = VA.getLocReg();
2700 switch (Reg) {
2701 default: break;
2702 case X86::EAX: case X86::EDX: case X86::ECX:
2703 if (++NumInRegs == 3)
Evan Cheng9c044672010-05-29 01:35:22 +00002704 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002705 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002706 }
2707 }
2708 }
Evan Chenga6bff982010-01-30 01:22:00 +00002709 }
Evan Chengb1712452010-01-27 06:25:16 +00002710
Evan Cheng86809cc2010-02-03 03:28:02 +00002711 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002712}
2713
Dan Gohman3df24e62008-09-03 23:12:08 +00002714FastISel *
Dan Gohmana4160c32010-07-07 16:29:44 +00002715X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
2716 return X86::createFastISel(funcInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00002717}
2718
2719
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002720//===----------------------------------------------------------------------===//
2721// Other Lowering Hooks
2722//===----------------------------------------------------------------------===//
2723
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00002724static bool MayFoldLoad(SDValue Op) {
2725 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
2726}
2727
2728static bool MayFoldIntoStore(SDValue Op) {
2729 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
2730}
2731
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002732static bool isTargetShuffle(unsigned Opcode) {
2733 switch(Opcode) {
2734 default: return false;
2735 case X86ISD::PSHUFD:
2736 case X86ISD::PSHUFHW:
2737 case X86ISD::PSHUFLW:
2738 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002739 case X86ISD::PALIGN:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002740 case X86ISD::SHUFPS:
2741 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002742 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002743 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002744 case X86ISD::MOVLPS:
2745 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002746 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002747 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002748 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002749 case X86ISD::MOVSS:
2750 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002751 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002752 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +00002753 case X86ISD::VUNPCKLPS:
2754 case X86ISD::VUNPCKLPD:
2755 case X86ISD::VUNPCKLPSY:
2756 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002757 case X86ISD::PUNPCKLWD:
2758 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002759 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002760 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002761 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002762 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002763 case X86ISD::PUNPCKHWD:
2764 case X86ISD::PUNPCKHBW:
2765 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002766 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002767 return true;
2768 }
2769 return false;
2770}
2771
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002772static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002773 SDValue V1, SelectionDAG &DAG) {
2774 switch(Opc) {
2775 default: llvm_unreachable("Unknown x86 shuffle node");
2776 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002777 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002778 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002779 return DAG.getNode(Opc, dl, VT, V1);
2780 }
2781
2782 return SDValue();
2783}
2784
2785static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00002786 SDValue V1, unsigned TargetMask, SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002787 switch(Opc) {
2788 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002789 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002790 case X86ISD::PSHUFHW:
2791 case X86ISD::PSHUFLW:
2792 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
2793 }
2794
2795 return SDValue();
2796}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002797
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002798static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2799 SDValue V1, SDValue V2, unsigned TargetMask, SelectionDAG &DAG) {
2800 switch(Opc) {
2801 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002802 case X86ISD::PALIGN:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002803 case X86ISD::SHUFPD:
2804 case X86ISD::SHUFPS:
2805 return DAG.getNode(Opc, dl, VT, V1, V2,
2806 DAG.getConstant(TargetMask, MVT::i8));
2807 }
2808 return SDValue();
2809}
2810
2811static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2812 SDValue V1, SDValue V2, SelectionDAG &DAG) {
2813 switch(Opc) {
2814 default: llvm_unreachable("Unknown x86 shuffle node");
2815 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00002816 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002817 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002818 case X86ISD::MOVLPS:
2819 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002820 case X86ISD::MOVSS:
2821 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002822 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002823 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +00002824 case X86ISD::VUNPCKLPS:
2825 case X86ISD::VUNPCKLPD:
2826 case X86ISD::VUNPCKLPSY:
2827 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002828 case X86ISD::PUNPCKLWD:
2829 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002830 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002831 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002832 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002833 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002834 case X86ISD::PUNPCKHWD:
2835 case X86ISD::PUNPCKHBW:
2836 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002837 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002838 return DAG.getNode(Opc, dl, VT, V1, V2);
2839 }
2840 return SDValue();
2841}
2842
Dan Gohmand858e902010-04-17 15:26:15 +00002843SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002844 MachineFunction &MF = DAG.getMachineFunction();
2845 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2846 int ReturnAddrIndex = FuncInfo->getRAIndex();
2847
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002848 if (ReturnAddrIndex == 0) {
2849 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002850 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002851 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00002852 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002853 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002854 }
2855
Evan Cheng25ab6902006-09-08 06:48:29 +00002856 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002857}
2858
2859
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002860bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2861 bool hasSymbolicDisplacement) {
2862 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00002863 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002864 return false;
2865
2866 // If we don't have a symbolic displacement - we don't have any extra
2867 // restrictions.
2868 if (!hasSymbolicDisplacement)
2869 return true;
2870
2871 // FIXME: Some tweaks might be needed for medium code model.
2872 if (M != CodeModel::Small && M != CodeModel::Kernel)
2873 return false;
2874
2875 // For small code model we assume that latest object is 16MB before end of 31
2876 // bits boundary. We may also accept pretty large negative constants knowing
2877 // that all objects are in the positive half of address space.
2878 if (M == CodeModel::Small && Offset < 16*1024*1024)
2879 return true;
2880
2881 // For kernel code model we know that all object resist in the negative half
2882 // of 32bits address space. We may not accept negative offsets, since they may
2883 // be just off and we may accept pretty large positive ones.
2884 if (M == CodeModel::Kernel && Offset > 0)
2885 return true;
2886
2887 return false;
2888}
2889
Evan Chengef41ff62011-06-23 17:54:54 +00002890/// isCalleePop - Determines whether the callee is required to pop its
2891/// own arguments. Callee pop is necessary to support tail calls.
2892bool X86::isCalleePop(CallingConv::ID CallingConv,
2893 bool is64Bit, bool IsVarArg, bool TailCallOpt) {
2894 if (IsVarArg)
2895 return false;
2896
2897 switch (CallingConv) {
2898 default:
2899 return false;
2900 case CallingConv::X86_StdCall:
2901 return !is64Bit;
2902 case CallingConv::X86_FastCall:
2903 return !is64Bit;
2904 case CallingConv::X86_ThisCall:
2905 return !is64Bit;
2906 case CallingConv::Fast:
2907 return TailCallOpt;
2908 case CallingConv::GHC:
2909 return TailCallOpt;
2910 }
2911}
2912
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002913/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2914/// specific condition code, returning the condition code and the LHS/RHS of the
2915/// comparison to make.
2916static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2917 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00002918 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002919 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2920 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2921 // X > -1 -> X == 0, jump !sign.
2922 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002923 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002924 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2925 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002926 return X86::COND_S;
Andrew Trickf6c39412011-03-23 23:11:02 +00002927 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00002928 // X < 1 -> X <= 0
2929 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002930 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002931 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002932 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002933
Evan Chengd9558e02006-01-06 00:43:03 +00002934 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002935 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002936 case ISD::SETEQ: return X86::COND_E;
2937 case ISD::SETGT: return X86::COND_G;
2938 case ISD::SETGE: return X86::COND_GE;
2939 case ISD::SETLT: return X86::COND_L;
2940 case ISD::SETLE: return X86::COND_LE;
2941 case ISD::SETNE: return X86::COND_NE;
2942 case ISD::SETULT: return X86::COND_B;
2943 case ISD::SETUGT: return X86::COND_A;
2944 case ISD::SETULE: return X86::COND_BE;
2945 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00002946 }
Chris Lattner4c78e022008-12-23 23:42:27 +00002947 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002948
Chris Lattner4c78e022008-12-23 23:42:27 +00002949 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00002950
Chris Lattner4c78e022008-12-23 23:42:27 +00002951 // If LHS is a foldable load, but RHS is not, flip the condition.
Rafael Espindolaf297c932011-02-03 03:58:05 +00002952 if (ISD::isNON_EXTLoad(LHS.getNode()) &&
2953 !ISD::isNON_EXTLoad(RHS.getNode())) {
Chris Lattner4c78e022008-12-23 23:42:27 +00002954 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
2955 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00002956 }
2957
Chris Lattner4c78e022008-12-23 23:42:27 +00002958 switch (SetCCOpcode) {
2959 default: break;
2960 case ISD::SETOLT:
2961 case ISD::SETOLE:
2962 case ISD::SETUGT:
2963 case ISD::SETUGE:
2964 std::swap(LHS, RHS);
2965 break;
2966 }
2967
2968 // On a floating point condition, the flags are set as follows:
2969 // ZF PF CF op
2970 // 0 | 0 | 0 | X > Y
2971 // 0 | 0 | 1 | X < Y
2972 // 1 | 0 | 0 | X == Y
2973 // 1 | 1 | 1 | unordered
2974 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002975 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00002976 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002977 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00002978 case ISD::SETOLT: // flipped
2979 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002980 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00002981 case ISD::SETOLE: // flipped
2982 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002983 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002984 case ISD::SETUGT: // flipped
2985 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002986 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00002987 case ISD::SETUGE: // flipped
2988 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002989 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002990 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002991 case ISD::SETNE: return X86::COND_NE;
2992 case ISD::SETUO: return X86::COND_P;
2993 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00002994 case ISD::SETOEQ:
2995 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00002996 }
Evan Chengd9558e02006-01-06 00:43:03 +00002997}
2998
Evan Cheng4a460802006-01-11 00:33:36 +00002999/// hasFPCMov - is there a floating point cmov for the specific X86 condition
3000/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00003001/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00003002static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00003003 switch (X86CC) {
3004 default:
3005 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00003006 case X86::COND_B:
3007 case X86::COND_BE:
3008 case X86::COND_E:
3009 case X86::COND_P:
3010 case X86::COND_A:
3011 case X86::COND_AE:
3012 case X86::COND_NE:
3013 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00003014 return true;
3015 }
3016}
3017
Evan Chengeb2f9692009-10-27 19:56:55 +00003018/// isFPImmLegal - Returns true if the target can instruction select the
3019/// specified FP immediate natively. If false, the legalizer will
3020/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00003021bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00003022 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
3023 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
3024 return true;
3025 }
3026 return false;
3027}
3028
Nate Begeman9008ca62009-04-27 18:41:29 +00003029/// isUndefOrInRange - Return true if Val is undef or if its value falls within
3030/// the specified range (L, H].
3031static bool isUndefOrInRange(int Val, int Low, int Hi) {
3032 return (Val < 0) || (Val >= Low && Val < Hi);
3033}
3034
3035/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
3036/// specified value.
3037static bool isUndefOrEqual(int Val, int CmpVal) {
3038 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003039 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00003040 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00003041}
3042
Nate Begeman9008ca62009-04-27 18:41:29 +00003043/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
3044/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
3045/// the second operand.
Owen Andersone50ed302009-08-10 22:56:29 +00003046static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00003047 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00003048 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00003049 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00003050 return (Mask[0] < 2 && Mask[1] < 2);
3051 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003052}
3053
Nate Begeman9008ca62009-04-27 18:41:29 +00003054bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003055 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003056 N->getMask(M);
3057 return ::isPSHUFDMask(M, N->getValueType(0));
3058}
Evan Cheng0188ecb2006-03-22 18:59:22 +00003059
Nate Begeman9008ca62009-04-27 18:41:29 +00003060/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
3061/// is suitable for input to PSHUFHW.
Owen Andersone50ed302009-08-10 22:56:29 +00003062static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003063 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00003064 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003065
Nate Begeman9008ca62009-04-27 18:41:29 +00003066 // Lower quadword copied in order or undef.
3067 for (int i = 0; i != 4; ++i)
3068 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00003069 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003070
Evan Cheng506d3df2006-03-29 23:07:14 +00003071 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00003072 for (int i = 4; i != 8; ++i)
3073 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00003074 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003075
Evan Cheng506d3df2006-03-29 23:07:14 +00003076 return true;
3077}
3078
Nate Begeman9008ca62009-04-27 18:41:29 +00003079bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003080 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003081 N->getMask(M);
3082 return ::isPSHUFHWMask(M, N->getValueType(0));
3083}
Evan Cheng506d3df2006-03-29 23:07:14 +00003084
Nate Begeman9008ca62009-04-27 18:41:29 +00003085/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
3086/// is suitable for input to PSHUFLW.
Owen Andersone50ed302009-08-10 22:56:29 +00003087static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003088 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00003089 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003090
Rafael Espindola15684b22009-04-24 12:40:33 +00003091 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00003092 for (int i = 4; i != 8; ++i)
3093 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00003094 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003095
Rafael Espindola15684b22009-04-24 12:40:33 +00003096 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00003097 for (int i = 0; i != 4; ++i)
3098 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00003099 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003100
Rafael Espindola15684b22009-04-24 12:40:33 +00003101 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003102}
3103
Nate Begeman9008ca62009-04-27 18:41:29 +00003104bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003105 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003106 N->getMask(M);
3107 return ::isPSHUFLWMask(M, N->getValueType(0));
3108}
3109
Nate Begemana09008b2009-10-19 02:17:23 +00003110/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
3111/// is suitable for input to PALIGNR.
3112static bool isPALIGNRMask(const SmallVectorImpl<int> &Mask, EVT VT,
3113 bool hasSSSE3) {
3114 int i, e = VT.getVectorNumElements();
Michael J. Spencerec38de22010-10-10 22:04:20 +00003115
Nate Begemana09008b2009-10-19 02:17:23 +00003116 // Do not handle v2i64 / v2f64 shuffles with palignr.
3117 if (e < 4 || !hasSSSE3)
3118 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003119
Nate Begemana09008b2009-10-19 02:17:23 +00003120 for (i = 0; i != e; ++i)
3121 if (Mask[i] >= 0)
3122 break;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003123
Nate Begemana09008b2009-10-19 02:17:23 +00003124 // All undef, not a palignr.
3125 if (i == e)
3126 return false;
3127
3128 // Determine if it's ok to perform a palignr with only the LHS, since we
3129 // don't have access to the actual shuffle elements to see if RHS is undef.
3130 bool Unary = Mask[i] < (int)e;
3131 bool NeedsUnary = false;
3132
3133 int s = Mask[i] - i;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003134
Nate Begemana09008b2009-10-19 02:17:23 +00003135 // Check the rest of the elements to see if they are consecutive.
3136 for (++i; i != e; ++i) {
3137 int m = Mask[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00003138 if (m < 0)
Nate Begemana09008b2009-10-19 02:17:23 +00003139 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003140
Nate Begemana09008b2009-10-19 02:17:23 +00003141 Unary = Unary && (m < (int)e);
3142 NeedsUnary = NeedsUnary || (m < s);
3143
3144 if (NeedsUnary && !Unary)
3145 return false;
3146 if (Unary && m != ((s+i) & (e-1)))
3147 return false;
3148 if (!Unary && m != (s+i))
3149 return false;
3150 }
3151 return true;
3152}
3153
3154bool X86::isPALIGNRMask(ShuffleVectorSDNode *N) {
3155 SmallVector<int, 8> M;
3156 N->getMask(M);
3157 return ::isPALIGNRMask(M, N->getValueType(0), true);
3158}
3159
Evan Cheng14aed5e2006-03-24 01:18:28 +00003160/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
3161/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Owen Andersone50ed302009-08-10 22:56:29 +00003162static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003163 int NumElems = VT.getVectorNumElements();
3164 if (NumElems != 2 && NumElems != 4)
3165 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003166
Nate Begeman9008ca62009-04-27 18:41:29 +00003167 int Half = NumElems / 2;
3168 for (int i = 0; i < Half; ++i)
3169 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003170 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003171 for (int i = Half; i < NumElems; ++i)
3172 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00003173 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003174
Evan Cheng14aed5e2006-03-24 01:18:28 +00003175 return true;
3176}
3177
Nate Begeman9008ca62009-04-27 18:41:29 +00003178bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
3179 SmallVector<int, 8> M;
3180 N->getMask(M);
3181 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003182}
3183
Evan Cheng213d2cf2007-05-17 18:45:50 +00003184/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00003185/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
3186/// half elements to come from vector 1 (which would equal the dest.) and
3187/// the upper half to come from vector 2.
Owen Andersone50ed302009-08-10 22:56:29 +00003188static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003189 int NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003190
3191 if (NumElems != 2 && NumElems != 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00003192 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003193
Nate Begeman9008ca62009-04-27 18:41:29 +00003194 int Half = NumElems / 2;
3195 for (int i = 0; i < Half; ++i)
3196 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00003197 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003198 for (int i = Half; i < NumElems; ++i)
3199 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003200 return false;
3201 return true;
3202}
3203
Nate Begeman9008ca62009-04-27 18:41:29 +00003204static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
3205 SmallVector<int, 8> M;
3206 N->getMask(M);
3207 return isCommutedSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003208}
3209
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003210/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
3211/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00003212bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
3213 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003214 return false;
3215
Evan Cheng2064a2b2006-03-28 06:50:32 +00003216 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00003217 return isUndefOrEqual(N->getMaskElt(0), 6) &&
3218 isUndefOrEqual(N->getMaskElt(1), 7) &&
3219 isUndefOrEqual(N->getMaskElt(2), 2) &&
3220 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003221}
3222
Nate Begeman0b10b912009-11-07 23:17:15 +00003223/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3224/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3225/// <2, 3, 2, 3>
3226bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
3227 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Michael J. Spencerec38de22010-10-10 22:04:20 +00003228
Nate Begeman0b10b912009-11-07 23:17:15 +00003229 if (NumElems != 4)
3230 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003231
Nate Begeman0b10b912009-11-07 23:17:15 +00003232 return isUndefOrEqual(N->getMaskElt(0), 2) &&
3233 isUndefOrEqual(N->getMaskElt(1), 3) &&
3234 isUndefOrEqual(N->getMaskElt(2), 2) &&
3235 isUndefOrEqual(N->getMaskElt(3), 3);
3236}
3237
Evan Cheng5ced1d82006-04-06 23:23:56 +00003238/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3239/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00003240bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
3241 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003242
Evan Cheng5ced1d82006-04-06 23:23:56 +00003243 if (NumElems != 2 && NumElems != 4)
3244 return false;
3245
Evan Chengc5cdff22006-04-07 21:53:05 +00003246 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003247 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003248 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003249
Evan Chengc5cdff22006-04-07 21:53:05 +00003250 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003251 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003252 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003253
3254 return true;
3255}
3256
Nate Begeman0b10b912009-11-07 23:17:15 +00003257/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3258/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
3259bool X86::isMOVLHPSMask(ShuffleVectorSDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003260 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003261
David Greenea20244d2011-03-02 17:23:43 +00003262 if ((NumElems != 2 && NumElems != 4)
3263 || N->getValueType(0).getSizeInBits() > 128)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003264 return false;
3265
Evan Chengc5cdff22006-04-07 21:53:05 +00003266 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003267 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003268 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003269
Nate Begeman9008ca62009-04-27 18:41:29 +00003270 for (unsigned i = 0; i < NumElems/2; ++i)
3271 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003272 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003273
3274 return true;
3275}
3276
Evan Cheng0038e592006-03-28 00:39:58 +00003277/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3278/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Owen Andersone50ed302009-08-10 22:56:29 +00003279static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003280 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003281 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003282 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng0038e592006-03-28 00:39:58 +00003283 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003284
David Greenea20244d2011-03-02 17:23:43 +00003285 // Handle vector lengths > 128 bits. Define a "section" as a set of
3286 // 128 bits. AVX defines UNPCK* to operate independently on 128-bit
3287 // sections.
3288 unsigned NumSections = VT.getSizeInBits() / 128;
3289 if (NumSections == 0 ) NumSections = 1; // Handle MMX
3290 unsigned NumSectionElts = NumElts / NumSections;
3291
3292 unsigned Start = 0;
3293 unsigned End = NumSectionElts;
3294 for (unsigned s = 0; s < NumSections; ++s) {
3295 for (unsigned i = Start, j = s * NumSectionElts;
3296 i != End;
3297 i += 2, ++j) {
3298 int BitI = Mask[i];
3299 int BitI1 = Mask[i+1];
3300 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003301 return false;
David Greenea20244d2011-03-02 17:23:43 +00003302 if (V2IsSplat) {
3303 if (!isUndefOrEqual(BitI1, NumElts))
3304 return false;
3305 } else {
3306 if (!isUndefOrEqual(BitI1, j + NumElts))
3307 return false;
3308 }
Evan Cheng39623da2006-04-20 08:58:49 +00003309 }
David Greenea20244d2011-03-02 17:23:43 +00003310 // Process the next 128 bits.
3311 Start += NumSectionElts;
3312 End += NumSectionElts;
Evan Cheng0038e592006-03-28 00:39:58 +00003313 }
David Greenea20244d2011-03-02 17:23:43 +00003314
Evan Cheng0038e592006-03-28 00:39:58 +00003315 return true;
3316}
3317
Nate Begeman9008ca62009-04-27 18:41:29 +00003318bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3319 SmallVector<int, 8> M;
3320 N->getMask(M);
3321 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003322}
3323
Evan Cheng4fcb9222006-03-28 02:43:26 +00003324/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3325/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Eric Christopherfd179292009-08-27 18:07:15 +00003326static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003327 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003328 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003329 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng4fcb9222006-03-28 02:43:26 +00003330 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003331
Nate Begeman9008ca62009-04-27 18:41:29 +00003332 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
3333 int BitI = Mask[i];
3334 int BitI1 = Mask[i+1];
Chris Lattner5a88b832007-02-25 07:10:00 +00003335 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengc5cdff22006-04-07 21:53:05 +00003336 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00003337 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00003338 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003339 return false;
3340 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00003341 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003342 return false;
3343 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003344 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003345 return true;
3346}
3347
Nate Begeman9008ca62009-04-27 18:41:29 +00003348bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3349 SmallVector<int, 8> M;
3350 N->getMask(M);
3351 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003352}
3353
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003354/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3355/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3356/// <0, 0, 1, 1>
Owen Andersone50ed302009-08-10 22:56:29 +00003357static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003358 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003359 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003360 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003361
David Greenea20244d2011-03-02 17:23:43 +00003362 // Handle vector lengths > 128 bits. Define a "section" as a set of
3363 // 128 bits. AVX defines UNPCK* to operate independently on 128-bit
3364 // sections.
3365 unsigned NumSections = VT.getSizeInBits() / 128;
3366 if (NumSections == 0 ) NumSections = 1; // Handle MMX
3367 unsigned NumSectionElts = NumElems / NumSections;
3368
3369 for (unsigned s = 0; s < NumSections; ++s) {
3370 for (unsigned i = s * NumSectionElts, j = s * NumSectionElts;
3371 i != NumSectionElts * (s + 1);
3372 i += 2, ++j) {
3373 int BitI = Mask[i];
3374 int BitI1 = Mask[i+1];
3375
3376 if (!isUndefOrEqual(BitI, j))
3377 return false;
3378 if (!isUndefOrEqual(BitI1, j))
3379 return false;
3380 }
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003381 }
David Greenea20244d2011-03-02 17:23:43 +00003382
Rafael Espindola15684b22009-04-24 12:40:33 +00003383 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003384}
3385
Nate Begeman9008ca62009-04-27 18:41:29 +00003386bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
3387 SmallVector<int, 8> M;
3388 N->getMask(M);
3389 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
3390}
3391
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003392/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3393/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3394/// <2, 2, 3, 3>
Owen Andersone50ed302009-08-10 22:56:29 +00003395static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003396 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003397 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
3398 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003399
Nate Begeman9008ca62009-04-27 18:41:29 +00003400 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
3401 int BitI = Mask[i];
3402 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003403 if (!isUndefOrEqual(BitI, j))
3404 return false;
3405 if (!isUndefOrEqual(BitI1, j))
3406 return false;
3407 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003408 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003409}
3410
Nate Begeman9008ca62009-04-27 18:41:29 +00003411bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
3412 SmallVector<int, 8> M;
3413 N->getMask(M);
3414 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
3415}
3416
Evan Cheng017dcc62006-04-21 01:05:10 +00003417/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3418/// specifies a shuffle of elements that is suitable for input to MOVSS,
3419/// MOVSD, and MOVD, i.e. setting the lowest element.
Owen Andersone50ed302009-08-10 22:56:29 +00003420static bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003421 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003422 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003423
3424 int NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003425
Nate Begeman9008ca62009-04-27 18:41:29 +00003426 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003427 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003428
Nate Begeman9008ca62009-04-27 18:41:29 +00003429 for (int i = 1; i < NumElts; ++i)
3430 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003431 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003432
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003433 return true;
3434}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003435
Nate Begeman9008ca62009-04-27 18:41:29 +00003436bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
3437 SmallVector<int, 8> M;
3438 N->getMask(M);
3439 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003440}
3441
Evan Cheng017dcc62006-04-21 01:05:10 +00003442/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
3443/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003444/// element of vector 2 and the other elements to come from vector 1 in order.
Owen Andersone50ed302009-08-10 22:56:29 +00003445static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003446 bool V2IsSplat = false, bool V2IsUndef = false) {
3447 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003448 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003449 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003450
Nate Begeman9008ca62009-04-27 18:41:29 +00003451 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003452 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003453
Nate Begeman9008ca62009-04-27 18:41:29 +00003454 for (int i = 1; i < NumOps; ++i)
3455 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3456 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3457 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003458 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003459
Evan Cheng39623da2006-04-20 08:58:49 +00003460 return true;
3461}
3462
Nate Begeman9008ca62009-04-27 18:41:29 +00003463static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00003464 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003465 SmallVector<int, 8> M;
3466 N->getMask(M);
3467 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00003468}
3469
Evan Chengd9539472006-04-14 21:59:03 +00003470/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3471/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003472bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N) {
3473 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003474 return false;
3475
3476 // Expect 1, 1, 3, 3
Rafael Espindola15684b22009-04-24 12:40:33 +00003477 for (unsigned i = 0; i < 2; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003478 int Elt = N->getMaskElt(i);
3479 if (Elt >= 0 && Elt != 1)
3480 return false;
Rafael Espindola15684b22009-04-24 12:40:33 +00003481 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003482
3483 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003484 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003485 int Elt = N->getMaskElt(i);
3486 if (Elt >= 0 && Elt != 3)
3487 return false;
3488 if (Elt == 3)
3489 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003490 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003491 // Don't use movshdup if it can be done with a shufps.
Nate Begeman9008ca62009-04-27 18:41:29 +00003492 // FIXME: verify that matching u, u, 3, 3 is what we want.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003493 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003494}
3495
3496/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3497/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003498bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N) {
3499 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003500 return false;
3501
3502 // Expect 0, 0, 2, 2
Nate Begeman9008ca62009-04-27 18:41:29 +00003503 for (unsigned i = 0; i < 2; ++i)
3504 if (N->getMaskElt(i) > 0)
3505 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003506
3507 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003508 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003509 int Elt = N->getMaskElt(i);
3510 if (Elt >= 0 && Elt != 2)
3511 return false;
3512 if (Elt == 2)
3513 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003514 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003515 // Don't use movsldup if it can be done with a shufps.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003516 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003517}
3518
Evan Cheng0b457f02008-09-25 20:50:48 +00003519/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3520/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003521bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
3522 int e = N->getValueType(0).getVectorNumElements() / 2;
Eric Christopherfd179292009-08-27 18:07:15 +00003523
Nate Begeman9008ca62009-04-27 18:41:29 +00003524 for (int i = 0; i < e; ++i)
3525 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003526 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003527 for (int i = 0; i < e; ++i)
3528 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003529 return false;
3530 return true;
3531}
3532
David Greenec38a03e2011-02-03 15:50:00 +00003533/// isVEXTRACTF128Index - Return true if the specified
3534/// EXTRACT_SUBVECTOR operand specifies a vector extract that is
3535/// suitable for input to VEXTRACTF128.
3536bool X86::isVEXTRACTF128Index(SDNode *N) {
3537 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3538 return false;
3539
3540 // The index should be aligned on a 128-bit boundary.
3541 uint64_t Index =
3542 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
3543
3544 unsigned VL = N->getValueType(0).getVectorNumElements();
3545 unsigned VBits = N->getValueType(0).getSizeInBits();
3546 unsigned ElSize = VBits / VL;
3547 bool Result = (Index * ElSize) % 128 == 0;
3548
3549 return Result;
3550}
3551
David Greeneccacdc12011-02-04 16:08:29 +00003552/// isVINSERTF128Index - Return true if the specified INSERT_SUBVECTOR
3553/// operand specifies a subvector insert that is suitable for input to
3554/// VINSERTF128.
3555bool X86::isVINSERTF128Index(SDNode *N) {
3556 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
3557 return false;
3558
3559 // The index should be aligned on a 128-bit boundary.
3560 uint64_t Index =
3561 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
3562
3563 unsigned VL = N->getValueType(0).getVectorNumElements();
3564 unsigned VBits = N->getValueType(0).getSizeInBits();
3565 unsigned ElSize = VBits / VL;
3566 bool Result = (Index * ElSize) % 128 == 0;
3567
3568 return Result;
3569}
3570
Evan Cheng63d33002006-03-22 08:01:21 +00003571/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003572/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Evan Cheng63d33002006-03-22 08:01:21 +00003573unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003574 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3575 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
3576
Evan Chengb9df0ca2006-03-22 02:53:00 +00003577 unsigned Shift = (NumOperands == 4) ? 2 : 1;
3578 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00003579 for (int i = 0; i < NumOperands; ++i) {
3580 int Val = SVOp->getMaskElt(NumOperands-i-1);
3581 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00003582 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00003583 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00003584 if (i != NumOperands - 1)
3585 Mask <<= Shift;
3586 }
Evan Cheng63d33002006-03-22 08:01:21 +00003587 return Mask;
3588}
3589
Evan Cheng506d3df2006-03-29 23:07:14 +00003590/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003591/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003592unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003593 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003594 unsigned Mask = 0;
3595 // 8 nodes, but we only care about the last 4.
3596 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003597 int Val = SVOp->getMaskElt(i);
3598 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00003599 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00003600 if (i != 4)
3601 Mask <<= 2;
3602 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003603 return Mask;
3604}
3605
3606/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003607/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003608unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003609 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003610 unsigned Mask = 0;
3611 // 8 nodes, but we only care about the first 4.
3612 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003613 int Val = SVOp->getMaskElt(i);
3614 if (Val >= 0)
3615 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00003616 if (i != 0)
3617 Mask <<= 2;
3618 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003619 return Mask;
3620}
3621
Nate Begemana09008b2009-10-19 02:17:23 +00003622/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
3623/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
3624unsigned X86::getShufflePALIGNRImmediate(SDNode *N) {
3625 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3626 EVT VVT = N->getValueType(0);
3627 unsigned EltSize = VVT.getVectorElementType().getSizeInBits() >> 3;
3628 int Val = 0;
3629
3630 unsigned i, e;
3631 for (i = 0, e = VVT.getVectorNumElements(); i != e; ++i) {
3632 Val = SVOp->getMaskElt(i);
3633 if (Val >= 0)
3634 break;
3635 }
3636 return (Val - i) * EltSize;
3637}
3638
David Greenec38a03e2011-02-03 15:50:00 +00003639/// getExtractVEXTRACTF128Immediate - Return the appropriate immediate
3640/// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF128
3641/// instructions.
3642unsigned X86::getExtractVEXTRACTF128Immediate(SDNode *N) {
3643 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3644 llvm_unreachable("Illegal extract subvector for VEXTRACTF128");
3645
3646 uint64_t Index =
3647 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
3648
3649 EVT VecVT = N->getOperand(0).getValueType();
3650 EVT ElVT = VecVT.getVectorElementType();
3651
3652 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
3653
3654 return Index / NumElemsPerChunk;
3655}
3656
David Greeneccacdc12011-02-04 16:08:29 +00003657/// getInsertVINSERTF128Immediate - Return the appropriate immediate
3658/// to insert at the specified INSERT_SUBVECTOR index with VINSERTF128
3659/// instructions.
3660unsigned X86::getInsertVINSERTF128Immediate(SDNode *N) {
3661 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
3662 llvm_unreachable("Illegal insert subvector for VINSERTF128");
3663
3664 uint64_t Index =
NAKAMURA Takumi27635382011-02-05 15:10:54 +00003665 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
David Greeneccacdc12011-02-04 16:08:29 +00003666
3667 EVT VecVT = N->getValueType(0);
3668 EVT ElVT = VecVT.getVectorElementType();
3669
3670 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
3671
3672 return Index / NumElemsPerChunk;
3673}
3674
Evan Cheng37b73872009-07-30 08:33:02 +00003675/// isZeroNode - Returns true if Elt is a constant zero or a floating point
3676/// constant +0.0.
3677bool X86::isZeroNode(SDValue Elt) {
3678 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00003679 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00003680 (isa<ConstantFPSDNode>(Elt) &&
3681 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
3682}
3683
Nate Begeman9008ca62009-04-27 18:41:29 +00003684/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
3685/// their permute mask.
3686static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
3687 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003688 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003689 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00003690 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00003691
Nate Begeman5a5ca152009-04-29 05:20:52 +00003692 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003693 int idx = SVOp->getMaskElt(i);
3694 if (idx < 0)
3695 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003696 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003697 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003698 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003699 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003700 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003701 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
3702 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003703}
3704
Evan Cheng779ccea2007-12-07 21:30:01 +00003705/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3706/// the two vector operands have swapped position.
Owen Andersone50ed302009-08-10 22:56:29 +00003707static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00003708 unsigned NumElems = VT.getVectorNumElements();
3709 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003710 int idx = Mask[i];
3711 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003712 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003713 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003714 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003715 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003716 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003717 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003718}
3719
Evan Cheng533a0aa2006-04-19 20:35:22 +00003720/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
3721/// match movhlps. The lower half elements should come from upper half of
3722/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003723/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00003724static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
3725 if (Op->getValueType(0).getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00003726 return false;
3727 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003728 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003729 return false;
3730 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003731 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003732 return false;
3733 return true;
3734}
3735
Evan Cheng5ced1d82006-04-06 23:23:56 +00003736/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00003737/// is promoted to a vector. It also returns the LoadSDNode by reference if
3738/// required.
3739static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00003740 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
3741 return false;
3742 N = N->getOperand(0).getNode();
3743 if (!ISD::isNON_EXTLoad(N))
3744 return false;
3745 if (LD)
3746 *LD = cast<LoadSDNode>(N);
3747 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003748}
3749
Evan Cheng533a0aa2006-04-19 20:35:22 +00003750/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
3751/// match movlp{s|d}. The lower half elements should come from lower half of
3752/// V1 (and in order), and the upper half elements should come from the upper
3753/// half of V2 (and in order). And since V1 will become the source of the
3754/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003755static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
3756 ShuffleVectorSDNode *Op) {
Evan Cheng466685d2006-10-09 20:57:25 +00003757 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003758 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00003759 // Is V2 is a vector load, don't do this transformation. We will try to use
3760 // load folding shufps op.
3761 if (ISD::isNON_EXTLoad(V2))
3762 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003763
Nate Begeman5a5ca152009-04-29 05:20:52 +00003764 unsigned NumElems = Op->getValueType(0).getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003765
Evan Cheng533a0aa2006-04-19 20:35:22 +00003766 if (NumElems != 2 && NumElems != 4)
3767 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003768 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003769 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003770 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003771 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003772 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003773 return false;
3774 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003775}
3776
Evan Cheng39623da2006-04-20 08:58:49 +00003777/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
3778/// all the same.
3779static bool isSplatVector(SDNode *N) {
3780 if (N->getOpcode() != ISD::BUILD_VECTOR)
3781 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003782
Dan Gohman475871a2008-07-27 21:46:04 +00003783 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00003784 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
3785 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003786 return false;
3787 return true;
3788}
3789
Evan Cheng213d2cf2007-05-17 18:45:50 +00003790/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00003791/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00003792/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00003793static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00003794 SDValue V1 = N->getOperand(0);
3795 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003796 unsigned NumElems = N->getValueType(0).getVectorNumElements();
3797 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003798 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003799 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003800 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00003801 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
3802 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003803 if (Opc != ISD::BUILD_VECTOR ||
3804 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00003805 return false;
3806 } else if (Idx >= 0) {
3807 unsigned Opc = V1.getOpcode();
3808 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
3809 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003810 if (Opc != ISD::BUILD_VECTOR ||
3811 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00003812 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00003813 }
3814 }
3815 return true;
3816}
3817
3818/// getZeroVector - Returns a vector of specified type with all zero elements.
3819///
Owen Andersone50ed302009-08-10 22:56:29 +00003820static SDValue getZeroVector(EVT VT, bool HasSSE2, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00003821 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003822 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003823
Dale Johannesen0488fb62010-09-30 23:57:10 +00003824 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003825 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00003826 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00003827 if (VT.getSizeInBits() == 128) { // SSE
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003828 if (HasSSE2) { // SSE2
3829 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3830 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
3831 } else { // SSE1
3832 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
3833 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
3834 }
3835 } else if (VT.getSizeInBits() == 256) { // AVX
3836 // 256-bit logic and arithmetic instructions in AVX are
3837 // all floating-point, no support for integer ops. Default
3838 // to emitting fp zeroed vectors then.
Owen Anderson825b72b2009-08-11 20:47:22 +00003839 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003840 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
3841 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
Evan Chengf0df0312008-05-15 08:39:06 +00003842 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003843 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00003844}
3845
Chris Lattner8a594482007-11-25 00:24:49 +00003846/// getOnesVector - Returns a vector of specified type with all bits set.
3847///
Owen Andersone50ed302009-08-10 22:56:29 +00003848static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003849 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003850
Chris Lattner8a594482007-11-25 00:24:49 +00003851 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
3852 // type. This ensures they get CSE'd.
Owen Anderson825b72b2009-08-11 20:47:22 +00003853 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00003854 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00003855 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003856 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00003857}
3858
3859
Evan Cheng39623da2006-04-20 08:58:49 +00003860/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
3861/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00003862static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003863 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003864 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003865
Evan Cheng39623da2006-04-20 08:58:49 +00003866 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003867 SmallVector<int, 8> MaskVec;
3868 SVOp->getMask(MaskVec);
Eric Christopherfd179292009-08-27 18:07:15 +00003869
Nate Begeman5a5ca152009-04-29 05:20:52 +00003870 for (unsigned i = 0; i != NumElems; ++i) {
3871 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003872 MaskVec[i] = NumElems;
3873 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00003874 }
Evan Cheng39623da2006-04-20 08:58:49 +00003875 }
Evan Cheng39623da2006-04-20 08:58:49 +00003876 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00003877 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
3878 SVOp->getOperand(1), &MaskVec[0]);
3879 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00003880}
3881
Evan Cheng017dcc62006-04-21 01:05:10 +00003882/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
3883/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00003884static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003885 SDValue V2) {
3886 unsigned NumElems = VT.getVectorNumElements();
3887 SmallVector<int, 8> Mask;
3888 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00003889 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003890 Mask.push_back(i);
3891 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00003892}
3893
Nate Begeman9008ca62009-04-27 18:41:29 +00003894/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003895static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003896 SDValue V2) {
3897 unsigned NumElems = VT.getVectorNumElements();
3898 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00003899 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003900 Mask.push_back(i);
3901 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00003902 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003903 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00003904}
3905
Nate Begeman9008ca62009-04-27 18:41:29 +00003906/// getUnpackhMask - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003907static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003908 SDValue V2) {
3909 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00003910 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00003911 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00003912 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003913 Mask.push_back(i + Half);
3914 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00003915 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003916 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003917}
3918
Bruno Cardoso Lopesbb0a9482010-08-13 17:50:47 +00003919/// PromoteSplat - Promote a splat of v4i32, v8i16 or v16i8 to v4f32.
3920static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003921 EVT PVT = MVT::v4f32;
Owen Andersone50ed302009-08-10 22:56:29 +00003922 EVT VT = SV->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00003923 DebugLoc dl = SV->getDebugLoc();
3924 SDValue V1 = SV->getOperand(0);
3925 int NumElems = VT.getVectorNumElements();
3926 int EltNo = SV->getSplatIndex();
Rafael Espindola15684b22009-04-24 12:40:33 +00003927
Nate Begeman9008ca62009-04-27 18:41:29 +00003928 // unpack elements to the correct location
3929 while (NumElems > 4) {
3930 if (EltNo < NumElems/2) {
3931 V1 = getUnpackl(DAG, dl, VT, V1, V1);
3932 } else {
3933 V1 = getUnpackh(DAG, dl, VT, V1, V1);
3934 EltNo -= NumElems/2;
3935 }
3936 NumElems >>= 1;
3937 }
Eric Christopherfd179292009-08-27 18:07:15 +00003938
Nate Begeman9008ca62009-04-27 18:41:29 +00003939 // Perform the splat.
3940 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003941 V1 = DAG.getNode(ISD::BITCAST, dl, PVT, V1);
Nate Begeman9008ca62009-04-27 18:41:29 +00003942 V1 = DAG.getVectorShuffle(PVT, dl, V1, DAG.getUNDEF(PVT), &SplatMask[0]);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003943 return DAG.getNode(ISD::BITCAST, dl, VT, V1);
Evan Chengc575ca22006-04-17 20:43:08 +00003944}
3945
Evan Chengba05f722006-04-21 23:03:30 +00003946/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00003947/// vector of zero or undef vector. This produces a shuffle where the low
3948/// element of V2 is swizzled into the zero/undef vector, landing at element
3949/// 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 +00003950static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Chengf0df0312008-05-15 08:39:06 +00003951 bool isZero, bool HasSSE2,
3952 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003953 EVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00003954 SDValue V1 = isZero
Nate Begeman9008ca62009-04-27 18:41:29 +00003955 ? getZeroVector(VT, HasSSE2, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
3956 unsigned NumElems = VT.getVectorNumElements();
3957 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00003958 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003959 // If this is the insertion idx, put the low elt of V2 here.
3960 MaskVec.push_back(i == Idx ? NumElems : i);
3961 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00003962}
3963
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003964/// getShuffleScalarElt - Returns the scalar element that will make up the ith
3965/// element of the result of the vector shuffle.
Benjamin Kramer050db522011-03-26 12:38:19 +00003966static SDValue getShuffleScalarElt(SDNode *N, int Index, SelectionDAG &DAG,
3967 unsigned Depth) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003968 if (Depth == 6)
3969 return SDValue(); // Limit search depth.
3970
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003971 SDValue V = SDValue(N, 0);
3972 EVT VT = V.getValueType();
3973 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003974
3975 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
3976 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
3977 Index = SV->getMaskElt(Index);
3978
3979 if (Index < 0)
3980 return DAG.getUNDEF(VT.getVectorElementType());
3981
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003982 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003983 SDValue NewV = (Index < NumElems) ? SV->getOperand(0) : SV->getOperand(1);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003984 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00003985 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003986
3987 // Recurse into target specific vector shuffles to find scalars.
3988 if (isTargetShuffle(Opcode)) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003989 int NumElems = VT.getVectorNumElements();
3990 SmallVector<unsigned, 16> ShuffleMask;
3991 SDValue ImmN;
3992
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003993 switch(Opcode) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003994 case X86ISD::SHUFPS:
3995 case X86ISD::SHUFPD:
3996 ImmN = N->getOperand(N->getNumOperands()-1);
3997 DecodeSHUFPSMask(NumElems,
3998 cast<ConstantSDNode>(ImmN)->getZExtValue(),
3999 ShuffleMask);
4000 break;
4001 case X86ISD::PUNPCKHBW:
4002 case X86ISD::PUNPCKHWD:
4003 case X86ISD::PUNPCKHDQ:
4004 case X86ISD::PUNPCKHQDQ:
4005 DecodePUNPCKHMask(NumElems, ShuffleMask);
4006 break;
4007 case X86ISD::UNPCKHPS:
4008 case X86ISD::UNPCKHPD:
4009 DecodeUNPCKHPMask(NumElems, ShuffleMask);
4010 break;
4011 case X86ISD::PUNPCKLBW:
4012 case X86ISD::PUNPCKLWD:
4013 case X86ISD::PUNPCKLDQ:
4014 case X86ISD::PUNPCKLQDQ:
David Greenec4db4e52011-02-28 19:06:56 +00004015 DecodePUNPCKLMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004016 break;
4017 case X86ISD::UNPCKLPS:
4018 case X86ISD::UNPCKLPD:
David Greenec4db4e52011-02-28 19:06:56 +00004019 case X86ISD::VUNPCKLPS:
4020 case X86ISD::VUNPCKLPD:
4021 case X86ISD::VUNPCKLPSY:
4022 case X86ISD::VUNPCKLPDY:
4023 DecodeUNPCKLPMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004024 break;
4025 case X86ISD::MOVHLPS:
4026 DecodeMOVHLPSMask(NumElems, ShuffleMask);
4027 break;
4028 case X86ISD::MOVLHPS:
4029 DecodeMOVLHPSMask(NumElems, ShuffleMask);
4030 break;
4031 case X86ISD::PSHUFD:
4032 ImmN = N->getOperand(N->getNumOperands()-1);
4033 DecodePSHUFMask(NumElems,
4034 cast<ConstantSDNode>(ImmN)->getZExtValue(),
4035 ShuffleMask);
4036 break;
4037 case X86ISD::PSHUFHW:
4038 ImmN = N->getOperand(N->getNumOperands()-1);
4039 DecodePSHUFHWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
4040 ShuffleMask);
4041 break;
4042 case X86ISD::PSHUFLW:
4043 ImmN = N->getOperand(N->getNumOperands()-1);
4044 DecodePSHUFLWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
4045 ShuffleMask);
4046 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004047 case X86ISD::MOVSS:
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00004048 case X86ISD::MOVSD: {
4049 // The index 0 always comes from the first element of the second source,
4050 // this is why MOVSS and MOVSD are used in the first place. The other
4051 // elements come from the other positions of the first source vector.
4052 unsigned OpNum = (Index == 0) ? 1 : 0;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004053 return getShuffleScalarElt(V.getOperand(OpNum).getNode(), Index, DAG,
4054 Depth+1);
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00004055 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004056 default:
4057 assert("not implemented for target shuffle node");
4058 return SDValue();
4059 }
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004060
4061 Index = ShuffleMask[Index];
4062 if (Index < 0)
4063 return DAG.getUNDEF(VT.getVectorElementType());
4064
4065 SDValue NewV = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
4066 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG,
4067 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004068 }
4069
4070 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004071 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004072 V = V.getOperand(0);
4073 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004074 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004075
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004076 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004077 return SDValue();
4078 }
4079
4080 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
4081 return (Index == 0) ? V.getOperand(0)
4082 : DAG.getUNDEF(VT.getVectorElementType());
4083
4084 if (V.getOpcode() == ISD::BUILD_VECTOR)
4085 return V.getOperand(Index);
4086
4087 return SDValue();
4088}
4089
4090/// getNumOfConsecutiveZeros - Return the number of elements of a vector
4091/// shuffle operation which come from a consecutively from a zero. The
Chris Lattner7a2bdde2011-04-15 05:18:47 +00004092/// search can start in two different directions, from left or right.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004093static
4094unsigned getNumOfConsecutiveZeros(SDNode *N, int NumElems,
4095 bool ZerosFromLeft, SelectionDAG &DAG) {
4096 int i = 0;
4097
4098 while (i < NumElems) {
4099 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004100 SDValue Elt = getShuffleScalarElt(N, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004101 if (!(Elt.getNode() &&
4102 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
4103 break;
4104 ++i;
4105 }
4106
4107 return i;
4108}
4109
4110/// isShuffleMaskConsecutive - Check if the shuffle mask indicies from MaskI to
4111/// MaskE correspond consecutively to elements from one of the vector operands,
4112/// starting from its index OpIdx. Also tell OpNum which source vector operand.
4113static
4114bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp, int MaskI, int MaskE,
4115 int OpIdx, int NumElems, unsigned &OpNum) {
4116 bool SeenV1 = false;
4117 bool SeenV2 = false;
4118
4119 for (int i = MaskI; i <= MaskE; ++i, ++OpIdx) {
4120 int Idx = SVOp->getMaskElt(i);
4121 // Ignore undef indicies
4122 if (Idx < 0)
4123 continue;
4124
4125 if (Idx < NumElems)
4126 SeenV1 = true;
4127 else
4128 SeenV2 = true;
4129
4130 // Only accept consecutive elements from the same vector
4131 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
4132 return false;
4133 }
4134
4135 OpNum = SeenV1 ? 0 : 1;
4136 return true;
4137}
4138
4139/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
4140/// logical left shift of a vector.
4141static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4142 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4143 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4144 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4145 false /* check zeros from right */, DAG);
4146 unsigned OpSrc;
4147
4148 if (!NumZeros)
4149 return false;
4150
4151 // Considering the elements in the mask that are not consecutive zeros,
4152 // check if they consecutively come from only one of the source vectors.
4153 //
4154 // V1 = {X, A, B, C} 0
4155 // \ \ \ /
4156 // vector_shuffle V1, V2 <1, 2, 3, X>
4157 //
4158 if (!isShuffleMaskConsecutive(SVOp,
4159 0, // Mask Start Index
4160 NumElems-NumZeros-1, // Mask End Index
4161 NumZeros, // Where to start looking in the src vector
4162 NumElems, // Number of elements in vector
4163 OpSrc)) // Which source operand ?
4164 return false;
4165
4166 isLeft = false;
4167 ShAmt = NumZeros;
4168 ShVal = SVOp->getOperand(OpSrc);
4169 return true;
4170}
4171
4172/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
4173/// logical left shift of a vector.
4174static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4175 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4176 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4177 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4178 true /* check zeros from left */, DAG);
4179 unsigned OpSrc;
4180
4181 if (!NumZeros)
4182 return false;
4183
4184 // Considering the elements in the mask that are not consecutive zeros,
4185 // check if they consecutively come from only one of the source vectors.
4186 //
4187 // 0 { A, B, X, X } = V2
4188 // / \ / /
4189 // vector_shuffle V1, V2 <X, X, 4, 5>
4190 //
4191 if (!isShuffleMaskConsecutive(SVOp,
4192 NumZeros, // Mask Start Index
4193 NumElems-1, // Mask End Index
4194 0, // Where to start looking in the src vector
4195 NumElems, // Number of elements in vector
4196 OpSrc)) // Which source operand ?
4197 return false;
4198
4199 isLeft = true;
4200 ShAmt = NumZeros;
4201 ShVal = SVOp->getOperand(OpSrc);
4202 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004203}
4204
4205/// isVectorShift - Returns true if the shuffle can be implemented as a
4206/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004207static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00004208 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004209 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
4210 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
4211 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004212
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004213 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00004214}
4215
Evan Chengc78d3b42006-04-24 18:01:45 +00004216/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
4217///
Dan Gohman475871a2008-07-27 21:46:04 +00004218static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00004219 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00004220 SelectionDAG &DAG,
4221 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004222 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00004223 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004224
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004225 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004226 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004227 bool First = true;
4228 for (unsigned i = 0; i < 16; ++i) {
4229 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
4230 if (ThisIsNonZero && First) {
4231 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004232 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004233 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004234 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004235 First = false;
4236 }
4237
4238 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00004239 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004240 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
4241 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004242 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004243 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00004244 }
4245 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004246 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
4247 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
4248 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00004249 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004250 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00004251 } else
4252 ThisElt = LastElt;
4253
Gabor Greifba36cb52008-08-28 21:40:38 +00004254 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00004255 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00004256 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00004257 }
4258 }
4259
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004260 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00004261}
4262
Bill Wendlinga348c562007-03-22 18:42:45 +00004263/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00004264///
Dan Gohman475871a2008-07-27 21:46:04 +00004265static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00004266 unsigned NumNonZero, unsigned NumZero,
4267 SelectionDAG &DAG,
4268 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004269 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00004270 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004271
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004272 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004273 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004274 bool First = true;
4275 for (unsigned i = 0; i < 8; ++i) {
4276 bool isNonZero = (NonZeros & (1 << i)) != 0;
4277 if (isNonZero) {
4278 if (First) {
4279 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004280 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004281 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004282 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004283 First = false;
4284 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004285 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004286 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00004287 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00004288 }
4289 }
4290
4291 return V;
4292}
4293
Evan Chengf26ffe92008-05-29 08:22:04 +00004294/// getVShift - Return a vector logical shift node.
4295///
Owen Andersone50ed302009-08-10 22:56:29 +00004296static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00004297 unsigned NumBits, SelectionDAG &DAG,
4298 const TargetLowering &TLI, DebugLoc dl) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004299 EVT ShVT = MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00004300 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004301 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
4302 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004303 DAG.getNode(Opc, dl, ShVT, SrcOp,
Owen Anderson95771af2011-02-25 21:41:48 +00004304 DAG.getConstant(NumBits,
4305 TLI.getShiftAmountTy(SrcOp.getValueType()))));
Evan Chengf26ffe92008-05-29 08:22:04 +00004306}
4307
Dan Gohman475871a2008-07-27 21:46:04 +00004308SDValue
Evan Chengc3630942009-12-09 21:00:30 +00004309X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00004310 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00004311
Evan Chengc3630942009-12-09 21:00:30 +00004312 // Check if the scalar load can be widened into a vector load. And if
4313 // the address is "base + cst" see if the cst can be "absorbed" into
4314 // the shuffle mask.
4315 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
4316 SDValue Ptr = LD->getBasePtr();
4317 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
4318 return SDValue();
4319 EVT PVT = LD->getValueType(0);
4320 if (PVT != MVT::i32 && PVT != MVT::f32)
4321 return SDValue();
4322
4323 int FI = -1;
4324 int64_t Offset = 0;
4325 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
4326 FI = FINode->getIndex();
4327 Offset = 0;
Chris Lattner0a9481f2011-02-13 22:25:43 +00004328 } else if (DAG.isBaseWithConstantOffset(Ptr) &&
Evan Chengc3630942009-12-09 21:00:30 +00004329 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4330 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4331 Offset = Ptr.getConstantOperandVal(1);
4332 Ptr = Ptr.getOperand(0);
4333 } else {
4334 return SDValue();
4335 }
4336
4337 SDValue Chain = LD->getChain();
4338 // Make sure the stack object alignment is at least 16.
4339 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
4340 if (DAG.InferPtrAlignment(Ptr) < 16) {
4341 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00004342 // Can't change the alignment. FIXME: It's possible to compute
4343 // the exact stack offset and reference FI + adjust offset instead.
4344 // If someone *really* cares about this. That's the way to implement it.
4345 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004346 } else {
4347 MFI->setObjectAlignment(FI, 16);
4348 }
4349 }
4350
4351 // (Offset % 16) must be multiple of 4. Then address is then
4352 // Ptr + (Offset & ~15).
4353 if (Offset < 0)
4354 return SDValue();
4355 if ((Offset % 16) & 3)
4356 return SDValue();
4357 int64_t StartOffset = Offset & ~15;
4358 if (StartOffset)
4359 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
4360 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
4361
4362 int EltNo = (Offset - StartOffset) >> 2;
4363 int Mask[4] = { EltNo, EltNo, EltNo, EltNo };
4364 EVT VT = (PVT == MVT::i32) ? MVT::v4i32 : MVT::v4f32;
Chris Lattner51abfe42010-09-21 06:02:19 +00004365 SDValue V1 = DAG.getLoad(VT, dl, Chain, Ptr,
4366 LD->getPointerInfo().getWithOffset(StartOffset),
David Greene67c9d422010-02-15 16:53:33 +00004367 false, false, 0);
Evan Chengc3630942009-12-09 21:00:30 +00004368 // Canonicalize it to a v4i32 shuffle.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004369 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, V1);
4370 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Chengc3630942009-12-09 21:00:30 +00004371 DAG.getVectorShuffle(MVT::v4i32, dl, V1,
Chris Lattner51abfe42010-09-21 06:02:19 +00004372 DAG.getUNDEF(MVT::v4i32),&Mask[0]));
Evan Chengc3630942009-12-09 21:00:30 +00004373 }
4374
4375 return SDValue();
4376}
4377
Michael J. Spencerec38de22010-10-10 22:04:20 +00004378/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
4379/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00004380/// load which has the same value as a build_vector whose operands are 'elts'.
4381///
4382/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00004383///
Nate Begeman1449f292010-03-24 22:19:06 +00004384/// FIXME: we'd also like to handle the case where the last elements are zero
4385/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
4386/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004387static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00004388 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004389 EVT EltVT = VT.getVectorElementType();
4390 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00004391
Nate Begemanfdea31a2010-03-24 20:49:50 +00004392 LoadSDNode *LDBase = NULL;
4393 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004394
Nate Begeman1449f292010-03-24 22:19:06 +00004395 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00004396 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00004397 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004398 for (unsigned i = 0; i < NumElems; ++i) {
4399 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00004400
Nate Begemanfdea31a2010-03-24 20:49:50 +00004401 if (!Elt.getNode() ||
4402 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
4403 return SDValue();
4404 if (!LDBase) {
4405 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
4406 return SDValue();
4407 LDBase = cast<LoadSDNode>(Elt.getNode());
4408 LastLoadedElt = i;
4409 continue;
4410 }
4411 if (Elt.getOpcode() == ISD::UNDEF)
4412 continue;
4413
4414 LoadSDNode *LD = cast<LoadSDNode>(Elt);
4415 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
4416 return SDValue();
4417 LastLoadedElt = i;
4418 }
Nate Begeman1449f292010-03-24 22:19:06 +00004419
4420 // If we have found an entire vector of loads and undefs, then return a large
4421 // load of the entire vector width starting at the base pointer. If we found
4422 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004423 if (LastLoadedElt == NumElems - 1) {
4424 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00004425 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004426 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004427 LDBase->isVolatile(), LDBase->isNonTemporal(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00004428 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004429 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004430 LDBase->isVolatile(), LDBase->isNonTemporal(),
4431 LDBase->getAlignment());
4432 } else if (NumElems == 4 && LastLoadedElt == 1) {
4433 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
4434 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Chris Lattner88641552010-09-22 00:34:38 +00004435 SDValue ResNode = DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys,
4436 Ops, 2, MVT::i32,
4437 LDBase->getMemOperand());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004438 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00004439 }
4440 return SDValue();
4441}
4442
Evan Chengc3630942009-12-09 21:00:30 +00004443SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004444X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004445 DebugLoc dl = Op.getDebugLoc();
David Greenea5f26012011-02-07 19:36:54 +00004446
David Greenef125a292011-02-08 19:04:41 +00004447 EVT VT = Op.getValueType();
4448 EVT ExtVT = VT.getVectorElementType();
4449
4450 unsigned NumElems = Op.getNumOperands();
4451
4452 // For AVX-length vectors, build the individual 128-bit pieces and
4453 // use shuffles to put them in place.
Owen Anderson95771af2011-02-25 21:41:48 +00004454 if (VT.getSizeInBits() > 256 &&
4455 Subtarget->hasAVX() &&
David Greenef125a292011-02-08 19:04:41 +00004456 !ISD::isBuildVectorAllZeros(Op.getNode())) {
4457 SmallVector<SDValue, 8> V;
4458 V.resize(NumElems);
4459 for (unsigned i = 0; i < NumElems; ++i) {
4460 V[i] = Op.getOperand(i);
4461 }
Owen Anderson95771af2011-02-25 21:41:48 +00004462
David Greenef125a292011-02-08 19:04:41 +00004463 EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElems/2);
4464
4465 // Build the lower subvector.
4466 SDValue Lower = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[0], NumElems/2);
4467 // Build the upper subvector.
4468 SDValue Upper = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[NumElems / 2],
4469 NumElems/2);
4470
4471 return ConcatVectors(Lower, Upper, DAG);
4472 }
4473
Chris Lattner6e80e442010-08-28 17:15:43 +00004474 // All zero's are handled with pxor in SSE2 and above, xorps in SSE1.
4475 // All one's are handled with pcmpeqd. In AVX, zero's are handled with
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004476 // vpxor in 128-bit and xor{pd,ps} in 256-bit, but no 256 version of pcmpeqd
4477 // is present, so AllOnes is ignored.
4478 if (ISD::isBuildVectorAllZeros(Op.getNode()) ||
4479 (Op.getValueType().getSizeInBits() != 256 &&
4480 ISD::isBuildVectorAllOnes(Op.getNode()))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004481 // Canonicalize this to <4 x i32> (SSE) to
Chris Lattner8a594482007-11-25 00:24:49 +00004482 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
4483 // eliminated on x86-32 hosts.
Dale Johannesen0488fb62010-09-30 23:57:10 +00004484 if (Op.getValueType() == MVT::v4i32)
Chris Lattner8a594482007-11-25 00:24:49 +00004485 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004486
Gabor Greifba36cb52008-08-28 21:40:38 +00004487 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00004488 return getOnesVector(Op.getValueType(), DAG, dl);
4489 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00004490 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004491
Owen Andersone50ed302009-08-10 22:56:29 +00004492 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004493
Evan Cheng0db9fe62006-04-25 20:13:52 +00004494 unsigned NumZero = 0;
4495 unsigned NumNonZero = 0;
4496 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004497 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00004498 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004499 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00004500 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00004501 if (Elt.getOpcode() == ISD::UNDEF)
4502 continue;
4503 Values.insert(Elt);
4504 if (Elt.getOpcode() != ISD::Constant &&
4505 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004506 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00004507 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00004508 NumZero++;
4509 else {
4510 NonZeros |= (1 << i);
4511 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004512 }
4513 }
4514
Chris Lattner97a2a562010-08-26 05:24:29 +00004515 // All undef vector. Return an UNDEF. All zero vectors were handled above.
4516 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00004517 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004518
Chris Lattner67f453a2008-03-09 05:42:06 +00004519 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00004520 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004521 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00004522 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00004523
Chris Lattner62098042008-03-09 01:05:04 +00004524 // If this is an insertion of an i64 value on x86-32, and if the top bits of
4525 // the value are obviously zero, truncate the value to i32 and do the
4526 // insertion that way. Only do this if the value is non-constant or if the
4527 // value is a constant being inserted into element 0. It is cheaper to do
4528 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00004529 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00004530 (!IsAllConstants || Idx == 0)) {
4531 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004532 // Handle SSE only.
4533 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
4534 EVT VecVT = MVT::v4i32;
4535 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00004536
Chris Lattner62098042008-03-09 01:05:04 +00004537 // Truncate the value (which may itself be a constant) to i32, and
4538 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00004539 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00004540 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00004541 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
4542 Subtarget->hasSSE2(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00004543
Chris Lattner62098042008-03-09 01:05:04 +00004544 // Now we have our 32-bit value zero extended in the low element of
4545 // a vector. If Idx != 0, swizzle it into place.
4546 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004547 SmallVector<int, 4> Mask;
4548 Mask.push_back(Idx);
4549 for (unsigned i = 1; i != VecElts; ++i)
4550 Mask.push_back(i);
4551 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopherfd179292009-08-27 18:07:15 +00004552 DAG.getUNDEF(Item.getValueType()),
Nate Begeman9008ca62009-04-27 18:41:29 +00004553 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004554 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004555 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00004556 }
4557 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004558
Chris Lattner19f79692008-03-08 22:59:52 +00004559 // If we have a constant or non-constant insertion into the low element of
4560 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
4561 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00004562 // depending on what the source datatype is.
4563 if (Idx == 0) {
4564 if (NumZero == 0) {
4565 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Owen Anderson825b72b2009-08-11 20:47:22 +00004566 } else if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
4567 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Eli Friedman10415532009-06-06 06:05:10 +00004568 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
4569 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
4570 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget->hasSSE2(),
4571 DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00004572 } else if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
4573 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Dale Johannesen0488fb62010-09-30 23:57:10 +00004574 assert(VT.getSizeInBits() == 128 && "Expected an SSE value type!");
4575 EVT MiddleVT = MVT::v4i32;
Eli Friedman10415532009-06-06 06:05:10 +00004576 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
4577 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
4578 Subtarget->hasSSE2(), DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004579 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00004580 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004581 }
Evan Chengf26ffe92008-05-29 08:22:04 +00004582
4583 // Is it a vector logical left shift?
4584 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00004585 X86::isZeroNode(Op.getOperand(0)) &&
4586 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004587 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00004588 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00004589 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004590 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00004591 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004592 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004593
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004594 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00004595 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004596
Chris Lattner19f79692008-03-08 22:59:52 +00004597 // Otherwise, if this is a vector with i32 or f32 elements, and the element
4598 // is a non-constant being inserted into an element other than the low one,
4599 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
4600 // movd/movss) to move this into the low element, then shuffle it into
4601 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004602 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00004603 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00004604
Evan Cheng0db9fe62006-04-25 20:13:52 +00004605 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00004606 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
4607 Subtarget->hasSSE2(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00004608 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004609 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00004610 MaskVec.push_back(i == Idx ? 0 : 1);
4611 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004612 }
4613 }
4614
Chris Lattner67f453a2008-03-09 05:42:06 +00004615 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00004616 if (Values.size() == 1) {
4617 if (EVTBits == 32) {
4618 // Instead of a shuffle like this:
4619 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
4620 // Check if it's possible to issue this instead.
4621 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
4622 unsigned Idx = CountTrailingZeros_32(NonZeros);
4623 SDValue Item = Op.getOperand(Idx);
4624 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
4625 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
4626 }
Dan Gohman475871a2008-07-27 21:46:04 +00004627 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004628 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004629
Dan Gohmana3941172007-07-24 22:55:08 +00004630 // A vector full of immediates; various special cases are already
4631 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004632 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00004633 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00004634
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00004635 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00004636 if (EVTBits == 64) {
4637 if (NumNonZero == 1) {
4638 // One half is zero or undef.
4639 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00004640 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00004641 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00004642 return getShuffleVectorZeroOrUndef(V2, Idx, true,
4643 Subtarget->hasSSE2(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00004644 }
Dan Gohman475871a2008-07-27 21:46:04 +00004645 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00004646 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004647
4648 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00004649 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00004650 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00004651 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004652 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004653 }
4654
Bill Wendling826f36f2007-03-28 00:57:11 +00004655 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00004656 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Chris Lattner97a2a562010-08-26 05:24:29 +00004657 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004658 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004659 }
4660
4661 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00004662 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00004663 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004664 if (NumElems == 4 && NumZero > 0) {
4665 for (unsigned i = 0; i < 4; ++i) {
4666 bool isZero = !(NonZeros & (1 << i));
4667 if (isZero)
Dale Johannesenace16102009-02-03 19:33:06 +00004668 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004669 else
Dale Johannesenace16102009-02-03 19:33:06 +00004670 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004671 }
4672
4673 for (unsigned i = 0; i < 2; ++i) {
4674 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
4675 default: break;
4676 case 0:
4677 V[i] = V[i*2]; // Must be a zero vector.
4678 break;
4679 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00004680 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004681 break;
4682 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00004683 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004684 break;
4685 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00004686 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004687 break;
4688 }
4689 }
4690
Nate Begeman9008ca62009-04-27 18:41:29 +00004691 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004692 bool Reverse = (NonZeros & 0x3) == 2;
4693 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004694 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004695 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
4696 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004697 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
4698 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004699 }
4700
Nate Begemanfdea31a2010-03-24 20:49:50 +00004701 if (Values.size() > 1 && VT.getSizeInBits() == 128) {
4702 // Check for a build vector of consecutive loads.
4703 for (unsigned i = 0; i < NumElems; ++i)
4704 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00004705
Nate Begemanfdea31a2010-03-24 20:49:50 +00004706 // Check for elements which are consecutive loads.
4707 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
4708 if (LD.getNode())
4709 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004710
4711 // For SSE 4.1, use insertps to put the high elements into the low element.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004712 if (getSubtarget()->hasSSE41()) {
Chris Lattner24faf612010-08-28 17:59:08 +00004713 SDValue Result;
4714 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
4715 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
4716 else
4717 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00004718
Chris Lattner24faf612010-08-28 17:59:08 +00004719 for (unsigned i = 1; i < NumElems; ++i) {
4720 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
4721 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00004722 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00004723 }
4724 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00004725 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00004726
Chris Lattner6e80e442010-08-28 17:15:43 +00004727 // Otherwise, expand into a number of unpckl*, start by extending each of
4728 // our (non-undef) elements to the full vector width with the element in the
4729 // bottom slot of the vector (which generates no code for SSE).
4730 for (unsigned i = 0; i < NumElems; ++i) {
4731 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
4732 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
4733 else
4734 V[i] = DAG.getUNDEF(VT);
4735 }
4736
4737 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004738 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
4739 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
4740 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00004741 unsigned EltStride = NumElems >> 1;
4742 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00004743 for (unsigned i = 0; i < EltStride; ++i) {
4744 // If V[i+EltStride] is undef and this is the first round of mixing,
4745 // then it is safe to just drop this shuffle: V[i] is already in the
4746 // right place, the one element (since it's the first round) being
4747 // inserted as undef can be dropped. This isn't safe for successive
4748 // rounds because they will permute elements within both vectors.
4749 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
4750 EltStride == NumElems/2)
4751 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004752
Chris Lattner6e80e442010-08-28 17:15:43 +00004753 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00004754 }
Chris Lattner6e80e442010-08-28 17:15:43 +00004755 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004756 }
4757 return V[0];
4758 }
Dan Gohman475871a2008-07-27 21:46:04 +00004759 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004760}
4761
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004762SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004763X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const {
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004764 // We support concatenate two MMX registers and place them in a MMX
4765 // register. This is better than doing a stack convert.
4766 DebugLoc dl = Op.getDebugLoc();
4767 EVT ResVT = Op.getValueType();
4768 assert(Op.getNumOperands() == 2);
4769 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
4770 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
4771 int Mask[2];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004772 SDValue InVec = DAG.getNode(ISD::BITCAST,dl, MVT::v1i64, Op.getOperand(0));
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004773 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4774 InVec = Op.getOperand(1);
4775 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4776 unsigned NumElts = ResVT.getVectorNumElements();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004777 VecOp = DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004778 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
4779 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
4780 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004781 InVec = DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, InVec);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004782 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4783 Mask[0] = 0; Mask[1] = 2;
4784 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
4785 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004786 return DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004787}
4788
Nate Begemanb9a47b82009-02-23 08:49:38 +00004789// v8i16 shuffles - Prefer shuffles in the following order:
4790// 1. [all] pshuflw, pshufhw, optional move
4791// 2. [ssse3] 1 x pshufb
4792// 3. [ssse3] 2 x pshufb + 1 x por
4793// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004794SDValue
4795X86TargetLowering::LowerVECTOR_SHUFFLEv8i16(SDValue Op,
4796 SelectionDAG &DAG) const {
4797 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00004798 SDValue V1 = SVOp->getOperand(0);
4799 SDValue V2 = SVOp->getOperand(1);
4800 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004801 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00004802
Nate Begemanb9a47b82009-02-23 08:49:38 +00004803 // Determine if more than 1 of the words in each of the low and high quadwords
4804 // of the result come from the same quadword of one of the two inputs. Undef
4805 // mask values count as coming from any quadword, for better codegen.
4806 SmallVector<unsigned, 4> LoQuad(4);
4807 SmallVector<unsigned, 4> HiQuad(4);
4808 BitVector InputQuads(4);
4809 for (unsigned i = 0; i < 8; ++i) {
4810 SmallVectorImpl<unsigned> &Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00004811 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004812 MaskVals.push_back(EltIdx);
4813 if (EltIdx < 0) {
4814 ++Quad[0];
4815 ++Quad[1];
4816 ++Quad[2];
4817 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00004818 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004819 }
4820 ++Quad[EltIdx / 4];
4821 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00004822 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004823
Nate Begemanb9a47b82009-02-23 08:49:38 +00004824 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004825 unsigned MaxQuad = 1;
4826 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004827 if (LoQuad[i] > MaxQuad) {
4828 BestLoQuad = i;
4829 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004830 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004831 }
4832
Nate Begemanb9a47b82009-02-23 08:49:38 +00004833 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004834 MaxQuad = 1;
4835 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004836 if (HiQuad[i] > MaxQuad) {
4837 BestHiQuad = i;
4838 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004839 }
4840 }
4841
Nate Begemanb9a47b82009-02-23 08:49:38 +00004842 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00004843 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00004844 // single pshufb instruction is necessary. If There are more than 2 input
4845 // quads, disable the next transformation since it does not help SSSE3.
4846 bool V1Used = InputQuads[0] || InputQuads[1];
4847 bool V2Used = InputQuads[2] || InputQuads[3];
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004848 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004849 if (InputQuads.count() == 2 && V1Used && V2Used) {
4850 BestLoQuad = InputQuads.find_first();
4851 BestHiQuad = InputQuads.find_next(BestLoQuad);
4852 }
4853 if (InputQuads.count() > 2) {
4854 BestLoQuad = -1;
4855 BestHiQuad = -1;
4856 }
4857 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004858
Nate Begemanb9a47b82009-02-23 08:49:38 +00004859 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
4860 // the shuffle mask. If a quad is scored as -1, that means that it contains
4861 // words from all 4 input quadwords.
4862 SDValue NewV;
4863 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004864 SmallVector<int, 8> MaskV;
4865 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
4866 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Eric Christopherfd179292009-08-27 18:07:15 +00004867 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004868 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
4869 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
4870 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00004871
Nate Begemanb9a47b82009-02-23 08:49:38 +00004872 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
4873 // source words for the shuffle, to aid later transformations.
4874 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00004875 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00004876 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004877 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00004878 if (idx != (int)i)
4879 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004880 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00004881 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004882 AllWordsInNewV = false;
4883 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00004884 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004885
Nate Begemanb9a47b82009-02-23 08:49:38 +00004886 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
4887 if (AllWordsInNewV) {
4888 for (int i = 0; i != 8; ++i) {
4889 int idx = MaskVals[i];
4890 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00004891 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004892 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004893 if ((idx != i) && idx < 4)
4894 pshufhw = false;
4895 if ((idx != i) && idx > 3)
4896 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00004897 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00004898 V1 = NewV;
4899 V2Used = false;
4900 BestLoQuad = 0;
4901 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004902 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004903
Nate Begemanb9a47b82009-02-23 08:49:38 +00004904 // If we've eliminated the use of V2, and the new mask is a pshuflw or
4905 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00004906 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00004907 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
4908 unsigned TargetMask = 0;
4909 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00004910 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00004911 TargetMask = pshufhw ? X86::getShufflePSHUFHWImmediate(NewV.getNode()):
4912 X86::getShufflePSHUFLWImmediate(NewV.getNode());
4913 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004914 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00004915 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004916 }
Eric Christopherfd179292009-08-27 18:07:15 +00004917
Nate Begemanb9a47b82009-02-23 08:49:38 +00004918 // If we have SSSE3, and all words of the result are from 1 input vector,
4919 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
4920 // is present, fall back to case 4.
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004921 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004922 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004923
Nate Begemanb9a47b82009-02-23 08:49:38 +00004924 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00004925 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00004926 // mask, and elements that come from V1 in the V2 mask, so that the two
4927 // results can be OR'd together.
4928 bool TwoInputs = V1Used && V2Used;
4929 for (unsigned i = 0; i != 8; ++i) {
4930 int EltIdx = MaskVals[i] * 2;
4931 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004932 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4933 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004934 continue;
4935 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004936 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
4937 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004938 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004939 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004940 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004941 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004942 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004943 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004944 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004945
Nate Begemanb9a47b82009-02-23 08:49:38 +00004946 // Calculate the shuffle mask for the second input, shuffle it, and
4947 // OR it with the first shuffled input.
4948 pshufbMask.clear();
4949 for (unsigned i = 0; i != 8; ++i) {
4950 int EltIdx = MaskVals[i] * 2;
4951 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004952 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4953 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004954 continue;
4955 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004956 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
4957 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004958 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004959 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00004960 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00004961 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004962 MVT::v16i8, &pshufbMask[0], 16));
4963 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004964 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004965 }
4966
4967 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
4968 // and update MaskVals with new element order.
4969 BitVector InOrder(8);
4970 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004971 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004972 for (int i = 0; i != 4; ++i) {
4973 int idx = MaskVals[i];
4974 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004975 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004976 InOrder.set(i);
4977 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004978 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004979 InOrder.set(i);
4980 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004981 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004982 }
4983 }
4984 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004985 MaskV.push_back(i);
Owen Anderson825b72b2009-08-11 20:47:22 +00004986 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004987 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004988
4989 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
4990 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
4991 NewV.getOperand(0),
4992 X86::getShufflePSHUFLWImmediate(NewV.getNode()),
4993 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004994 }
Eric Christopherfd179292009-08-27 18:07:15 +00004995
Nate Begemanb9a47b82009-02-23 08:49:38 +00004996 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
4997 // and update MaskVals with the new element order.
4998 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004999 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005000 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005001 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005002 for (unsigned i = 4; i != 8; ++i) {
5003 int idx = MaskVals[i];
5004 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005005 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005006 InOrder.set(i);
5007 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005008 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005009 InOrder.set(i);
5010 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005011 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005012 }
5013 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005014 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005015 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005016
5017 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
5018 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
5019 NewV.getOperand(0),
5020 X86::getShufflePSHUFHWImmediate(NewV.getNode()),
5021 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005022 }
Eric Christopherfd179292009-08-27 18:07:15 +00005023
Nate Begemanb9a47b82009-02-23 08:49:38 +00005024 // In case BestHi & BestLo were both -1, which means each quadword has a word
5025 // from each of the four input quadwords, calculate the InOrder bitvector now
5026 // before falling through to the insert/extract cleanup.
5027 if (BestLoQuad == -1 && BestHiQuad == -1) {
5028 NewV = V1;
5029 for (int i = 0; i != 8; ++i)
5030 if (MaskVals[i] < 0 || MaskVals[i] == i)
5031 InOrder.set(i);
5032 }
Eric Christopherfd179292009-08-27 18:07:15 +00005033
Nate Begemanb9a47b82009-02-23 08:49:38 +00005034 // The other elements are put in the right place using pextrw and pinsrw.
5035 for (unsigned i = 0; i != 8; ++i) {
5036 if (InOrder[i])
5037 continue;
5038 int EltIdx = MaskVals[i];
5039 if (EltIdx < 0)
5040 continue;
5041 SDValue ExtOp = (EltIdx < 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00005042 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005043 DAG.getIntPtrConstant(EltIdx))
Owen Anderson825b72b2009-08-11 20:47:22 +00005044 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005045 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00005046 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005047 DAG.getIntPtrConstant(i));
5048 }
5049 return NewV;
5050}
5051
5052// v16i8 shuffles - Prefer shuffles in the following order:
5053// 1. [ssse3] 1 x pshufb
5054// 2. [ssse3] 2 x pshufb + 1 x por
5055// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
5056static
Nate Begeman9008ca62009-04-27 18:41:29 +00005057SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00005058 SelectionDAG &DAG,
5059 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005060 SDValue V1 = SVOp->getOperand(0);
5061 SDValue V2 = SVOp->getOperand(1);
5062 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005063 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00005064 SVOp->getMask(MaskVals);
Eric Christopherfd179292009-08-27 18:07:15 +00005065
Nate Begemanb9a47b82009-02-23 08:49:38 +00005066 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00005067 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00005068 // present, fall back to case 3.
5069 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
5070 bool V1Only = true;
5071 bool V2Only = true;
5072 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005073 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00005074 if (EltIdx < 0)
5075 continue;
5076 if (EltIdx < 16)
5077 V2Only = false;
5078 else
5079 V1Only = false;
5080 }
Eric Christopherfd179292009-08-27 18:07:15 +00005081
Nate Begemanb9a47b82009-02-23 08:49:38 +00005082 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
5083 if (TLI.getSubtarget()->hasSSSE3()) {
5084 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005085
Nate Begemanb9a47b82009-02-23 08:49:38 +00005086 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00005087 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005088 //
5089 // Otherwise, we have elements from both input vectors, and must zero out
5090 // elements that come from V2 in the first mask, and V1 in the second mask
5091 // so that we can OR them together.
5092 bool TwoInputs = !(V1Only || V2Only);
5093 for (unsigned i = 0; i != 16; ++i) {
5094 int EltIdx = MaskVals[i];
5095 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005096 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005097 continue;
5098 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005099 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005100 }
5101 // If all the elements are from V2, assign it to V1 and return after
5102 // building the first pshufb.
5103 if (V2Only)
5104 V1 = V2;
Owen Anderson825b72b2009-08-11 20:47:22 +00005105 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005106 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005107 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005108 if (!TwoInputs)
5109 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00005110
Nate Begemanb9a47b82009-02-23 08:49:38 +00005111 // Calculate the shuffle mask for the second input, shuffle it, and
5112 // OR it with the first shuffled input.
5113 pshufbMask.clear();
5114 for (unsigned i = 0; i != 16; ++i) {
5115 int EltIdx = MaskVals[i];
5116 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005117 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005118 continue;
5119 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005120 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005121 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005122 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005123 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005124 MVT::v16i8, &pshufbMask[0], 16));
5125 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005126 }
Eric Christopherfd179292009-08-27 18:07:15 +00005127
Nate Begemanb9a47b82009-02-23 08:49:38 +00005128 // No SSSE3 - Calculate in place words and then fix all out of place words
5129 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
5130 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005131 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
5132 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005133 SDValue NewV = V2Only ? V2 : V1;
5134 for (int i = 0; i != 8; ++i) {
5135 int Elt0 = MaskVals[i*2];
5136 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00005137
Nate Begemanb9a47b82009-02-23 08:49:38 +00005138 // This word of the result is all undef, skip it.
5139 if (Elt0 < 0 && Elt1 < 0)
5140 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005141
Nate Begemanb9a47b82009-02-23 08:49:38 +00005142 // This word of the result is already in the correct place, skip it.
5143 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
5144 continue;
5145 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
5146 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005147
Nate Begemanb9a47b82009-02-23 08:49:38 +00005148 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
5149 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
5150 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00005151
5152 // If Elt0 and Elt1 are defined, are consecutive, and can be load
5153 // using a single extract together, load it and store it.
5154 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005155 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005156 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00005157 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005158 DAG.getIntPtrConstant(i));
5159 continue;
5160 }
5161
Nate Begemanb9a47b82009-02-23 08:49:38 +00005162 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00005163 // source byte is not also odd, shift the extracted word left 8 bits
5164 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005165 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005166 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005167 DAG.getIntPtrConstant(Elt1 / 2));
5168 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005169 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Owen Anderson95771af2011-02-25 21:41:48 +00005170 DAG.getConstant(8,
5171 TLI.getShiftAmountTy(InsElt.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005172 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005173 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
5174 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005175 }
5176 // If Elt0 is defined, extract it from the appropriate source. If the
5177 // source byte is not also even, shift the extracted word right 8 bits. If
5178 // Elt1 was also defined, OR the extracted values together before
5179 // inserting them in the result.
5180 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005181 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005182 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
5183 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005184 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Owen Anderson95771af2011-02-25 21:41:48 +00005185 DAG.getConstant(8,
5186 TLI.getShiftAmountTy(InsElt0.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005187 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005188 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
5189 DAG.getConstant(0x00FF, MVT::i16));
5190 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00005191 : InsElt0;
5192 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005193 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005194 DAG.getIntPtrConstant(i));
5195 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005196 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005197}
5198
Evan Cheng7a831ce2007-12-15 03:00:47 +00005199/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005200/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00005201/// done when every pair / quad of shuffle mask elements point to elements in
5202/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005203/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00005204static
Nate Begeman9008ca62009-04-27 18:41:29 +00005205SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005206 SelectionDAG &DAG, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00005207 EVT VT = SVOp->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00005208 SDValue V1 = SVOp->getOperand(0);
5209 SDValue V2 = SVOp->getOperand(1);
5210 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00005211 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005212 EVT NewVT;
Owen Anderson825b72b2009-08-11 20:47:22 +00005213 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005214 default: assert(false && "Unexpected!");
Owen Anderson825b72b2009-08-11 20:47:22 +00005215 case MVT::v4f32: NewVT = MVT::v2f64; break;
5216 case MVT::v4i32: NewVT = MVT::v2i64; break;
5217 case MVT::v8i16: NewVT = MVT::v4i32; break;
5218 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00005219 }
5220
Nate Begeman9008ca62009-04-27 18:41:29 +00005221 int Scale = NumElems / NewWidth;
5222 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00005223 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005224 int StartIdx = -1;
5225 for (int j = 0; j < Scale; ++j) {
5226 int EltIdx = SVOp->getMaskElt(i+j);
5227 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005228 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00005229 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00005230 StartIdx = EltIdx - (EltIdx % Scale);
5231 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00005232 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00005233 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005234 if (StartIdx == -1)
5235 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00005236 else
Nate Begeman9008ca62009-04-27 18:41:29 +00005237 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005238 }
5239
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005240 V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, V1);
5241 V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00005242 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005243}
5244
Evan Chengd880b972008-05-09 21:53:03 +00005245/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005246///
Owen Andersone50ed302009-08-10 22:56:29 +00005247static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00005248 SDValue SrcOp, SelectionDAG &DAG,
5249 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005250 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005251 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00005252 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00005253 LD = dyn_cast<LoadSDNode>(SrcOp);
5254 if (!LD) {
5255 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
5256 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00005257 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00005258 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00005259 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005260 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00005261 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005262 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00005263 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005264 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005265 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
5266 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
5267 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00005268 SrcOp.getOperand(0)
5269 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005270 }
5271 }
5272 }
5273
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005274 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005275 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005276 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00005277 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005278}
5279
Evan Chengace3c172008-07-22 21:13:36 +00005280/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
5281/// shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00005282static SDValue
Nate Begeman9008ca62009-04-27 18:41:29 +00005283LowerVECTOR_SHUFFLE_4wide(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
5284 SDValue V1 = SVOp->getOperand(0);
5285 SDValue V2 = SVOp->getOperand(1);
5286 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005287 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00005288
Evan Chengace3c172008-07-22 21:13:36 +00005289 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00005290 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00005291 SmallVector<int, 8> Mask1(4U, -1);
5292 SmallVector<int, 8> PermMask;
5293 SVOp->getMask(PermMask);
5294
Evan Chengace3c172008-07-22 21:13:36 +00005295 unsigned NumHi = 0;
5296 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00005297 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005298 int Idx = PermMask[i];
5299 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00005300 Locs[i] = std::make_pair(-1, -1);
5301 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005302 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
5303 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00005304 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00005305 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005306 NumLo++;
5307 } else {
5308 Locs[i] = std::make_pair(1, NumHi);
5309 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00005310 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005311 NumHi++;
5312 }
5313 }
5314 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005315
Evan Chengace3c172008-07-22 21:13:36 +00005316 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005317 // If no more than two elements come from either vector. This can be
5318 // implemented with two shuffles. First shuffle gather the elements.
5319 // The second shuffle, which takes the first shuffle as both of its
5320 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00005321 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005322
Nate Begeman9008ca62009-04-27 18:41:29 +00005323 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopherfd179292009-08-27 18:07:15 +00005324
Evan Chengace3c172008-07-22 21:13:36 +00005325 for (unsigned i = 0; i != 4; ++i) {
5326 if (Locs[i].first == -1)
5327 continue;
5328 else {
5329 unsigned Idx = (i < 2) ? 0 : 4;
5330 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00005331 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005332 }
5333 }
5334
Nate Begeman9008ca62009-04-27 18:41:29 +00005335 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005336 } else if (NumLo == 3 || NumHi == 3) {
5337 // Otherwise, we must have three elements from one vector, call it X, and
5338 // one element from the other, call it Y. First, use a shufps to build an
5339 // intermediate vector with the one element from Y and the element from X
5340 // that will be in the same half in the final destination (the indexes don't
5341 // matter). Then, use a shufps to build the final vector, taking the half
5342 // containing the element from Y from the intermediate, and the other half
5343 // from X.
5344 if (NumHi == 3) {
5345 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00005346 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005347 std::swap(V1, V2);
5348 }
5349
5350 // Find the element from V2.
5351 unsigned HiIndex;
5352 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005353 int Val = PermMask[HiIndex];
5354 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005355 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005356 if (Val >= 4)
5357 break;
5358 }
5359
Nate Begeman9008ca62009-04-27 18:41:29 +00005360 Mask1[0] = PermMask[HiIndex];
5361 Mask1[1] = -1;
5362 Mask1[2] = PermMask[HiIndex^1];
5363 Mask1[3] = -1;
5364 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005365
5366 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005367 Mask1[0] = PermMask[0];
5368 Mask1[1] = PermMask[1];
5369 Mask1[2] = HiIndex & 1 ? 6 : 4;
5370 Mask1[3] = HiIndex & 1 ? 4 : 6;
5371 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005372 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005373 Mask1[0] = HiIndex & 1 ? 2 : 0;
5374 Mask1[1] = HiIndex & 1 ? 0 : 2;
5375 Mask1[2] = PermMask[2];
5376 Mask1[3] = PermMask[3];
5377 if (Mask1[2] >= 0)
5378 Mask1[2] += 4;
5379 if (Mask1[3] >= 0)
5380 Mask1[3] += 4;
5381 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005382 }
Evan Chengace3c172008-07-22 21:13:36 +00005383 }
5384
5385 // Break it into (shuffle shuffle_hi, shuffle_lo).
5386 Locs.clear();
David Greenec4db4e52011-02-28 19:06:56 +00005387 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00005388 SmallVector<int,8> LoMask(4U, -1);
5389 SmallVector<int,8> HiMask(4U, -1);
5390
5391 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00005392 unsigned MaskIdx = 0;
5393 unsigned LoIdx = 0;
5394 unsigned HiIdx = 2;
5395 for (unsigned i = 0; i != 4; ++i) {
5396 if (i == 2) {
5397 MaskPtr = &HiMask;
5398 MaskIdx = 1;
5399 LoIdx = 0;
5400 HiIdx = 2;
5401 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005402 int Idx = PermMask[i];
5403 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00005404 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00005405 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00005406 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00005407 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005408 LoIdx++;
5409 } else {
5410 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00005411 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005412 HiIdx++;
5413 }
5414 }
5415
Nate Begeman9008ca62009-04-27 18:41:29 +00005416 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
5417 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
5418 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00005419 for (unsigned i = 0; i != 4; ++i) {
5420 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005421 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00005422 } else {
5423 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00005424 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00005425 }
5426 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005427 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00005428}
5429
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005430static bool MayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005431 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005432 V = V.getOperand(0);
5433 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
5434 V = V.getOperand(0);
5435 if (MayFoldLoad(V))
5436 return true;
5437 return false;
5438}
5439
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005440// FIXME: the version above should always be used. Since there's
5441// a bug where several vector shuffles can't be folded because the
5442// DAG is not updated during lowering and a node claims to have two
5443// uses while it only has one, use this version, and let isel match
5444// another instruction if the load really happens to have more than
5445// one use. Remove this version after this bug get fixed.
Evan Cheng835580f2010-10-07 20:50:20 +00005446// rdar://8434668, PR8156
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005447static bool RelaxedMayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005448 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005449 V = V.getOperand(0);
5450 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
5451 V = V.getOperand(0);
5452 if (ISD::isNormalLoad(V.getNode()))
5453 return true;
5454 return false;
5455}
5456
5457/// CanFoldShuffleIntoVExtract - Check if the current shuffle is used by
5458/// a vector extract, and if both can be later optimized into a single load.
5459/// This is done in visitEXTRACT_VECTOR_ELT and the conditions are checked
5460/// here because otherwise a target specific shuffle node is going to be
5461/// emitted for this shuffle, and the optimization not done.
5462/// FIXME: This is probably not the best approach, but fix the problem
5463/// until the right path is decided.
5464static
5465bool CanXFormVExtractWithShuffleIntoLoad(SDValue V, SelectionDAG &DAG,
5466 const TargetLowering &TLI) {
5467 EVT VT = V.getValueType();
5468 ShuffleVectorSDNode *SVOp = dyn_cast<ShuffleVectorSDNode>(V);
5469
5470 // Be sure that the vector shuffle is present in a pattern like this:
5471 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), c) -> (f32 load $addr)
5472 if (!V.hasOneUse())
5473 return false;
5474
5475 SDNode *N = *V.getNode()->use_begin();
5476 if (N->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
5477 return false;
5478
5479 SDValue EltNo = N->getOperand(1);
5480 if (!isa<ConstantSDNode>(EltNo))
5481 return false;
5482
5483 // If the bit convert changed the number of elements, it is unsafe
5484 // to examine the mask.
5485 bool HasShuffleIntoBitcast = false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005486 if (V.getOpcode() == ISD::BITCAST) {
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005487 EVT SrcVT = V.getOperand(0).getValueType();
5488 if (SrcVT.getVectorNumElements() != VT.getVectorNumElements())
5489 return false;
5490 V = V.getOperand(0);
5491 HasShuffleIntoBitcast = true;
5492 }
5493
5494 // Select the input vector, guarding against out of range extract vector.
5495 unsigned NumElems = VT.getVectorNumElements();
5496 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
5497 int Idx = (Elt > NumElems) ? -1 : SVOp->getMaskElt(Elt);
5498 V = (Idx < (int)NumElems) ? V.getOperand(0) : V.getOperand(1);
5499
5500 // Skip one more bit_convert if necessary
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005501 if (V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005502 V = V.getOperand(0);
5503
5504 if (ISD::isNormalLoad(V.getNode())) {
5505 // Is the original load suitable?
5506 LoadSDNode *LN0 = cast<LoadSDNode>(V);
5507
5508 // FIXME: avoid the multi-use bug that is preventing lots of
5509 // of foldings to be detected, this is still wrong of course, but
5510 // give the temporary desired behavior, and if it happens that
5511 // the load has real more uses, during isel it will not fold, and
5512 // will generate poor code.
5513 if (!LN0 || LN0->isVolatile()) // || !LN0->hasOneUse()
5514 return false;
5515
5516 if (!HasShuffleIntoBitcast)
5517 return true;
5518
5519 // If there's a bitcast before the shuffle, check if the load type and
5520 // alignment is valid.
5521 unsigned Align = LN0->getAlignment();
5522 unsigned NewAlign =
5523 TLI.getTargetData()->getABITypeAlignment(
5524 VT.getTypeForEVT(*DAG.getContext()));
5525
5526 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
5527 return false;
5528 }
5529
5530 return true;
5531}
5532
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005533static
Evan Cheng835580f2010-10-07 20:50:20 +00005534SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
5535 EVT VT = Op.getValueType();
5536
5537 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005538 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
5539 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00005540 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
5541 V1, DAG));
5542}
5543
5544static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005545SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
5546 bool HasSSE2) {
5547 SDValue V1 = Op.getOperand(0);
5548 SDValue V2 = Op.getOperand(1);
5549 EVT VT = Op.getValueType();
5550
5551 assert(VT != MVT::v2i64 && "unsupported shuffle type");
5552
5553 if (HasSSE2 && VT == MVT::v2f64)
5554 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
5555
5556 // v4f32 or v4i32
5557 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V2, DAG);
5558}
5559
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00005560static
5561SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
5562 SDValue V1 = Op.getOperand(0);
5563 SDValue V2 = Op.getOperand(1);
5564 EVT VT = Op.getValueType();
5565
5566 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
5567 "unsupported shuffle type");
5568
5569 if (V2.getOpcode() == ISD::UNDEF)
5570 V2 = V1;
5571
5572 // v4i32 or v4f32
5573 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
5574}
5575
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005576static
5577SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasSSE2) {
5578 SDValue V1 = Op.getOperand(0);
5579 SDValue V2 = Op.getOperand(1);
5580 EVT VT = Op.getValueType();
5581 unsigned NumElems = VT.getVectorNumElements();
5582
5583 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
5584 // operand of these instructions is only memory, so check if there's a
5585 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
5586 // same masks.
5587 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005588
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00005589 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005590 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005591 CanFoldLoad = true;
5592
5593 // When V1 is a load, it can be folded later into a store in isel, example:
5594 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
5595 // turns into:
5596 // (MOVLPSmr addr:$src1, VR128:$src2)
5597 // So, recognize this potential and also use MOVLPS or MOVLPD
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005598 if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005599 CanFoldLoad = true;
5600
Eric Christopher893a8822011-02-20 05:04:42 +00005601 // Both of them can't be memory operations though.
5602 if (MayFoldVectorLoad(V1) && MayFoldVectorLoad(V2))
5603 CanFoldLoad = false;
Owen Anderson95771af2011-02-25 21:41:48 +00005604
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005605 if (CanFoldLoad) {
5606 if (HasSSE2 && NumElems == 2)
5607 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
5608
5609 if (NumElems == 4)
5610 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
5611 }
5612
5613 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5614 // movl and movlp will both match v2i64, but v2i64 is never matched by
5615 // movl earlier because we make it strict to avoid messing with the movlp load
5616 // folding logic (see the code above getMOVLP call). Match it here then,
5617 // this is horrible, but will stay like this until we move all shuffle
5618 // matching to x86 specific nodes. Note that for the 1st condition all
5619 // types are matched with movsd.
5620 if ((HasSSE2 && NumElems == 2) || !X86::isMOVLMask(SVOp))
5621 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
5622 else if (HasSSE2)
5623 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
5624
5625
5626 assert(VT != MVT::v4i32 && "unsupported shuffle type");
5627
5628 // Invert the operand order and use SHUFPS to match it.
5629 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V2, V1,
5630 X86::getShuffleSHUFImmediate(SVOp), DAG);
5631}
5632
David Greenec4db4e52011-02-28 19:06:56 +00005633static inline unsigned getUNPCKLOpcode(EVT VT, const X86Subtarget *Subtarget) {
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005634 switch(VT.getSimpleVT().SimpleTy) {
5635 case MVT::v4i32: return X86ISD::PUNPCKLDQ;
5636 case MVT::v2i64: return X86ISD::PUNPCKLQDQ;
David Greenec4db4e52011-02-28 19:06:56 +00005637 case MVT::v4f32:
5638 return Subtarget->hasAVX() ? X86ISD::VUNPCKLPS : X86ISD::UNPCKLPS;
5639 case MVT::v2f64:
5640 return Subtarget->hasAVX() ? X86ISD::VUNPCKLPD : X86ISD::UNPCKLPD;
5641 case MVT::v8f32: return X86ISD::VUNPCKLPSY;
5642 case MVT::v4f64: return X86ISD::VUNPCKLPDY;
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005643 case MVT::v16i8: return X86ISD::PUNPCKLBW;
5644 case MVT::v8i16: return X86ISD::PUNPCKLWD;
5645 default:
Eric Christopherdd6e40a2011-02-19 03:19:09 +00005646 llvm_unreachable("Unknown type for unpckl");
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005647 }
5648 return 0;
5649}
5650
5651static inline unsigned getUNPCKHOpcode(EVT VT) {
5652 switch(VT.getSimpleVT().SimpleTy) {
5653 case MVT::v4i32: return X86ISD::PUNPCKHDQ;
5654 case MVT::v2i64: return X86ISD::PUNPCKHQDQ;
5655 case MVT::v4f32: return X86ISD::UNPCKHPS;
5656 case MVT::v2f64: return X86ISD::UNPCKHPD;
5657 case MVT::v16i8: return X86ISD::PUNPCKHBW;
5658 case MVT::v8i16: return X86ISD::PUNPCKHWD;
5659 default:
Eric Christopherdd6e40a2011-02-19 03:19:09 +00005660 llvm_unreachable("Unknown type for unpckh");
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005661 }
5662 return 0;
5663}
5664
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005665static
5666SDValue NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG,
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005667 const TargetLowering &TLI,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005668 const X86Subtarget *Subtarget) {
5669 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5670 EVT VT = Op.getValueType();
5671 DebugLoc dl = Op.getDebugLoc();
5672 SDValue V1 = Op.getOperand(0);
5673 SDValue V2 = Op.getOperand(1);
5674
5675 if (isZeroShuffle(SVOp))
5676 return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
5677
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005678 // Handle splat operations
5679 if (SVOp->isSplat()) {
5680 // Special case, this is the only place now where it's
5681 // allowed to return a vector_shuffle operation without
5682 // using a target specific node, because *hopefully* it
5683 // will be optimized away by the dag combiner.
5684 if (VT.getVectorNumElements() <= 4 &&
5685 CanXFormVExtractWithShuffleIntoLoad(Op, DAG, TLI))
5686 return Op;
5687
5688 // Handle splats by matching through known masks
5689 if (VT.getVectorNumElements() <= 4)
5690 return SDValue();
5691
Evan Cheng835580f2010-10-07 20:50:20 +00005692 // Canonicalize all of the remaining to v4f32.
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005693 return PromoteSplat(SVOp, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005694 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005695
5696 // If the shuffle can be profitably rewritten as a narrower shuffle, then
5697 // do it!
5698 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
5699 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5700 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005701 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005702 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
5703 // FIXME: Figure out a cleaner way to do this.
5704 // Try to make use of movq to zero out the top part.
5705 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
5706 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5707 if (NewOp.getNode()) {
5708 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
5709 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
5710 DAG, Subtarget, dl);
5711 }
5712 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
5713 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5714 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
5715 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
5716 DAG, Subtarget, dl);
5717 }
5718 }
5719 return SDValue();
5720}
5721
Dan Gohman475871a2008-07-27 21:46:04 +00005722SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005723X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00005724 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00005725 SDValue V1 = Op.getOperand(0);
5726 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00005727 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005728 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00005729 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005730 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005731 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
5732 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00005733 bool V1IsSplat = false;
5734 bool V2IsSplat = false;
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005735 bool HasSSE2 = Subtarget->hasSSE2() || Subtarget->hasAVX();
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00005736 bool HasSSE3 = Subtarget->hasSSE3() || Subtarget->hasAVX();
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00005737 bool HasSSSE3 = Subtarget->hasSSSE3() || Subtarget->hasAVX();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005738 MachineFunction &MF = DAG.getMachineFunction();
5739 bool OptForSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005740
Dale Johannesen0488fb62010-09-30 23:57:10 +00005741 // Shuffle operations on MMX not supported.
5742 if (isMMX)
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00005743 return Op;
5744
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005745 // Vector shuffle lowering takes 3 steps:
5746 //
5747 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
5748 // narrowing and commutation of operands should be handled.
5749 // 2) Matching of shuffles with known shuffle masks to x86 target specific
5750 // shuffle nodes.
5751 // 3) Rewriting of unmatched masks into new generic shuffle operations,
5752 // so the shuffle can be broken into other shuffles and the legalizer can
5753 // try the lowering again.
5754 //
5755 // The general ideia is that no vector_shuffle operation should be left to
5756 // be matched during isel, all of them must be converted to a target specific
5757 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00005758
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005759 // Normalize the input vectors. Here splats, zeroed vectors, profitable
5760 // narrowing and commutation of operands should be handled. The actual code
5761 // doesn't include all of those, work in progress...
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005762 SDValue NewOp = NormalizeVectorShuffle(Op, DAG, *this, Subtarget);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005763 if (NewOp.getNode())
5764 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00005765
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005766 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
5767 // unpckh_undef). Only use pshufd if speed is more important than size.
5768 if (OptForSize && X86::isUNPCKL_v_undef_Mask(SVOp))
5769 if (VT != MVT::v2i64 && VT != MVT::v2f64)
David Greenec4db4e52011-02-28 19:06:56 +00005770 return getTargetShuffleNode(getUNPCKLOpcode(VT, getSubtarget()), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005771 if (OptForSize && X86::isUNPCKH_v_undef_Mask(SVOp))
5772 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5773 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00005774
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005775 if (X86::isMOVDDUPMask(SVOp) && HasSSE3 && V2IsUndef &&
Dale Johannesen0488fb62010-09-30 23:57:10 +00005776 RelaxedMayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00005777 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005778
Dale Johannesen0488fb62010-09-30 23:57:10 +00005779 if (X86::isMOVHLPS_v_undef_Mask(SVOp))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005780 return getMOVHighToLow(Op, dl, DAG);
5781
5782 // Use to match splats
5783 if (HasSSE2 && X86::isUNPCKHMask(SVOp) && V2IsUndef &&
5784 (VT == MVT::v2f64 || VT == MVT::v2i64))
5785 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
5786
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005787 if (X86::isPSHUFDMask(SVOp)) {
5788 // The actual implementation will match the mask in the if above and then
5789 // during isel it can match several different instructions, not only pshufd
5790 // as its name says, sad but true, emulate the behavior for now...
5791 if (X86::isMOVDDUPMask(SVOp) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
5792 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
5793
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005794 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
5795
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005796 if (HasSSE2 && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005797 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
5798
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005799 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005800 return getTargetShuffleNode(X86ISD::SHUFPD, dl, VT, V1, V1,
5801 TargetMask, DAG);
5802
5803 if (VT == MVT::v4f32)
5804 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V1, V1,
5805 TargetMask, DAG);
5806 }
Eric Christopherfd179292009-08-27 18:07:15 +00005807
Evan Chengf26ffe92008-05-29 08:22:04 +00005808 // Check if this can be converted into a logical shift.
5809 bool isLeft = false;
5810 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00005811 SDValue ShVal;
Nate Begeman9008ca62009-04-27 18:41:29 +00005812 bool isShift = getSubtarget()->hasSSE2() &&
Evan Chengc3630942009-12-09 21:00:30 +00005813 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00005814 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00005815 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00005816 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00005817 EVT EltVT = VT.getVectorElementType();
5818 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00005819 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005820 }
Eric Christopherfd179292009-08-27 18:07:15 +00005821
Nate Begeman9008ca62009-04-27 18:41:29 +00005822 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005823 if (V1IsUndef)
5824 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00005825 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00005826 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Dale Johannesen0488fb62010-09-30 23:57:10 +00005827 if (!X86::isMOVLPMask(SVOp)) {
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005828 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00005829 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
5830
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005831 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00005832 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
5833 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00005834 }
Eric Christopherfd179292009-08-27 18:07:15 +00005835
Nate Begeman9008ca62009-04-27 18:41:29 +00005836 // FIXME: fold these into legal mask.
Dale Johannesen0488fb62010-09-30 23:57:10 +00005837 if (X86::isMOVLHPSMask(SVOp) && !X86::isUNPCKLMask(SVOp))
5838 return getMOVLowToHigh(Op, dl, DAG, HasSSE2);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005839
Dale Johannesen0488fb62010-09-30 23:57:10 +00005840 if (X86::isMOVHLPSMask(SVOp))
5841 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00005842
Dale Johannesen0488fb62010-09-30 23:57:10 +00005843 if (X86::isMOVSHDUPMask(SVOp) && HasSSE3 && V2IsUndef && NumElems == 4)
5844 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00005845
Dale Johannesen0488fb62010-09-30 23:57:10 +00005846 if (X86::isMOVSLDUPMask(SVOp) && HasSSE3 && V2IsUndef && NumElems == 4)
5847 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00005848
Dale Johannesen0488fb62010-09-30 23:57:10 +00005849 if (X86::isMOVLPMask(SVOp))
5850 return getMOVLP(Op, dl, DAG, HasSSE2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005851
Nate Begeman9008ca62009-04-27 18:41:29 +00005852 if (ShouldXformToMOVHLPS(SVOp) ||
5853 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
5854 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005855
Evan Chengf26ffe92008-05-29 08:22:04 +00005856 if (isShift) {
5857 // No better options. Use a vshl / vsrl.
Dan Gohman8a55ce42009-09-23 21:02:20 +00005858 EVT EltVT = VT.getVectorElementType();
5859 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00005860 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005861 }
Eric Christopherfd179292009-08-27 18:07:15 +00005862
Evan Cheng9eca5e82006-10-25 21:49:50 +00005863 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00005864 // FIXME: This should also accept a bitcast of a splat? Be careful, not
5865 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00005866 V1IsSplat = isSplatVector(V1.getNode());
5867 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00005868
Chris Lattner8a594482007-11-25 00:24:49 +00005869 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00005870 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005871 Op = CommuteVectorShuffle(SVOp, DAG);
5872 SVOp = cast<ShuffleVectorSDNode>(Op);
5873 V1 = SVOp->getOperand(0);
5874 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00005875 std::swap(V1IsSplat, V2IsSplat);
5876 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00005877 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00005878 }
5879
Nate Begeman9008ca62009-04-27 18:41:29 +00005880 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
5881 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00005882 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00005883 return V1;
5884 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
5885 // the instruction selector will not match, so get a canonical MOVL with
5886 // swapped operands to undo the commute.
5887 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00005888 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005889
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005890 if (X86::isUNPCKLMask(SVOp))
David Greenec4db4e52011-02-28 19:06:56 +00005891 return getTargetShuffleNode(getUNPCKLOpcode(VT, getSubtarget()),
5892 dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005893
5894 if (X86::isUNPCKHMask(SVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005895 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00005896
Evan Cheng9bbbb982006-10-25 20:48:19 +00005897 if (V2IsSplat) {
5898 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005899 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00005900 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00005901 SDValue NewMask = NormalizeMask(SVOp, DAG);
5902 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
5903 if (NSVOp != SVOp) {
5904 if (X86::isUNPCKLMask(NSVOp, true)) {
5905 return NewMask;
5906 } else if (X86::isUNPCKHMask(NSVOp, true)) {
5907 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005908 }
5909 }
5910 }
5911
Evan Cheng9eca5e82006-10-25 21:49:50 +00005912 if (Commuted) {
5913 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00005914 // FIXME: this seems wrong.
5915 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
5916 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005917
5918 if (X86::isUNPCKLMask(NewSVOp))
David Greenec4db4e52011-02-28 19:06:56 +00005919 return getTargetShuffleNode(getUNPCKLOpcode(VT, getSubtarget()),
5920 dl, VT, V2, V1, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005921
5922 if (X86::isUNPCKHMask(NewSVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005923 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V2, V1, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00005924 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005925
Nate Begeman9008ca62009-04-27 18:41:29 +00005926 // Normalize the node to match x86 shuffle ops if needed
Dale Johannesen0488fb62010-09-30 23:57:10 +00005927 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
Nate Begeman9008ca62009-04-27 18:41:29 +00005928 return CommuteVectorShuffle(SVOp, DAG);
5929
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00005930 // The checks below are all present in isShuffleMaskLegal, but they are
5931 // inlined here right now to enable us to directly emit target specific
5932 // nodes, and remove one by one until they don't return Op anymore.
5933 SmallVector<int, 16> M;
5934 SVOp->getMask(M);
5935
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00005936 if (isPALIGNRMask(M, VT, HasSSSE3))
5937 return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
5938 X86::getShufflePALIGNRImmediate(SVOp),
5939 DAG);
5940
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00005941 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
5942 SVOp->getSplatIndex() == 0 && V2IsUndef) {
David Greenec4db4e52011-02-28 19:06:56 +00005943 if (VT == MVT::v2f64) {
5944 X86ISD::NodeType Opcode =
5945 getSubtarget()->hasAVX() ? X86ISD::VUNPCKLPD : X86ISD::UNPCKLPD;
5946 return getTargetShuffleNode(Opcode, dl, VT, V1, V1, DAG);
5947 }
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00005948 if (VT == MVT::v2i64)
5949 return getTargetShuffleNode(X86ISD::PUNPCKLQDQ, dl, VT, V1, V1, DAG);
5950 }
5951
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00005952 if (isPSHUFHWMask(M, VT))
5953 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
5954 X86::getShufflePSHUFHWImmediate(SVOp),
5955 DAG);
5956
5957 if (isPSHUFLWMask(M, VT))
5958 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
5959 X86::getShufflePSHUFLWImmediate(SVOp),
5960 DAG);
5961
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00005962 if (isSHUFPMask(M, VT)) {
5963 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
5964 if (VT == MVT::v4f32 || VT == MVT::v4i32)
5965 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V1, V2,
5966 TargetMask, DAG);
5967 if (VT == MVT::v2f64 || VT == MVT::v2i64)
5968 return getTargetShuffleNode(X86ISD::SHUFPD, dl, VT, V1, V2,
5969 TargetMask, DAG);
5970 }
5971
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005972 if (X86::isUNPCKL_v_undef_Mask(SVOp))
5973 if (VT != MVT::v2i64 && VT != MVT::v2f64)
David Greenec4db4e52011-02-28 19:06:56 +00005974 return getTargetShuffleNode(getUNPCKLOpcode(VT, getSubtarget()),
5975 dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005976 if (X86::isUNPCKH_v_undef_Mask(SVOp))
5977 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5978 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
5979
Evan Cheng14b32e12007-12-11 01:46:18 +00005980 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
Owen Anderson825b72b2009-08-11 20:47:22 +00005981 if (VT == MVT::v8i16) {
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005982 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00005983 if (NewOp.getNode())
Evan Cheng14b32e12007-12-11 01:46:18 +00005984 return NewOp;
5985 }
5986
Owen Anderson825b72b2009-08-11 20:47:22 +00005987 if (VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005988 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005989 if (NewOp.getNode())
5990 return NewOp;
5991 }
Eric Christopherfd179292009-08-27 18:07:15 +00005992
Dale Johannesen0488fb62010-09-30 23:57:10 +00005993 // Handle all 4 wide cases with a number of shuffles.
5994 if (NumElems == 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00005995 return LowerVECTOR_SHUFFLE_4wide(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005996
Dan Gohman475871a2008-07-27 21:46:04 +00005997 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005998}
5999
Dan Gohman475871a2008-07-27 21:46:04 +00006000SDValue
6001X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00006002 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006003 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006004 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006005 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006006 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006007 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006008 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006009 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006010 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006011 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00006012 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
6013 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
6014 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006015 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6016 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006017 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006018 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00006019 Op.getOperand(0)),
6020 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006021 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006022 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006023 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006024 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006025 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Anderson825b72b2009-08-11 20:47:22 +00006026 } else if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00006027 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
6028 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006029 // result has a single use which is a store or a bitcast to i32. And in
6030 // the case of a store, it's not worth it if the index is a constant 0,
6031 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00006032 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00006033 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00006034 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006035 if ((User->getOpcode() != ISD::STORE ||
6036 (isa<ConstantSDNode>(Op.getOperand(1)) &&
6037 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006038 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00006039 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00006040 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00006041 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006042 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00006043 Op.getOperand(0)),
6044 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006045 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Owen Anderson825b72b2009-08-11 20:47:22 +00006046 } else if (VT == MVT::i32) {
Mon P Wangf0fcdd82009-01-15 21:10:20 +00006047 // ExtractPS works with constant index.
6048 if (isa<ConstantSDNode>(Op.getOperand(1)))
6049 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00006050 }
Dan Gohman475871a2008-07-27 21:46:04 +00006051 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006052}
6053
6054
Dan Gohman475871a2008-07-27 21:46:04 +00006055SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006056X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
6057 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006058 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00006059 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006060
David Greene74a579d2011-02-10 16:57:36 +00006061 SDValue Vec = Op.getOperand(0);
6062 EVT VecVT = Vec.getValueType();
6063
6064 // If this is a 256-bit vector result, first extract the 128-bit
6065 // vector and then extract from the 128-bit vector.
6066 if (VecVT.getSizeInBits() > 128) {
6067 DebugLoc dl = Op.getNode()->getDebugLoc();
6068 unsigned NumElems = VecVT.getVectorNumElements();
6069 SDValue Idx = Op.getOperand(1);
6070
6071 if (!isa<ConstantSDNode>(Idx))
6072 return SDValue();
6073
6074 unsigned ExtractNumElems = NumElems / (VecVT.getSizeInBits() / 128);
6075 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
6076
6077 // Get the 128-bit vector.
6078 bool Upper = IdxVal >= ExtractNumElems;
6079 Vec = Extract128BitVector(Vec, Idx, DAG, dl);
6080
6081 // Extract from it.
6082 SDValue ScaledIdx = Idx;
6083 if (Upper)
6084 ScaledIdx = DAG.getNode(ISD::SUB, dl, Idx.getValueType(), Idx,
6085 DAG.getConstant(ExtractNumElems,
6086 Idx.getValueType()));
6087 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, Op.getValueType(), Vec,
6088 ScaledIdx);
6089 }
6090
6091 assert(Vec.getValueSizeInBits() <= 128 && "Unexpected vector length");
6092
Evan Cheng62a3f152008-03-24 21:52:23 +00006093 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006094 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006095 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00006096 return Res;
6097 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00006098
Owen Andersone50ed302009-08-10 22:56:29 +00006099 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006100 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006101 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006102 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00006103 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006104 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00006105 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006106 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6107 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006108 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006109 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00006110 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006111 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00006112 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00006113 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00006114 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00006115 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00006116 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006117 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006118 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006119 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006120 if (Idx == 0)
6121 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00006122
Evan Cheng0db9fe62006-04-25 20:13:52 +00006123 // SHUFPS the element to the lowest double word, then movss.
Nate Begeman9008ca62009-04-27 18:41:29 +00006124 int Mask[4] = { Idx, -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006125 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006126 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00006127 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00006128 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00006129 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00006130 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00006131 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
6132 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
6133 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006134 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006135 if (Idx == 0)
6136 return Op;
6137
6138 // UNPCKHPD the element to the lowest double word, then movsd.
6139 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
6140 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00006141 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006142 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006143 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00006144 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00006145 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00006146 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006147 }
6148
Dan Gohman475871a2008-07-27 21:46:04 +00006149 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006150}
6151
Dan Gohman475871a2008-07-27 21:46:04 +00006152SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006153X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
6154 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006155 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006156 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006157 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006158
Dan Gohman475871a2008-07-27 21:46:04 +00006159 SDValue N0 = Op.getOperand(0);
6160 SDValue N1 = Op.getOperand(1);
6161 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00006162
Dan Gohman8a55ce42009-09-23 21:02:20 +00006163 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00006164 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00006165 unsigned Opc;
6166 if (VT == MVT::v8i16)
6167 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00006168 else if (VT == MVT::v16i8)
6169 Opc = X86ISD::PINSRB;
6170 else
6171 Opc = X86ISD::PINSRB;
6172
Nate Begeman14d12ca2008-02-11 04:19:36 +00006173 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
6174 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00006175 if (N1.getValueType() != MVT::i32)
6176 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
6177 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006178 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00006179 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00006180 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00006181 // Bits [7:6] of the constant are the source select. This will always be
6182 // zero here. The DAG Combiner may combine an extract_elt index into these
6183 // bits. For example (insert (extract, 3), 2) could be matched by putting
6184 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00006185 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00006186 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00006187 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00006188 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006189 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00006190 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00006191 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00006192 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00006193 } else if (EltVT == MVT::i32 && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00006194 // PINSR* works with constant index.
6195 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00006196 }
Dan Gohman475871a2008-07-27 21:46:04 +00006197 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006198}
6199
Dan Gohman475871a2008-07-27 21:46:04 +00006200SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006201X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006202 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006203 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006204
David Greene6b381262011-02-09 15:32:06 +00006205 DebugLoc dl = Op.getDebugLoc();
6206 SDValue N0 = Op.getOperand(0);
6207 SDValue N1 = Op.getOperand(1);
6208 SDValue N2 = Op.getOperand(2);
6209
6210 // If this is a 256-bit vector result, first insert into a 128-bit
6211 // vector and then insert into the 256-bit vector.
6212 if (VT.getSizeInBits() > 128) {
6213 if (!isa<ConstantSDNode>(N2))
6214 return SDValue();
6215
6216 // Get the 128-bit vector.
6217 unsigned NumElems = VT.getVectorNumElements();
6218 unsigned IdxVal = cast<ConstantSDNode>(N2)->getZExtValue();
6219 bool Upper = IdxVal >= NumElems / 2;
6220
6221 SDValue SubN0 = Extract128BitVector(N0, N2, DAG, dl);
6222
6223 // Insert into it.
6224 SDValue ScaledN2 = N2;
6225 if (Upper)
6226 ScaledN2 = DAG.getNode(ISD::SUB, dl, N2.getValueType(), N2,
Owen Anderson95771af2011-02-25 21:41:48 +00006227 DAG.getConstant(NumElems /
David Greene6b381262011-02-09 15:32:06 +00006228 (VT.getSizeInBits() / 128),
6229 N2.getValueType()));
6230 Op = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, SubN0.getValueType(), SubN0,
6231 N1, ScaledN2);
6232
6233 // Insert the 128-bit vector
6234 // FIXME: Why UNDEF?
6235 return Insert128BitVector(N0, Op, N2, DAG, dl);
6236 }
6237
Nate Begeman14d12ca2008-02-11 04:19:36 +00006238 if (Subtarget->hasSSE41())
6239 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
6240
Dan Gohman8a55ce42009-09-23 21:02:20 +00006241 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00006242 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00006243
Dan Gohman8a55ce42009-09-23 21:02:20 +00006244 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00006245 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
6246 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00006247 if (N1.getValueType() != MVT::i32)
6248 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
6249 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006250 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00006251 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006252 }
Dan Gohman475871a2008-07-27 21:46:04 +00006253 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006254}
6255
Dan Gohman475871a2008-07-27 21:46:04 +00006256SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006257X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) const {
David Greene2fcdfb42011-02-10 23:11:29 +00006258 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006259 DebugLoc dl = Op.getDebugLoc();
David Greene2fcdfb42011-02-10 23:11:29 +00006260 EVT OpVT = Op.getValueType();
6261
6262 // If this is a 256-bit vector result, first insert into a 128-bit
6263 // vector and then insert into the 256-bit vector.
6264 if (OpVT.getSizeInBits() > 128) {
6265 // Insert into a 128-bit vector.
6266 EVT VT128 = EVT::getVectorVT(*Context,
6267 OpVT.getVectorElementType(),
6268 OpVT.getVectorNumElements() / 2);
6269
6270 Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT128, Op.getOperand(0));
6271
6272 // Insert the 128-bit vector.
6273 return Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, OpVT), Op,
6274 DAG.getConstant(0, MVT::i32),
6275 DAG, dl);
6276 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00006277
Chris Lattnerf172ecd2010-07-04 23:07:25 +00006278 if (Op.getValueType() == MVT::v1i64 &&
6279 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00006280 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00006281
Owen Anderson825b72b2009-08-11 20:47:22 +00006282 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Dale Johannesen0488fb62010-09-30 23:57:10 +00006283 assert(Op.getValueType().getSimpleVT().getSizeInBits() == 128 &&
6284 "Expected an SSE type!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006285 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(),
Dale Johannesen0488fb62010-09-30 23:57:10 +00006286 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006287}
6288
David Greene91585092011-01-26 15:38:49 +00006289// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
6290// a simple subregister reference or explicit instructions to grab
6291// upper bits of a vector.
6292SDValue
6293X86TargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
6294 if (Subtarget->hasAVX()) {
David Greenea5f26012011-02-07 19:36:54 +00006295 DebugLoc dl = Op.getNode()->getDebugLoc();
6296 SDValue Vec = Op.getNode()->getOperand(0);
6297 SDValue Idx = Op.getNode()->getOperand(1);
6298
6299 if (Op.getNode()->getValueType(0).getSizeInBits() == 128
6300 && Vec.getNode()->getValueType(0).getSizeInBits() == 256) {
6301 return Extract128BitVector(Vec, Idx, DAG, dl);
6302 }
David Greene91585092011-01-26 15:38:49 +00006303 }
6304 return SDValue();
6305}
6306
David Greenecfe33c42011-01-26 19:13:22 +00006307// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
6308// simple superregister reference or explicit instructions to insert
6309// the upper bits of a vector.
6310SDValue
6311X86TargetLowering::LowerINSERT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
6312 if (Subtarget->hasAVX()) {
6313 DebugLoc dl = Op.getNode()->getDebugLoc();
6314 SDValue Vec = Op.getNode()->getOperand(0);
6315 SDValue SubVec = Op.getNode()->getOperand(1);
6316 SDValue Idx = Op.getNode()->getOperand(2);
6317
6318 if (Op.getNode()->getValueType(0).getSizeInBits() == 256
6319 && SubVec.getNode()->getValueType(0).getSizeInBits() == 128) {
David Greenea5f26012011-02-07 19:36:54 +00006320 return Insert128BitVector(Vec, SubVec, Idx, DAG, dl);
David Greenecfe33c42011-01-26 19:13:22 +00006321 }
6322 }
6323 return SDValue();
6324}
6325
Bill Wendling056292f2008-09-16 21:48:12 +00006326// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
6327// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
6328// one of the above mentioned nodes. It has to be wrapped because otherwise
6329// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
6330// be used to form addressing mode. These wrapped nodes will be selected
6331// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00006332SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006333X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006334 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00006335
Chris Lattner41621a22009-06-26 19:22:52 +00006336 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6337 // global base reg.
6338 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006339 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006340 CodeModel::Model M = getTargetMachine().getCodeModel();
6341
Chris Lattner4f066492009-07-11 20:29:19 +00006342 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006343 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00006344 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00006345 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006346 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00006347 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006348 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00006349
Evan Cheng1606e8e2009-03-13 07:51:59 +00006350 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00006351 CP->getAlignment(),
6352 CP->getOffset(), OpFlag);
6353 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00006354 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006355 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00006356 if (OpFlag) {
6357 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00006358 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006359 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006360 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006361 }
6362
6363 return Result;
6364}
6365
Dan Gohmand858e902010-04-17 15:26:15 +00006366SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00006367 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00006368
Chris Lattner18c59872009-06-27 04:16:01 +00006369 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6370 // global base reg.
6371 unsigned char OpFlag = 0;
6372 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006373 CodeModel::Model M = getTargetMachine().getCodeModel();
6374
Chris Lattner4f066492009-07-11 20:29:19 +00006375 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006376 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00006377 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00006378 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006379 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00006380 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006381 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00006382
Chris Lattner18c59872009-06-27 04:16:01 +00006383 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
6384 OpFlag);
6385 DebugLoc DL = JT->getDebugLoc();
6386 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00006387
Chris Lattner18c59872009-06-27 04:16:01 +00006388 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00006389 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00006390 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6391 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006392 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00006393 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00006394
Chris Lattner18c59872009-06-27 04:16:01 +00006395 return Result;
6396}
6397
6398SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006399X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00006400 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00006401
Chris Lattner18c59872009-06-27 04:16:01 +00006402 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6403 // global base reg.
6404 unsigned char OpFlag = 0;
6405 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006406 CodeModel::Model M = getTargetMachine().getCodeModel();
6407
Chris Lattner4f066492009-07-11 20:29:19 +00006408 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006409 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00006410 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00006411 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006412 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00006413 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006414 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00006415
Chris Lattner18c59872009-06-27 04:16:01 +00006416 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00006417
Chris Lattner18c59872009-06-27 04:16:01 +00006418 DebugLoc DL = Op.getDebugLoc();
6419 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00006420
6421
Chris Lattner18c59872009-06-27 04:16:01 +00006422 // With PIC, the address is actually $g + Offset.
6423 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00006424 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00006425 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6426 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006427 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00006428 Result);
6429 }
Eric Christopherfd179292009-08-27 18:07:15 +00006430
Chris Lattner18c59872009-06-27 04:16:01 +00006431 return Result;
6432}
6433
Dan Gohman475871a2008-07-27 21:46:04 +00006434SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006435X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00006436 // Create the TargetBlockAddressAddress node.
6437 unsigned char OpFlags =
6438 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00006439 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00006440 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +00006441 DebugLoc dl = Op.getDebugLoc();
6442 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
6443 /*isTarget=*/true, OpFlags);
6444
Dan Gohmanf705adb2009-10-30 01:28:02 +00006445 if (Subtarget->isPICStyleRIPRel() &&
6446 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00006447 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
6448 else
6449 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00006450
Dan Gohman29cbade2009-11-20 23:18:13 +00006451 // With PIC, the address is actually $g + Offset.
6452 if (isGlobalRelativeToPICBase(OpFlags)) {
6453 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
6454 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
6455 Result);
6456 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00006457
6458 return Result;
6459}
6460
6461SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00006462X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00006463 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00006464 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00006465 // Create the TargetGlobalAddress node, folding in the constant
6466 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00006467 unsigned char OpFlags =
6468 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006469 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00006470 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006471 if (OpFlags == X86II::MO_NO_FLAG &&
6472 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00006473 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00006474 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00006475 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006476 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00006477 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00006478 }
Eric Christopherfd179292009-08-27 18:07:15 +00006479
Chris Lattner4f066492009-07-11 20:29:19 +00006480 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006481 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00006482 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
6483 else
6484 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00006485
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006486 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00006487 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00006488 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
6489 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006490 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006491 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006492
Chris Lattner36c25012009-07-10 07:34:39 +00006493 // For globals that require a load from a stub to get the address, emit the
6494 // load.
6495 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00006496 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00006497 MachinePointerInfo::getGOT(), false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006498
Dan Gohman6520e202008-10-18 02:06:02 +00006499 // If there was a non-zero offset that we didn't fold, create an explicit
6500 // addition for it.
6501 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00006502 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00006503 DAG.getConstant(Offset, getPointerTy()));
6504
Evan Cheng0db9fe62006-04-25 20:13:52 +00006505 return Result;
6506}
6507
Evan Chengda43bcf2008-09-24 00:05:32 +00006508SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006509X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00006510 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00006511 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006512 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00006513}
6514
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006515static SDValue
6516GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00006517 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerb903bed2009-06-26 21:20:29 +00006518 unsigned char OperandFlags) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006519 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006520 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006521 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00006522 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006523 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00006524 GA->getOffset(),
6525 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006526 if (InFlag) {
6527 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00006528 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006529 } else {
6530 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00006531 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006532 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006533
6534 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00006535 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006536
Rafael Espindola15f1b662009-04-24 12:59:40 +00006537 SDValue Flag = Chain.getValue(1);
6538 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006539}
6540
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006541// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00006542static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006543LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006544 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00006545 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00006546 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
6547 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006548 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006549 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006550 InFlag = Chain.getValue(1);
6551
Chris Lattnerb903bed2009-06-26 21:20:29 +00006552 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006553}
6554
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006555// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00006556static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006557LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006558 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00006559 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
6560 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006561}
6562
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006563// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
6564// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00006565static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006566 const EVT PtrVT, TLSModel::Model model,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00006567 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00006568 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00006569
Chris Lattnerf93b90c2010-09-22 04:39:11 +00006570 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
6571 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
6572 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00006573
Michael J. Spencerec38de22010-10-10 22:04:20 +00006574 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00006575 DAG.getIntPtrConstant(0),
6576 MachinePointerInfo(Ptr), false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00006577
Chris Lattnerb903bed2009-06-26 21:20:29 +00006578 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006579 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
6580 // initialexec.
6581 unsigned WrapperKind = X86ISD::Wrapper;
6582 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00006583 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00006584 } else if (is64Bit) {
6585 assert(model == TLSModel::InitialExec);
6586 OperandFlags = X86II::MO_GOTTPOFF;
6587 WrapperKind = X86ISD::WrapperRIP;
6588 } else {
6589 assert(model == TLSModel::InitialExec);
6590 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00006591 }
Eric Christopherfd179292009-08-27 18:07:15 +00006592
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006593 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
6594 // exec)
Michael J. Spencerec38de22010-10-10 22:04:20 +00006595 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00006596 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00006597 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00006598 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00006599
Rafael Espindola9a580232009-02-27 13:37:18 +00006600 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00006601 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00006602 MachinePointerInfo::getGOT(), false, false, 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00006603
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006604 // The address of the thread local variable is the add of the thread
6605 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00006606 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006607}
6608
Dan Gohman475871a2008-07-27 21:46:04 +00006609SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006610X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00006611
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006612 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00006613 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00006614
Eric Christopher30ef0e52010-06-03 04:07:48 +00006615 if (Subtarget->isTargetELF()) {
6616 // TODO: implement the "local dynamic" model
6617 // TODO: implement the "initial exec"model for pic executables
Michael J. Spencerec38de22010-10-10 22:04:20 +00006618
Eric Christopher30ef0e52010-06-03 04:07:48 +00006619 // If GV is an alias then use the aliasee for determining
6620 // thread-localness.
6621 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
6622 GV = GA->resolveAliasedGlobal(false);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006623
6624 TLSModel::Model model
Eric Christopher30ef0e52010-06-03 04:07:48 +00006625 = getTLSModel(GV, getTargetMachine().getRelocationModel());
Michael J. Spencerec38de22010-10-10 22:04:20 +00006626
Eric Christopher30ef0e52010-06-03 04:07:48 +00006627 switch (model) {
6628 case TLSModel::GeneralDynamic:
6629 case TLSModel::LocalDynamic: // not implemented
6630 if (Subtarget->is64Bit())
6631 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
6632 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Michael J. Spencerec38de22010-10-10 22:04:20 +00006633
Eric Christopher30ef0e52010-06-03 04:07:48 +00006634 case TLSModel::InitialExec:
6635 case TLSModel::LocalExec:
6636 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
6637 Subtarget->is64Bit());
6638 }
6639 } else if (Subtarget->isTargetDarwin()) {
6640 // Darwin only has one model of TLS. Lower to that.
6641 unsigned char OpFlag = 0;
6642 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
6643 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006644
Eric Christopher30ef0e52010-06-03 04:07:48 +00006645 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6646 // global base reg.
6647 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
6648 !Subtarget->is64Bit();
6649 if (PIC32)
6650 OpFlag = X86II::MO_TLVP_PIC_BASE;
6651 else
6652 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006653 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00006654 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00006655 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00006656 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00006657 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006658
Eric Christopher30ef0e52010-06-03 04:07:48 +00006659 // With PIC32, the address is actually $g + Offset.
6660 if (PIC32)
6661 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6662 DAG.getNode(X86ISD::GlobalBaseReg,
6663 DebugLoc(), getPointerTy()),
6664 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006665
Eric Christopher30ef0e52010-06-03 04:07:48 +00006666 // Lowering the machine isd will make sure everything is in the right
6667 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00006668 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006669 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00006670 SDValue Args[] = { Chain, Offset };
6671 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006672
Eric Christopher30ef0e52010-06-03 04:07:48 +00006673 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
6674 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
6675 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00006676
Eric Christopher30ef0e52010-06-03 04:07:48 +00006677 // And our return value (tls address) is in the standard call return value
6678 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00006679 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
6680 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy());
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006681 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00006682
Eric Christopher30ef0e52010-06-03 04:07:48 +00006683 assert(false &&
6684 "TLS not implemented for this target.");
Eric Christopherfd179292009-08-27 18:07:15 +00006685
Torok Edwinc23197a2009-07-14 16:55:14 +00006686 llvm_unreachable("Unreachable");
Chris Lattner5867de12009-04-01 22:14:45 +00006687 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006688}
6689
Evan Cheng0db9fe62006-04-25 20:13:52 +00006690
Nadav Rotem43012222011-05-11 08:12:09 +00006691/// LowerShiftParts - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00006692/// take a 2 x i32 value to shift plus a shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +00006693SDValue X86TargetLowering::LowerShiftParts(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman4c1fa612008-03-03 22:22:09 +00006694 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00006695 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006696 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006697 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006698 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00006699 SDValue ShOpLo = Op.getOperand(0);
6700 SDValue ShOpHi = Op.getOperand(1);
6701 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00006702 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00006703 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00006704 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00006705
Dan Gohman475871a2008-07-27 21:46:04 +00006706 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006707 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00006708 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
6709 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006710 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006711 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
6712 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006713 }
Evan Chenge3413162006-01-09 18:33:28 +00006714
Owen Anderson825b72b2009-08-11 20:47:22 +00006715 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
6716 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00006717 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00006718 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00006719
Dan Gohman475871a2008-07-27 21:46:04 +00006720 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00006721 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00006722 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
6723 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00006724
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006725 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00006726 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
6727 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006728 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006729 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
6730 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006731 }
6732
Dan Gohman475871a2008-07-27 21:46:04 +00006733 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00006734 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006735}
Evan Chenga3195e82006-01-12 22:54:21 +00006736
Dan Gohmand858e902010-04-17 15:26:15 +00006737SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
6738 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006739 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00006740
Dale Johannesen0488fb62010-09-30 23:57:10 +00006741 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00006742 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00006743
Owen Anderson825b72b2009-08-11 20:47:22 +00006744 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00006745 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00006746
Eli Friedman36df4992009-05-27 00:47:34 +00006747 // These are really Legal; return the operand so the caller accepts it as
6748 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00006749 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00006750 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00006751 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00006752 Subtarget->is64Bit()) {
6753 return Op;
6754 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006755
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006756 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006757 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006758 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00006759 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006760 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00006761 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00006762 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006763 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006764 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00006765 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
6766}
Evan Cheng0db9fe62006-04-25 20:13:52 +00006767
Owen Andersone50ed302009-08-10 22:56:29 +00006768SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00006769 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00006770 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006771 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00006772 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00006773 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00006774 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006775 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006776 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00006777 else
Owen Anderson825b72b2009-08-11 20:47:22 +00006778 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006779
Chris Lattner492a43e2010-09-22 01:28:21 +00006780 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006781
Stuart Hastings84be9582011-06-02 15:57:11 +00006782 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(StackSlot);
6783 MachineMemOperand *MMO;
6784 if (FI) {
6785 int SSFI = FI->getIndex();
6786 MMO =
6787 DAG.getMachineFunction()
6788 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6789 MachineMemOperand::MOLoad, ByteSize, ByteSize);
6790 } else {
6791 MMO = cast<LoadSDNode>(StackSlot)->getMemOperand();
6792 StackSlot = StackSlot.getOperand(1);
6793 }
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006794 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00006795 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
6796 X86ISD::FILD, DL,
6797 Tys, Ops, array_lengthof(Ops),
6798 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006799
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006800 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006801 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00006802 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006803
6804 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
6805 // shouldn't be necessary except that RFP cannot be live across
6806 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006807 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00006808 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
6809 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006810 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00006811 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006812 SDValue Ops[] = {
6813 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
6814 };
Chris Lattner492a43e2010-09-22 01:28:21 +00006815 MachineMemOperand *MMO =
6816 DAG.getMachineFunction()
6817 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00006818 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006819
Chris Lattner492a43e2010-09-22 01:28:21 +00006820 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
6821 Ops, array_lengthof(Ops),
6822 Op.getValueType(), MMO);
6823 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006824 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006825 false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006826 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006827
Evan Cheng0db9fe62006-04-25 20:13:52 +00006828 return Result;
6829}
6830
Bill Wendling8b8a6362009-01-17 03:56:04 +00006831// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00006832SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
6833 SelectionDAG &DAG) const {
Bill Wendling8b8a6362009-01-17 03:56:04 +00006834 // This algorithm is not obvious. Here it is in C code, more or less:
6835 /*
6836 double uint64_to_double( uint32_t hi, uint32_t lo ) {
6837 static const __m128i exp = { 0x4330000045300000ULL, 0 };
6838 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00006839
Bill Wendling8b8a6362009-01-17 03:56:04 +00006840 // Copy ints to xmm registers.
6841 __m128i xh = _mm_cvtsi32_si128( hi );
6842 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00006843
Bill Wendling8b8a6362009-01-17 03:56:04 +00006844 // Combine into low half of a single xmm register.
6845 __m128i x = _mm_unpacklo_epi32( xh, xl );
6846 __m128d d;
6847 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00006848
Bill Wendling8b8a6362009-01-17 03:56:04 +00006849 // Merge in appropriate exponents to give the integer bits the right
6850 // magnitude.
6851 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00006852
Bill Wendling8b8a6362009-01-17 03:56:04 +00006853 // Subtract away the biases to deal with the IEEE-754 double precision
6854 // implicit 1.
6855 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00006856
Bill Wendling8b8a6362009-01-17 03:56:04 +00006857 // All conversions up to here are exact. The correctly rounded result is
6858 // calculated using the current rounding mode using the following
6859 // horizontal add.
6860 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
6861 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
6862 // store doesn't really need to be here (except
6863 // maybe to zero the other double)
6864 return sd;
6865 }
6866 */
Dale Johannesen040225f2008-10-21 23:07:49 +00006867
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006868 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00006869 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00006870
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006871 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00006872 std::vector<Constant*> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00006873 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
6874 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
6875 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
6876 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00006877 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006878 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006879
Bill Wendling8b8a6362009-01-17 03:56:04 +00006880 std::vector<Constant*> CV1;
Owen Andersona90b3dc2009-07-15 21:51:10 +00006881 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006882 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
Owen Andersona90b3dc2009-07-15 21:51:10 +00006883 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006884 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00006885 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006886 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006887
Owen Anderson825b72b2009-08-11 20:47:22 +00006888 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6889 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00006890 Op.getOperand(0),
6891 DAG.getIntPtrConstant(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006892 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6893 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00006894 Op.getOperand(0),
6895 DAG.getIntPtrConstant(0)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006896 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
6897 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00006898 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006899 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00006900 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006901 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck2);
Owen Anderson825b72b2009-08-11 20:47:22 +00006902 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00006903 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006904 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00006905 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006906
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006907 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00006908 int ShufMask[2] = { 1, -1 };
Owen Anderson825b72b2009-08-11 20:47:22 +00006909 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
6910 DAG.getUNDEF(MVT::v2f64), ShufMask);
6911 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
6912 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006913 DAG.getIntPtrConstant(0));
6914}
6915
Bill Wendling8b8a6362009-01-17 03:56:04 +00006916// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00006917SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
6918 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006919 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00006920 // FP constant to bias correct the final result.
6921 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00006922 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006923
6924 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00006925 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6926 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Bill Wendling8b8a6362009-01-17 03:56:04 +00006927 Op.getOperand(0),
6928 DAG.getIntPtrConstant(0)));
6929
Owen Anderson825b72b2009-08-11 20:47:22 +00006930 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006931 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00006932 DAG.getIntPtrConstant(0));
6933
6934 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00006935 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006936 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00006937 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006938 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006939 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00006940 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006941 MVT::v2f64, Bias)));
6942 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006943 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00006944 DAG.getIntPtrConstant(0));
6945
6946 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00006947 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006948
6949 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00006950 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00006951
Owen Anderson825b72b2009-08-11 20:47:22 +00006952 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006953 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00006954 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00006955 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006956 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00006957 }
6958
6959 // Handle final rounding.
6960 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00006961}
6962
Dan Gohmand858e902010-04-17 15:26:15 +00006963SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
6964 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00006965 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006966 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00006967
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006968 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00006969 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
6970 // the optimization here.
6971 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00006972 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00006973
Owen Andersone50ed302009-08-10 22:56:29 +00006974 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006975 EVT DstVT = Op.getValueType();
6976 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00006977 return LowerUINT_TO_FP_i64(Op, DAG);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006978 else if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00006979 return LowerUINT_TO_FP_i32(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00006980
6981 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00006982 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006983 if (SrcVT == MVT::i32) {
6984 SDValue WordOff = DAG.getConstant(4, getPointerTy());
6985 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
6986 getPointerTy(), StackSlot, WordOff);
6987 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006988 StackSlot, MachinePointerInfo(),
6989 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006990 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006991 OffsetSlot, MachinePointerInfo(),
6992 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006993 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
6994 return Fild;
6995 }
6996
6997 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
6998 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006999 StackSlot, MachinePointerInfo(),
7000 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007001 // For i64 source, we need to add the appropriate power of 2 if the input
7002 // was negative. This is the same as the optimization in
7003 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
7004 // we must be careful to do the computation in x87 extended precision, not
7005 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00007006 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
7007 MachineMemOperand *MMO =
7008 DAG.getMachineFunction()
7009 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7010 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007011
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007012 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
7013 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007014 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
7015 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007016
7017 APInt FF(32, 0x5F800000ULL);
7018
7019 // Check whether the sign bit is set.
7020 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
7021 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
7022 ISD::SETLT);
7023
7024 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
7025 SDValue FudgePtr = DAG.getConstantPool(
7026 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
7027 getPointerTy());
7028
7029 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
7030 SDValue Zero = DAG.getIntPtrConstant(0);
7031 SDValue Four = DAG.getIntPtrConstant(4);
7032 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
7033 Zero, Four);
7034 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
7035
7036 // Load the value out, extending it from f32 to f80.
7037 // FIXME: Avoid the extend by constructing the right constant pool?
Stuart Hastingsa9011292011-02-16 16:23:55 +00007038 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00007039 FudgePtr, MachinePointerInfo::getConstantPool(),
7040 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007041 // Extend everything to 80 bits to force it to be done on x87.
7042 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
7043 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00007044}
7045
Dan Gohman475871a2008-07-27 21:46:04 +00007046std::pair<SDValue,SDValue> X86TargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00007047FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) const {
Chris Lattner07290932010-09-22 01:05:16 +00007048 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00007049
Owen Andersone50ed302009-08-10 22:56:29 +00007050 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00007051
7052 if (!IsSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007053 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
7054 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00007055 }
7056
Owen Anderson825b72b2009-08-11 20:47:22 +00007057 assert(DstTy.getSimpleVT() <= MVT::i64 &&
7058 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00007059 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00007060
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007061 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007062 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00007063 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00007064 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00007065 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00007066 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00007067 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00007068 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007069
Evan Cheng87c89352007-10-15 20:11:21 +00007070 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
7071 // stack slot.
7072 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00007073 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00007074 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007075 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00007076
Michael J. Spencerec38de22010-10-10 22:04:20 +00007077
7078
Evan Cheng0db9fe62006-04-25 20:13:52 +00007079 unsigned Opc;
Owen Anderson825b72b2009-08-11 20:47:22 +00007080 switch (DstTy.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007081 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson825b72b2009-08-11 20:47:22 +00007082 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
7083 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
7084 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007085 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007086
Dan Gohman475871a2008-07-27 21:46:04 +00007087 SDValue Chain = DAG.getEntryNode();
7088 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00007089 EVT TheVT = Op.getOperand(0).getValueType();
7090 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007091 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00007092 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007093 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007094 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007095 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00007096 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00007097 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00007098 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00007099
Chris Lattner492a43e2010-09-22 01:28:21 +00007100 MachineMemOperand *MMO =
7101 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7102 MachineMemOperand::MOLoad, MemSize, MemSize);
7103 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
7104 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007105 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00007106 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007107 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
7108 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007109
Chris Lattner07290932010-09-22 01:05:16 +00007110 MachineMemOperand *MMO =
7111 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7112 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007113
Evan Cheng0db9fe62006-04-25 20:13:52 +00007114 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00007115 SDValue Ops[] = { Chain, Value, StackSlot };
Chris Lattner07290932010-09-22 01:05:16 +00007116 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
7117 Ops, 3, DstTy, MMO);
Evan Chengd9558e02006-01-06 00:43:03 +00007118
Chris Lattner27a6c732007-11-24 07:07:01 +00007119 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007120}
7121
Dan Gohmand858e902010-04-17 15:26:15 +00007122SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
7123 SelectionDAG &DAG) const {
Dale Johannesen0488fb62010-09-30 23:57:10 +00007124 if (Op.getValueType().isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007125 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007126
Eli Friedman948e95a2009-05-23 09:59:16 +00007127 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00007128 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00007129 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
7130 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00007131
Chris Lattner27a6c732007-11-24 07:07:01 +00007132 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007133 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00007134 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00007135}
7136
Dan Gohmand858e902010-04-17 15:26:15 +00007137SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
7138 SelectionDAG &DAG) const {
Eli Friedman948e95a2009-05-23 09:59:16 +00007139 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
7140 SDValue FIST = Vals.first, StackSlot = Vals.second;
7141 assert(FIST.getNode() && "Unexpected failure");
7142
7143 // Load the result.
7144 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00007145 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007146}
7147
Dan Gohmand858e902010-04-17 15:26:15 +00007148SDValue X86TargetLowering::LowerFABS(SDValue Op,
7149 SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007150 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007151 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007152 EVT VT = Op.getValueType();
7153 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007154 if (VT.isVector())
7155 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007156 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007157 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007158 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00007159 CV.push_back(C);
7160 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007161 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007162 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00007163 CV.push_back(C);
7164 CV.push_back(C);
7165 CV.push_back(C);
7166 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007167 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007168 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007169 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007170 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007171 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007172 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007173 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007174}
7175
Dan Gohmand858e902010-04-17 15:26:15 +00007176SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007177 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007178 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007179 EVT VT = Op.getValueType();
7180 EVT EltVT = VT;
Duncan Sandsda9ad382009-09-06 19:29:07 +00007181 if (VT.isVector())
Duncan Sands83ec4b62008-06-06 12:08:01 +00007182 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007183 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007184 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007185 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00007186 CV.push_back(C);
7187 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007188 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007189 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00007190 CV.push_back(C);
7191 CV.push_back(C);
7192 CV.push_back(C);
7193 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007194 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007195 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007196 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007197 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007198 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007199 false, false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00007200 if (VT.isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007201 return DAG.getNode(ISD::BITCAST, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007202 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007203 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007204 Op.getOperand(0)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007205 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00007206 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007207 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00007208 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00007209}
7210
Dan Gohmand858e902010-04-17 15:26:15 +00007211SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007212 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00007213 SDValue Op0 = Op.getOperand(0);
7214 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007215 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007216 EVT VT = Op.getValueType();
7217 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00007218
7219 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007220 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007221 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00007222 SrcVT = VT;
7223 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007224 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007225 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007226 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007227 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007228 }
7229
7230 // At this point the operands and the result should have the same
7231 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00007232
Evan Cheng68c47cb2007-01-05 07:55:56 +00007233 // First get the sign bit of second operand.
7234 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007235 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007236 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
7237 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007238 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007239 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
7240 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7241 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7242 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007243 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007244 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007245 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007246 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007247 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007248 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007249 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00007250
7251 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007252 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007253 // Op0 is MVT::f32, Op1 is MVT::f64.
7254 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
7255 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
7256 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007257 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00007258 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00007259 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007260 }
7261
Evan Cheng73d6cf12007-01-05 21:37:56 +00007262 // Clear first operand sign bit.
7263 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00007264 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007265 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
7266 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00007267 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007268 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
7269 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7270 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7271 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00007272 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007273 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007274 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007275 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007276 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007277 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007278 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00007279
7280 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00007281 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00007282}
7283
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00007284SDValue X86TargetLowering::LowerFGETSIGN(SDValue Op, SelectionDAG &DAG) const {
7285 SDValue N0 = Op.getOperand(0);
7286 DebugLoc dl = Op.getDebugLoc();
7287 EVT VT = Op.getValueType();
7288
7289 // Lower ISD::FGETSIGN to (AND (X86ISD::FGETSIGNx86 ...) 1).
7290 SDValue xFGETSIGN = DAG.getNode(X86ISD::FGETSIGNx86, dl, VT, N0,
7291 DAG.getConstant(1, VT));
7292 return DAG.getNode(ISD::AND, dl, VT, xFGETSIGN, DAG.getConstant(1, VT));
7293}
7294
Dan Gohman076aee32009-03-04 19:44:21 +00007295/// Emit nodes that will be selected as "test Op0,Op0", or something
7296/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00007297SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00007298 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00007299 DebugLoc dl = Op.getDebugLoc();
7300
Dan Gohman31125812009-03-07 01:58:32 +00007301 // CF and OF aren't always set the way we want. Determine which
7302 // of these we need.
7303 bool NeedCF = false;
7304 bool NeedOF = false;
7305 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007306 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00007307 case X86::COND_A: case X86::COND_AE:
7308 case X86::COND_B: case X86::COND_BE:
7309 NeedCF = true;
7310 break;
7311 case X86::COND_G: case X86::COND_GE:
7312 case X86::COND_L: case X86::COND_LE:
7313 case X86::COND_O: case X86::COND_NO:
7314 NeedOF = true;
7315 break;
Dan Gohman31125812009-03-07 01:58:32 +00007316 }
7317
Dan Gohman076aee32009-03-04 19:44:21 +00007318 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00007319 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
7320 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007321 if (Op.getResNo() != 0 || NeedOF || NeedCF)
7322 // Emit a CMP with 0, which is the TEST pattern.
7323 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
7324 DAG.getConstant(0, Op.getValueType()));
7325
7326 unsigned Opcode = 0;
7327 unsigned NumOperands = 0;
7328 switch (Op.getNode()->getOpcode()) {
7329 case ISD::ADD:
7330 // Due to an isel shortcoming, be conservative if this add is likely to be
7331 // selected as part of a load-modify-store instruction. When the root node
7332 // in a match is a store, isel doesn't know how to remap non-chain non-flag
7333 // uses of other nodes in the match, such as the ADD in this case. This
7334 // leads to the ADD being left around and reselected, with the result being
7335 // two adds in the output. Alas, even if none our users are stores, that
7336 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
7337 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
7338 // climbing the DAG back to the root, and it doesn't seem to be worth the
7339 // effort.
7340 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Dan Gohman076aee32009-03-04 19:44:21 +00007341 UE = Op.getNode()->use_end(); UI != UE; ++UI)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007342 if (UI->getOpcode() != ISD::CopyToReg && UI->getOpcode() != ISD::SETCC)
7343 goto default_case;
7344
7345 if (ConstantSDNode *C =
7346 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
7347 // An add of one will be selected as an INC.
7348 if (C->getAPIntValue() == 1) {
7349 Opcode = X86ISD::INC;
7350 NumOperands = 1;
7351 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00007352 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007353
7354 // An add of negative one (subtract of one) will be selected as a DEC.
7355 if (C->getAPIntValue().isAllOnesValue()) {
7356 Opcode = X86ISD::DEC;
7357 NumOperands = 1;
7358 break;
7359 }
Dan Gohman076aee32009-03-04 19:44:21 +00007360 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007361
7362 // Otherwise use a regular EFLAGS-setting add.
7363 Opcode = X86ISD::ADD;
7364 NumOperands = 2;
7365 break;
7366 case ISD::AND: {
7367 // If the primary and result isn't used, don't bother using X86ISD::AND,
7368 // because a TEST instruction will be better.
7369 bool NonFlagUse = false;
7370 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
7371 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
7372 SDNode *User = *UI;
7373 unsigned UOpNo = UI.getOperandNo();
7374 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
7375 // Look pass truncate.
7376 UOpNo = User->use_begin().getOperandNo();
7377 User = *User->use_begin();
7378 }
7379
7380 if (User->getOpcode() != ISD::BRCOND &&
7381 User->getOpcode() != ISD::SETCC &&
7382 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
7383 NonFlagUse = true;
7384 break;
7385 }
Dan Gohman076aee32009-03-04 19:44:21 +00007386 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007387
7388 if (!NonFlagUse)
7389 break;
7390 }
7391 // FALL THROUGH
7392 case ISD::SUB:
7393 case ISD::OR:
7394 case ISD::XOR:
7395 // Due to the ISEL shortcoming noted above, be conservative if this op is
7396 // likely to be selected as part of a load-modify-store instruction.
7397 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
7398 UE = Op.getNode()->use_end(); UI != UE; ++UI)
7399 if (UI->getOpcode() == ISD::STORE)
7400 goto default_case;
7401
7402 // Otherwise use a regular EFLAGS-setting instruction.
7403 switch (Op.getNode()->getOpcode()) {
7404 default: llvm_unreachable("unexpected operator!");
7405 case ISD::SUB: Opcode = X86ISD::SUB; break;
7406 case ISD::OR: Opcode = X86ISD::OR; break;
7407 case ISD::XOR: Opcode = X86ISD::XOR; break;
7408 case ISD::AND: Opcode = X86ISD::AND; break;
7409 }
7410
7411 NumOperands = 2;
7412 break;
7413 case X86ISD::ADD:
7414 case X86ISD::SUB:
7415 case X86ISD::INC:
7416 case X86ISD::DEC:
7417 case X86ISD::OR:
7418 case X86ISD::XOR:
7419 case X86ISD::AND:
7420 return SDValue(Op.getNode(), 1);
7421 default:
7422 default_case:
7423 break;
Dan Gohman076aee32009-03-04 19:44:21 +00007424 }
7425
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007426 if (Opcode == 0)
7427 // Emit a CMP with 0, which is the TEST pattern.
7428 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
7429 DAG.getConstant(0, Op.getValueType()));
7430
7431 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
7432 SmallVector<SDValue, 4> Ops;
7433 for (unsigned i = 0; i != NumOperands; ++i)
7434 Ops.push_back(Op.getOperand(i));
7435
7436 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
7437 DAG.ReplaceAllUsesWith(Op, New);
7438 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00007439}
7440
7441/// Emit nodes that will be selected as "cmp Op0,Op1", or something
7442/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00007443SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00007444 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00007445 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
7446 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00007447 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00007448
7449 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00007450 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00007451}
7452
Evan Chengd40d03e2010-01-06 19:38:29 +00007453/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
7454/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00007455SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
7456 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00007457 SDValue Op0 = And.getOperand(0);
7458 SDValue Op1 = And.getOperand(1);
7459 if (Op0.getOpcode() == ISD::TRUNCATE)
7460 Op0 = Op0.getOperand(0);
7461 if (Op1.getOpcode() == ISD::TRUNCATE)
7462 Op1 = Op1.getOperand(0);
7463
Evan Chengd40d03e2010-01-06 19:38:29 +00007464 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00007465 if (Op1.getOpcode() == ISD::SHL)
7466 std::swap(Op0, Op1);
7467 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00007468 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
7469 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00007470 // If we looked past a truncate, check that it's only truncating away
7471 // known zeros.
7472 unsigned BitWidth = Op0.getValueSizeInBits();
7473 unsigned AndBitWidth = And.getValueSizeInBits();
7474 if (BitWidth > AndBitWidth) {
7475 APInt Mask = APInt::getAllOnesValue(BitWidth), Zeros, Ones;
7476 DAG.ComputeMaskedBits(Op0, Mask, Zeros, Ones);
7477 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
7478 return SDValue();
7479 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007480 LHS = Op1;
7481 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00007482 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007483 } else if (Op1.getOpcode() == ISD::Constant) {
7484 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
7485 SDValue AndLHS = Op0;
Evan Chengd40d03e2010-01-06 19:38:29 +00007486 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
7487 LHS = AndLHS.getOperand(0);
7488 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00007489 }
Evan Chengd40d03e2010-01-06 19:38:29 +00007490 }
Evan Cheng0488db92007-09-25 01:57:46 +00007491
Evan Chengd40d03e2010-01-06 19:38:29 +00007492 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00007493 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00007494 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00007495 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00007496 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00007497 // Also promote i16 to i32 for performance / code size reason.
7498 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00007499 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00007500 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00007501
Evan Chengd40d03e2010-01-06 19:38:29 +00007502 // If the operand types disagree, extend the shift amount to match. Since
7503 // BT ignores high bits (like shifts) we can use anyextend.
7504 if (LHS.getValueType() != RHS.getValueType())
7505 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00007506
Evan Chengd40d03e2010-01-06 19:38:29 +00007507 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
7508 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
7509 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7510 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00007511 }
7512
Evan Cheng54de3ea2010-01-05 06:52:31 +00007513 return SDValue();
7514}
7515
Dan Gohmand858e902010-04-17 15:26:15 +00007516SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng54de3ea2010-01-05 06:52:31 +00007517 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
7518 SDValue Op0 = Op.getOperand(0);
7519 SDValue Op1 = Op.getOperand(1);
7520 DebugLoc dl = Op.getDebugLoc();
7521 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
7522
7523 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00007524 // Lower (X & (1 << N)) == 0 to BT(X, N).
7525 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
7526 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Andrew Trickf6c39412011-03-23 23:11:02 +00007527 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00007528 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00007529 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00007530 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
7531 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
7532 if (NewSetCC.getNode())
7533 return NewSetCC;
7534 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00007535
Chris Lattner481eebc2010-12-19 21:23:48 +00007536 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
7537 // these.
7538 if (Op1.getOpcode() == ISD::Constant &&
Andrew Trickf6c39412011-03-23 23:11:02 +00007539 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
Evan Cheng2c755ba2010-02-27 07:36:59 +00007540 cast<ConstantSDNode>(Op1)->isNullValue()) &&
7541 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007542
Chris Lattner481eebc2010-12-19 21:23:48 +00007543 // If the input is a setcc, then reuse the input setcc or use a new one with
7544 // the inverted condition.
7545 if (Op0.getOpcode() == X86ISD::SETCC) {
7546 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
7547 bool Invert = (CC == ISD::SETNE) ^
7548 cast<ConstantSDNode>(Op1)->isNullValue();
7549 if (!Invert) return Op0;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007550
Evan Cheng2c755ba2010-02-27 07:36:59 +00007551 CCode = X86::GetOppositeBranchCondition(CCode);
Chris Lattner481eebc2010-12-19 21:23:48 +00007552 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7553 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
7554 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007555 }
7556
Evan Chenge5b51ac2010-04-17 06:13:15 +00007557 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00007558 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00007559 if (X86CC == X86::COND_INVALID)
7560 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00007561
Chris Lattnerc19d1c32010-12-19 22:08:31 +00007562 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00007563 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnerc19d1c32010-12-19 22:08:31 +00007564 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
Evan Cheng0488db92007-09-25 01:57:46 +00007565}
7566
Dan Gohmand858e902010-04-17 15:26:15 +00007567SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00007568 SDValue Cond;
7569 SDValue Op0 = Op.getOperand(0);
7570 SDValue Op1 = Op.getOperand(1);
7571 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00007572 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00007573 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
7574 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007575 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00007576
7577 if (isFP) {
7578 unsigned SSECC = 8;
Owen Andersone50ed302009-08-10 22:56:29 +00007579 EVT VT0 = Op0.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00007580 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
7581 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00007582 bool Swap = false;
7583
7584 switch (SetCCOpcode) {
7585 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00007586 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00007587 case ISD::SETEQ: SSECC = 0; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00007588 case ISD::SETOGT:
Nate Begeman30a0de92008-07-17 16:51:19 +00007589 case ISD::SETGT: Swap = true; // Fallthrough
7590 case ISD::SETLT:
7591 case ISD::SETOLT: SSECC = 1; break;
7592 case ISD::SETOGE:
7593 case ISD::SETGE: Swap = true; // Fallthrough
7594 case ISD::SETLE:
7595 case ISD::SETOLE: SSECC = 2; break;
7596 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00007597 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00007598 case ISD::SETNE: SSECC = 4; break;
7599 case ISD::SETULE: Swap = true;
7600 case ISD::SETUGE: SSECC = 5; break;
7601 case ISD::SETULT: Swap = true;
7602 case ISD::SETUGT: SSECC = 6; break;
7603 case ISD::SETO: SSECC = 7; break;
7604 }
7605 if (Swap)
7606 std::swap(Op0, Op1);
7607
Nate Begemanfb8ead02008-07-25 19:05:58 +00007608 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00007609 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00007610 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00007611 SDValue UNORD, EQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00007612 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
7613 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00007614 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00007615 }
7616 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00007617 SDValue ORD, NEQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00007618 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
7619 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00007620 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00007621 }
Torok Edwinc23197a2009-07-14 16:55:14 +00007622 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00007623 }
7624 // Handle all other FP comparisons here.
Owen Anderson825b72b2009-08-11 20:47:22 +00007625 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00007626 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007627
Nate Begeman30a0de92008-07-17 16:51:19 +00007628 // We are handling one of the integer comparisons here. Since SSE only has
7629 // GT and EQ comparisons for integer, swapping operands and multiple
7630 // operations may be required for some comparisons.
7631 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
7632 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007633
Owen Anderson825b72b2009-08-11 20:47:22 +00007634 switch (VT.getSimpleVT().SimpleTy) {
Nate Begeman30a0de92008-07-17 16:51:19 +00007635 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007636 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007637 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007638 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
7639 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00007640 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007641
Nate Begeman30a0de92008-07-17 16:51:19 +00007642 switch (SetCCOpcode) {
7643 default: break;
7644 case ISD::SETNE: Invert = true;
7645 case ISD::SETEQ: Opc = EQOpc; break;
7646 case ISD::SETLT: Swap = true;
7647 case ISD::SETGT: Opc = GTOpc; break;
7648 case ISD::SETGE: Swap = true;
7649 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
7650 case ISD::SETULT: Swap = true;
7651 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
7652 case ISD::SETUGE: Swap = true;
7653 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
7654 }
7655 if (Swap)
7656 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007657
Nate Begeman30a0de92008-07-17 16:51:19 +00007658 // Since SSE has no unsigned integer comparisons, we need to flip the sign
7659 // bits of the inputs before performing those operations.
7660 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00007661 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00007662 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
7663 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00007664 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00007665 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
7666 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00007667 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
7668 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00007669 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007670
Dale Johannesenace16102009-02-03 19:33:06 +00007671 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00007672
7673 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00007674 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00007675 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00007676
Nate Begeman30a0de92008-07-17 16:51:19 +00007677 return Result;
7678}
Evan Cheng0488db92007-09-25 01:57:46 +00007679
Evan Cheng370e5342008-12-03 08:38:43 +00007680// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00007681static bool isX86LogicalCmp(SDValue Op) {
7682 unsigned Opc = Op.getNode()->getOpcode();
7683 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
7684 return true;
7685 if (Op.getResNo() == 1 &&
7686 (Opc == X86ISD::ADD ||
7687 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00007688 Opc == X86ISD::ADC ||
7689 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00007690 Opc == X86ISD::SMUL ||
7691 Opc == X86ISD::UMUL ||
7692 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00007693 Opc == X86ISD::DEC ||
7694 Opc == X86ISD::OR ||
7695 Opc == X86ISD::XOR ||
7696 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00007697 return true;
7698
Chris Lattner9637d5b2010-12-05 07:49:54 +00007699 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
7700 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007701
Dan Gohman076aee32009-03-04 19:44:21 +00007702 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00007703}
7704
Chris Lattnera2b56002010-12-05 01:23:24 +00007705static bool isZero(SDValue V) {
7706 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
7707 return C && C->isNullValue();
7708}
7709
Chris Lattner96908b12010-12-05 02:00:51 +00007710static bool isAllOnes(SDValue V) {
7711 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
7712 return C && C->isAllOnesValue();
7713}
7714
Dan Gohmand858e902010-04-17 15:26:15 +00007715SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00007716 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007717 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00007718 SDValue Op1 = Op.getOperand(1);
7719 SDValue Op2 = Op.getOperand(2);
7720 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00007721 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00007722
Dan Gohman1a492952009-10-20 16:22:37 +00007723 if (Cond.getOpcode() == ISD::SETCC) {
7724 SDValue NewCond = LowerSETCC(Cond, DAG);
7725 if (NewCond.getNode())
7726 Cond = NewCond;
7727 }
Evan Cheng734503b2006-09-11 02:19:56 +00007728
Chris Lattnera2b56002010-12-05 01:23:24 +00007729 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00007730 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00007731 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00007732 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007733 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00007734 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
7735 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007736 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007737
Chris Lattnera2b56002010-12-05 01:23:24 +00007738 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007739
7740 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00007741 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
7742 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00007743
7744 SDValue CmpOp0 = Cmp.getOperand(0);
7745 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
7746 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007747
Chris Lattner96908b12010-12-05 02:00:51 +00007748 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00007749 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
7750 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007751
Chris Lattner96908b12010-12-05 02:00:51 +00007752 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
7753 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007754
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007755 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00007756 if (N2C == 0 || !N2C->isNullValue())
7757 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
7758 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007759 }
7760 }
7761
Chris Lattnera2b56002010-12-05 01:23:24 +00007762 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00007763 if (Cond.getOpcode() == ISD::AND &&
7764 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
7765 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00007766 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00007767 Cond = Cond.getOperand(0);
7768 }
7769
Evan Cheng3f41d662007-10-08 22:16:29 +00007770 // If condition flag is set by a X86ISD::CMP, then use it as the condition
7771 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00007772 if (Cond.getOpcode() == X86ISD::SETCC ||
7773 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00007774 CC = Cond.getOperand(0);
7775
Dan Gohman475871a2008-07-27 21:46:04 +00007776 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00007777 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00007778 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00007779
Evan Cheng3f41d662007-10-08 22:16:29 +00007780 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007781 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00007782 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00007783 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00007784
Chris Lattnerd1980a52009-03-12 06:52:53 +00007785 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
7786 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00007787 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00007788 addTest = false;
7789 }
7790 }
7791
7792 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007793 // Look pass the truncate.
7794 if (Cond.getOpcode() == ISD::TRUNCATE)
7795 Cond = Cond.getOperand(0);
7796
7797 // We know the result of AND is compared against zero. Try to match
7798 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00007799 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00007800 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00007801 if (NewSetCC.getNode()) {
7802 CC = NewSetCC.getOperand(0);
7803 Cond = NewSetCC.getOperand(1);
7804 addTest = false;
7805 }
7806 }
7807 }
7808
7809 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007810 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00007811 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00007812 }
7813
Benjamin Kramere915ff32010-12-22 23:09:28 +00007814 // a < b ? -1 : 0 -> RES = ~setcc_carry
7815 // a < b ? 0 : -1 -> RES = setcc_carry
7816 // a >= b ? -1 : 0 -> RES = setcc_carry
7817 // a >= b ? 0 : -1 -> RES = ~setcc_carry
7818 if (Cond.getOpcode() == X86ISD::CMP) {
7819 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
7820
7821 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
7822 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
7823 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
7824 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
7825 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
7826 return DAG.getNOT(DL, Res, Res.getValueType());
7827 return Res;
7828 }
7829 }
7830
Evan Cheng0488db92007-09-25 01:57:46 +00007831 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
7832 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007833 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007834 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00007835 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00007836}
7837
Evan Cheng370e5342008-12-03 08:38:43 +00007838// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
7839// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
7840// from the AND / OR.
7841static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
7842 Opc = Op.getOpcode();
7843 if (Opc != ISD::OR && Opc != ISD::AND)
7844 return false;
7845 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
7846 Op.getOperand(0).hasOneUse() &&
7847 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
7848 Op.getOperand(1).hasOneUse());
7849}
7850
Evan Cheng961d6d42009-02-02 08:19:07 +00007851// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
7852// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00007853static bool isXor1OfSetCC(SDValue Op) {
7854 if (Op.getOpcode() != ISD::XOR)
7855 return false;
7856 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
7857 if (N1C && N1C->getAPIntValue() == 1) {
7858 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
7859 Op.getOperand(0).hasOneUse();
7860 }
7861 return false;
7862}
7863
Dan Gohmand858e902010-04-17 15:26:15 +00007864SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00007865 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007866 SDValue Chain = Op.getOperand(0);
7867 SDValue Cond = Op.getOperand(1);
7868 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007869 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00007870 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00007871
Dan Gohman1a492952009-10-20 16:22:37 +00007872 if (Cond.getOpcode() == ISD::SETCC) {
7873 SDValue NewCond = LowerSETCC(Cond, DAG);
7874 if (NewCond.getNode())
7875 Cond = NewCond;
7876 }
Chris Lattnere55484e2008-12-25 05:34:37 +00007877#if 0
7878 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00007879 else if (Cond.getOpcode() == X86ISD::ADD ||
7880 Cond.getOpcode() == X86ISD::SUB ||
7881 Cond.getOpcode() == X86ISD::SMUL ||
7882 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00007883 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00007884#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00007885
Evan Chengad9c0a32009-12-15 00:53:42 +00007886 // Look pass (and (setcc_carry (cmp ...)), 1).
7887 if (Cond.getOpcode() == ISD::AND &&
7888 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
7889 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00007890 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00007891 Cond = Cond.getOperand(0);
7892 }
7893
Evan Cheng3f41d662007-10-08 22:16:29 +00007894 // If condition flag is set by a X86ISD::CMP, then use it as the condition
7895 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00007896 if (Cond.getOpcode() == X86ISD::SETCC ||
7897 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00007898 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007899
Dan Gohman475871a2008-07-27 21:46:04 +00007900 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00007901 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00007902 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00007903 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00007904 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00007905 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00007906 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00007907 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00007908 default: break;
7909 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00007910 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00007911 // These can only come from an arithmetic instruction with overflow,
7912 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00007913 Cond = Cond.getNode()->getOperand(1);
7914 addTest = false;
7915 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00007916 }
Evan Cheng0488db92007-09-25 01:57:46 +00007917 }
Evan Cheng370e5342008-12-03 08:38:43 +00007918 } else {
7919 unsigned CondOpc;
7920 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
7921 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00007922 if (CondOpc == ISD::OR) {
7923 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
7924 // two branches instead of an explicit OR instruction with a
7925 // separate test.
7926 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00007927 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00007928 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007929 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00007930 Chain, Dest, CC, Cmp);
7931 CC = Cond.getOperand(1).getOperand(0);
7932 Cond = Cmp;
7933 addTest = false;
7934 }
7935 } else { // ISD::AND
7936 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
7937 // two branches instead of an explicit AND instruction with a
7938 // separate test. However, we only do this if this block doesn't
7939 // have a fall-through edge, because this requires an explicit
7940 // jmp when the condition is false.
7941 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00007942 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00007943 Op.getNode()->hasOneUse()) {
7944 X86::CondCode CCode =
7945 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
7946 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007947 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00007948 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00007949 // Look for an unconditional branch following this conditional branch.
7950 // We need this because we need to reverse the successors in order
7951 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00007952 if (User->getOpcode() == ISD::BR) {
7953 SDValue FalseBB = User->getOperand(1);
7954 SDNode *NewBR =
7955 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00007956 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00007957 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00007958 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00007959
Dale Johannesene4d209d2009-02-03 20:21:25 +00007960 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00007961 Chain, Dest, CC, Cmp);
7962 X86::CondCode CCode =
7963 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
7964 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007965 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00007966 Cond = Cmp;
7967 addTest = false;
7968 }
7969 }
Dan Gohman279c22e2008-10-21 03:29:32 +00007970 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00007971 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
7972 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
7973 // It should be transformed during dag combiner except when the condition
7974 // is set by a arithmetics with overflow node.
7975 X86::CondCode CCode =
7976 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
7977 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007978 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00007979 Cond = Cond.getOperand(0).getOperand(1);
7980 addTest = false;
Dan Gohman279c22e2008-10-21 03:29:32 +00007981 }
Evan Cheng0488db92007-09-25 01:57:46 +00007982 }
7983
7984 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007985 // Look pass the truncate.
7986 if (Cond.getOpcode() == ISD::TRUNCATE)
7987 Cond = Cond.getOperand(0);
7988
7989 // We know the result of AND is compared against zero. Try to match
7990 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00007991 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007992 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
7993 if (NewSetCC.getNode()) {
7994 CC = NewSetCC.getOperand(0);
7995 Cond = NewSetCC.getOperand(1);
7996 addTest = false;
7997 }
7998 }
7999 }
8000
8001 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008002 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00008003 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00008004 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00008005 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00008006 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00008007}
8008
Anton Korobeynikove060b532007-04-17 19:34:00 +00008009
8010// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
8011// Calls to _alloca is needed to probe the stack when allocating more than 4k
8012// bytes in one go. Touching the stack at 4K increments is necessary to ensure
8013// that the guard pages used by the OS virtual memory manager are allocated in
8014// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00008015SDValue
8016X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008017 SelectionDAG &DAG) const {
Duncan Sands1e1ca0b2010-10-21 16:02:12 +00008018 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows()) &&
Michael J. Spencere9c253e2010-10-21 01:41:01 +00008019 "This should be used only on Windows targets");
NAKAMURA Takumia2e07622011-03-24 07:07:00 +00008020 assert(!Subtarget->isTargetEnvMacho());
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008021 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008022
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008023 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00008024 SDValue Chain = Op.getOperand(0);
8025 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008026 // FIXME: Ensure alignment here
8027
Dan Gohman475871a2008-07-27 21:46:04 +00008028 SDValue Flag;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008029
Owen Anderson825b72b2009-08-11 20:47:22 +00008030 EVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
NAKAMURA Takumia2e07622011-03-24 07:07:00 +00008031 unsigned Reg = (Subtarget->is64Bit() ? X86::RAX : X86::EAX);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008032
NAKAMURA Takumia2e07622011-03-24 07:07:00 +00008033 Chain = DAG.getCopyToReg(Chain, dl, Reg, Size, Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00008034 Flag = Chain.getValue(1);
8035
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008036 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00008037
Michael J. Spencere9c253e2010-10-21 01:41:01 +00008038 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00008039 Flag = Chain.getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008040
Dale Johannesendd64c412009-02-04 00:33:20 +00008041 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008042
Dan Gohman475871a2008-07-27 21:46:04 +00008043 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008044 return DAG.getMergeValues(Ops1, 2, dl);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008045}
8046
Dan Gohmand858e902010-04-17 15:26:15 +00008047SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00008048 MachineFunction &MF = DAG.getMachineFunction();
8049 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
8050
Dan Gohman69de1932008-02-06 22:27:42 +00008051 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00008052 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00008053
Anton Korobeynikove7beda12010-10-03 22:52:07 +00008054 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00008055 // vastart just stores the address of the VarArgsFrameIndex slot into the
8056 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00008057 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
8058 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00008059 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
8060 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008061 }
8062
8063 // __va_list_tag:
8064 // gp_offset (0 - 6 * 8)
8065 // fp_offset (48 - 48 + 8 * 16)
8066 // overflow_arg_area (point to parameters coming in memory).
8067 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00008068 SmallVector<SDValue, 8> MemOps;
8069 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00008070 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00008071 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00008072 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
8073 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008074 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008075 MemOps.push_back(Store);
8076
8077 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00008078 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008079 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008080 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00008081 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
8082 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008083 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008084 MemOps.push_back(Store);
8085
8086 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +00008087 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008088 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00008089 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
8090 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00008091 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
8092 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +00008093 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008094 MemOps.push_back(Store);
8095
8096 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +00008097 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008098 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00008099 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
8100 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00008101 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
8102 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008103 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +00008104 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00008105 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00008106}
8107
Dan Gohmand858e902010-04-17 15:26:15 +00008108SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +00008109 assert(Subtarget->is64Bit() &&
8110 "LowerVAARG only handles 64-bit va_arg!");
8111 assert((Subtarget->isTargetLinux() ||
8112 Subtarget->isTargetDarwin()) &&
8113 "Unhandled target in LowerVAARG");
8114 assert(Op.getNode()->getNumOperands() == 4);
8115 SDValue Chain = Op.getOperand(0);
8116 SDValue SrcPtr = Op.getOperand(1);
8117 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
8118 unsigned Align = Op.getConstantOperandVal(3);
8119 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +00008120
Dan Gohman320afb82010-10-12 18:00:49 +00008121 EVT ArgVT = Op.getNode()->getValueType(0);
8122 const Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
8123 uint32_t ArgSize = getTargetData()->getTypeAllocSize(ArgTy);
8124 uint8_t ArgMode;
8125
8126 // Decide which area this value should be read from.
8127 // TODO: Implement the AMD64 ABI in its entirety. This simple
8128 // selection mechanism works only for the basic types.
8129 if (ArgVT == MVT::f80) {
8130 llvm_unreachable("va_arg for f80 not yet implemented");
8131 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
8132 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
8133 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
8134 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
8135 } else {
8136 llvm_unreachable("Unhandled argument type in LowerVAARG");
8137 }
8138
8139 if (ArgMode == 2) {
8140 // Sanity Check: Make sure using fp_offset makes sense.
Michael J. Spencer87b86652010-10-19 07:32:42 +00008141 assert(!UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +00008142 !(DAG.getMachineFunction()
8143 .getFunction()->hasFnAttr(Attribute::NoImplicitFloat)) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00008144 Subtarget->hasXMM());
Dan Gohman320afb82010-10-12 18:00:49 +00008145 }
8146
8147 // Insert VAARG_64 node into the DAG
8148 // VAARG_64 returns two values: Variable Argument Address, Chain
8149 SmallVector<SDValue, 11> InstOps;
8150 InstOps.push_back(Chain);
8151 InstOps.push_back(SrcPtr);
8152 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
8153 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
8154 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
8155 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
8156 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
8157 VTs, &InstOps[0], InstOps.size(),
8158 MVT::i64,
8159 MachinePointerInfo(SV),
8160 /*Align=*/0,
8161 /*Volatile=*/false,
8162 /*ReadMem=*/true,
8163 /*WriteMem=*/true);
8164 Chain = VAARG.getValue(1);
8165
8166 // Load the next argument and return it
8167 return DAG.getLoad(ArgVT, dl,
8168 Chain,
8169 VAARG,
8170 MachinePointerInfo(),
8171 false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +00008172}
8173
Dan Gohmand858e902010-04-17 15:26:15 +00008174SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) const {
Evan Chengae642192007-03-02 23:16:35 +00008175 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00008176 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00008177 SDValue Chain = Op.getOperand(0);
8178 SDValue DstPtr = Op.getOperand(1);
8179 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00008180 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
8181 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +00008182 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00008183
Chris Lattnere72f2022010-09-21 05:40:29 +00008184 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00008185 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +00008186 false,
Chris Lattnere72f2022010-09-21 05:40:29 +00008187 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +00008188}
8189
Dan Gohman475871a2008-07-27 21:46:04 +00008190SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00008191X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008192 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00008193 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00008194 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00008195 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00008196 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00008197 case Intrinsic::x86_sse_comieq_ss:
8198 case Intrinsic::x86_sse_comilt_ss:
8199 case Intrinsic::x86_sse_comile_ss:
8200 case Intrinsic::x86_sse_comigt_ss:
8201 case Intrinsic::x86_sse_comige_ss:
8202 case Intrinsic::x86_sse_comineq_ss:
8203 case Intrinsic::x86_sse_ucomieq_ss:
8204 case Intrinsic::x86_sse_ucomilt_ss:
8205 case Intrinsic::x86_sse_ucomile_ss:
8206 case Intrinsic::x86_sse_ucomigt_ss:
8207 case Intrinsic::x86_sse_ucomige_ss:
8208 case Intrinsic::x86_sse_ucomineq_ss:
8209 case Intrinsic::x86_sse2_comieq_sd:
8210 case Intrinsic::x86_sse2_comilt_sd:
8211 case Intrinsic::x86_sse2_comile_sd:
8212 case Intrinsic::x86_sse2_comigt_sd:
8213 case Intrinsic::x86_sse2_comige_sd:
8214 case Intrinsic::x86_sse2_comineq_sd:
8215 case Intrinsic::x86_sse2_ucomieq_sd:
8216 case Intrinsic::x86_sse2_ucomilt_sd:
8217 case Intrinsic::x86_sse2_ucomile_sd:
8218 case Intrinsic::x86_sse2_ucomigt_sd:
8219 case Intrinsic::x86_sse2_ucomige_sd:
8220 case Intrinsic::x86_sse2_ucomineq_sd: {
8221 unsigned Opc = 0;
8222 ISD::CondCode CC = ISD::SETCC_INVALID;
8223 switch (IntNo) {
8224 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008225 case Intrinsic::x86_sse_comieq_ss:
8226 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008227 Opc = X86ISD::COMI;
8228 CC = ISD::SETEQ;
8229 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00008230 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008231 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008232 Opc = X86ISD::COMI;
8233 CC = ISD::SETLT;
8234 break;
8235 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008236 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008237 Opc = X86ISD::COMI;
8238 CC = ISD::SETLE;
8239 break;
8240 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008241 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008242 Opc = X86ISD::COMI;
8243 CC = ISD::SETGT;
8244 break;
8245 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008246 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008247 Opc = X86ISD::COMI;
8248 CC = ISD::SETGE;
8249 break;
8250 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008251 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008252 Opc = X86ISD::COMI;
8253 CC = ISD::SETNE;
8254 break;
8255 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008256 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008257 Opc = X86ISD::UCOMI;
8258 CC = ISD::SETEQ;
8259 break;
8260 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008261 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008262 Opc = X86ISD::UCOMI;
8263 CC = ISD::SETLT;
8264 break;
8265 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008266 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008267 Opc = X86ISD::UCOMI;
8268 CC = ISD::SETLE;
8269 break;
8270 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008271 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008272 Opc = X86ISD::UCOMI;
8273 CC = ISD::SETGT;
8274 break;
8275 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008276 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008277 Opc = X86ISD::UCOMI;
8278 CC = ISD::SETGE;
8279 break;
8280 case Intrinsic::x86_sse_ucomineq_ss:
8281 case Intrinsic::x86_sse2_ucomineq_sd:
8282 Opc = X86ISD::UCOMI;
8283 CC = ISD::SETNE;
8284 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00008285 }
Evan Cheng734503b2006-09-11 02:19:56 +00008286
Dan Gohman475871a2008-07-27 21:46:04 +00008287 SDValue LHS = Op.getOperand(1);
8288 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00008289 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00008290 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00008291 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
8292 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8293 DAG.getConstant(X86CC, MVT::i8), Cond);
8294 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00008295 }
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008296 // ptest and testp intrinsics. The intrinsic these come from are designed to
8297 // return an integer value, not just an instruction so lower it to the ptest
8298 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00008299 case Intrinsic::x86_sse41_ptestz:
8300 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008301 case Intrinsic::x86_sse41_ptestnzc:
8302 case Intrinsic::x86_avx_ptestz_256:
8303 case Intrinsic::x86_avx_ptestc_256:
8304 case Intrinsic::x86_avx_ptestnzc_256:
8305 case Intrinsic::x86_avx_vtestz_ps:
8306 case Intrinsic::x86_avx_vtestc_ps:
8307 case Intrinsic::x86_avx_vtestnzc_ps:
8308 case Intrinsic::x86_avx_vtestz_pd:
8309 case Intrinsic::x86_avx_vtestc_pd:
8310 case Intrinsic::x86_avx_vtestnzc_pd:
8311 case Intrinsic::x86_avx_vtestz_ps_256:
8312 case Intrinsic::x86_avx_vtestc_ps_256:
8313 case Intrinsic::x86_avx_vtestnzc_ps_256:
8314 case Intrinsic::x86_avx_vtestz_pd_256:
8315 case Intrinsic::x86_avx_vtestc_pd_256:
8316 case Intrinsic::x86_avx_vtestnzc_pd_256: {
8317 bool IsTestPacked = false;
Eric Christopher71c67532009-07-29 00:28:05 +00008318 unsigned X86CC = 0;
8319 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00008320 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008321 case Intrinsic::x86_avx_vtestz_ps:
8322 case Intrinsic::x86_avx_vtestz_pd:
8323 case Intrinsic::x86_avx_vtestz_ps_256:
8324 case Intrinsic::x86_avx_vtestz_pd_256:
8325 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00008326 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008327 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +00008328 // ZF = 1
8329 X86CC = X86::COND_E;
8330 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008331 case Intrinsic::x86_avx_vtestc_ps:
8332 case Intrinsic::x86_avx_vtestc_pd:
8333 case Intrinsic::x86_avx_vtestc_ps_256:
8334 case Intrinsic::x86_avx_vtestc_pd_256:
8335 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00008336 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008337 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00008338 // CF = 1
8339 X86CC = X86::COND_B;
8340 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008341 case Intrinsic::x86_avx_vtestnzc_ps:
8342 case Intrinsic::x86_avx_vtestnzc_pd:
8343 case Intrinsic::x86_avx_vtestnzc_ps_256:
8344 case Intrinsic::x86_avx_vtestnzc_pd_256:
8345 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +00008346 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008347 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00008348 // ZF and CF = 0
8349 X86CC = X86::COND_A;
8350 break;
8351 }
Eric Christopherfd179292009-08-27 18:07:15 +00008352
Eric Christopher71c67532009-07-29 00:28:05 +00008353 SDValue LHS = Op.getOperand(1);
8354 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008355 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
8356 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00008357 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
8358 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
8359 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00008360 }
Evan Cheng5759f972008-05-04 09:15:50 +00008361
8362 // Fix vector shift instructions where the last operand is a non-immediate
8363 // i32 value.
8364 case Intrinsic::x86_sse2_pslli_w:
8365 case Intrinsic::x86_sse2_pslli_d:
8366 case Intrinsic::x86_sse2_pslli_q:
8367 case Intrinsic::x86_sse2_psrli_w:
8368 case Intrinsic::x86_sse2_psrli_d:
8369 case Intrinsic::x86_sse2_psrli_q:
8370 case Intrinsic::x86_sse2_psrai_w:
8371 case Intrinsic::x86_sse2_psrai_d:
8372 case Intrinsic::x86_mmx_pslli_w:
8373 case Intrinsic::x86_mmx_pslli_d:
8374 case Intrinsic::x86_mmx_pslli_q:
8375 case Intrinsic::x86_mmx_psrli_w:
8376 case Intrinsic::x86_mmx_psrli_d:
8377 case Intrinsic::x86_mmx_psrli_q:
8378 case Intrinsic::x86_mmx_psrai_w:
8379 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00008380 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00008381 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00008382 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00008383
8384 unsigned NewIntNo = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00008385 EVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00008386 switch (IntNo) {
8387 case Intrinsic::x86_sse2_pslli_w:
8388 NewIntNo = Intrinsic::x86_sse2_psll_w;
8389 break;
8390 case Intrinsic::x86_sse2_pslli_d:
8391 NewIntNo = Intrinsic::x86_sse2_psll_d;
8392 break;
8393 case Intrinsic::x86_sse2_pslli_q:
8394 NewIntNo = Intrinsic::x86_sse2_psll_q;
8395 break;
8396 case Intrinsic::x86_sse2_psrli_w:
8397 NewIntNo = Intrinsic::x86_sse2_psrl_w;
8398 break;
8399 case Intrinsic::x86_sse2_psrli_d:
8400 NewIntNo = Intrinsic::x86_sse2_psrl_d;
8401 break;
8402 case Intrinsic::x86_sse2_psrli_q:
8403 NewIntNo = Intrinsic::x86_sse2_psrl_q;
8404 break;
8405 case Intrinsic::x86_sse2_psrai_w:
8406 NewIntNo = Intrinsic::x86_sse2_psra_w;
8407 break;
8408 case Intrinsic::x86_sse2_psrai_d:
8409 NewIntNo = Intrinsic::x86_sse2_psra_d;
8410 break;
8411 default: {
Owen Anderson825b72b2009-08-11 20:47:22 +00008412 ShAmtVT = MVT::v2i32;
Evan Cheng5759f972008-05-04 09:15:50 +00008413 switch (IntNo) {
8414 case Intrinsic::x86_mmx_pslli_w:
8415 NewIntNo = Intrinsic::x86_mmx_psll_w;
8416 break;
8417 case Intrinsic::x86_mmx_pslli_d:
8418 NewIntNo = Intrinsic::x86_mmx_psll_d;
8419 break;
8420 case Intrinsic::x86_mmx_pslli_q:
8421 NewIntNo = Intrinsic::x86_mmx_psll_q;
8422 break;
8423 case Intrinsic::x86_mmx_psrli_w:
8424 NewIntNo = Intrinsic::x86_mmx_psrl_w;
8425 break;
8426 case Intrinsic::x86_mmx_psrli_d:
8427 NewIntNo = Intrinsic::x86_mmx_psrl_d;
8428 break;
8429 case Intrinsic::x86_mmx_psrli_q:
8430 NewIntNo = Intrinsic::x86_mmx_psrl_q;
8431 break;
8432 case Intrinsic::x86_mmx_psrai_w:
8433 NewIntNo = Intrinsic::x86_mmx_psra_w;
8434 break;
8435 case Intrinsic::x86_mmx_psrai_d:
8436 NewIntNo = Intrinsic::x86_mmx_psra_d;
8437 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00008438 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00008439 }
8440 break;
8441 }
8442 }
Mon P Wangefa42202009-09-03 19:56:25 +00008443
8444 // The vector shift intrinsics with scalars uses 32b shift amounts but
8445 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
8446 // to be zero.
8447 SDValue ShOps[4];
8448 ShOps[0] = ShAmt;
8449 ShOps[1] = DAG.getConstant(0, MVT::i32);
8450 if (ShAmtVT == MVT::v4i32) {
8451 ShOps[2] = DAG.getUNDEF(MVT::i32);
8452 ShOps[3] = DAG.getUNDEF(MVT::i32);
8453 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
8454 } else {
8455 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
Dale Johannesen0488fb62010-09-30 23:57:10 +00008456// FIXME this must be lowered to get rid of the invalid type.
Mon P Wangefa42202009-09-03 19:56:25 +00008457 }
8458
Owen Andersone50ed302009-08-10 22:56:29 +00008459 EVT VT = Op.getValueType();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008460 ShAmt = DAG.getNode(ISD::BITCAST, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008461 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008462 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00008463 Op.getOperand(1), ShAmt);
8464 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00008465 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008466}
Evan Cheng72261582005-12-20 06:22:03 +00008467
Dan Gohmand858e902010-04-17 15:26:15 +00008468SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
8469 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +00008470 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
8471 MFI->setReturnAddressIsTaken(true);
8472
Bill Wendling64e87322009-01-16 19:25:27 +00008473 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008474 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00008475
8476 if (Depth > 0) {
8477 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
8478 SDValue Offset =
8479 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00008480 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008481 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00008482 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008483 FrameAddr, Offset),
Chris Lattner51abfe42010-09-21 06:02:19 +00008484 MachinePointerInfo(), false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +00008485 }
8486
8487 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00008488 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00008489 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Chris Lattner51abfe42010-09-21 06:02:19 +00008490 RetAddrFI, MachinePointerInfo(), false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00008491}
8492
Dan Gohmand858e902010-04-17 15:26:15 +00008493SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +00008494 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
8495 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00008496
Owen Andersone50ed302009-08-10 22:56:29 +00008497 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008498 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00008499 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
8500 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00008501 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00008502 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +00008503 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
8504 MachinePointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +00008505 false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00008506 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00008507}
8508
Dan Gohman475871a2008-07-27 21:46:04 +00008509SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008510 SelectionDAG &DAG) const {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00008511 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008512}
8513
Dan Gohmand858e902010-04-17 15:26:15 +00008514SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008515 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00008516 SDValue Chain = Op.getOperand(0);
8517 SDValue Offset = Op.getOperand(1);
8518 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008519 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008520
Dan Gohmand8816272010-08-11 18:14:00 +00008521 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
8522 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
8523 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008524 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008525
Dan Gohmand8816272010-08-11 18:14:00 +00008526 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
8527 DAG.getIntPtrConstant(TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008528 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +00008529 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
8530 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00008531 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008532 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008533
Dale Johannesene4d209d2009-02-03 20:21:25 +00008534 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008535 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008536 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008537}
8538
Dan Gohman475871a2008-07-27 21:46:04 +00008539SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008540 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008541 SDValue Root = Op.getOperand(0);
8542 SDValue Trmp = Op.getOperand(1); // trampoline
8543 SDValue FPtr = Op.getOperand(2); // nested function
8544 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008545 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008546
Dan Gohman69de1932008-02-06 22:27:42 +00008547 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008548
8549 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00008550 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00008551
8552 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +00008553 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
8554 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +00008555
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00008556 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
8557 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00008558
8559 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
8560
8561 // Load the pointer to the nested function into R11.
8562 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00008563 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00008564 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008565 Addr, MachinePointerInfo(TrmpAddr),
8566 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008567
Owen Anderson825b72b2009-08-11 20:47:22 +00008568 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8569 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008570 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
8571 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +00008572 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00008573
8574 // Load the 'nest' parameter value into R10.
8575 // R10 is specified in X86CallingConv.td
8576 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +00008577 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8578 DAG.getConstant(10, MVT::i64));
8579 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008580 Addr, MachinePointerInfo(TrmpAddr, 10),
8581 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008582
Owen Anderson825b72b2009-08-11 20:47:22 +00008583 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8584 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008585 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
8586 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +00008587 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00008588
8589 // Jump to the nested function.
8590 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +00008591 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8592 DAG.getConstant(20, MVT::i64));
8593 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008594 Addr, MachinePointerInfo(TrmpAddr, 20),
8595 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008596
8597 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +00008598 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8599 DAG.getConstant(22, MVT::i64));
8600 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00008601 MachinePointerInfo(TrmpAddr, 22),
8602 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008603
Dan Gohman475871a2008-07-27 21:46:04 +00008604 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00008605 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008606 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008607 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00008608 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00008609 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00008610 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00008611 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008612
8613 switch (CC) {
8614 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00008615 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00008616 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00008617 case CallingConv::X86_StdCall: {
8618 // Pass 'nest' parameter in ECX.
8619 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00008620 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008621
8622 // Check that ECX wasn't needed by an 'inreg' parameter.
8623 const FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00008624 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008625
Chris Lattner58d74912008-03-12 17:45:29 +00008626 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00008627 unsigned InRegCount = 0;
8628 unsigned Idx = 1;
8629
8630 for (FunctionType::param_iterator I = FTy->param_begin(),
8631 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00008632 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00008633 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00008634 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008635
8636 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +00008637 report_fatal_error("Nest register in use - reduce number of inreg"
8638 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00008639 }
8640 }
8641 break;
8642 }
8643 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +00008644 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00008645 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00008646 // Pass 'nest' parameter in EAX.
8647 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00008648 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008649 break;
8650 }
8651
Dan Gohman475871a2008-07-27 21:46:04 +00008652 SDValue OutChains[4];
8653 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008654
Owen Anderson825b72b2009-08-11 20:47:22 +00008655 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8656 DAG.getConstant(10, MVT::i32));
8657 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008658
Chris Lattnera62fe662010-02-05 19:20:30 +00008659 // This is storing the opcode for MOV32ri.
8660 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00008661 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00008662 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008663 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008664 Trmp, MachinePointerInfo(TrmpAddr),
8665 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008666
Owen Anderson825b72b2009-08-11 20:47:22 +00008667 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8668 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008669 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
8670 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +00008671 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008672
Chris Lattnera62fe662010-02-05 19:20:30 +00008673 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +00008674 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8675 DAG.getConstant(5, MVT::i32));
8676 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00008677 MachinePointerInfo(TrmpAddr, 5),
8678 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008679
Owen Anderson825b72b2009-08-11 20:47:22 +00008680 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8681 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008682 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
8683 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +00008684 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008685
Dan Gohman475871a2008-07-27 21:46:04 +00008686 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00008687 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008688 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008689 }
8690}
8691
Dan Gohmand858e902010-04-17 15:26:15 +00008692SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
8693 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008694 /*
8695 The rounding mode is in bits 11:10 of FPSR, and has the following
8696 settings:
8697 00 Round to nearest
8698 01 Round to -inf
8699 10 Round to +inf
8700 11 Round to 0
8701
8702 FLT_ROUNDS, on the other hand, expects the following:
8703 -1 Undefined
8704 0 Round to 0
8705 1 Round to nearest
8706 2 Round to +inf
8707 3 Round to -inf
8708
8709 To perform the conversion, we do:
8710 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
8711 */
8712
8713 MachineFunction &MF = DAG.getMachineFunction();
8714 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00008715 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008716 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00008717 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +00008718 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008719
8720 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00008721 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +00008722 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008723
Michael J. Spencerec38de22010-10-10 22:04:20 +00008724
Chris Lattner2156b792010-09-22 01:11:26 +00008725 MachineMemOperand *MMO =
8726 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8727 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008728
Chris Lattner2156b792010-09-22 01:11:26 +00008729 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
8730 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
8731 DAG.getVTList(MVT::Other),
8732 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008733
8734 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +00008735 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Chris Lattner51abfe42010-09-21 06:02:19 +00008736 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008737
8738 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00008739 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +00008740 DAG.getNode(ISD::SRL, DL, MVT::i16,
8741 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00008742 CWD, DAG.getConstant(0x800, MVT::i16)),
8743 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00008744 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +00008745 DAG.getNode(ISD::SRL, DL, MVT::i16,
8746 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00008747 CWD, DAG.getConstant(0x400, MVT::i16)),
8748 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008749
Dan Gohman475871a2008-07-27 21:46:04 +00008750 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +00008751 DAG.getNode(ISD::AND, DL, MVT::i16,
8752 DAG.getNode(ISD::ADD, DL, MVT::i16,
8753 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +00008754 DAG.getConstant(1, MVT::i16)),
8755 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008756
8757
Duncan Sands83ec4b62008-06-06 12:08:01 +00008758 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +00008759 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008760}
8761
Dan Gohmand858e902010-04-17 15:26:15 +00008762SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008763 EVT VT = Op.getValueType();
8764 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008765 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008766 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00008767
8768 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008769 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00008770 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +00008771 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00008772 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008773 }
Evan Cheng18efe262007-12-14 02:13:44 +00008774
Evan Cheng152804e2007-12-14 08:30:15 +00008775 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008776 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008777 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00008778
8779 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00008780 SDValue Ops[] = {
8781 Op,
8782 DAG.getConstant(NumBits+NumBits-1, OpVT),
8783 DAG.getConstant(X86::COND_E, MVT::i8),
8784 Op.getValue(1)
8785 };
8786 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00008787
8788 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00008789 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00008790
Owen Anderson825b72b2009-08-11 20:47:22 +00008791 if (VT == MVT::i8)
8792 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008793 return Op;
8794}
8795
Dan Gohmand858e902010-04-17 15:26:15 +00008796SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008797 EVT VT = Op.getValueType();
8798 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008799 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008800 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00008801
8802 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008803 if (VT == MVT::i8) {
8804 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00008805 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008806 }
Evan Cheng152804e2007-12-14 08:30:15 +00008807
8808 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008809 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008810 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00008811
8812 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00008813 SDValue Ops[] = {
8814 Op,
8815 DAG.getConstant(NumBits, OpVT),
8816 DAG.getConstant(X86::COND_E, MVT::i8),
8817 Op.getValue(1)
8818 };
8819 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00008820
Owen Anderson825b72b2009-08-11 20:47:22 +00008821 if (VT == MVT::i8)
8822 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008823 return Op;
8824}
8825
Dan Gohmand858e902010-04-17 15:26:15 +00008826SDValue X86TargetLowering::LowerMUL_V2I64(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008827 EVT VT = Op.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00008828 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008829 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00008830
Mon P Wangaf9b9522008-12-18 21:42:19 +00008831 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
8832 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
8833 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
8834 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
8835 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
8836 //
8837 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
8838 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
8839 // return AloBlo + AloBhi + AhiBlo;
8840
8841 SDValue A = Op.getOperand(0);
8842 SDValue B = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008843
Dale Johannesene4d209d2009-02-03 20:21:25 +00008844 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008845 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8846 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008847 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008848 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8849 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008850 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008851 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008852 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008853 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008854 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008855 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008856 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008857 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008858 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008859 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008860 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8861 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008862 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008863 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8864 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008865 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
8866 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +00008867 return Res;
8868}
8869
Nadav Rotem43012222011-05-11 08:12:09 +00008870SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
8871
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008872 EVT VT = Op.getValueType();
8873 DebugLoc dl = Op.getDebugLoc();
8874 SDValue R = Op.getOperand(0);
Nadav Rotem43012222011-05-11 08:12:09 +00008875 SDValue Amt = Op.getOperand(1);
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008876
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008877 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008878
Nadav Rotem43012222011-05-11 08:12:09 +00008879 // Must have SSE2.
8880 if (!Subtarget->hasSSE2()) return SDValue();
Nate Begeman51409212010-07-28 00:21:48 +00008881
Nadav Rotem43012222011-05-11 08:12:09 +00008882 // Optimize shl/srl/sra with constant shift amount.
8883 if (isSplatVector(Amt.getNode())) {
8884 SDValue SclrAmt = Amt->getOperand(0);
8885 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SclrAmt)) {
8886 uint64_t ShiftAmt = C->getZExtValue();
8887
8888 if (VT == MVT::v2i64 && Op.getOpcode() == ISD::SHL)
8889 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8890 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8891 R, DAG.getConstant(ShiftAmt, MVT::i32));
8892
8893 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SHL)
8894 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8895 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
8896 R, DAG.getConstant(ShiftAmt, MVT::i32));
8897
8898 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SHL)
8899 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8900 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
8901 R, DAG.getConstant(ShiftAmt, MVT::i32));
8902
8903 if (VT == MVT::v2i64 && Op.getOpcode() == ISD::SRL)
8904 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8905 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8906 R, DAG.getConstant(ShiftAmt, MVT::i32));
8907
8908 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SRL)
8909 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8910 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
8911 R, DAG.getConstant(ShiftAmt, MVT::i32));
8912
8913 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SRL)
8914 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8915 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
8916 R, DAG.getConstant(ShiftAmt, MVT::i32));
8917
8918 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SRA)
8919 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8920 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
8921 R, DAG.getConstant(ShiftAmt, MVT::i32));
8922
8923 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SRA)
8924 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8925 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
8926 R, DAG.getConstant(ShiftAmt, MVT::i32));
8927 }
8928 }
8929
8930 // Lower SHL with variable shift amount.
8931 // Cannot lower SHL without SSE4.1 or later.
8932 if (!Subtarget->hasSSE41()) return SDValue();
8933
8934 if (VT == MVT::v4i32 && Op->getOpcode() == ISD::SHL) {
Nate Begeman51409212010-07-28 00:21:48 +00008935 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8936 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
8937 Op.getOperand(1), DAG.getConstant(23, MVT::i32));
8938
8939 ConstantInt *CI = ConstantInt::get(*Context, APInt(32, 0x3f800000U));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008940
Nate Begeman51409212010-07-28 00:21:48 +00008941 std::vector<Constant*> CV(4, CI);
8942 Constant *C = ConstantVector::get(CV);
8943 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8944 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008945 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00008946 false, false, 16);
8947
8948 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008949 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +00008950 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
8951 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
8952 }
Nadav Rotem43012222011-05-11 08:12:09 +00008953 if (VT == MVT::v16i8 && Op->getOpcode() == ISD::SHL) {
Nate Begeman51409212010-07-28 00:21:48 +00008954 // a = a << 5;
8955 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8956 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
8957 Op.getOperand(1), DAG.getConstant(5, MVT::i32));
8958
8959 ConstantInt *CM1 = ConstantInt::get(*Context, APInt(8, 15));
8960 ConstantInt *CM2 = ConstantInt::get(*Context, APInt(8, 63));
8961
8962 std::vector<Constant*> CVM1(16, CM1);
8963 std::vector<Constant*> CVM2(16, CM2);
8964 Constant *C = ConstantVector::get(CVM1);
8965 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8966 SDValue M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008967 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00008968 false, false, 16);
8969
8970 // r = pblendv(r, psllw(r & (char16)15, 4), a);
8971 M = DAG.getNode(ISD::AND, dl, VT, R, M);
8972 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8973 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
8974 DAG.getConstant(4, MVT::i32));
Nate Begeman672fb622010-12-20 22:04:24 +00008975 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT, R, M, Op);
Nate Begeman51409212010-07-28 00:21:48 +00008976 // a += a
8977 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008978
Nate Begeman51409212010-07-28 00:21:48 +00008979 C = ConstantVector::get(CVM2);
8980 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8981 M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008982 MachinePointerInfo::getConstantPool(),
Chris Lattner51abfe42010-09-21 06:02:19 +00008983 false, false, 16);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008984
Nate Begeman51409212010-07-28 00:21:48 +00008985 // r = pblendv(r, psllw(r & (char16)63, 2), a);
8986 M = DAG.getNode(ISD::AND, dl, VT, R, M);
8987 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8988 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
8989 DAG.getConstant(2, MVT::i32));
Nate Begeman672fb622010-12-20 22:04:24 +00008990 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT, R, M, Op);
Nate Begeman51409212010-07-28 00:21:48 +00008991 // a += a
8992 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008993
Nate Begeman51409212010-07-28 00:21:48 +00008994 // return pblendv(r, r+r, a);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008995 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT,
Nate Begeman51409212010-07-28 00:21:48 +00008996 R, DAG.getNode(ISD::ADD, dl, VT, R, R), Op);
8997 return R;
8998 }
8999 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00009000}
Mon P Wangaf9b9522008-12-18 21:42:19 +00009001
Dan Gohmand858e902010-04-17 15:26:15 +00009002SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
Bill Wendling74c37652008-12-09 22:08:41 +00009003 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
9004 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +00009005 // looks for this combo and may remove the "setcc" instruction if the "setcc"
9006 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +00009007 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +00009008 SDValue LHS = N->getOperand(0);
9009 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +00009010 unsigned BaseOp = 0;
9011 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +00009012 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +00009013 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00009014 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +00009015 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +00009016 // A subtract of one will be selected as a INC. Note that INC doesn't
9017 // set CF, so we can't do this for UADDO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +00009018 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
9019 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +00009020 BaseOp = X86ISD::INC;
9021 Cond = X86::COND_O;
9022 break;
9023 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00009024 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +00009025 Cond = X86::COND_O;
9026 break;
9027 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00009028 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +00009029 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00009030 break;
9031 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +00009032 // A subtract of one will be selected as a DEC. Note that DEC doesn't
9033 // set CF, so we can't do this for USUBO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +00009034 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
9035 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +00009036 BaseOp = X86ISD::DEC;
9037 Cond = X86::COND_O;
9038 break;
9039 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00009040 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +00009041 Cond = X86::COND_O;
9042 break;
9043 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00009044 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +00009045 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00009046 break;
9047 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00009048 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +00009049 Cond = X86::COND_O;
9050 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +00009051 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
9052 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
9053 MVT::i32);
9054 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009055
Chris Lattnerb20e0b12010-12-05 07:30:36 +00009056 SDValue SetCC =
9057 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
9058 DAG.getConstant(X86::COND_O, MVT::i32),
9059 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009060
Chris Lattnerb20e0b12010-12-05 07:30:36 +00009061 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
9062 return Sum;
9063 }
Bill Wendling74c37652008-12-09 22:08:41 +00009064 }
Bill Wendling3fafd932008-11-26 22:37:40 +00009065
Bill Wendling61edeb52008-12-02 01:06:39 +00009066 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00009067 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +00009068 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +00009069
Bill Wendling61edeb52008-12-02 01:06:39 +00009070 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +00009071 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
9072 DAG.getConstant(Cond, MVT::i32),
9073 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +00009074
Bill Wendling61edeb52008-12-02 01:06:39 +00009075 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
9076 return Sum;
Bill Wendling41ea7e72008-11-24 19:21:46 +00009077}
9078
Eric Christopher9a9d2752010-07-22 02:48:34 +00009079SDValue X86TargetLowering::LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG) const{
9080 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00009081
Eric Christopher77ed1352011-07-08 00:04:56 +00009082 // Go ahead and emit the fence on x86-64 even if we asked for no-sse2.
9083 // There isn't any reason to disable it if the target processor supports it.
9084 if (!Subtarget->hasSSE2() && !Subtarget->is64Bit()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +00009085 SDValue Chain = Op.getOperand(0);
Eric Christopher77ed1352011-07-08 00:04:56 +00009086 SDValue Zero = DAG.getConstant(0, MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +00009087 SDValue Ops[] = {
9088 DAG.getRegister(X86::ESP, MVT::i32), // Base
9089 DAG.getTargetConstant(1, MVT::i8), // Scale
9090 DAG.getRegister(0, MVT::i32), // Index
9091 DAG.getTargetConstant(0, MVT::i32), // Disp
9092 DAG.getRegister(0, MVT::i32), // Segment.
9093 Zero,
9094 Chain
9095 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00009096 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +00009097 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
9098 array_lengthof(Ops));
9099 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +00009100 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00009101
Eric Christopher9a9d2752010-07-22 02:48:34 +00009102 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +00009103 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +00009104 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009105
Chris Lattner132929a2010-08-14 17:26:09 +00009106 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
9107 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
9108 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
9109 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +00009110
Chris Lattner132929a2010-08-14 17:26:09 +00009111 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
9112 if (!Op1 && !Op2 && !Op3 && Op4)
9113 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009114
Chris Lattner132929a2010-08-14 17:26:09 +00009115 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
9116 if (Op1 && !Op2 && !Op3 && !Op4)
9117 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009118
9119 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +00009120 // (MFENCE)>;
9121 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +00009122}
9123
Dan Gohmand858e902010-04-17 15:26:15 +00009124SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009125 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +00009126 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +00009127 unsigned Reg = 0;
9128 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00009129 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00009130 default:
9131 assert(false && "Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +00009132 case MVT::i8: Reg = X86::AL; size = 1; break;
9133 case MVT::i16: Reg = X86::AX; size = 2; break;
9134 case MVT::i32: Reg = X86::EAX; size = 4; break;
9135 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +00009136 assert(Subtarget->is64Bit() && "Node not type legal!");
9137 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00009138 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00009139 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +00009140 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +00009141 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +00009142 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +00009143 Op.getOperand(1),
9144 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +00009145 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +00009146 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009147 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +00009148 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
9149 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
9150 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +00009151 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +00009152 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +00009153 return cpOut;
9154}
9155
Duncan Sands1607f052008-12-01 11:39:25 +00009156SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00009157 SelectionDAG &DAG) const {
Duncan Sands1607f052008-12-01 11:39:25 +00009158 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009159 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +00009160 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009161 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +00009162 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00009163 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
9164 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +00009165 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +00009166 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
9167 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +00009168 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +00009169 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +00009170 rdx.getValue(1)
9171 };
Dale Johannesene4d209d2009-02-03 20:21:25 +00009172 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009173}
9174
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009175SDValue X86TargetLowering::LowerBITCAST(SDValue Op,
Dale Johannesen7d07b482010-05-21 00:52:33 +00009176 SelectionDAG &DAG) const {
9177 EVT SrcVT = Op.getOperand(0).getValueType();
9178 EVT DstVT = Op.getValueType();
Chris Lattner2a786eb2010-12-19 20:19:20 +00009179 assert(Subtarget->is64Bit() && !Subtarget->hasSSE2() &&
9180 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +00009181 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +00009182 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009183 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +00009184 // i64 <=> MMX conversions are Legal.
9185 if (SrcVT==MVT::i64 && DstVT.isVector())
9186 return Op;
9187 if (DstVT==MVT::i64 && SrcVT.isVector())
9188 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +00009189 // MMX <=> MMX conversions are Legal.
9190 if (SrcVT.isVector() && DstVT.isVector())
9191 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +00009192 // All other conversions need to be expanded.
9193 return SDValue();
9194}
Chris Lattner5b856542010-12-20 00:59:46 +00009195
Dan Gohmand858e902010-04-17 15:26:15 +00009196SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen71d1bf52008-09-29 22:25:26 +00009197 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +00009198 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00009199 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009200 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +00009201 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009202 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009203 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +00009204 Node->getOperand(0),
9205 Node->getOperand(1), negOp,
9206 cast<AtomicSDNode>(Node)->getSrcValue(),
9207 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang63307c32008-05-05 19:05:59 +00009208}
9209
Chris Lattner5b856542010-12-20 00:59:46 +00009210static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
9211 EVT VT = Op.getNode()->getValueType(0);
9212
9213 // Let legalize expand this if it isn't a legal type yet.
9214 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
9215 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009216
Chris Lattner5b856542010-12-20 00:59:46 +00009217 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009218
Chris Lattner5b856542010-12-20 00:59:46 +00009219 unsigned Opc;
9220 bool ExtraOp = false;
9221 switch (Op.getOpcode()) {
9222 default: assert(0 && "Invalid code");
9223 case ISD::ADDC: Opc = X86ISD::ADD; break;
9224 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
9225 case ISD::SUBC: Opc = X86ISD::SUB; break;
9226 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
9227 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009228
Chris Lattner5b856542010-12-20 00:59:46 +00009229 if (!ExtraOp)
9230 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
9231 Op.getOperand(1));
9232 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
9233 Op.getOperand(1), Op.getOperand(2));
9234}
9235
Evan Cheng0db9fe62006-04-25 20:13:52 +00009236/// LowerOperation - Provide custom lowering hooks for some operations.
9237///
Dan Gohmand858e902010-04-17 15:26:15 +00009238SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00009239 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00009240 default: llvm_unreachable("Should not custom lower this!");
Eric Christopher9a9d2752010-07-22 02:48:34 +00009241 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op,DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009242 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
9243 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009244 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00009245 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009246 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
9247 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
9248 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
David Greene91585092011-01-26 15:38:49 +00009249 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
David Greenecfe33c42011-01-26 19:13:22 +00009250 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009251 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
9252 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
9253 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00009254 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +00009255 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +00009256 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009257 case ISD::SHL_PARTS:
9258 case ISD::SRA_PARTS:
Nadav Rotem43012222011-05-11 08:12:09 +00009259 case ISD::SRL_PARTS: return LowerShiftParts(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009260 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00009261 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009262 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00009263 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009264 case ISD::FABS: return LowerFABS(Op, DAG);
9265 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00009266 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00009267 case ISD::FGETSIGN: return LowerFGETSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00009268 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman30a0de92008-07-17 16:51:19 +00009269 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00009270 case ISD::SELECT: return LowerSELECT(Op, DAG);
9271 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009272 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009273 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +00009274 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +00009275 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009276 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00009277 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
9278 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009279 case ISD::FRAME_TO_ARGS_OFFSET:
9280 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009281 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009282 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009283 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00009284 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +00009285 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
9286 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Mon P Wangaf9b9522008-12-18 21:42:19 +00009287 case ISD::MUL: return LowerMUL_V2I64(Op, DAG);
Nadav Rotem43012222011-05-11 08:12:09 +00009288 case ISD::SRA:
9289 case ISD::SRL:
9290 case ISD::SHL: return LowerShift(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +00009291 case ISD::SADDO:
9292 case ISD::UADDO:
9293 case ISD::SSUBO:
9294 case ISD::USUBO:
9295 case ISD::SMULO:
9296 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00009297 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009298 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +00009299 case ISD::ADDC:
9300 case ISD::ADDE:
9301 case ISD::SUBC:
9302 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009303 }
Chris Lattner27a6c732007-11-24 07:07:01 +00009304}
9305
Duncan Sands1607f052008-12-01 11:39:25 +00009306void X86TargetLowering::
9307ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00009308 SelectionDAG &DAG, unsigned NewOp) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009309 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009310 DebugLoc dl = Node->getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00009311 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +00009312
9313 SDValue Chain = Node->getOperand(0);
9314 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00009315 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00009316 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00009317 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00009318 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +00009319 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +00009320 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +00009321 SDValue Result =
9322 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
9323 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +00009324 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +00009325 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00009326 Results.push_back(Result.getValue(2));
9327}
9328
Duncan Sands126d9072008-07-04 11:47:58 +00009329/// ReplaceNodeResults - Replace a node with an illegal result type
9330/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00009331void X86TargetLowering::ReplaceNodeResults(SDNode *N,
9332 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00009333 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +00009334 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +00009335 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +00009336 default:
Duncan Sands1607f052008-12-01 11:39:25 +00009337 assert(false && "Do not know how to custom type legalize this operation!");
9338 return;
Chris Lattner5b856542010-12-20 00:59:46 +00009339 case ISD::ADDC:
9340 case ISD::ADDE:
9341 case ISD::SUBC:
9342 case ISD::SUBE:
9343 // We don't want to expand or promote these.
9344 return;
Duncan Sands1607f052008-12-01 11:39:25 +00009345 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +00009346 std::pair<SDValue,SDValue> Vals =
9347 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +00009348 SDValue FIST = Vals.first, StackSlot = Vals.second;
9349 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +00009350 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +00009351 // Return a load from the stack slot.
Chris Lattner51abfe42010-09-21 06:02:19 +00009352 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
9353 MachinePointerInfo(), false, false, 0));
Duncan Sands1607f052008-12-01 11:39:25 +00009354 }
9355 return;
9356 }
9357 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009358 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +00009359 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009360 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00009361 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +00009362 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00009363 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00009364 eax.getValue(2));
9365 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
9366 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +00009367 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00009368 Results.push_back(edx.getValue(1));
9369 return;
9370 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009371 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +00009372 EVT T = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009373 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Duncan Sands1607f052008-12-01 11:39:25 +00009374 SDValue cpInL, cpInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00009375 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
9376 DAG.getConstant(0, MVT::i32));
9377 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
9378 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00009379 cpInL = DAG.getCopyToReg(N->getOperand(0), dl, X86::EAX, cpInL, SDValue());
9380 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl, X86::EDX, cpInH,
Duncan Sands1607f052008-12-01 11:39:25 +00009381 cpInL.getValue(1));
9382 SDValue swapInL, swapInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00009383 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
9384 DAG.getConstant(0, MVT::i32));
9385 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
9386 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00009387 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl, X86::EBX, swapInL,
Duncan Sands1607f052008-12-01 11:39:25 +00009388 cpInH.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00009389 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl, X86::ECX, swapInH,
Duncan Sands1607f052008-12-01 11:39:25 +00009390 swapInL.getValue(1));
9391 SDValue Ops[] = { swapInH.getValue(0),
9392 N->getOperand(1),
9393 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009394 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +00009395 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
9396 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG8_DAG, dl, Tys,
9397 Ops, 3, T, MMO);
Dale Johannesendd64c412009-02-04 00:33:20 +00009398 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl, X86::EAX,
Owen Anderson825b72b2009-08-11 20:47:22 +00009399 MVT::i32, Result.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00009400 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl, X86::EDX,
Owen Anderson825b72b2009-08-11 20:47:22 +00009401 MVT::i32, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +00009402 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Owen Anderson825b72b2009-08-11 20:47:22 +00009403 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00009404 Results.push_back(cpOutH.getValue(1));
9405 return;
9406 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009407 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +00009408 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
9409 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009410 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +00009411 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
9412 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009413 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +00009414 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
9415 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009416 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +00009417 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
9418 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009419 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +00009420 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
9421 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009422 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +00009423 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
9424 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009425 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +00009426 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
9427 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00009428 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00009429}
9430
Evan Cheng72261582005-12-20 06:22:03 +00009431const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
9432 switch (Opcode) {
9433 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +00009434 case X86ISD::BSF: return "X86ISD::BSF";
9435 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +00009436 case X86ISD::SHLD: return "X86ISD::SHLD";
9437 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00009438 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00009439 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00009440 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00009441 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00009442 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00009443 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00009444 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
9445 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
9446 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00009447 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00009448 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +00009449 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +00009450 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +00009451 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +00009452 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00009453 case X86ISD::COMI: return "X86ISD::COMI";
9454 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00009455 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +00009456 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Stuart Hastings865f0932011-06-03 23:53:54 +00009457 case X86ISD::FSETCCsd: return "X86ISD::FSETCCsd";
9458 case X86ISD::FSETCCss: return "X86ISD::FSETCCss";
Evan Cheng72261582005-12-20 06:22:03 +00009459 case X86ISD::CMOV: return "X86ISD::CMOV";
9460 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00009461 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00009462 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
9463 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +00009464 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00009465 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +00009466 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +00009467 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +00009468 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +00009469 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
9470 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +00009471 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +00009472 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +00009473 case X86ISD::ANDNP: return "X86ISD::ANDNP";
Nate Begemanb65c1752010-12-17 22:55:37 +00009474 case X86ISD::PSIGNB: return "X86ISD::PSIGNB";
9475 case X86ISD::PSIGNW: return "X86ISD::PSIGNW";
9476 case X86ISD::PSIGND: return "X86ISD::PSIGND";
Nate Begeman672fb622010-12-20 22:04:24 +00009477 case X86ISD::PBLENDVB: return "X86ISD::PBLENDVB";
Evan Cheng8ca29322006-11-10 21:43:37 +00009478 case X86ISD::FMAX: return "X86ISD::FMAX";
9479 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +00009480 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
9481 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00009482 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +00009483 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009484 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00009485 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009486 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +00009487 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
9488 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009489 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
9490 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
9491 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
9492 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
9493 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
9494 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +00009495 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
9496 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +00009497 case X86ISD::VSHL: return "X86ISD::VSHL";
9498 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +00009499 case X86ISD::CMPPD: return "X86ISD::CMPPD";
9500 case X86ISD::CMPPS: return "X86ISD::CMPPS";
9501 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
9502 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
9503 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
9504 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
9505 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
9506 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
9507 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
9508 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +00009509 case X86ISD::ADD: return "X86ISD::ADD";
9510 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +00009511 case X86ISD::ADC: return "X86ISD::ADC";
9512 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +00009513 case X86ISD::SMUL: return "X86ISD::SMUL";
9514 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +00009515 case X86ISD::INC: return "X86ISD::INC";
9516 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +00009517 case X86ISD::OR: return "X86ISD::OR";
9518 case X86ISD::XOR: return "X86ISD::XOR";
9519 case X86ISD::AND: return "X86ISD::AND";
Evan Cheng73f24c92009-03-30 21:36:47 +00009520 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +00009521 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009522 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009523 case X86ISD::PALIGN: return "X86ISD::PALIGN";
9524 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
9525 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
9526 case X86ISD::PSHUFHW_LD: return "X86ISD::PSHUFHW_LD";
9527 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
9528 case X86ISD::PSHUFLW_LD: return "X86ISD::PSHUFLW_LD";
9529 case X86ISD::SHUFPS: return "X86ISD::SHUFPS";
9530 case X86ISD::SHUFPD: return "X86ISD::SHUFPD";
9531 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009532 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00009533 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009534 case X86ISD::MOVHLPD: return "X86ISD::MOVHLPD";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00009535 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
9536 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009537 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
9538 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
9539 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
9540 case X86ISD::MOVSHDUP_LD: return "X86ISD::MOVSHDUP_LD";
9541 case X86ISD::MOVSLDUP_LD: return "X86ISD::MOVSLDUP_LD";
9542 case X86ISD::MOVSD: return "X86ISD::MOVSD";
9543 case X86ISD::MOVSS: return "X86ISD::MOVSS";
9544 case X86ISD::UNPCKLPS: return "X86ISD::UNPCKLPS";
9545 case X86ISD::UNPCKLPD: return "X86ISD::UNPCKLPD";
David Greenefbf05d32011-02-22 23:31:46 +00009546 case X86ISD::VUNPCKLPS: return "X86ISD::VUNPCKLPS";
9547 case X86ISD::VUNPCKLPD: return "X86ISD::VUNPCKLPD";
9548 case X86ISD::VUNPCKLPSY: return "X86ISD::VUNPCKLPSY";
9549 case X86ISD::VUNPCKLPDY: return "X86ISD::VUNPCKLPDY";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009550 case X86ISD::UNPCKHPS: return "X86ISD::UNPCKHPS";
9551 case X86ISD::UNPCKHPD: return "X86ISD::UNPCKHPD";
9552 case X86ISD::PUNPCKLBW: return "X86ISD::PUNPCKLBW";
9553 case X86ISD::PUNPCKLWD: return "X86ISD::PUNPCKLWD";
9554 case X86ISD::PUNPCKLDQ: return "X86ISD::PUNPCKLDQ";
9555 case X86ISD::PUNPCKLQDQ: return "X86ISD::PUNPCKLQDQ";
9556 case X86ISD::PUNPCKHBW: return "X86ISD::PUNPCKHBW";
9557 case X86ISD::PUNPCKHWD: return "X86ISD::PUNPCKHWD";
9558 case X86ISD::PUNPCKHDQ: return "X86ISD::PUNPCKHDQ";
9559 case X86ISD::PUNPCKHQDQ: return "X86ISD::PUNPCKHQDQ";
Dan Gohmand6708ea2009-08-15 01:38:56 +00009560 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +00009561 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +00009562 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Evan Cheng72261582005-12-20 06:22:03 +00009563 }
9564}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00009565
Chris Lattnerc9addb72007-03-30 23:15:24 +00009566// isLegalAddressingMode - Return true if the addressing mode represented
9567// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +00009568bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerc9addb72007-03-30 23:15:24 +00009569 const Type *Ty) const {
9570 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009571 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +00009572 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +00009573
Chris Lattnerc9addb72007-03-30 23:15:24 +00009574 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009575 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +00009576 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00009577
Chris Lattnerc9addb72007-03-30 23:15:24 +00009578 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +00009579 unsigned GVFlags =
9580 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009581
Chris Lattnerdfed4132009-07-10 07:38:24 +00009582 // If a reference to this global requires an extra load, we can't fold it.
9583 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +00009584 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009585
Chris Lattnerdfed4132009-07-10 07:38:24 +00009586 // If BaseGV requires a register for the PIC base, we cannot also have a
9587 // BaseReg specified.
9588 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +00009589 return false;
Evan Cheng52787842007-08-01 23:46:47 +00009590
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009591 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +00009592 if ((M != CodeModel::Small || R != Reloc::Static) &&
9593 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009594 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +00009595 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009596
Chris Lattnerc9addb72007-03-30 23:15:24 +00009597 switch (AM.Scale) {
9598 case 0:
9599 case 1:
9600 case 2:
9601 case 4:
9602 case 8:
9603 // These scales always work.
9604 break;
9605 case 3:
9606 case 5:
9607 case 9:
9608 // These scales are formed with basereg+scalereg. Only accept if there is
9609 // no basereg yet.
9610 if (AM.HasBaseReg)
9611 return false;
9612 break;
9613 default: // Other stuff never works.
9614 return false;
9615 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009616
Chris Lattnerc9addb72007-03-30 23:15:24 +00009617 return true;
9618}
9619
9620
Evan Cheng2bd122c2007-10-26 01:56:11 +00009621bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009622 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +00009623 return false;
Evan Chenge127a732007-10-29 07:57:50 +00009624 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
9625 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00009626 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +00009627 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00009628 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +00009629}
9630
Owen Andersone50ed302009-08-10 22:56:29 +00009631bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +00009632 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009633 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009634 unsigned NumBits1 = VT1.getSizeInBits();
9635 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00009636 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009637 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00009638 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009639}
Evan Cheng2bd122c2007-10-26 01:56:11 +00009640
Dan Gohman97121ba2009-04-08 00:15:30 +00009641bool X86TargetLowering::isZExtFree(const Type *Ty1, const Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00009642 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009643 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00009644}
9645
Owen Andersone50ed302009-08-10 22:56:29 +00009646bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00009647 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00009648 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00009649}
9650
Owen Andersone50ed302009-08-10 22:56:29 +00009651bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +00009652 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +00009653 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +00009654}
9655
Evan Cheng60c07e12006-07-05 22:17:51 +00009656/// isShuffleMaskLegal - Targets can use this to indicate that they only
9657/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
9658/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
9659/// are assumed to be legal.
9660bool
Eric Christopherfd179292009-08-27 18:07:15 +00009661X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +00009662 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +00009663 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +00009664 if (VT.getSizeInBits() == 64)
Eric Christophercff6f852010-04-15 01:40:20 +00009665 return isPALIGNRMask(M, VT, Subtarget->hasSSSE3());
Nate Begeman9008ca62009-04-27 18:41:29 +00009666
Nate Begemana09008b2009-10-19 02:17:23 +00009667 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +00009668 return (VT.getVectorNumElements() == 2 ||
9669 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
9670 isMOVLMask(M, VT) ||
9671 isSHUFPMask(M, VT) ||
9672 isPSHUFDMask(M, VT) ||
9673 isPSHUFHWMask(M, VT) ||
9674 isPSHUFLWMask(M, VT) ||
Nate Begemana09008b2009-10-19 02:17:23 +00009675 isPALIGNRMask(M, VT, Subtarget->hasSSSE3()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +00009676 isUNPCKLMask(M, VT) ||
9677 isUNPCKHMask(M, VT) ||
9678 isUNPCKL_v_undef_Mask(M, VT) ||
9679 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00009680}
9681
Dan Gohman7d8143f2008-04-09 20:09:42 +00009682bool
Nate Begeman5a5ca152009-04-29 05:20:52 +00009683X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +00009684 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00009685 unsigned NumElts = VT.getVectorNumElements();
9686 // FIXME: This collection of masks seems suspect.
9687 if (NumElts == 2)
9688 return true;
9689 if (NumElts == 4 && VT.getSizeInBits() == 128) {
9690 return (isMOVLMask(Mask, VT) ||
9691 isCommutedMOVLMask(Mask, VT, true) ||
9692 isSHUFPMask(Mask, VT) ||
9693 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00009694 }
9695 return false;
9696}
9697
9698//===----------------------------------------------------------------------===//
9699// X86 Scheduler Hooks
9700//===----------------------------------------------------------------------===//
9701
Mon P Wang63307c32008-05-05 19:05:59 +00009702// private utility function
9703MachineBasicBlock *
9704X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
9705 MachineBasicBlock *MBB,
9706 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009707 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009708 unsigned LoadOpc,
9709 unsigned CXchgOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009710 unsigned notOpc,
9711 unsigned EAXreg,
9712 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009713 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00009714 // For the atomic bitwise operator, we generate
9715 // thisMBB:
9716 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00009717 // ld t1 = [bitinstr.addr]
9718 // op t2 = t1, [bitinstr.val]
9719 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00009720 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
9721 // bz newMBB
9722 // fallthrough -->nextMBB
9723 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9724 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009725 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00009726 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009727
Mon P Wang63307c32008-05-05 19:05:59 +00009728 /// First build the CFG
9729 MachineFunction *F = MBB->getParent();
9730 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009731 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9732 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9733 F->insert(MBBIter, newMBB);
9734 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009735
Dan Gohman14152b42010-07-06 20:24:04 +00009736 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9737 nextMBB->splice(nextMBB->begin(), thisMBB,
9738 llvm::next(MachineBasicBlock::iterator(bInstr)),
9739 thisMBB->end());
9740 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009741
Mon P Wang63307c32008-05-05 19:05:59 +00009742 // Update thisMBB to fall through to newMBB
9743 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009744
Mon P Wang63307c32008-05-05 19:05:59 +00009745 // newMBB jumps to itself and fall through to nextMBB
9746 newMBB->addSuccessor(nextMBB);
9747 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009748
Mon P Wang63307c32008-05-05 19:05:59 +00009749 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009750 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009751 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +00009752 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00009753 MachineOperand& destOper = bInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009754 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00009755 int numArgs = bInstr->getNumOperands() - 1;
9756 for (int i=0; i < numArgs; ++i)
9757 argOpers[i] = &bInstr->getOperand(i+1);
9758
9759 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009760 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009761 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00009762
Dale Johannesen140be2d2008-08-19 18:47:28 +00009763 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009764 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00009765 for (int i=0; i <= lastAddrIndx; ++i)
9766 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009767
Dale Johannesen140be2d2008-08-19 18:47:28 +00009768 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009769 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00009770 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009771 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009772 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009773 tt = t1;
9774
Dale Johannesen140be2d2008-08-19 18:47:28 +00009775 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +00009776 assert((argOpers[valArgIndx]->isReg() ||
9777 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00009778 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +00009779 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009780 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00009781 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009782 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009783 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +00009784 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009785
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009786 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +00009787 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009788
Dale Johannesene4d209d2009-02-03 20:21:25 +00009789 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +00009790 for (int i=0; i <= lastAddrIndx; ++i)
9791 (*MIB).addOperand(*argOpers[i]);
9792 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +00009793 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009794 (*MIB).setMemRefs(bInstr->memoperands_begin(),
9795 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +00009796
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009797 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +00009798 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +00009799
Mon P Wang63307c32008-05-05 19:05:59 +00009800 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009801 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00009802
Dan Gohman14152b42010-07-06 20:24:04 +00009803 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00009804 return nextMBB;
9805}
9806
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00009807// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +00009808MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009809X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
9810 MachineBasicBlock *MBB,
9811 unsigned regOpcL,
9812 unsigned regOpcH,
9813 unsigned immOpcL,
9814 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009815 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009816 // For the atomic bitwise operator, we generate
9817 // thisMBB (instructions are in pairs, except cmpxchg8b)
9818 // ld t1,t2 = [bitinstr.addr]
9819 // newMBB:
9820 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
9821 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +00009822 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009823 // mov ECX, EBX <- t5, t6
9824 // mov EAX, EDX <- t1, t2
9825 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
9826 // mov t3, t4 <- EAX, EDX
9827 // bz newMBB
9828 // result in out1, out2
9829 // fallthrough -->nextMBB
9830
9831 const TargetRegisterClass *RC = X86::GR32RegisterClass;
9832 const unsigned LoadOpc = X86::MOV32rm;
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009833 const unsigned NotOpc = X86::NOT32r;
9834 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9835 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
9836 MachineFunction::iterator MBBIter = MBB;
9837 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009838
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009839 /// First build the CFG
9840 MachineFunction *F = MBB->getParent();
9841 MachineBasicBlock *thisMBB = MBB;
9842 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9843 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9844 F->insert(MBBIter, newMBB);
9845 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009846
Dan Gohman14152b42010-07-06 20:24:04 +00009847 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9848 nextMBB->splice(nextMBB->begin(), thisMBB,
9849 llvm::next(MachineBasicBlock::iterator(bInstr)),
9850 thisMBB->end());
9851 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009852
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009853 // Update thisMBB to fall through to newMBB
9854 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009855
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009856 // newMBB jumps to itself and fall through to nextMBB
9857 newMBB->addSuccessor(nextMBB);
9858 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009859
Dale Johannesene4d209d2009-02-03 20:21:25 +00009860 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009861 // Insert instructions into newMBB based on incoming instruction
9862 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009863 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009864 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009865 MachineOperand& dest1Oper = bInstr->getOperand(0);
9866 MachineOperand& dest2Oper = bInstr->getOperand(1);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009867 MachineOperand* argOpers[2 + X86::AddrNumOperands];
9868 for (int i=0; i < 2 + X86::AddrNumOperands; ++i) {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009869 argOpers[i] = &bInstr->getOperand(i+2);
9870
Dan Gohman71ea4e52010-05-14 21:01:44 +00009871 // We use some of the operands multiple times, so conservatively just
9872 // clear any kill flags that might be present.
9873 if (argOpers[i]->isReg() && argOpers[i]->isUse())
9874 argOpers[i]->setIsKill(false);
9875 }
9876
Evan Chengad5b52f2010-01-08 19:14:57 +00009877 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009878 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +00009879
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009880 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009881 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009882 for (int i=0; i <= lastAddrIndx; ++i)
9883 (*MIB).addOperand(*argOpers[i]);
9884 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009885 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +00009886 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +00009887 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009888 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +00009889 MachineOperand newOp3 = *(argOpers[3]);
9890 if (newOp3.isImm())
9891 newOp3.setImm(newOp3.getImm()+4);
9892 else
9893 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009894 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +00009895 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009896
9897 // t3/4 are defined later, at the bottom of the loop
9898 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
9899 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009900 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009901 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009902 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009903 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
9904
Evan Cheng306b4ca2010-01-08 23:41:50 +00009905 // The subsequent operations should be using the destination registers of
9906 //the PHI instructions.
Scott Michelfdc40a02009-02-17 22:15:04 +00009907 if (invSrc) {
Evan Cheng306b4ca2010-01-08 23:41:50 +00009908 t1 = F->getRegInfo().createVirtualRegister(RC);
9909 t2 = F->getRegInfo().createVirtualRegister(RC);
9910 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
9911 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009912 } else {
Evan Cheng306b4ca2010-01-08 23:41:50 +00009913 t1 = dest1Oper.getReg();
9914 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009915 }
9916
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009917 int valArgIndx = lastAddrIndx + 1;
9918 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +00009919 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009920 "invalid operand");
9921 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
9922 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009923 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009924 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009925 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009926 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +00009927 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00009928 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009929 (*MIB).addOperand(*argOpers[valArgIndx]);
9930 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00009931 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009932 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00009933 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009934 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009935 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009936 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009937 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +00009938 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00009939 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009940 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009941
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009942 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009943 MIB.addReg(t1);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009944 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009945 MIB.addReg(t2);
9946
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009947 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009948 MIB.addReg(t5);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009949 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009950 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +00009951
Dale Johannesene4d209d2009-02-03 20:21:25 +00009952 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009953 for (int i=0; i <= lastAddrIndx; ++i)
9954 (*MIB).addOperand(*argOpers[i]);
9955
9956 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009957 (*MIB).setMemRefs(bInstr->memoperands_begin(),
9958 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009959
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009960 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009961 MIB.addReg(X86::EAX);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009962 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009963 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +00009964
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009965 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009966 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009967
Dan Gohman14152b42010-07-06 20:24:04 +00009968 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009969 return nextMBB;
9970}
9971
9972// private utility function
9973MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +00009974X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
9975 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009976 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00009977 // For the atomic min/max operator, we generate
9978 // thisMBB:
9979 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00009980 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +00009981 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +00009982 // cmp t1, t2
9983 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +00009984 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00009985 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
9986 // bz newMBB
9987 // fallthrough -->nextMBB
9988 //
9989 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9990 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009991 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00009992 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009993
Mon P Wang63307c32008-05-05 19:05:59 +00009994 /// First build the CFG
9995 MachineFunction *F = MBB->getParent();
9996 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009997 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9998 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9999 F->insert(MBBIter, newMBB);
10000 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010001
Dan Gohman14152b42010-07-06 20:24:04 +000010002 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
10003 nextMBB->splice(nextMBB->begin(), thisMBB,
10004 llvm::next(MachineBasicBlock::iterator(mInstr)),
10005 thisMBB->end());
10006 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010007
Mon P Wang63307c32008-05-05 19:05:59 +000010008 // Update thisMBB to fall through to newMBB
10009 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010010
Mon P Wang63307c32008-05-05 19:05:59 +000010011 // newMBB jumps to newMBB and fall through to nextMBB
10012 newMBB->addSuccessor(nextMBB);
10013 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010014
Dale Johannesene4d209d2009-02-03 20:21:25 +000010015 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +000010016 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010017 assert(mInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000010018 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +000010019 MachineOperand& destOper = mInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010020 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +000010021 int numArgs = mInstr->getNumOperands() - 1;
10022 for (int i=0; i < numArgs; ++i)
10023 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +000010024
Mon P Wang63307c32008-05-05 19:05:59 +000010025 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010026 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000010027 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +000010028
Mon P Wangab3e7472008-05-05 22:56:23 +000010029 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010030 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +000010031 for (int i=0; i <= lastAddrIndx; ++i)
10032 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +000010033
Mon P Wang63307c32008-05-05 19:05:59 +000010034 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +000010035 assert((argOpers[valArgIndx]->isReg() ||
10036 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +000010037 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +000010038
10039 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +000010040 if (argOpers[valArgIndx]->isReg())
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010041 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +000010042 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000010043 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +000010044 (*MIB).addOperand(*argOpers[valArgIndx]);
10045
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010046 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +000010047 MIB.addReg(t1);
10048
Dale Johannesene4d209d2009-02-03 20:21:25 +000010049 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +000010050 MIB.addReg(t1);
10051 MIB.addReg(t2);
10052
10053 // Generate movc
10054 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010055 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +000010056 MIB.addReg(t2);
10057 MIB.addReg(t1);
10058
10059 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +000010060 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +000010061 for (int i=0; i <= lastAddrIndx; ++i)
10062 (*MIB).addOperand(*argOpers[i]);
10063 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +000010064 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000010065 (*MIB).setMemRefs(mInstr->memoperands_begin(),
10066 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +000010067
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010068 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +000010069 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +000010070
Mon P Wang63307c32008-05-05 19:05:59 +000010071 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000010072 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000010073
Dan Gohman14152b42010-07-06 20:24:04 +000010074 mInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +000010075 return nextMBB;
10076}
10077
Eric Christopherf83a5de2009-08-27 18:08:16 +000010078// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010079// or XMM0_V32I8 in AVX all of this code can be replaced with that
10080// in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +000010081MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +000010082X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +000010083 unsigned numArgs, bool memArg) const {
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010084 assert((Subtarget->hasSSE42() || Subtarget->hasAVX()) &&
10085 "Target must have SSE4.2 or AVX features enabled");
10086
Eric Christopherb120ab42009-08-18 22:50:32 +000010087 DebugLoc dl = MI->getDebugLoc();
10088 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Eric Christopherb120ab42009-08-18 22:50:32 +000010089 unsigned Opc;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010090 if (!Subtarget->hasAVX()) {
10091 if (memArg)
10092 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
10093 else
10094 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
10095 } else {
10096 if (memArg)
10097 Opc = numArgs == 3 ? X86::VPCMPISTRM128rm : X86::VPCMPESTRM128rm;
10098 else
10099 Opc = numArgs == 3 ? X86::VPCMPISTRM128rr : X86::VPCMPESTRM128rr;
10100 }
Eric Christopherb120ab42009-08-18 22:50:32 +000010101
Eric Christopher41c902f2010-11-30 08:20:21 +000010102 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Eric Christopherb120ab42009-08-18 22:50:32 +000010103 for (unsigned i = 0; i < numArgs; ++i) {
10104 MachineOperand &Op = MI->getOperand(i+1);
Eric Christopherb120ab42009-08-18 22:50:32 +000010105 if (!(Op.isReg() && Op.isImplicit()))
10106 MIB.addOperand(Op);
10107 }
Eric Christopher41c902f2010-11-30 08:20:21 +000010108 BuildMI(*BB, MI, dl, TII->get(X86::MOVAPSrr), MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +000010109 .addReg(X86::XMM0);
10110
Dan Gohman14152b42010-07-06 20:24:04 +000010111 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +000010112 return BB;
10113}
10114
10115MachineBasicBlock *
Eric Christopher228232b2010-11-30 07:20:12 +000010116X86TargetLowering::EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000010117 DebugLoc dl = MI->getDebugLoc();
10118 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010119
Eric Christopher228232b2010-11-30 07:20:12 +000010120 // Address into RAX/EAX, other two args into ECX, EDX.
10121 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
10122 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
10123 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
10124 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +000010125 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010126
Eric Christopher228232b2010-11-30 07:20:12 +000010127 unsigned ValOps = X86::AddrNumOperands;
10128 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
10129 .addReg(MI->getOperand(ValOps).getReg());
10130 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
10131 .addReg(MI->getOperand(ValOps+1).getReg());
10132
10133 // The instruction doesn't actually take any operands though.
10134 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010135
Eric Christopher228232b2010-11-30 07:20:12 +000010136 MI->eraseFromParent(); // The pseudo is gone now.
10137 return BB;
10138}
10139
10140MachineBasicBlock *
10141X86TargetLowering::EmitMwait(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000010142 DebugLoc dl = MI->getDebugLoc();
10143 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010144
Eric Christopher228232b2010-11-30 07:20:12 +000010145 // First arg in ECX, the second in EAX.
10146 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
10147 .addReg(MI->getOperand(0).getReg());
10148 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EAX)
10149 .addReg(MI->getOperand(1).getReg());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010150
Eric Christopher228232b2010-11-30 07:20:12 +000010151 // The instruction doesn't actually take any operands though.
10152 BuildMI(*BB, MI, dl, TII->get(X86::MWAITrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010153
Eric Christopher228232b2010-11-30 07:20:12 +000010154 MI->eraseFromParent(); // The pseudo is gone now.
10155 return BB;
10156}
10157
10158MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +000010159X86TargetLowering::EmitVAARG64WithCustomInserter(
10160 MachineInstr *MI,
10161 MachineBasicBlock *MBB) const {
10162 // Emit va_arg instruction on X86-64.
10163
10164 // Operands to this pseudo-instruction:
10165 // 0 ) Output : destination address (reg)
10166 // 1-5) Input : va_list address (addr, i64mem)
10167 // 6 ) ArgSize : Size (in bytes) of vararg type
10168 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
10169 // 8 ) Align : Alignment of type
10170 // 9 ) EFLAGS (implicit-def)
10171
10172 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
10173 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
10174
10175 unsigned DestReg = MI->getOperand(0).getReg();
10176 MachineOperand &Base = MI->getOperand(1);
10177 MachineOperand &Scale = MI->getOperand(2);
10178 MachineOperand &Index = MI->getOperand(3);
10179 MachineOperand &Disp = MI->getOperand(4);
10180 MachineOperand &Segment = MI->getOperand(5);
10181 unsigned ArgSize = MI->getOperand(6).getImm();
10182 unsigned ArgMode = MI->getOperand(7).getImm();
10183 unsigned Align = MI->getOperand(8).getImm();
10184
10185 // Memory Reference
10186 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
10187 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
10188 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
10189
10190 // Machine Information
10191 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10192 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
10193 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
10194 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
10195 DebugLoc DL = MI->getDebugLoc();
10196
10197 // struct va_list {
10198 // i32 gp_offset
10199 // i32 fp_offset
10200 // i64 overflow_area (address)
10201 // i64 reg_save_area (address)
10202 // }
10203 // sizeof(va_list) = 24
10204 // alignment(va_list) = 8
10205
10206 unsigned TotalNumIntRegs = 6;
10207 unsigned TotalNumXMMRegs = 8;
10208 bool UseGPOffset = (ArgMode == 1);
10209 bool UseFPOffset = (ArgMode == 2);
10210 unsigned MaxOffset = TotalNumIntRegs * 8 +
10211 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
10212
10213 /* Align ArgSize to a multiple of 8 */
10214 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
10215 bool NeedsAlign = (Align > 8);
10216
10217 MachineBasicBlock *thisMBB = MBB;
10218 MachineBasicBlock *overflowMBB;
10219 MachineBasicBlock *offsetMBB;
10220 MachineBasicBlock *endMBB;
10221
10222 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
10223 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
10224 unsigned OffsetReg = 0;
10225
10226 if (!UseGPOffset && !UseFPOffset) {
10227 // If we only pull from the overflow region, we don't create a branch.
10228 // We don't need to alter control flow.
10229 OffsetDestReg = 0; // unused
10230 OverflowDestReg = DestReg;
10231
10232 offsetMBB = NULL;
10233 overflowMBB = thisMBB;
10234 endMBB = thisMBB;
10235 } else {
10236 // First emit code to check if gp_offset (or fp_offset) is below the bound.
10237 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
10238 // If not, pull from overflow_area. (branch to overflowMBB)
10239 //
10240 // thisMBB
10241 // | .
10242 // | .
10243 // offsetMBB overflowMBB
10244 // | .
10245 // | .
10246 // endMBB
10247
10248 // Registers for the PHI in endMBB
10249 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
10250 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
10251
10252 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
10253 MachineFunction *MF = MBB->getParent();
10254 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
10255 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
10256 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
10257
10258 MachineFunction::iterator MBBIter = MBB;
10259 ++MBBIter;
10260
10261 // Insert the new basic blocks
10262 MF->insert(MBBIter, offsetMBB);
10263 MF->insert(MBBIter, overflowMBB);
10264 MF->insert(MBBIter, endMBB);
10265
10266 // Transfer the remainder of MBB and its successor edges to endMBB.
10267 endMBB->splice(endMBB->begin(), thisMBB,
10268 llvm::next(MachineBasicBlock::iterator(MI)),
10269 thisMBB->end());
10270 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
10271
10272 // Make offsetMBB and overflowMBB successors of thisMBB
10273 thisMBB->addSuccessor(offsetMBB);
10274 thisMBB->addSuccessor(overflowMBB);
10275
10276 // endMBB is a successor of both offsetMBB and overflowMBB
10277 offsetMBB->addSuccessor(endMBB);
10278 overflowMBB->addSuccessor(endMBB);
10279
10280 // Load the offset value into a register
10281 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
10282 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
10283 .addOperand(Base)
10284 .addOperand(Scale)
10285 .addOperand(Index)
10286 .addDisp(Disp, UseFPOffset ? 4 : 0)
10287 .addOperand(Segment)
10288 .setMemRefs(MMOBegin, MMOEnd);
10289
10290 // Check if there is enough room left to pull this argument.
10291 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
10292 .addReg(OffsetReg)
10293 .addImm(MaxOffset + 8 - ArgSizeA8);
10294
10295 // Branch to "overflowMBB" if offset >= max
10296 // Fall through to "offsetMBB" otherwise
10297 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
10298 .addMBB(overflowMBB);
10299 }
10300
10301 // In offsetMBB, emit code to use the reg_save_area.
10302 if (offsetMBB) {
10303 assert(OffsetReg != 0);
10304
10305 // Read the reg_save_area address.
10306 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
10307 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
10308 .addOperand(Base)
10309 .addOperand(Scale)
10310 .addOperand(Index)
10311 .addDisp(Disp, 16)
10312 .addOperand(Segment)
10313 .setMemRefs(MMOBegin, MMOEnd);
10314
10315 // Zero-extend the offset
10316 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
10317 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
10318 .addImm(0)
10319 .addReg(OffsetReg)
10320 .addImm(X86::sub_32bit);
10321
10322 // Add the offset to the reg_save_area to get the final address.
10323 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
10324 .addReg(OffsetReg64)
10325 .addReg(RegSaveReg);
10326
10327 // Compute the offset for the next argument
10328 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
10329 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
10330 .addReg(OffsetReg)
10331 .addImm(UseFPOffset ? 16 : 8);
10332
10333 // Store it back into the va_list.
10334 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
10335 .addOperand(Base)
10336 .addOperand(Scale)
10337 .addOperand(Index)
10338 .addDisp(Disp, UseFPOffset ? 4 : 0)
10339 .addOperand(Segment)
10340 .addReg(NextOffsetReg)
10341 .setMemRefs(MMOBegin, MMOEnd);
10342
10343 // Jump to endMBB
10344 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
10345 .addMBB(endMBB);
10346 }
10347
10348 //
10349 // Emit code to use overflow area
10350 //
10351
10352 // Load the overflow_area address into a register.
10353 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
10354 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
10355 .addOperand(Base)
10356 .addOperand(Scale)
10357 .addOperand(Index)
10358 .addDisp(Disp, 8)
10359 .addOperand(Segment)
10360 .setMemRefs(MMOBegin, MMOEnd);
10361
10362 // If we need to align it, do so. Otherwise, just copy the address
10363 // to OverflowDestReg.
10364 if (NeedsAlign) {
10365 // Align the overflow address
10366 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
10367 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
10368
10369 // aligned_addr = (addr + (align-1)) & ~(align-1)
10370 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
10371 .addReg(OverflowAddrReg)
10372 .addImm(Align-1);
10373
10374 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
10375 .addReg(TmpReg)
10376 .addImm(~(uint64_t)(Align-1));
10377 } else {
10378 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
10379 .addReg(OverflowAddrReg);
10380 }
10381
10382 // Compute the next overflow address after this argument.
10383 // (the overflow address should be kept 8-byte aligned)
10384 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
10385 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
10386 .addReg(OverflowDestReg)
10387 .addImm(ArgSizeA8);
10388
10389 // Store the new overflow address.
10390 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
10391 .addOperand(Base)
10392 .addOperand(Scale)
10393 .addOperand(Index)
10394 .addDisp(Disp, 8)
10395 .addOperand(Segment)
10396 .addReg(NextAddrReg)
10397 .setMemRefs(MMOBegin, MMOEnd);
10398
10399 // If we branched, emit the PHI to the front of endMBB.
10400 if (offsetMBB) {
10401 BuildMI(*endMBB, endMBB->begin(), DL,
10402 TII->get(X86::PHI), DestReg)
10403 .addReg(OffsetDestReg).addMBB(offsetMBB)
10404 .addReg(OverflowDestReg).addMBB(overflowMBB);
10405 }
10406
10407 // Erase the pseudo instruction
10408 MI->eraseFromParent();
10409
10410 return endMBB;
10411}
10412
10413MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +000010414X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
10415 MachineInstr *MI,
10416 MachineBasicBlock *MBB) const {
10417 // Emit code to save XMM registers to the stack. The ABI says that the
10418 // number of registers to save is given in %al, so it's theoretically
10419 // possible to do an indirect jump trick to avoid saving all of them,
10420 // however this code takes a simpler approach and just executes all
10421 // of the stores if %al is non-zero. It's less code, and it's probably
10422 // easier on the hardware branch predictor, and stores aren't all that
10423 // expensive anyway.
10424
10425 // Create the new basic blocks. One block contains all the XMM stores,
10426 // and one block is the final destination regardless of whether any
10427 // stores were performed.
10428 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
10429 MachineFunction *F = MBB->getParent();
10430 MachineFunction::iterator MBBIter = MBB;
10431 ++MBBIter;
10432 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
10433 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
10434 F->insert(MBBIter, XMMSaveMBB);
10435 F->insert(MBBIter, EndMBB);
10436
Dan Gohman14152b42010-07-06 20:24:04 +000010437 // Transfer the remainder of MBB and its successor edges to EndMBB.
10438 EndMBB->splice(EndMBB->begin(), MBB,
10439 llvm::next(MachineBasicBlock::iterator(MI)),
10440 MBB->end());
10441 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
10442
Dan Gohmand6708ea2009-08-15 01:38:56 +000010443 // The original block will now fall through to the XMM save block.
10444 MBB->addSuccessor(XMMSaveMBB);
10445 // The XMMSaveMBB will fall through to the end block.
10446 XMMSaveMBB->addSuccessor(EndMBB);
10447
10448 // Now add the instructions.
10449 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10450 DebugLoc DL = MI->getDebugLoc();
10451
10452 unsigned CountReg = MI->getOperand(0).getReg();
10453 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
10454 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
10455
10456 if (!Subtarget->isTargetWin64()) {
10457 // If %al is 0, branch around the XMM save block.
10458 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +000010459 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010460 MBB->addSuccessor(EndMBB);
10461 }
10462
10463 // In the XMM save block, save all the XMM argument registers.
10464 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
10465 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +000010466 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +000010467 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +000010468 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +000010469 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +000010470 /*Size=*/16, /*Align=*/16);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010471 BuildMI(XMMSaveMBB, DL, TII->get(X86::MOVAPSmr))
10472 .addFrameIndex(RegSaveFrameIndex)
10473 .addImm(/*Scale=*/1)
10474 .addReg(/*IndexReg=*/0)
10475 .addImm(/*Disp=*/Offset)
10476 .addReg(/*Segment=*/0)
10477 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +000010478 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010479 }
10480
Dan Gohman14152b42010-07-06 20:24:04 +000010481 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +000010482
10483 return EndMBB;
10484}
Mon P Wang63307c32008-05-05 19:05:59 +000010485
Evan Cheng60c07e12006-07-05 22:17:51 +000010486MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +000010487X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010488 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +000010489 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10490 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +000010491
Chris Lattner52600972009-09-02 05:57:00 +000010492 // To "insert" a SELECT_CC instruction, we actually have to insert the
10493 // diamond control-flow pattern. The incoming instruction knows the
10494 // destination vreg to set, the condition code register to branch on, the
10495 // true/false values to select between, and a branch opcode to use.
10496 const BasicBlock *LLVM_BB = BB->getBasicBlock();
10497 MachineFunction::iterator It = BB;
10498 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +000010499
Chris Lattner52600972009-09-02 05:57:00 +000010500 // thisMBB:
10501 // ...
10502 // TrueVal = ...
10503 // cmpTY ccX, r1, r2
10504 // bCC copy1MBB
10505 // fallthrough --> copy0MBB
10506 MachineBasicBlock *thisMBB = BB;
10507 MachineFunction *F = BB->getParent();
10508 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
10509 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +000010510 F->insert(It, copy0MBB);
10511 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +000010512
Bill Wendling730c07e2010-06-25 20:48:10 +000010513 // If the EFLAGS register isn't dead in the terminator, then claim that it's
10514 // live into the sink and copy blocks.
10515 const MachineFunction *MF = BB->getParent();
10516 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
10517 BitVector ReservedRegs = TRI->getReservedRegs(*MF);
Bill Wendling730c07e2010-06-25 20:48:10 +000010518
Dan Gohman14152b42010-07-06 20:24:04 +000010519 for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) {
10520 const MachineOperand &MO = MI->getOperand(I);
10521 if (!MO.isReg() || !MO.isUse() || MO.isKill()) continue;
Bill Wendling730c07e2010-06-25 20:48:10 +000010522 unsigned Reg = MO.getReg();
10523 if (Reg != X86::EFLAGS) continue;
10524 copy0MBB->addLiveIn(Reg);
10525 sinkMBB->addLiveIn(Reg);
10526 }
10527
Dan Gohman14152b42010-07-06 20:24:04 +000010528 // Transfer the remainder of BB and its successor edges to sinkMBB.
10529 sinkMBB->splice(sinkMBB->begin(), BB,
10530 llvm::next(MachineBasicBlock::iterator(MI)),
10531 BB->end());
10532 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
10533
10534 // Add the true and fallthrough blocks as its successors.
10535 BB->addSuccessor(copy0MBB);
10536 BB->addSuccessor(sinkMBB);
10537
10538 // Create the conditional branch instruction.
10539 unsigned Opc =
10540 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
10541 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
10542
Chris Lattner52600972009-09-02 05:57:00 +000010543 // copy0MBB:
10544 // %FalseValue = ...
10545 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000010546 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000010547
Chris Lattner52600972009-09-02 05:57:00 +000010548 // sinkMBB:
10549 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
10550 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000010551 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
10552 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000010553 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
10554 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
10555
Dan Gohman14152b42010-07-06 20:24:04 +000010556 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000010557 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000010558}
10559
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010560MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010561X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010562 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010563 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10564 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010565
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000010566 assert(!Subtarget->isTargetEnvMacho());
10567
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010568 // The lowering is pretty easy: we're just emitting the call to _alloca. The
10569 // non-trivial part is impdef of ESP.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010570
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000010571 if (Subtarget->isTargetWin64()) {
10572 if (Subtarget->isTargetCygMing()) {
10573 // ___chkstk(Mingw64):
10574 // Clobbers R10, R11, RAX and EFLAGS.
10575 // Updates RSP.
10576 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
10577 .addExternalSymbol("___chkstk")
10578 .addReg(X86::RAX, RegState::Implicit)
10579 .addReg(X86::RSP, RegState::Implicit)
10580 .addReg(X86::RAX, RegState::Define | RegState::Implicit)
10581 .addReg(X86::RSP, RegState::Define | RegState::Implicit)
10582 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
10583 } else {
10584 // __chkstk(MSVCRT): does not update stack pointer.
10585 // Clobbers R10, R11 and EFLAGS.
10586 // FIXME: RAX(allocated size) might be reused and not killed.
10587 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
10588 .addExternalSymbol("__chkstk")
10589 .addReg(X86::RAX, RegState::Implicit)
10590 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
10591 // RAX has the offset to subtracted from RSP.
10592 BuildMI(*BB, MI, DL, TII->get(X86::SUB64rr), X86::RSP)
10593 .addReg(X86::RSP)
10594 .addReg(X86::RAX);
10595 }
10596 } else {
10597 const char *StackProbeSymbol =
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010598 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
10599
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000010600 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
10601 .addExternalSymbol(StackProbeSymbol)
10602 .addReg(X86::EAX, RegState::Implicit)
10603 .addReg(X86::ESP, RegState::Implicit)
10604 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
10605 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
10606 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
10607 }
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010608
Dan Gohman14152b42010-07-06 20:24:04 +000010609 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010610 return BB;
10611}
Chris Lattner52600972009-09-02 05:57:00 +000010612
10613MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000010614X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
10615 MachineBasicBlock *BB) const {
10616 // This is pretty easy. We're taking the value that we received from
10617 // our load from the relocation, sticking it in either RDI (x86-64)
10618 // or EAX and doing an indirect call. The return value will then
10619 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000010620 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000010621 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000010622 DebugLoc DL = MI->getDebugLoc();
10623 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000010624
10625 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000010626 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000010627
Eric Christopher30ef0e52010-06-03 04:07:48 +000010628 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000010629 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10630 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000010631 .addReg(X86::RIP)
10632 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010633 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000010634 MI->getOperand(3).getTargetFlags())
10635 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000010636 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000010637 addDirectMem(MIB, X86::RDI);
Eric Christopher61025492010-06-15 23:08:42 +000010638 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000010639 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10640 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000010641 .addReg(0)
10642 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010643 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000010644 MI->getOperand(3).getTargetFlags())
10645 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000010646 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000010647 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010648 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000010649 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10650 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000010651 .addReg(TII->getGlobalBaseReg(F))
10652 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010653 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000010654 MI->getOperand(3).getTargetFlags())
10655 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000010656 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000010657 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010658 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000010659
Dan Gohman14152b42010-07-06 20:24:04 +000010660 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000010661 return BB;
10662}
10663
10664MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000010665X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010666 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000010667 switch (MI->getOpcode()) {
10668 default: assert(false && "Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000010669 case X86::TAILJMPd64:
10670 case X86::TAILJMPr64:
10671 case X86::TAILJMPm64:
10672 assert(!"TAILJMP64 would not be touched here.");
10673 case X86::TCRETURNdi64:
10674 case X86::TCRETURNri64:
10675 case X86::TCRETURNmi64:
10676 // Defs of TCRETURNxx64 has Win64's callee-saved registers, as subset.
10677 // On AMD64, additional defs should be added before register allocation.
10678 if (!Subtarget->isTargetWin64()) {
10679 MI->addRegisterDefined(X86::RSI);
10680 MI->addRegisterDefined(X86::RDI);
10681 MI->addRegisterDefined(X86::XMM6);
10682 MI->addRegisterDefined(X86::XMM7);
10683 MI->addRegisterDefined(X86::XMM8);
10684 MI->addRegisterDefined(X86::XMM9);
10685 MI->addRegisterDefined(X86::XMM10);
10686 MI->addRegisterDefined(X86::XMM11);
10687 MI->addRegisterDefined(X86::XMM12);
10688 MI->addRegisterDefined(X86::XMM13);
10689 MI->addRegisterDefined(X86::XMM14);
10690 MI->addRegisterDefined(X86::XMM15);
10691 }
10692 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010693 case X86::WIN_ALLOCA:
10694 return EmitLoweredWinAlloca(MI, BB);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010695 case X86::TLSCall_32:
10696 case X86::TLSCall_64:
10697 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000010698 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000010699 case X86::CMOV_FR32:
10700 case X86::CMOV_FR64:
10701 case X86::CMOV_V4F32:
10702 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000010703 case X86::CMOV_V2I64:
Chris Lattner314a1132010-03-14 18:31:44 +000010704 case X86::CMOV_GR16:
10705 case X86::CMOV_GR32:
10706 case X86::CMOV_RFP32:
10707 case X86::CMOV_RFP64:
10708 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010709 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000010710
Dale Johannesen849f2142007-07-03 00:53:03 +000010711 case X86::FP32_TO_INT16_IN_MEM:
10712 case X86::FP32_TO_INT32_IN_MEM:
10713 case X86::FP32_TO_INT64_IN_MEM:
10714 case X86::FP64_TO_INT16_IN_MEM:
10715 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000010716 case X86::FP64_TO_INT64_IN_MEM:
10717 case X86::FP80_TO_INT16_IN_MEM:
10718 case X86::FP80_TO_INT32_IN_MEM:
10719 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000010720 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10721 DebugLoc DL = MI->getDebugLoc();
10722
Evan Cheng60c07e12006-07-05 22:17:51 +000010723 // Change the floating point control register to use "round towards zero"
10724 // mode when truncating to an integer value.
10725 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000010726 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000010727 addFrameReference(BuildMI(*BB, MI, DL,
10728 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010729
10730 // Load the old value of the high byte of the control word...
10731 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +000010732 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohman14152b42010-07-06 20:24:04 +000010733 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000010734 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010735
10736 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000010737 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000010738 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000010739
10740 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000010741 addFrameReference(BuildMI(*BB, MI, DL,
10742 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010743
10744 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000010745 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000010746 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000010747
10748 // Get the X86 opcode to use.
10749 unsigned Opc;
10750 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010751 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000010752 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
10753 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
10754 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
10755 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
10756 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
10757 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000010758 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
10759 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
10760 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000010761 }
10762
10763 X86AddressMode AM;
10764 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000010765 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000010766 AM.BaseType = X86AddressMode::RegBase;
10767 AM.Base.Reg = Op.getReg();
10768 } else {
10769 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000010770 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000010771 }
10772 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000010773 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000010774 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010775 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000010776 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000010777 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010778 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000010779 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000010780 AM.GV = Op.getGlobal();
10781 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000010782 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010783 }
Dan Gohman14152b42010-07-06 20:24:04 +000010784 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010785 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000010786
10787 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000010788 addFrameReference(BuildMI(*BB, MI, DL,
10789 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010790
Dan Gohman14152b42010-07-06 20:24:04 +000010791 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000010792 return BB;
10793 }
Eric Christopherb120ab42009-08-18 22:50:32 +000010794 // String/text processing lowering.
10795 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010796 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000010797 return EmitPCMP(MI, BB, 3, false /* in-mem */);
10798 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010799 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000010800 return EmitPCMP(MI, BB, 3, true /* in-mem */);
10801 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010802 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000010803 return EmitPCMP(MI, BB, 5, false /* in mem */);
10804 case X86::PCMPESTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010805 case X86::VPCMPESTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000010806 return EmitPCMP(MI, BB, 5, true /* in mem */);
10807
Eric Christopher228232b2010-11-30 07:20:12 +000010808 // Thread synchronization.
10809 case X86::MONITOR:
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010810 return EmitMonitor(MI, BB);
Eric Christopher228232b2010-11-30 07:20:12 +000010811 case X86::MWAIT:
10812 return EmitMwait(MI, BB);
10813
Eric Christopherb120ab42009-08-18 22:50:32 +000010814 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +000010815 case X86::ATOMAND32:
10816 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010817 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010818 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010819 X86::NOT32r, X86::EAX,
10820 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000010821 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +000010822 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
10823 X86::OR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010824 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010825 X86::NOT32r, X86::EAX,
10826 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000010827 case X86::ATOMXOR32:
10828 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010829 X86::XOR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010830 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010831 X86::NOT32r, X86::EAX,
10832 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010833 case X86::ATOMNAND32:
10834 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010835 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010836 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010837 X86::NOT32r, X86::EAX,
10838 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +000010839 case X86::ATOMMIN32:
10840 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
10841 case X86::ATOMMAX32:
10842 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
10843 case X86::ATOMUMIN32:
10844 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
10845 case X86::ATOMUMAX32:
10846 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +000010847
10848 case X86::ATOMAND16:
10849 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
10850 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010851 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010852 X86::NOT16r, X86::AX,
10853 X86::GR16RegisterClass);
10854 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +000010855 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010856 X86::OR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010857 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010858 X86::NOT16r, X86::AX,
10859 X86::GR16RegisterClass);
10860 case X86::ATOMXOR16:
10861 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
10862 X86::XOR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010863 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010864 X86::NOT16r, X86::AX,
10865 X86::GR16RegisterClass);
10866 case X86::ATOMNAND16:
10867 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
10868 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010869 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010870 X86::NOT16r, X86::AX,
10871 X86::GR16RegisterClass, true);
10872 case X86::ATOMMIN16:
10873 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
10874 case X86::ATOMMAX16:
10875 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
10876 case X86::ATOMUMIN16:
10877 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
10878 case X86::ATOMUMAX16:
10879 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
10880
10881 case X86::ATOMAND8:
10882 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
10883 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010884 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010885 X86::NOT8r, X86::AL,
10886 X86::GR8RegisterClass);
10887 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +000010888 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010889 X86::OR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010890 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010891 X86::NOT8r, X86::AL,
10892 X86::GR8RegisterClass);
10893 case X86::ATOMXOR8:
10894 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
10895 X86::XOR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010896 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010897 X86::NOT8r, X86::AL,
10898 X86::GR8RegisterClass);
10899 case X86::ATOMNAND8:
10900 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
10901 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010902 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010903 X86::NOT8r, X86::AL,
10904 X86::GR8RegisterClass, true);
10905 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010906 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +000010907 case X86::ATOMAND64:
10908 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010909 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010910 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010911 X86::NOT64r, X86::RAX,
10912 X86::GR64RegisterClass);
10913 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +000010914 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
10915 X86::OR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010916 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010917 X86::NOT64r, X86::RAX,
10918 X86::GR64RegisterClass);
10919 case X86::ATOMXOR64:
10920 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010921 X86::XOR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010922 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010923 X86::NOT64r, X86::RAX,
10924 X86::GR64RegisterClass);
10925 case X86::ATOMNAND64:
10926 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
10927 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010928 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010929 X86::NOT64r, X86::RAX,
10930 X86::GR64RegisterClass, true);
10931 case X86::ATOMMIN64:
10932 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
10933 case X86::ATOMMAX64:
10934 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
10935 case X86::ATOMUMIN64:
10936 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
10937 case X86::ATOMUMAX64:
10938 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010939
10940 // This group does 64-bit operations on a 32-bit host.
10941 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010942 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010943 X86::AND32rr, X86::AND32rr,
10944 X86::AND32ri, X86::AND32ri,
10945 false);
10946 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010947 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010948 X86::OR32rr, X86::OR32rr,
10949 X86::OR32ri, X86::OR32ri,
10950 false);
10951 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010952 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010953 X86::XOR32rr, X86::XOR32rr,
10954 X86::XOR32ri, X86::XOR32ri,
10955 false);
10956 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010957 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010958 X86::AND32rr, X86::AND32rr,
10959 X86::AND32ri, X86::AND32ri,
10960 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010961 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010962 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010963 X86::ADD32rr, X86::ADC32rr,
10964 X86::ADD32ri, X86::ADC32ri,
10965 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010966 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010967 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010968 X86::SUB32rr, X86::SBB32rr,
10969 X86::SUB32ri, X86::SBB32ri,
10970 false);
Dale Johannesen880ae362008-10-03 22:25:52 +000010971 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010972 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +000010973 X86::MOV32rr, X86::MOV32rr,
10974 X86::MOV32ri, X86::MOV32ri,
10975 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010976 case X86::VASTART_SAVE_XMM_REGS:
10977 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000010978
10979 case X86::VAARG_64:
10980 return EmitVAARG64WithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000010981 }
10982}
10983
10984//===----------------------------------------------------------------------===//
10985// X86 Optimization Hooks
10986//===----------------------------------------------------------------------===//
10987
Dan Gohman475871a2008-07-27 21:46:04 +000010988void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +000010989 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000010990 APInt &KnownZero,
10991 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000010992 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000010993 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010994 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000010995 assert((Opc >= ISD::BUILTIN_OP_END ||
10996 Opc == ISD::INTRINSIC_WO_CHAIN ||
10997 Opc == ISD::INTRINSIC_W_CHAIN ||
10998 Opc == ISD::INTRINSIC_VOID) &&
10999 "Should use MaskedValueIsZero if you don't know whether Op"
11000 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000011001
Dan Gohmanf4f92f52008-02-13 23:07:24 +000011002 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000011003 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000011004 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000011005 case X86ISD::ADD:
11006 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000011007 case X86ISD::ADC:
11008 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000011009 case X86ISD::SMUL:
11010 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000011011 case X86ISD::INC:
11012 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000011013 case X86ISD::OR:
11014 case X86ISD::XOR:
11015 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000011016 // These nodes' second result is a boolean.
11017 if (Op.getResNo() == 0)
11018 break;
11019 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011020 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000011021 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
11022 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000011023 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +000011024 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000011025}
Chris Lattner259e97c2006-01-31 19:43:35 +000011026
Owen Andersonbc146b02010-09-21 20:42:50 +000011027unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
11028 unsigned Depth) const {
11029 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
11030 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
11031 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000011032
Owen Andersonbc146b02010-09-21 20:42:50 +000011033 // Fallback case.
11034 return 1;
11035}
11036
Evan Cheng206ee9d2006-07-07 08:33:52 +000011037/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000011038/// node is a GlobalAddress + offset.
11039bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000011040 const GlobalValue* &GA,
11041 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000011042 if (N->getOpcode() == X86ISD::Wrapper) {
11043 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000011044 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000011045 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000011046 return true;
11047 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000011048 }
Evan Chengad4196b2008-05-12 19:56:52 +000011049 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000011050}
11051
Evan Cheng206ee9d2006-07-07 08:33:52 +000011052/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
11053/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
11054/// if the load addresses are consecutive, non-overlapping, and in the right
Nate Begemanfdea31a2010-03-24 20:49:50 +000011055/// order.
Dan Gohman475871a2008-07-27 21:46:04 +000011056static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Mon P Wanga0fd0d52010-12-19 23:55:53 +000011057 TargetLowering::DAGCombinerInfo &DCI) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000011058 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000011059 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000011060
Eli Friedman7a5e5552009-06-07 06:52:44 +000011061 if (VT.getSizeInBits() != 128)
11062 return SDValue();
11063
Mon P Wanga0fd0d52010-12-19 23:55:53 +000011064 // Don't create instructions with illegal types after legalize types has run.
11065 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
11066 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
11067 return SDValue();
11068
Nate Begemanfdea31a2010-03-24 20:49:50 +000011069 SmallVector<SDValue, 16> Elts;
11070 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000011071 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000011072
Nate Begemanfdea31a2010-03-24 20:49:50 +000011073 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000011074}
Evan Chengd880b972008-05-09 21:53:03 +000011075
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000011076/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
11077/// generation and convert it from being a bunch of shuffles and extracts
11078/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000011079static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
11080 const TargetLowering &TLI) {
11081 SDValue InputVector = N->getOperand(0);
11082
11083 // Only operate on vectors of 4 elements, where the alternative shuffling
11084 // gets to be more expensive.
11085 if (InputVector.getValueType() != MVT::v4i32)
11086 return SDValue();
11087
11088 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
11089 // single use which is a sign-extend or zero-extend, and all elements are
11090 // used.
11091 SmallVector<SDNode *, 4> Uses;
11092 unsigned ExtractedElements = 0;
11093 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
11094 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
11095 if (UI.getUse().getResNo() != InputVector.getResNo())
11096 return SDValue();
11097
11098 SDNode *Extract = *UI;
11099 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
11100 return SDValue();
11101
11102 if (Extract->getValueType(0) != MVT::i32)
11103 return SDValue();
11104 if (!Extract->hasOneUse())
11105 return SDValue();
11106 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
11107 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
11108 return SDValue();
11109 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
11110 return SDValue();
11111
11112 // Record which element was extracted.
11113 ExtractedElements |=
11114 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
11115
11116 Uses.push_back(Extract);
11117 }
11118
11119 // If not all the elements were used, this may not be worthwhile.
11120 if (ExtractedElements != 15)
11121 return SDValue();
11122
11123 // Ok, we've now decided to do the transformation.
11124 DebugLoc dl = InputVector.getDebugLoc();
11125
11126 // Store the value to a temporary stack slot.
11127 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000011128 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
11129 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000011130
11131 // Replace each use (extract) with a load of the appropriate element.
11132 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
11133 UE = Uses.end(); UI != UE; ++UI) {
11134 SDNode *Extract = *UI;
11135
Nadav Rotem86694292011-05-17 08:31:57 +000011136 // cOMpute the element's address.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000011137 SDValue Idx = Extract->getOperand(1);
11138 unsigned EltSize =
11139 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
11140 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
11141 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
11142
Nadav Rotem86694292011-05-17 08:31:57 +000011143 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
Chris Lattner51abfe42010-09-21 06:02:19 +000011144 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000011145
11146 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000011147 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000011148 ScalarAddr, MachinePointerInfo(),
11149 false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000011150
11151 // Replace the exact with the load.
11152 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
11153 }
11154
11155 // The replacement was made in place; don't return anything.
11156 return SDValue();
11157}
11158
Chris Lattner83e6c992006-10-04 06:57:07 +000011159/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000011160static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +000011161 const X86Subtarget *Subtarget) {
11162 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000011163 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000011164 // Get the LHS/RHS of the select.
11165 SDValue LHS = N->getOperand(1);
11166 SDValue RHS = N->getOperand(2);
Eric Christopherfd179292009-08-27 18:07:15 +000011167
Dan Gohman670e5392009-09-21 18:03:22 +000011168 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000011169 // instructions match the semantics of the common C idiom x<y?x:y but not
11170 // x<=y?x:y, because of how they handle negative zero (which can be
11171 // ignored in unsafe-math mode).
Chris Lattner83e6c992006-10-04 06:57:07 +000011172 if (Subtarget->hasSSE2() &&
Owen Anderson825b72b2009-08-11 20:47:22 +000011173 (LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64) &&
Chris Lattner47b4ce82009-03-11 05:48:52 +000011174 Cond.getOpcode() == ISD::SETCC) {
11175 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011176
Chris Lattner47b4ce82009-03-11 05:48:52 +000011177 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000011178 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000011179 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
11180 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000011181 switch (CC) {
11182 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000011183 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000011184 // Converting this to a min would handle NaNs incorrectly, and swapping
11185 // the operands would cause it to handle comparisons between positive
11186 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000011187 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000011188 if (!UnsafeFPMath &&
11189 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
11190 break;
11191 std::swap(LHS, RHS);
11192 }
Dan Gohman670e5392009-09-21 18:03:22 +000011193 Opcode = X86ISD::FMIN;
11194 break;
11195 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000011196 // Converting this to a min would handle comparisons between positive
11197 // and negative zero incorrectly.
11198 if (!UnsafeFPMath &&
11199 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
11200 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011201 Opcode = X86ISD::FMIN;
11202 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000011203 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000011204 // Converting this to a min would handle both negative zeros and NaNs
11205 // incorrectly, but we can swap the operands to fix both.
11206 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000011207 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011208 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000011209 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011210 Opcode = X86ISD::FMIN;
11211 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011212
Dan Gohman670e5392009-09-21 18:03:22 +000011213 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000011214 // Converting this to a max would handle comparisons between positive
11215 // and negative zero incorrectly.
11216 if (!UnsafeFPMath &&
11217 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(LHS))
11218 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011219 Opcode = X86ISD::FMAX;
11220 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000011221 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000011222 // Converting this to a max would handle NaNs incorrectly, and swapping
11223 // the operands would cause it to handle comparisons between positive
11224 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000011225 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000011226 if (!UnsafeFPMath &&
11227 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
11228 break;
11229 std::swap(LHS, RHS);
11230 }
Dan Gohman670e5392009-09-21 18:03:22 +000011231 Opcode = X86ISD::FMAX;
11232 break;
11233 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000011234 // Converting this to a max would handle both negative zeros and NaNs
11235 // incorrectly, but we can swap the operands to fix both.
11236 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000011237 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011238 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011239 case ISD::SETGE:
11240 Opcode = X86ISD::FMAX;
11241 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000011242 }
Dan Gohman670e5392009-09-21 18:03:22 +000011243 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000011244 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
11245 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000011246 switch (CC) {
11247 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000011248 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000011249 // Converting this to a min would handle comparisons between positive
11250 // and negative zero incorrectly, and swapping the operands would
11251 // cause it to handle NaNs incorrectly.
11252 if (!UnsafeFPMath &&
11253 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000011254 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000011255 break;
11256 std::swap(LHS, RHS);
11257 }
Dan Gohman670e5392009-09-21 18:03:22 +000011258 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000011259 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011260 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000011261 // Converting this to a min would handle NaNs incorrectly.
11262 if (!UnsafeFPMath &&
11263 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
11264 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011265 Opcode = X86ISD::FMIN;
11266 break;
11267 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000011268 // Converting this to a min would handle both negative zeros and NaNs
11269 // incorrectly, but we can swap the operands to fix both.
11270 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000011271 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011272 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011273 case ISD::SETGE:
11274 Opcode = X86ISD::FMIN;
11275 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011276
Dan Gohman670e5392009-09-21 18:03:22 +000011277 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000011278 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000011279 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000011280 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011281 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000011282 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011283 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000011284 // Converting this to a max would handle comparisons between positive
11285 // and negative zero incorrectly, and swapping the operands would
11286 // cause it to handle NaNs incorrectly.
11287 if (!UnsafeFPMath &&
11288 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000011289 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000011290 break;
11291 std::swap(LHS, RHS);
11292 }
Dan Gohman670e5392009-09-21 18:03:22 +000011293 Opcode = X86ISD::FMAX;
11294 break;
11295 case ISD::SETULE:
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::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011300 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000011301 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011302 Opcode = X86ISD::FMAX;
11303 break;
11304 }
Chris Lattner83e6c992006-10-04 06:57:07 +000011305 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011306
Chris Lattner47b4ce82009-03-11 05:48:52 +000011307 if (Opcode)
11308 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000011309 }
Eric Christopherfd179292009-08-27 18:07:15 +000011310
Chris Lattnerd1980a52009-03-12 06:52:53 +000011311 // If this is a select between two integer constants, try to do some
11312 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000011313 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
11314 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000011315 // Don't do this for crazy integer types.
11316 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
11317 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000011318 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000011319 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000011320
Chris Lattnercee56e72009-03-13 05:53:31 +000011321 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000011322 // Efficiently invertible.
11323 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
11324 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
11325 isa<ConstantSDNode>(Cond.getOperand(1))))) {
11326 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000011327 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000011328 }
Eric Christopherfd179292009-08-27 18:07:15 +000011329
Chris Lattnerd1980a52009-03-12 06:52:53 +000011330 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000011331 if (FalseC->getAPIntValue() == 0 &&
11332 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000011333 if (NeedsCondInvert) // Invert the condition if needed.
11334 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
11335 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011336
Chris Lattnerd1980a52009-03-12 06:52:53 +000011337 // Zero extend the condition if needed.
11338 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011339
Chris Lattnercee56e72009-03-13 05:53:31 +000011340 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000011341 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000011342 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000011343 }
Eric Christopherfd179292009-08-27 18:07:15 +000011344
Chris Lattner97a29a52009-03-13 05:22:11 +000011345 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000011346 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000011347 if (NeedsCondInvert) // Invert the condition if needed.
11348 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
11349 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011350
Chris Lattner97a29a52009-03-13 05:22:11 +000011351 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000011352 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
11353 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000011354 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000011355 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000011356 }
Eric Christopherfd179292009-08-27 18:07:15 +000011357
Chris Lattnercee56e72009-03-13 05:53:31 +000011358 // Optimize cases that will turn into an LEA instruction. This requires
11359 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000011360 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000011361 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000011362 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000011363
Chris Lattnercee56e72009-03-13 05:53:31 +000011364 bool isFastMultiplier = false;
11365 if (Diff < 10) {
11366 switch ((unsigned char)Diff) {
11367 default: break;
11368 case 1: // result = add base, cond
11369 case 2: // result = lea base( , cond*2)
11370 case 3: // result = lea base(cond, cond*2)
11371 case 4: // result = lea base( , cond*4)
11372 case 5: // result = lea base(cond, cond*4)
11373 case 8: // result = lea base( , cond*8)
11374 case 9: // result = lea base(cond, cond*8)
11375 isFastMultiplier = true;
11376 break;
11377 }
11378 }
Eric Christopherfd179292009-08-27 18:07:15 +000011379
Chris Lattnercee56e72009-03-13 05:53:31 +000011380 if (isFastMultiplier) {
11381 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
11382 if (NeedsCondInvert) // Invert the condition if needed.
11383 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
11384 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011385
Chris Lattnercee56e72009-03-13 05:53:31 +000011386 // Zero extend the condition if needed.
11387 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
11388 Cond);
11389 // Scale the condition by the difference.
11390 if (Diff != 1)
11391 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
11392 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011393
Chris Lattnercee56e72009-03-13 05:53:31 +000011394 // Add the base if non-zero.
11395 if (FalseC->getAPIntValue() != 0)
11396 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
11397 SDValue(FalseC, 0));
11398 return Cond;
11399 }
Eric Christopherfd179292009-08-27 18:07:15 +000011400 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000011401 }
11402 }
Eric Christopherfd179292009-08-27 18:07:15 +000011403
Dan Gohman475871a2008-07-27 21:46:04 +000011404 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000011405}
11406
Chris Lattnerd1980a52009-03-12 06:52:53 +000011407/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
11408static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
11409 TargetLowering::DAGCombinerInfo &DCI) {
11410 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000011411
Chris Lattnerd1980a52009-03-12 06:52:53 +000011412 // If the flag operand isn't dead, don't touch this CMOV.
11413 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
11414 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000011415
Evan Chengb5a55d92011-05-24 01:48:22 +000011416 SDValue FalseOp = N->getOperand(0);
11417 SDValue TrueOp = N->getOperand(1);
11418 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
11419 SDValue Cond = N->getOperand(3);
11420 if (CC == X86::COND_E || CC == X86::COND_NE) {
11421 switch (Cond.getOpcode()) {
11422 default: break;
11423 case X86ISD::BSR:
11424 case X86ISD::BSF:
11425 // If operand of BSR / BSF are proven never zero, then ZF cannot be set.
11426 if (DAG.isKnownNeverZero(Cond.getOperand(0)))
11427 return (CC == X86::COND_E) ? FalseOp : TrueOp;
11428 }
11429 }
11430
Chris Lattnerd1980a52009-03-12 06:52:53 +000011431 // If this is a select between two integer constants, try to do some
11432 // optimizations. Note that the operands are ordered the opposite of SELECT
11433 // operands.
Evan Chengb5a55d92011-05-24 01:48:22 +000011434 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(TrueOp)) {
11435 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(FalseOp)) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000011436 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
11437 // larger than FalseC (the false value).
Chris Lattnerd1980a52009-03-12 06:52:53 +000011438 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
11439 CC = X86::GetOppositeBranchCondition(CC);
11440 std::swap(TrueC, FalseC);
11441 }
Eric Christopherfd179292009-08-27 18:07:15 +000011442
Chris Lattnerd1980a52009-03-12 06:52:53 +000011443 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000011444 // This is efficient for any integer data type (including i8/i16) and
11445 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000011446 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000011447 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11448 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011449
Chris Lattnerd1980a52009-03-12 06:52:53 +000011450 // Zero extend the condition if needed.
11451 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011452
Chris Lattnerd1980a52009-03-12 06:52:53 +000011453 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
11454 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000011455 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000011456 if (N->getNumValues() == 2) // Dead flag value?
11457 return DCI.CombineTo(N, Cond, SDValue());
11458 return Cond;
11459 }
Eric Christopherfd179292009-08-27 18:07:15 +000011460
Chris Lattnercee56e72009-03-13 05:53:31 +000011461 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
11462 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000011463 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000011464 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11465 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011466
Chris Lattner97a29a52009-03-13 05:22:11 +000011467 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000011468 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
11469 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000011470 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
11471 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000011472
Chris Lattner97a29a52009-03-13 05:22:11 +000011473 if (N->getNumValues() == 2) // Dead flag value?
11474 return DCI.CombineTo(N, Cond, SDValue());
11475 return Cond;
11476 }
Eric Christopherfd179292009-08-27 18:07:15 +000011477
Chris Lattnercee56e72009-03-13 05:53:31 +000011478 // Optimize cases that will turn into an LEA instruction. This requires
11479 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000011480 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000011481 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000011482 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000011483
Chris Lattnercee56e72009-03-13 05:53:31 +000011484 bool isFastMultiplier = false;
11485 if (Diff < 10) {
11486 switch ((unsigned char)Diff) {
11487 default: break;
11488 case 1: // result = add base, cond
11489 case 2: // result = lea base( , cond*2)
11490 case 3: // result = lea base(cond, cond*2)
11491 case 4: // result = lea base( , cond*4)
11492 case 5: // result = lea base(cond, cond*4)
11493 case 8: // result = lea base( , cond*8)
11494 case 9: // result = lea base(cond, cond*8)
11495 isFastMultiplier = true;
11496 break;
11497 }
11498 }
Eric Christopherfd179292009-08-27 18:07:15 +000011499
Chris Lattnercee56e72009-03-13 05:53:31 +000011500 if (isFastMultiplier) {
11501 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000011502 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11503 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000011504 // Zero extend the condition if needed.
11505 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
11506 Cond);
11507 // Scale the condition by the difference.
11508 if (Diff != 1)
11509 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
11510 DAG.getConstant(Diff, Cond.getValueType()));
11511
11512 // Add the base if non-zero.
11513 if (FalseC->getAPIntValue() != 0)
11514 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
11515 SDValue(FalseC, 0));
11516 if (N->getNumValues() == 2) // Dead flag value?
11517 return DCI.CombineTo(N, Cond, SDValue());
11518 return Cond;
11519 }
Eric Christopherfd179292009-08-27 18:07:15 +000011520 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000011521 }
11522 }
11523 return SDValue();
11524}
11525
11526
Evan Cheng0b0cd912009-03-28 05:57:29 +000011527/// PerformMulCombine - Optimize a single multiply with constant into two
11528/// in order to implement it with two cheaper instructions, e.g.
11529/// LEA + SHL, LEA + LEA.
11530static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
11531 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000011532 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
11533 return SDValue();
11534
Owen Andersone50ed302009-08-10 22:56:29 +000011535 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000011536 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000011537 return SDValue();
11538
11539 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
11540 if (!C)
11541 return SDValue();
11542 uint64_t MulAmt = C->getZExtValue();
11543 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
11544 return SDValue();
11545
11546 uint64_t MulAmt1 = 0;
11547 uint64_t MulAmt2 = 0;
11548 if ((MulAmt % 9) == 0) {
11549 MulAmt1 = 9;
11550 MulAmt2 = MulAmt / 9;
11551 } else if ((MulAmt % 5) == 0) {
11552 MulAmt1 = 5;
11553 MulAmt2 = MulAmt / 5;
11554 } else if ((MulAmt % 3) == 0) {
11555 MulAmt1 = 3;
11556 MulAmt2 = MulAmt / 3;
11557 }
11558 if (MulAmt2 &&
11559 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
11560 DebugLoc DL = N->getDebugLoc();
11561
11562 if (isPowerOf2_64(MulAmt2) &&
11563 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
11564 // If second multiplifer is pow2, issue it first. We want the multiply by
11565 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
11566 // is an add.
11567 std::swap(MulAmt1, MulAmt2);
11568
11569 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000011570 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000011571 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000011572 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000011573 else
Evan Cheng73f24c92009-03-30 21:36:47 +000011574 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000011575 DAG.getConstant(MulAmt1, VT));
11576
Eric Christopherfd179292009-08-27 18:07:15 +000011577 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000011578 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000011579 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000011580 else
Evan Cheng73f24c92009-03-30 21:36:47 +000011581 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000011582 DAG.getConstant(MulAmt2, VT));
11583
11584 // Do not add new nodes to DAG combiner worklist.
11585 DCI.CombineTo(N, NewMul, false);
11586 }
11587 return SDValue();
11588}
11589
Evan Chengad9c0a32009-12-15 00:53:42 +000011590static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
11591 SDValue N0 = N->getOperand(0);
11592 SDValue N1 = N->getOperand(1);
11593 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
11594 EVT VT = N0.getValueType();
11595
11596 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
11597 // since the result of setcc_c is all zero's or all ones.
11598 if (N1C && N0.getOpcode() == ISD::AND &&
11599 N0.getOperand(1).getOpcode() == ISD::Constant) {
11600 SDValue N00 = N0.getOperand(0);
11601 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
11602 ((N00.getOpcode() == ISD::ANY_EXTEND ||
11603 N00.getOpcode() == ISD::ZERO_EXTEND) &&
11604 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
11605 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
11606 APInt ShAmt = N1C->getAPIntValue();
11607 Mask = Mask.shl(ShAmt);
11608 if (Mask != 0)
11609 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
11610 N00, DAG.getConstant(Mask, VT));
11611 }
11612 }
11613
11614 return SDValue();
11615}
Evan Cheng0b0cd912009-03-28 05:57:29 +000011616
Nate Begeman740ab032009-01-26 00:52:55 +000011617/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
11618/// when possible.
11619static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
11620 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000011621 EVT VT = N->getValueType(0);
11622 if (!VT.isVector() && VT.isInteger() &&
11623 N->getOpcode() == ISD::SHL)
11624 return PerformSHLCombine(N, DAG);
11625
Nate Begeman740ab032009-01-26 00:52:55 +000011626 // On X86 with SSE2 support, we can transform this to a vector shift if
11627 // all elements are shifted by the same amount. We can't do this in legalize
11628 // because the a constant vector is typically transformed to a constant pool
11629 // so we have no knowledge of the shift amount.
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011630 if (!Subtarget->hasSSE2())
11631 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000011632
Owen Anderson825b72b2009-08-11 20:47:22 +000011633 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011634 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000011635
Mon P Wang3becd092009-01-28 08:12:05 +000011636 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000011637 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000011638 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000011639 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000011640 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
11641 unsigned NumElts = VT.getVectorNumElements();
11642 unsigned i = 0;
11643 for (; i != NumElts; ++i) {
11644 SDValue Arg = ShAmtOp.getOperand(i);
11645 if (Arg.getOpcode() == ISD::UNDEF) continue;
11646 BaseShAmt = Arg;
11647 break;
11648 }
11649 for (; i != NumElts; ++i) {
11650 SDValue Arg = ShAmtOp.getOperand(i);
11651 if (Arg.getOpcode() == ISD::UNDEF) continue;
11652 if (Arg != BaseShAmt) {
11653 return SDValue();
11654 }
11655 }
11656 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000011657 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000011658 SDValue InVec = ShAmtOp.getOperand(0);
11659 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
11660 unsigned NumElts = InVec.getValueType().getVectorNumElements();
11661 unsigned i = 0;
11662 for (; i != NumElts; ++i) {
11663 SDValue Arg = InVec.getOperand(i);
11664 if (Arg.getOpcode() == ISD::UNDEF) continue;
11665 BaseShAmt = Arg;
11666 break;
11667 }
11668 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
11669 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000011670 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000011671 if (C->getZExtValue() == SplatIdx)
11672 BaseShAmt = InVec.getOperand(1);
11673 }
11674 }
11675 if (BaseShAmt.getNode() == 0)
11676 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
11677 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +000011678 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011679 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000011680
Mon P Wangefa42202009-09-03 19:56:25 +000011681 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000011682 if (EltVT.bitsGT(MVT::i32))
11683 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
11684 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000011685 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000011686
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011687 // The shift amount is identical so we can do a vector shift.
11688 SDValue ValOp = N->getOperand(0);
11689 switch (N->getOpcode()) {
11690 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000011691 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011692 break;
11693 case ISD::SHL:
Owen Anderson825b72b2009-08-11 20:47:22 +000011694 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011695 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011696 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011697 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011698 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011699 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011700 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011701 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011702 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011703 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011704 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011705 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011706 break;
11707 case ISD::SRA:
Owen Anderson825b72b2009-08-11 20:47:22 +000011708 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011709 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011710 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011711 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011712 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011713 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011714 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011715 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011716 break;
11717 case ISD::SRL:
Owen Anderson825b72b2009-08-11 20:47:22 +000011718 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011719 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011720 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011721 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011722 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011723 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011724 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011725 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011726 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011727 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011728 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011729 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011730 break;
Nate Begeman740ab032009-01-26 00:52:55 +000011731 }
11732 return SDValue();
11733}
11734
Nate Begemanb65c1752010-12-17 22:55:37 +000011735
Stuart Hastings865f0932011-06-03 23:53:54 +000011736// CMPEQCombine - Recognize the distinctive (AND (setcc ...) (setcc ..))
11737// where both setccs reference the same FP CMP, and rewrite for CMPEQSS
11738// and friends. Likewise for OR -> CMPNEQSS.
11739static SDValue CMPEQCombine(SDNode *N, SelectionDAG &DAG,
11740 TargetLowering::DAGCombinerInfo &DCI,
11741 const X86Subtarget *Subtarget) {
11742 unsigned opcode;
11743
11744 // SSE1 supports CMP{eq|ne}SS, and SSE2 added CMP{eq|ne}SD, but
11745 // we're requiring SSE2 for both.
11746 if (Subtarget->hasSSE2() && isAndOrOfSetCCs(SDValue(N, 0U), opcode)) {
11747 SDValue N0 = N->getOperand(0);
11748 SDValue N1 = N->getOperand(1);
11749 SDValue CMP0 = N0->getOperand(1);
11750 SDValue CMP1 = N1->getOperand(1);
11751 DebugLoc DL = N->getDebugLoc();
11752
11753 // The SETCCs should both refer to the same CMP.
11754 if (CMP0.getOpcode() != X86ISD::CMP || CMP0 != CMP1)
11755 return SDValue();
11756
11757 SDValue CMP00 = CMP0->getOperand(0);
11758 SDValue CMP01 = CMP0->getOperand(1);
11759 EVT VT = CMP00.getValueType();
11760
11761 if (VT == MVT::f32 || VT == MVT::f64) {
11762 bool ExpectingFlags = false;
11763 // Check for any users that want flags:
11764 for (SDNode::use_iterator UI = N->use_begin(),
11765 UE = N->use_end();
11766 !ExpectingFlags && UI != UE; ++UI)
11767 switch (UI->getOpcode()) {
11768 default:
11769 case ISD::BR_CC:
11770 case ISD::BRCOND:
11771 case ISD::SELECT:
11772 ExpectingFlags = true;
11773 break;
11774 case ISD::CopyToReg:
11775 case ISD::SIGN_EXTEND:
11776 case ISD::ZERO_EXTEND:
11777 case ISD::ANY_EXTEND:
11778 break;
11779 }
11780
11781 if (!ExpectingFlags) {
11782 enum X86::CondCode cc0 = (enum X86::CondCode)N0.getConstantOperandVal(0);
11783 enum X86::CondCode cc1 = (enum X86::CondCode)N1.getConstantOperandVal(0);
11784
11785 if (cc1 == X86::COND_E || cc1 == X86::COND_NE) {
11786 X86::CondCode tmp = cc0;
11787 cc0 = cc1;
11788 cc1 = tmp;
11789 }
11790
11791 if ((cc0 == X86::COND_E && cc1 == X86::COND_NP) ||
11792 (cc0 == X86::COND_NE && cc1 == X86::COND_P)) {
11793 bool is64BitFP = (CMP00.getValueType() == MVT::f64);
11794 X86ISD::NodeType NTOperator = is64BitFP ?
11795 X86ISD::FSETCCsd : X86ISD::FSETCCss;
11796 // FIXME: need symbolic constants for these magic numbers.
11797 // See X86ATTInstPrinter.cpp:printSSECC().
11798 unsigned x86cc = (cc0 == X86::COND_E) ? 0 : 4;
11799 SDValue OnesOrZeroesF = DAG.getNode(NTOperator, DL, MVT::f32, CMP00, CMP01,
11800 DAG.getConstant(x86cc, MVT::i8));
11801 SDValue OnesOrZeroesI = DAG.getNode(ISD::BITCAST, DL, MVT::i32,
11802 OnesOrZeroesF);
11803 SDValue ANDed = DAG.getNode(ISD::AND, DL, MVT::i32, OnesOrZeroesI,
11804 DAG.getConstant(1, MVT::i32));
11805 SDValue OneBitOfTruth = DAG.getNode(ISD::TRUNCATE, DL, MVT::i8, ANDed);
11806 return OneBitOfTruth;
11807 }
11808 }
11809 }
11810 }
11811 return SDValue();
11812}
11813
Nate Begemanb65c1752010-12-17 22:55:37 +000011814static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
11815 TargetLowering::DAGCombinerInfo &DCI,
11816 const X86Subtarget *Subtarget) {
11817 if (DCI.isBeforeLegalizeOps())
11818 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011819
Stuart Hastings865f0932011-06-03 23:53:54 +000011820 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
11821 if (R.getNode())
11822 return R;
11823
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000011824 // Want to form ANDNP nodes:
11825 // 1) In the hopes of then easily combining them with OR and AND nodes
11826 // to form PBLEND/PSIGN.
11827 // 2) To match ANDN packed intrinsics
Nate Begemanb65c1752010-12-17 22:55:37 +000011828 EVT VT = N->getValueType(0);
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000011829 if (VT != MVT::v2i64 && VT != MVT::v4i64)
Nate Begemanb65c1752010-12-17 22:55:37 +000011830 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011831
Nate Begemanb65c1752010-12-17 22:55:37 +000011832 SDValue N0 = N->getOperand(0);
11833 SDValue N1 = N->getOperand(1);
11834 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011835
Nate Begemanb65c1752010-12-17 22:55:37 +000011836 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011837 if (N0.getOpcode() == ISD::XOR &&
Nate Begemanb65c1752010-12-17 22:55:37 +000011838 ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000011839 return DAG.getNode(X86ISD::ANDNP, DL, VT, N0.getOperand(0), N1);
Nate Begemanb65c1752010-12-17 22:55:37 +000011840
11841 // Check RHS for vnot
11842 if (N1.getOpcode() == ISD::XOR &&
11843 ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000011844 return DAG.getNode(X86ISD::ANDNP, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011845
Nate Begemanb65c1752010-12-17 22:55:37 +000011846 return SDValue();
11847}
11848
Evan Cheng760d1942010-01-04 21:22:48 +000011849static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000011850 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000011851 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +000011852 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000011853 return SDValue();
11854
Stuart Hastings865f0932011-06-03 23:53:54 +000011855 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
11856 if (R.getNode())
11857 return R;
11858
Evan Cheng760d1942010-01-04 21:22:48 +000011859 EVT VT = N->getValueType(0);
Nate Begemanb65c1752010-12-17 22:55:37 +000011860 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64 && VT != MVT::v2i64)
Evan Cheng760d1942010-01-04 21:22:48 +000011861 return SDValue();
11862
Evan Cheng760d1942010-01-04 21:22:48 +000011863 SDValue N0 = N->getOperand(0);
11864 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011865
Nate Begemanb65c1752010-12-17 22:55:37 +000011866 // look for psign/blend
11867 if (Subtarget->hasSSSE3()) {
11868 if (VT == MVT::v2i64) {
11869 // Canonicalize pandn to RHS
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000011870 if (N0.getOpcode() == X86ISD::ANDNP)
Nate Begemanb65c1752010-12-17 22:55:37 +000011871 std::swap(N0, N1);
11872 // or (and (m, x), (pandn m, y))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000011873 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::ANDNP) {
Nate Begemanb65c1752010-12-17 22:55:37 +000011874 SDValue Mask = N1.getOperand(0);
11875 SDValue X = N1.getOperand(1);
11876 SDValue Y;
11877 if (N0.getOperand(0) == Mask)
11878 Y = N0.getOperand(1);
11879 if (N0.getOperand(1) == Mask)
11880 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011881
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000011882 // Check to see if the mask appeared in both the AND and ANDNP and
Nate Begemanb65c1752010-12-17 22:55:37 +000011883 if (!Y.getNode())
11884 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011885
Nate Begemanb65c1752010-12-17 22:55:37 +000011886 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
11887 if (Mask.getOpcode() != ISD::BITCAST ||
11888 X.getOpcode() != ISD::BITCAST ||
11889 Y.getOpcode() != ISD::BITCAST)
11890 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011891
Nate Begemanb65c1752010-12-17 22:55:37 +000011892 // Look through mask bitcast.
11893 Mask = Mask.getOperand(0);
11894 EVT MaskVT = Mask.getValueType();
11895
11896 // Validate that the Mask operand is a vector sra node. The sra node
11897 // will be an intrinsic.
11898 if (Mask.getOpcode() != ISD::INTRINSIC_WO_CHAIN)
11899 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011900
Nate Begemanb65c1752010-12-17 22:55:37 +000011901 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
11902 // there is no psrai.b
11903 switch (cast<ConstantSDNode>(Mask.getOperand(0))->getZExtValue()) {
11904 case Intrinsic::x86_sse2_psrai_w:
11905 case Intrinsic::x86_sse2_psrai_d:
11906 break;
11907 default: return SDValue();
11908 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011909
Nate Begemanb65c1752010-12-17 22:55:37 +000011910 // Check that the SRA is all signbits.
11911 SDValue SraC = Mask.getOperand(2);
11912 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
11913 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
11914 if ((SraAmt + 1) != EltBits)
11915 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011916
Nate Begemanb65c1752010-12-17 22:55:37 +000011917 DebugLoc DL = N->getDebugLoc();
11918
11919 // Now we know we at least have a plendvb with the mask val. See if
11920 // we can form a psignb/w/d.
11921 // psign = x.type == y.type == mask.type && y = sub(0, x);
11922 X = X.getOperand(0);
11923 Y = Y.getOperand(0);
11924 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
11925 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
11926 X.getValueType() == MaskVT && X.getValueType() == Y.getValueType()){
11927 unsigned Opc = 0;
11928 switch (EltBits) {
11929 case 8: Opc = X86ISD::PSIGNB; break;
11930 case 16: Opc = X86ISD::PSIGNW; break;
11931 case 32: Opc = X86ISD::PSIGND; break;
11932 default: break;
11933 }
11934 if (Opc) {
11935 SDValue Sign = DAG.getNode(Opc, DL, MaskVT, X, Mask.getOperand(1));
11936 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Sign);
11937 }
11938 }
11939 // PBLENDVB only available on SSE 4.1
11940 if (!Subtarget->hasSSE41())
11941 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011942
Nate Begemanb65c1752010-12-17 22:55:37 +000011943 X = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, X);
11944 Y = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Y);
11945 Mask = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Mask);
Nate Begeman672fb622010-12-20 22:04:24 +000011946 Mask = DAG.getNode(X86ISD::PBLENDVB, DL, MVT::v16i8, X, Y, Mask);
Nate Begemanb65c1752010-12-17 22:55:37 +000011947 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Mask);
11948 }
11949 }
11950 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011951
Nate Begemanb65c1752010-12-17 22:55:37 +000011952 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000011953 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
11954 std::swap(N0, N1);
11955 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
11956 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000011957 if (!N0.hasOneUse() || !N1.hasOneUse())
11958 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000011959
11960 SDValue ShAmt0 = N0.getOperand(1);
11961 if (ShAmt0.getValueType() != MVT::i8)
11962 return SDValue();
11963 SDValue ShAmt1 = N1.getOperand(1);
11964 if (ShAmt1.getValueType() != MVT::i8)
11965 return SDValue();
11966 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
11967 ShAmt0 = ShAmt0.getOperand(0);
11968 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
11969 ShAmt1 = ShAmt1.getOperand(0);
11970
11971 DebugLoc DL = N->getDebugLoc();
11972 unsigned Opc = X86ISD::SHLD;
11973 SDValue Op0 = N0.getOperand(0);
11974 SDValue Op1 = N1.getOperand(0);
11975 if (ShAmt0.getOpcode() == ISD::SUB) {
11976 Opc = X86ISD::SHRD;
11977 std::swap(Op0, Op1);
11978 std::swap(ShAmt0, ShAmt1);
11979 }
11980
Evan Cheng8b1190a2010-04-28 01:18:01 +000011981 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000011982 if (ShAmt1.getOpcode() == ISD::SUB) {
11983 SDValue Sum = ShAmt1.getOperand(0);
11984 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000011985 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
11986 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
11987 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
11988 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000011989 return DAG.getNode(Opc, DL, VT,
11990 Op0, Op1,
11991 DAG.getNode(ISD::TRUNCATE, DL,
11992 MVT::i8, ShAmt0));
11993 }
11994 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
11995 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
11996 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000011997 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000011998 return DAG.getNode(Opc, DL, VT,
11999 N0.getOperand(0), N1.getOperand(0),
12000 DAG.getNode(ISD::TRUNCATE, DL,
12001 MVT::i8, ShAmt0));
12002 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012003
Evan Cheng760d1942010-01-04 21:22:48 +000012004 return SDValue();
12005}
12006
Chris Lattner149a4e52008-02-22 02:09:43 +000012007/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000012008static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000012009 const X86Subtarget *Subtarget) {
Chris Lattner149a4e52008-02-22 02:09:43 +000012010 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
12011 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000012012 // A preferable solution to the general problem is to figure out the right
12013 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000012014
12015 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng7e2ff772008-05-08 00:57:18 +000012016 StoreSDNode *St = cast<StoreSDNode>(N);
Owen Andersone50ed302009-08-10 22:56:29 +000012017 EVT VT = St->getValue().getValueType();
Evan Cheng536e6672009-03-12 05:59:15 +000012018 if (VT.getSizeInBits() != 64)
12019 return SDValue();
12020
Devang Patel578efa92009-06-05 21:57:13 +000012021 const Function *F = DAG.getMachineFunction().getFunction();
12022 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Eric Christopherfd179292009-08-27 18:07:15 +000012023 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
Devang Patel578efa92009-06-05 21:57:13 +000012024 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +000012025 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000012026 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000012027 isa<LoadSDNode>(St->getValue()) &&
12028 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
12029 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000012030 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000012031 LoadSDNode *Ld = 0;
12032 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000012033 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000012034 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000012035 // Must be a store of a load. We currently handle two cases: the load
12036 // is a direct child, and it's under an intervening TokenFactor. It is
12037 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000012038 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000012039 Ld = cast<LoadSDNode>(St->getChain());
12040 else if (St->getValue().hasOneUse() &&
12041 ChainVal->getOpcode() == ISD::TokenFactor) {
12042 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000012043 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000012044 TokenFactorIndex = i;
12045 Ld = cast<LoadSDNode>(St->getValue());
12046 } else
12047 Ops.push_back(ChainVal->getOperand(i));
12048 }
12049 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000012050
Evan Cheng536e6672009-03-12 05:59:15 +000012051 if (!Ld || !ISD::isNormalLoad(Ld))
12052 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000012053
Evan Cheng536e6672009-03-12 05:59:15 +000012054 // If this is not the MMX case, i.e. we are just turning i64 load/store
12055 // into f64 load/store, avoid the transformation if there are multiple
12056 // uses of the loaded value.
12057 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
12058 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000012059
Evan Cheng536e6672009-03-12 05:59:15 +000012060 DebugLoc LdDL = Ld->getDebugLoc();
12061 DebugLoc StDL = N->getDebugLoc();
12062 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
12063 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
12064 // pair instead.
12065 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000012066 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000012067 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
12068 Ld->getPointerInfo(), Ld->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000012069 Ld->isNonTemporal(), Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000012070 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000012071 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000012072 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000012073 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000012074 Ops.size());
12075 }
Evan Cheng536e6672009-03-12 05:59:15 +000012076 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000012077 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000012078 St->isVolatile(), St->isNonTemporal(),
12079 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000012080 }
Evan Cheng536e6672009-03-12 05:59:15 +000012081
12082 // Otherwise, lower to two pairs of 32-bit loads / stores.
12083 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000012084 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
12085 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000012086
Owen Anderson825b72b2009-08-11 20:47:22 +000012087 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000012088 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000012089 Ld->isVolatile(), Ld->isNonTemporal(),
12090 Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000012091 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000012092 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000012093 Ld->isVolatile(), Ld->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000012094 MinAlign(Ld->getAlignment(), 4));
12095
12096 SDValue NewChain = LoLd.getValue(1);
12097 if (TokenFactorIndex != -1) {
12098 Ops.push_back(LoLd);
12099 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000012100 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000012101 Ops.size());
12102 }
12103
12104 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000012105 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
12106 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000012107
12108 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000012109 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000012110 St->isVolatile(), St->isNonTemporal(),
12111 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000012112 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000012113 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000012114 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000012115 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000012116 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000012117 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000012118 }
Dan Gohman475871a2008-07-27 21:46:04 +000012119 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000012120}
12121
Chris Lattner6cf73262008-01-25 06:14:17 +000012122/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
12123/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000012124static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000012125 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
12126 // F[X]OR(0.0, x) -> x
12127 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000012128 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
12129 if (C->getValueAPF().isPosZero())
12130 return N->getOperand(1);
12131 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
12132 if (C->getValueAPF().isPosZero())
12133 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000012134 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000012135}
12136
12137/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000012138static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000012139 // FAND(0.0, x) -> 0.0
12140 // FAND(x, 0.0) -> 0.0
12141 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
12142 if (C->getValueAPF().isPosZero())
12143 return N->getOperand(0);
12144 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
12145 if (C->getValueAPF().isPosZero())
12146 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000012147 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000012148}
12149
Dan Gohmane5af2d32009-01-29 01:59:02 +000012150static SDValue PerformBTCombine(SDNode *N,
12151 SelectionDAG &DAG,
12152 TargetLowering::DAGCombinerInfo &DCI) {
12153 // BT ignores high bits in the bit index operand.
12154 SDValue Op1 = N->getOperand(1);
12155 if (Op1.hasOneUse()) {
12156 unsigned BitWidth = Op1.getValueSizeInBits();
12157 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
12158 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000012159 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
12160 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000012161 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000012162 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
12163 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
12164 DCI.CommitTargetLoweringOpt(TLO);
12165 }
12166 return SDValue();
12167}
Chris Lattner83e6c992006-10-04 06:57:07 +000012168
Eli Friedman7a5e5552009-06-07 06:52:44 +000012169static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
12170 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000012171 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000012172 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000012173 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000012174 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000012175 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000012176 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000012177 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000012178 }
12179 return SDValue();
12180}
12181
Evan Cheng2e489c42009-12-16 00:53:11 +000012182static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
12183 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
12184 // (and (i32 x86isd::setcc_carry), 1)
12185 // This eliminates the zext. This transformation is necessary because
12186 // ISD::SETCC is always legalized to i8.
12187 DebugLoc dl = N->getDebugLoc();
12188 SDValue N0 = N->getOperand(0);
12189 EVT VT = N->getValueType(0);
12190 if (N0.getOpcode() == ISD::AND &&
12191 N0.hasOneUse() &&
12192 N0.getOperand(0).hasOneUse()) {
12193 SDValue N00 = N0.getOperand(0);
12194 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
12195 return SDValue();
12196 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
12197 if (!C || C->getZExtValue() != 1)
12198 return SDValue();
12199 return DAG.getNode(ISD::AND, dl, VT,
12200 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
12201 N00.getOperand(0), N00.getOperand(1)),
12202 DAG.getConstant(1, VT));
12203 }
12204
12205 return SDValue();
12206}
12207
Chris Lattnerc19d1c32010-12-19 22:08:31 +000012208// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
12209static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG) {
12210 unsigned X86CC = N->getConstantOperandVal(0);
12211 SDValue EFLAG = N->getOperand(1);
12212 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012213
Chris Lattnerc19d1c32010-12-19 22:08:31 +000012214 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
12215 // a zext and produces an all-ones bit which is more useful than 0/1 in some
12216 // cases.
12217 if (X86CC == X86::COND_B)
12218 return DAG.getNode(ISD::AND, DL, MVT::i8,
12219 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
12220 DAG.getConstant(X86CC, MVT::i8), EFLAG),
12221 DAG.getConstant(1, MVT::i8));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012222
Chris Lattnerc19d1c32010-12-19 22:08:31 +000012223 return SDValue();
12224}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012225
Benjamin Kramer1396c402011-06-18 11:09:41 +000012226static SDValue PerformSINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG,
12227 const X86TargetLowering *XTLI) {
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000012228 SDValue Op0 = N->getOperand(0);
12229 // Transform (SINT_TO_FP (i64 ...)) into an x87 operation if we have
12230 // a 32-bit target where SSE doesn't support i64->FP operations.
12231 if (Op0.getOpcode() == ISD::LOAD) {
12232 LoadSDNode *Ld = cast<LoadSDNode>(Op0.getNode());
12233 EVT VT = Ld->getValueType(0);
12234 if (!Ld->isVolatile() && !N->getValueType(0).isVector() &&
12235 ISD::isNON_EXTLoad(Op0.getNode()) && Op0.hasOneUse() &&
12236 !XTLI->getSubtarget()->is64Bit() &&
12237 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Benjamin Kramer1396c402011-06-18 11:09:41 +000012238 SDValue FILDChain = XTLI->BuildFILD(SDValue(N, 0), Ld->getValueType(0),
12239 Ld->getChain(), Op0, DAG);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000012240 DAG.ReplaceAllUsesOfValueWith(Op0.getValue(1), FILDChain.getValue(1));
12241 return FILDChain;
12242 }
12243 }
12244 return SDValue();
12245}
12246
Chris Lattner23a01992010-12-20 01:37:09 +000012247// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
12248static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
12249 X86TargetLowering::DAGCombinerInfo &DCI) {
12250 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
12251 // the result is either zero or one (depending on the input carry bit).
12252 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
12253 if (X86::isZeroNode(N->getOperand(0)) &&
12254 X86::isZeroNode(N->getOperand(1)) &&
12255 // We don't have a good way to replace an EFLAGS use, so only do this when
12256 // dead right now.
12257 SDValue(N, 1).use_empty()) {
12258 DebugLoc DL = N->getDebugLoc();
12259 EVT VT = N->getValueType(0);
12260 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
12261 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
12262 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
12263 DAG.getConstant(X86::COND_B,MVT::i8),
12264 N->getOperand(2)),
12265 DAG.getConstant(1, VT));
12266 return DCI.CombineTo(N, Res1, CarryOut);
12267 }
12268
12269 return SDValue();
12270}
12271
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000012272// fold (add Y, (sete X, 0)) -> adc 0, Y
12273// (add Y, (setne X, 0)) -> sbb -1, Y
12274// (sub (sete X, 0), Y) -> sbb 0, Y
12275// (sub (setne X, 0), Y) -> adc -1, Y
12276static SDValue OptimizeConditonalInDecrement(SDNode *N, SelectionDAG &DAG) {
12277 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012278
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000012279 // Look through ZExts.
12280 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
12281 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
12282 return SDValue();
12283
12284 SDValue SetCC = Ext.getOperand(0);
12285 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
12286 return SDValue();
12287
12288 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
12289 if (CC != X86::COND_E && CC != X86::COND_NE)
12290 return SDValue();
12291
12292 SDValue Cmp = SetCC.getOperand(1);
12293 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000012294 !X86::isZeroNode(Cmp.getOperand(1)) ||
12295 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000012296 return SDValue();
12297
12298 SDValue CmpOp0 = Cmp.getOperand(0);
12299 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
12300 DAG.getConstant(1, CmpOp0.getValueType()));
12301
12302 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
12303 if (CC == X86::COND_NE)
12304 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
12305 DL, OtherVal.getValueType(), OtherVal,
12306 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
12307 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
12308 DL, OtherVal.getValueType(), OtherVal,
12309 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
12310}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000012311
Dan Gohman475871a2008-07-27 21:46:04 +000012312SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000012313 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000012314 SelectionDAG &DAG = DCI.DAG;
12315 switch (N->getOpcode()) {
12316 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012317 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerc19d1c32010-12-19 22:08:31 +000012318 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, *this);
Chris Lattneraf723b92008-01-25 05:46:26 +000012319 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +000012320 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000012321 case ISD::ADD:
12322 case ISD::SUB: return OptimizeConditonalInDecrement(N, DAG);
Chris Lattner23a01992010-12-20 01:37:09 +000012323 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000012324 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000012325 case ISD::SHL:
12326 case ISD::SRA:
12327 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000012328 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000012329 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000012330 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000012331 case ISD::SINT_TO_FP: return PerformSINT_TO_FPCombine(N, DAG, this);
Chris Lattner6cf73262008-01-25 06:14:17 +000012332 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000012333 case X86ISD::FOR: return PerformFORCombine(N, DAG);
12334 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000012335 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000012336 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Evan Cheng2e489c42009-12-16 00:53:11 +000012337 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG);
Chris Lattnerc19d1c32010-12-19 22:08:31 +000012338 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000012339 case X86ISD::SHUFPS: // Handle all target specific shuffles
12340 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000012341 case X86ISD::PALIGN:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000012342 case X86ISD::PUNPCKHBW:
12343 case X86ISD::PUNPCKHWD:
12344 case X86ISD::PUNPCKHDQ:
12345 case X86ISD::PUNPCKHQDQ:
12346 case X86ISD::UNPCKHPS:
12347 case X86ISD::UNPCKHPD:
12348 case X86ISD::PUNPCKLBW:
12349 case X86ISD::PUNPCKLWD:
12350 case X86ISD::PUNPCKLDQ:
12351 case X86ISD::PUNPCKLQDQ:
12352 case X86ISD::UNPCKLPS:
12353 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +000012354 case X86ISD::VUNPCKLPS:
12355 case X86ISD::VUNPCKLPD:
12356 case X86ISD::VUNPCKLPSY:
12357 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000012358 case X86ISD::MOVHLPS:
12359 case X86ISD::MOVLHPS:
12360 case X86ISD::PSHUFD:
12361 case X86ISD::PSHUFHW:
12362 case X86ISD::PSHUFLW:
12363 case X86ISD::MOVSS:
12364 case X86ISD::MOVSD:
Mon P Wanga0fd0d52010-12-19 23:55:53 +000012365 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI);
Evan Cheng206ee9d2006-07-07 08:33:52 +000012366 }
12367
Dan Gohman475871a2008-07-27 21:46:04 +000012368 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000012369}
12370
Evan Chenge5b51ac2010-04-17 06:13:15 +000012371/// isTypeDesirableForOp - Return true if the target has native support for
12372/// the specified value type and it is 'desirable' to use the type for the
12373/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
12374/// instruction encodings are longer and some i16 instructions are slow.
12375bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
12376 if (!isTypeLegal(VT))
12377 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000012378 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000012379 return true;
12380
12381 switch (Opc) {
12382 default:
12383 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000012384 case ISD::LOAD:
12385 case ISD::SIGN_EXTEND:
12386 case ISD::ZERO_EXTEND:
12387 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000012388 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000012389 case ISD::SRL:
12390 case ISD::SUB:
12391 case ISD::ADD:
12392 case ISD::MUL:
12393 case ISD::AND:
12394 case ISD::OR:
12395 case ISD::XOR:
12396 return false;
12397 }
12398}
12399
12400/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000012401/// beneficial for dag combiner to promote the specified node. If true, it
12402/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000012403bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000012404 EVT VT = Op.getValueType();
12405 if (VT != MVT::i16)
12406 return false;
12407
Evan Cheng4c26e932010-04-19 19:29:22 +000012408 bool Promote = false;
12409 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000012410 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000012411 default: break;
12412 case ISD::LOAD: {
12413 LoadSDNode *LD = cast<LoadSDNode>(Op);
12414 // If the non-extending load has a single use and it's not live out, then it
12415 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000012416 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
12417 Op.hasOneUse()*/) {
12418 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
12419 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
12420 // The only case where we'd want to promote LOAD (rather then it being
12421 // promoted as an operand is when it's only use is liveout.
12422 if (UI->getOpcode() != ISD::CopyToReg)
12423 return false;
12424 }
12425 }
Evan Cheng4c26e932010-04-19 19:29:22 +000012426 Promote = true;
12427 break;
12428 }
12429 case ISD::SIGN_EXTEND:
12430 case ISD::ZERO_EXTEND:
12431 case ISD::ANY_EXTEND:
12432 Promote = true;
12433 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000012434 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000012435 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000012436 SDValue N0 = Op.getOperand(0);
12437 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000012438 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000012439 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000012440 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000012441 break;
12442 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000012443 case ISD::ADD:
12444 case ISD::MUL:
12445 case ISD::AND:
12446 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000012447 case ISD::XOR:
12448 Commute = true;
12449 // fallthrough
12450 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000012451 SDValue N0 = Op.getOperand(0);
12452 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000012453 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000012454 return false;
12455 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000012456 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000012457 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000012458 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000012459 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000012460 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000012461 }
12462 }
12463
12464 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000012465 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000012466}
12467
Evan Cheng60c07e12006-07-05 22:17:51 +000012468//===----------------------------------------------------------------------===//
12469// X86 Inline Assembly Support
12470//===----------------------------------------------------------------------===//
12471
Chris Lattnerb8105652009-07-20 17:51:36 +000012472bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
12473 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000012474
12475 std::string AsmStr = IA->getAsmString();
12476
12477 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000012478 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000012479 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000012480
12481 switch (AsmPieces.size()) {
12482 default: return false;
12483 case 1:
12484 AsmStr = AsmPieces[0];
12485 AsmPieces.clear();
12486 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
12487
Chris Lattner7a2bdde2011-04-15 05:18:47 +000012488 // FIXME: this should verify that we are targeting a 486 or better. If not,
Evan Cheng55d42002011-01-08 01:24:27 +000012489 // we will turn this bswap into something that will be lowered to logical ops
12490 // instead of emitting the bswap asm. For now, we don't support 486 or lower
12491 // so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000012492 // bswap $0
12493 if (AsmPieces.size() == 2 &&
12494 (AsmPieces[0] == "bswap" ||
12495 AsmPieces[0] == "bswapq" ||
12496 AsmPieces[0] == "bswapl") &&
12497 (AsmPieces[1] == "$0" ||
12498 AsmPieces[1] == "${0:q}")) {
12499 // No need to check constraints, nothing other than the equivalent of
12500 // "=r,0" would be valid here.
Evan Cheng55d42002011-01-08 01:24:27 +000012501 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
12502 if (!Ty || Ty->getBitWidth() % 16 != 0)
12503 return false;
12504 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000012505 }
12506 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000012507 if (CI->getType()->isIntegerTy(16) &&
Chris Lattnerb8105652009-07-20 17:51:36 +000012508 AsmPieces.size() == 3 &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000012509 (AsmPieces[0] == "rorw" || AsmPieces[0] == "rolw") &&
Chris Lattnerb8105652009-07-20 17:51:36 +000012510 AsmPieces[1] == "$$8," &&
12511 AsmPieces[2] == "${0:w}" &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000012512 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
12513 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000012514 const std::string &ConstraintsStr = IA->getConstraintString();
12515 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000012516 std::sort(AsmPieces.begin(), AsmPieces.end());
12517 if (AsmPieces.size() == 4 &&
12518 AsmPieces[0] == "~{cc}" &&
12519 AsmPieces[1] == "~{dirflag}" &&
12520 AsmPieces[2] == "~{flags}" &&
12521 AsmPieces[3] == "~{fpsr}") {
Evan Cheng55d42002011-01-08 01:24:27 +000012522 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
12523 if (!Ty || Ty->getBitWidth() % 16 != 0)
12524 return false;
12525 return IntrinsicLowering::LowerToByteSwap(CI);
Dan Gohman0ef701e2010-03-04 19:58:08 +000012526 }
Chris Lattnerb8105652009-07-20 17:51:36 +000012527 }
12528 break;
12529 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000012530 if (CI->getType()->isIntegerTy(32) &&
12531 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
12532 SmallVector<StringRef, 4> Words;
12533 SplitString(AsmPieces[0], Words, " \t,");
12534 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
12535 Words[2] == "${0:w}") {
12536 Words.clear();
12537 SplitString(AsmPieces[1], Words, " \t,");
12538 if (Words.size() == 3 && Words[0] == "rorl" && Words[1] == "$$16" &&
12539 Words[2] == "$0") {
12540 Words.clear();
12541 SplitString(AsmPieces[2], Words, " \t,");
12542 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
12543 Words[2] == "${0:w}") {
12544 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000012545 const std::string &ConstraintsStr = IA->getConstraintString();
12546 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Peter Collingbourne948cf022010-11-13 19:54:30 +000012547 std::sort(AsmPieces.begin(), AsmPieces.end());
12548 if (AsmPieces.size() == 4 &&
12549 AsmPieces[0] == "~{cc}" &&
12550 AsmPieces[1] == "~{dirflag}" &&
12551 AsmPieces[2] == "~{flags}" &&
12552 AsmPieces[3] == "~{fpsr}") {
Evan Cheng55d42002011-01-08 01:24:27 +000012553 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
12554 if (!Ty || Ty->getBitWidth() % 16 != 0)
12555 return false;
12556 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000012557 }
12558 }
12559 }
12560 }
12561 }
Evan Cheng55d42002011-01-08 01:24:27 +000012562
12563 if (CI->getType()->isIntegerTy(64)) {
12564 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
12565 if (Constraints.size() >= 2 &&
12566 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
12567 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
12568 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
12569 SmallVector<StringRef, 4> Words;
12570 SplitString(AsmPieces[0], Words, " \t");
12571 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
Chris Lattnerb8105652009-07-20 17:51:36 +000012572 Words.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000012573 SplitString(AsmPieces[1], Words, " \t");
12574 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
12575 Words.clear();
12576 SplitString(AsmPieces[2], Words, " \t,");
12577 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
12578 Words[2] == "%edx") {
12579 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
12580 if (!Ty || Ty->getBitWidth() % 16 != 0)
12581 return false;
12582 return IntrinsicLowering::LowerToByteSwap(CI);
12583 }
Chris Lattnerb8105652009-07-20 17:51:36 +000012584 }
12585 }
12586 }
12587 }
12588 break;
12589 }
12590 return false;
12591}
12592
12593
12594
Chris Lattnerf4dff842006-07-11 02:54:03 +000012595/// getConstraintType - Given a constraint letter, return the type of
12596/// constraint it is for this target.
12597X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000012598X86TargetLowering::getConstraintType(const std::string &Constraint) const {
12599 if (Constraint.size() == 1) {
12600 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000012601 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000012602 case 'q':
12603 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000012604 case 'f':
12605 case 't':
12606 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000012607 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000012608 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000012609 case 'Y':
Eric Christopher31b5f002011-07-07 22:29:07 +000012610 case 'l':
Chris Lattner4234f572007-03-25 02:14:49 +000012611 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000012612 case 'a':
12613 case 'b':
12614 case 'c':
12615 case 'd':
12616 case 'S':
12617 case 'D':
12618 case 'A':
12619 return C_Register;
12620 case 'I':
12621 case 'J':
12622 case 'K':
12623 case 'L':
12624 case 'M':
12625 case 'N':
12626 case 'G':
12627 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000012628 case 'e':
12629 case 'Z':
12630 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000012631 default:
12632 break;
12633 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000012634 }
Chris Lattner4234f572007-03-25 02:14:49 +000012635 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000012636}
12637
John Thompson44ab89e2010-10-29 17:29:13 +000012638/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000012639/// This object must already have been set up with the operand type
12640/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000012641TargetLowering::ConstraintWeight
12642 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000012643 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000012644 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012645 Value *CallOperandVal = info.CallOperandVal;
12646 // If we don't have a value, we can't do a match,
12647 // but allow it at the lowest weight.
12648 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000012649 return CW_Default;
12650 const Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000012651 // Look at the constraint type.
12652 switch (*constraint) {
12653 default:
John Thompson44ab89e2010-10-29 17:29:13 +000012654 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
12655 case 'R':
12656 case 'q':
12657 case 'Q':
12658 case 'a':
12659 case 'b':
12660 case 'c':
12661 case 'd':
12662 case 'S':
12663 case 'D':
12664 case 'A':
12665 if (CallOperandVal->getType()->isIntegerTy())
12666 weight = CW_SpecificReg;
12667 break;
12668 case 'f':
12669 case 't':
12670 case 'u':
12671 if (type->isFloatingPointTy())
12672 weight = CW_SpecificReg;
12673 break;
12674 case 'y':
Chris Lattner2a786eb2010-12-19 20:19:20 +000012675 if (type->isX86_MMXTy() && Subtarget->hasMMX())
John Thompson44ab89e2010-10-29 17:29:13 +000012676 weight = CW_SpecificReg;
12677 break;
12678 case 'x':
12679 case 'Y':
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012680 if ((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasXMM())
John Thompson44ab89e2010-10-29 17:29:13 +000012681 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012682 break;
12683 case 'I':
12684 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
12685 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000012686 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012687 }
12688 break;
John Thompson44ab89e2010-10-29 17:29:13 +000012689 case 'J':
12690 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12691 if (C->getZExtValue() <= 63)
12692 weight = CW_Constant;
12693 }
12694 break;
12695 case 'K':
12696 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12697 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
12698 weight = CW_Constant;
12699 }
12700 break;
12701 case 'L':
12702 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12703 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
12704 weight = CW_Constant;
12705 }
12706 break;
12707 case 'M':
12708 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12709 if (C->getZExtValue() <= 3)
12710 weight = CW_Constant;
12711 }
12712 break;
12713 case 'N':
12714 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12715 if (C->getZExtValue() <= 0xff)
12716 weight = CW_Constant;
12717 }
12718 break;
12719 case 'G':
12720 case 'C':
12721 if (dyn_cast<ConstantFP>(CallOperandVal)) {
12722 weight = CW_Constant;
12723 }
12724 break;
12725 case 'e':
12726 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12727 if ((C->getSExtValue() >= -0x80000000LL) &&
12728 (C->getSExtValue() <= 0x7fffffffLL))
12729 weight = CW_Constant;
12730 }
12731 break;
12732 case 'Z':
12733 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12734 if (C->getZExtValue() <= 0xffffffff)
12735 weight = CW_Constant;
12736 }
12737 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012738 }
12739 return weight;
12740}
12741
Dale Johannesenba2a0b92008-01-29 02:21:21 +000012742/// LowerXConstraint - try to replace an X constraint, which matches anything,
12743/// with another that has more specific requirements based on the type of the
12744/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000012745const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000012746LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000012747 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
12748 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000012749 if (ConstraintVT.isFloatingPoint()) {
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012750 if (Subtarget->hasXMMInt())
Chris Lattner5e764232008-04-26 23:02:14 +000012751 return "Y";
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012752 if (Subtarget->hasXMM())
Chris Lattner5e764232008-04-26 23:02:14 +000012753 return "x";
12754 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012755
Chris Lattner5e764232008-04-26 23:02:14 +000012756 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000012757}
12758
Chris Lattner48884cd2007-08-25 00:47:38 +000012759/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
12760/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000012761void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +000012762 std::string &Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000012763 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000012764 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000012765 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000012766
Eric Christopher100c8332011-06-02 23:16:42 +000012767 // Only support length 1 constraints for now.
12768 if (Constraint.length() > 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +000012769
Eric Christopher100c8332011-06-02 23:16:42 +000012770 char ConstraintLetter = Constraint[0];
12771 switch (ConstraintLetter) {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012772 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000012773 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000012774 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000012775 if (C->getZExtValue() <= 31) {
12776 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000012777 break;
12778 }
Devang Patel84f7fd22007-03-17 00:13:28 +000012779 }
Chris Lattner48884cd2007-08-25 00:47:38 +000012780 return;
Evan Cheng364091e2008-09-22 23:57:37 +000012781 case 'J':
12782 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000012783 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000012784 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
12785 break;
12786 }
12787 }
12788 return;
12789 case 'K':
12790 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000012791 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000012792 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
12793 break;
12794 }
12795 }
12796 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000012797 case 'N':
12798 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000012799 if (C->getZExtValue() <= 255) {
12800 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000012801 break;
12802 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000012803 }
Chris Lattner48884cd2007-08-25 00:47:38 +000012804 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000012805 case 'e': {
12806 // 32-bit signed value
12807 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000012808 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
12809 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000012810 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000012811 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000012812 break;
12813 }
12814 // FIXME gcc accepts some relocatable values here too, but only in certain
12815 // memory models; it's complicated.
12816 }
12817 return;
12818 }
12819 case 'Z': {
12820 // 32-bit unsigned value
12821 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000012822 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
12823 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000012824 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
12825 break;
12826 }
12827 }
12828 // FIXME gcc accepts some relocatable values here too, but only in certain
12829 // memory models; it's complicated.
12830 return;
12831 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012832 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012833 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000012834 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000012835 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000012836 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000012837 break;
12838 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012839
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000012840 // In any sort of PIC mode addresses need to be computed at runtime by
12841 // adding in a register or some sort of table lookup. These can't
12842 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000012843 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000012844 return;
12845
Chris Lattnerdc43a882007-05-03 16:52:29 +000012846 // If we are in non-pic codegen mode, we allow the address of a global (with
12847 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000012848 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000012849 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000012850
Chris Lattner49921962009-05-08 18:23:14 +000012851 // Match either (GA), (GA+C), (GA+C1+C2), etc.
12852 while (1) {
12853 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
12854 Offset += GA->getOffset();
12855 break;
12856 } else if (Op.getOpcode() == ISD::ADD) {
12857 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
12858 Offset += C->getZExtValue();
12859 Op = Op.getOperand(0);
12860 continue;
12861 }
12862 } else if (Op.getOpcode() == ISD::SUB) {
12863 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
12864 Offset += -C->getZExtValue();
12865 Op = Op.getOperand(0);
12866 continue;
12867 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012868 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012869
Chris Lattner49921962009-05-08 18:23:14 +000012870 // Otherwise, this isn't something we can handle, reject it.
12871 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000012872 }
Eric Christopherfd179292009-08-27 18:07:15 +000012873
Dan Gohman46510a72010-04-15 01:51:59 +000012874 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012875 // If we require an extra load to get this address, as in PIC mode, we
12876 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000012877 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
12878 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012879 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000012880
Devang Patel0d881da2010-07-06 22:08:15 +000012881 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
12882 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000012883 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012884 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012885 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012886
Gabor Greifba36cb52008-08-28 21:40:38 +000012887 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000012888 Ops.push_back(Result);
12889 return;
12890 }
Dale Johannesen1784d162010-06-25 21:55:36 +000012891 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012892}
12893
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012894std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000012895X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000012896 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000012897 // First, see if this is a constraint that directly corresponds to an LLVM
12898 // register class.
12899 if (Constraint.size() == 1) {
12900 // GCC Constraint Letters
12901 switch (Constraint[0]) {
12902 default: break;
Eric Christopherd176af82011-06-29 17:23:50 +000012903 // TODO: Slight differences here in allocation order and leaving
12904 // RIP in the class. Do they matter any more here than they do
12905 // in the normal allocation?
12906 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
12907 if (Subtarget->is64Bit()) {
Nick Lewycky9bf45d02011-07-08 00:19:27 +000012908 if (VT == MVT::i32 || VT == MVT::f32)
Eric Christopherd176af82011-06-29 17:23:50 +000012909 return std::make_pair(0U, X86::GR32RegisterClass);
12910 else if (VT == MVT::i16)
12911 return std::make_pair(0U, X86::GR16RegisterClass);
12912 else if (VT == MVT::i8)
12913 return std::make_pair(0U, X86::GR8RegisterClass);
Nick Lewycky9bf45d02011-07-08 00:19:27 +000012914 else if (VT == MVT::i64 || VT == MVT::f64)
Eric Christopherd176af82011-06-29 17:23:50 +000012915 return std::make_pair(0U, X86::GR64RegisterClass);
12916 break;
12917 }
12918 // 32-bit fallthrough
12919 case 'Q': // Q_REGS
Nick Lewycky9bf45d02011-07-08 00:19:27 +000012920 if (VT == MVT::i32 || VT == MVT::f32)
Eric Christopherd176af82011-06-29 17:23:50 +000012921 return std::make_pair(0U, X86::GR32_ABCDRegisterClass);
12922 else if (VT == MVT::i16)
12923 return std::make_pair(0U, X86::GR16_ABCDRegisterClass);
12924 else if (VT == MVT::i8)
12925 return std::make_pair(0U, X86::GR8_ABCD_LRegisterClass);
12926 else if (VT == MVT::i64)
12927 return std::make_pair(0U, X86::GR64_ABCDRegisterClass);
12928 break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000012929 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000012930 case 'l': // INDEX_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000012931 if (VT == MVT::i8)
Chris Lattner0f65cad2007-04-09 05:49:22 +000012932 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012933 if (VT == MVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +000012934 return std::make_pair(0U, X86::GR16RegisterClass);
Eric Christopher2bbecd82011-05-19 21:33:47 +000012935 if (VT == MVT::i32 || VT == MVT::f32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +000012936 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +000012937 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000012938 case 'R': // LEGACY_REGS
12939 if (VT == MVT::i8)
12940 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
12941 if (VT == MVT::i16)
12942 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
12943 if (VT == MVT::i32 || !Subtarget->is64Bit())
12944 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
12945 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000012946 case 'f': // FP Stack registers.
12947 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
12948 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000012949 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000012950 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012951 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000012952 return std::make_pair(0U, X86::RFP64RegisterClass);
12953 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000012954 case 'y': // MMX_REGS if MMX allowed.
12955 if (!Subtarget->hasMMX()) break;
12956 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000012957 case 'Y': // SSE_REGS if SSE2 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012958 if (!Subtarget->hasXMMInt()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000012959 // FALL THROUGH.
12960 case 'x': // SSE_REGS if SSE1 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012961 if (!Subtarget->hasXMM()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000012962
Owen Anderson825b72b2009-08-11 20:47:22 +000012963 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000012964 default: break;
12965 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000012966 case MVT::f32:
12967 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +000012968 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012969 case MVT::f64:
12970 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +000012971 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000012972 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000012973 case MVT::v16i8:
12974 case MVT::v8i16:
12975 case MVT::v4i32:
12976 case MVT::v2i64:
12977 case MVT::v4f32:
12978 case MVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +000012979 return std::make_pair(0U, X86::VR128RegisterClass);
12980 }
Chris Lattnerad043e82007-04-09 05:11:28 +000012981 break;
12982 }
12983 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012984
Chris Lattnerf76d1802006-07-31 23:26:50 +000012985 // Use the default implementation in TargetLowering to convert the register
12986 // constraint into a member of a register class.
12987 std::pair<unsigned, const TargetRegisterClass*> Res;
12988 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000012989
12990 // Not found as a standard register?
12991 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000012992 // Map st(0) -> st(7) -> ST0
12993 if (Constraint.size() == 7 && Constraint[0] == '{' &&
12994 tolower(Constraint[1]) == 's' &&
12995 tolower(Constraint[2]) == 't' &&
12996 Constraint[3] == '(' &&
12997 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
12998 Constraint[5] == ')' &&
12999 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000013000
Chris Lattner56d77c72009-09-13 22:41:48 +000013001 Res.first = X86::ST0+Constraint[4]-'0';
13002 Res.second = X86::RFP80RegisterClass;
13003 return Res;
13004 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000013005
Chris Lattner56d77c72009-09-13 22:41:48 +000013006 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000013007 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000013008 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +000013009 Res.second = X86::RFP80RegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000013010 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000013011 }
Chris Lattner56d77c72009-09-13 22:41:48 +000013012
13013 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000013014 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000013015 Res.first = X86::EFLAGS;
13016 Res.second = X86::CCRRegisterClass;
13017 return Res;
13018 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000013019
Dale Johannesen330169f2008-11-13 21:52:36 +000013020 // 'A' means EAX + EDX.
13021 if (Constraint == "A") {
13022 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +000013023 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000013024 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000013025 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000013026 return Res;
13027 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013028
Chris Lattnerf76d1802006-07-31 23:26:50 +000013029 // Otherwise, check to see if this is a register class of the wrong value
13030 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
13031 // turn into {ax},{dx}.
13032 if (Res.second->hasType(VT))
13033 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013034
Chris Lattnerf76d1802006-07-31 23:26:50 +000013035 // All of the single-register GCC register classes map their values onto
13036 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
13037 // really want an 8-bit or 32-bit register, map to the appropriate register
13038 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +000013039 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000013040 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000013041 unsigned DestReg = 0;
13042 switch (Res.first) {
13043 default: break;
13044 case X86::AX: DestReg = X86::AL; break;
13045 case X86::DX: DestReg = X86::DL; break;
13046 case X86::CX: DestReg = X86::CL; break;
13047 case X86::BX: DestReg = X86::BL; break;
13048 }
13049 if (DestReg) {
13050 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000013051 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000013052 }
Owen Anderson825b72b2009-08-11 20:47:22 +000013053 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000013054 unsigned DestReg = 0;
13055 switch (Res.first) {
13056 default: break;
13057 case X86::AX: DestReg = X86::EAX; break;
13058 case X86::DX: DestReg = X86::EDX; break;
13059 case X86::CX: DestReg = X86::ECX; break;
13060 case X86::BX: DestReg = X86::EBX; break;
13061 case X86::SI: DestReg = X86::ESI; break;
13062 case X86::DI: DestReg = X86::EDI; break;
13063 case X86::BP: DestReg = X86::EBP; break;
13064 case X86::SP: DestReg = X86::ESP; break;
13065 }
13066 if (DestReg) {
13067 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000013068 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000013069 }
Owen Anderson825b72b2009-08-11 20:47:22 +000013070 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000013071 unsigned DestReg = 0;
13072 switch (Res.first) {
13073 default: break;
13074 case X86::AX: DestReg = X86::RAX; break;
13075 case X86::DX: DestReg = X86::RDX; break;
13076 case X86::CX: DestReg = X86::RCX; break;
13077 case X86::BX: DestReg = X86::RBX; break;
13078 case X86::SI: DestReg = X86::RSI; break;
13079 case X86::DI: DestReg = X86::RDI; break;
13080 case X86::BP: DestReg = X86::RBP; break;
13081 case X86::SP: DestReg = X86::RSP; break;
13082 }
13083 if (DestReg) {
13084 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000013085 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000013086 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000013087 }
Chris Lattner6ba50a92008-08-26 06:19:02 +000013088 } else if (Res.second == X86::FR32RegisterClass ||
13089 Res.second == X86::FR64RegisterClass ||
13090 Res.second == X86::VR128RegisterClass) {
13091 // Handle references to XMM physical registers that got mapped into the
13092 // wrong class. This can happen with constraints like {xmm0} where the
13093 // target independent register mapper will just pick the first match it can
13094 // find, ignoring the required type.
Owen Anderson825b72b2009-08-11 20:47:22 +000013095 if (VT == MVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +000013096 Res.second = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +000013097 else if (VT == MVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +000013098 Res.second = X86::FR64RegisterClass;
13099 else if (X86::VR128RegisterClass->hasType(VT))
13100 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000013101 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013102
Chris Lattnerf76d1802006-07-31 23:26:50 +000013103 return Res;
13104}